1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 package complex.dbaccess; 28 29 import com.sun.star.beans.XPropertySet; 30 import com.sun.star.container.XIndexAccess; 31 import com.sun.star.container.XNameAccess; 32 import com.sun.star.container.XNamed; 33 import com.sun.star.sdb.XQueriesSupplier; 34 import com.sun.star.sdbcx.XColumnsSupplier; 35 import com.sun.star.uno.UnoRuntime; 36 import connectivity.tools.CRMDatabase; 37 38 // ---------- junit imports ----------------- 39 import org.junit.Test; 40 import static org.junit.Assert.*; 41 // ------------------------------------------ 42 43 public class Query extends TestCase 44 { 45 46 connectivity.tools.HsqlDatabase m_database; 47 48 // -------------------------------------------------------------------------------------------------------- 49 private void createTestCase() 50 { 51 try 52 { 53 if (m_database == null) 54 { 55 final CRMDatabase database = new CRMDatabase(getMSF(), false); 56 m_database = database.getDatabase(); 57 } 58 } 59 catch (Exception e) 60 { 61 System.out.println("could not create the test case, error message:\n" + e.getMessage()); 62 e.printStackTrace(System.err); 63 fail("failed to created the test case"); 64 } 65 } 66 67 // -------------------------------------------------------------------------------------------------------- 68 // private XMultiServiceFactory getFactory() 69 // { 70 // return (XMultiServiceFactory)param.getMSF(); 71 // } 72 // -------------------------------------------------------------------------------------------------------- 73 @Test 74 public void testQueryColumns() 75 { 76 createTestCase(); 77 78 try 79 { 80 final XQueriesSupplier suppQueries = UnoRuntime.queryInterface( 81 XQueriesSupplier.class, m_database.defaultConnection().getXConnection() ); 82 final XNameAccess queries = suppQueries.getQueries(); 83 84 final String[] queryNames = new String[] { "parseable", "parseable native", "unparseable" }; 85 final String[][] expectedColumnNames = new String[][] { 86 new String[] { "ID", "Name", "Address", "City", "Postal","Comment" }, 87 new String[] { "TABLE_CATALOG", "TABLE_SCHEMA", "TABLE_NAME", "VIEW_DEFINITION", "CHECK_OPTION", "IS_UPDATABLE", "VALID" }, 88 new String[] { "ID_VARCHAR" } 89 }; 90 91 for ( int i = 0; i < queryNames.length; ++i ) 92 { 93 if (queries.hasByName(queryNames[i])) 94 { 95 final XPropertySet query = UnoRuntime.queryInterface( 96 XPropertySet.class, queries.getByName( queryNames[i] ) ); 97 98 final XColumnsSupplier suppCols = UnoRuntime.queryInterface( 99 XColumnsSupplier.class, query); 100 final XIndexAccess columns = UnoRuntime.queryInterface( 101 XIndexAccess.class, suppCols.getColumns()); 102 103 // check whether the columns supplied by the query match what we expected 104 assertTrue( "invalid column count (found " + columns.getCount() + ", expected: " + expectedColumnNames[i].length + ") for query \"" + queryNames[i] + "\"", 105 columns.getCount() == expectedColumnNames[i].length ); 106 for ( int col = 0; col < columns.getCount(); ++col ) 107 { 108 final XNamed columnName = UnoRuntime.queryInterface( 109 XNamed.class, columns.getByIndex(col) ); 110 assertTrue( "column no. " + col + " of query \"" + queryNames[i] + "\" not matching", 111 columnName.getName().equals( expectedColumnNames[i][col] ) ); 112 } 113 } 114 } 115 } 116 catch ( Exception e ) 117 { 118 fail( "caught an unexpected exception: " + e.getMessage() ); 119 } 120 } 121 } 122