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