1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 package complex.dbaccess; 24 25 import com.sun.star.beans.XPropertySet; 26 import com.sun.star.container.XIndexAccess; 27 import com.sun.star.container.XNameAccess; 28 import com.sun.star.container.XNamed; 29 import com.sun.star.sdb.XQueriesSupplier; 30 import com.sun.star.sdbcx.XColumnsSupplier; 31 import com.sun.star.uno.UnoRuntime; 32 import connectivity.tools.CRMDatabase; 33 34 // ---------- junit imports ----------------- 35 import org.junit.Test; 36 import static org.junit.Assert.*; 37 // ------------------------------------------ 38 39 public class Query extends TestCase 40 { 41 42 connectivity.tools.HsqlDatabase m_database; 43 44 // -------------------------------------------------------------------------------------------------------- createTestCase()45 private void createTestCase() 46 { 47 try 48 { 49 if (m_database == null) 50 { 51 final CRMDatabase database = new CRMDatabase(getMSF(), false); 52 m_database = database.getDatabase(); 53 } 54 } 55 catch (Exception e) 56 { 57 System.out.println("could not create the test case, error message:\n" + e.getMessage()); 58 e.printStackTrace(System.err); 59 fail("failed to created the test case"); 60 } 61 } 62 63 // -------------------------------------------------------------------------------------------------------- 64 // private XMultiServiceFactory getFactory() 65 // { 66 // return (XMultiServiceFactory)param.getMSF(); 67 // } 68 // -------------------------------------------------------------------------------------------------------- 69 @Test testQueryColumns()70 public void testQueryColumns() 71 { 72 createTestCase(); 73 74 try 75 { 76 final XQueriesSupplier suppQueries = UnoRuntime.queryInterface( 77 XQueriesSupplier.class, m_database.defaultConnection().getXConnection() ); 78 final XNameAccess queries = suppQueries.getQueries(); 79 80 final String[] queryNames = new String[] { "parseable", "parseable native", "unparseable" }; 81 final String[][] expectedColumnNames = new String[][] { 82 new String[] { "ID", "Name", "Address", "City", "Postal","Comment" }, 83 new String[] { "TABLE_CATALOG", "TABLE_SCHEMA", "TABLE_NAME", "VIEW_DEFINITION", "CHECK_OPTION", "IS_UPDATABLE", "VALID" }, 84 new String[] { "ID_VARCHAR" } 85 }; 86 87 for ( int i = 0; i < queryNames.length; ++i ) 88 { 89 if (queries.hasByName(queryNames[i])) 90 { 91 final XPropertySet query = UnoRuntime.queryInterface( 92 XPropertySet.class, queries.getByName( queryNames[i] ) ); 93 94 final XColumnsSupplier suppCols = UnoRuntime.queryInterface( 95 XColumnsSupplier.class, query); 96 final XIndexAccess columns = UnoRuntime.queryInterface( 97 XIndexAccess.class, suppCols.getColumns()); 98 99 // check whether the columns supplied by the query match what we expected 100 assertTrue( "invalid column count (found " + columns.getCount() + ", expected: " + expectedColumnNames[i].length + ") for query \"" + queryNames[i] + "\"", 101 columns.getCount() == expectedColumnNames[i].length ); 102 for ( int col = 0; col < columns.getCount(); ++col ) 103 { 104 final XNamed columnName = UnoRuntime.queryInterface( 105 XNamed.class, columns.getByIndex(col) ); 106 assertTrue( "column no. " + col + " of query \"" + queryNames[i] + "\" not matching", 107 columnName.getName().equals( expectedColumnNames[i][col] ) ); 108 } 109 } 110 } 111 } 112 catch ( Exception e ) 113 { 114 fail( "caught an unexpected exception: " + e.getMessage() ); 115 } 116 } 117 } 118