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 
24 package ifc.sdb;
25 
26 import com.sun.star.sdb.XSingleSelectQueryAnalyzer;
27 import lib.MultiMethodTest;
28 import com.sun.star.sdb.XSingleSelectQueryComposer;
29 import com.sun.star.uno.UnoRuntime;
30 import lib.StatusException;
31 import lib.Status;
32 import com.sun.star.beans.PropertyValue;
33 import com.sun.star.container.XIndexAccess;
34 
35 /**
36 * Testing <code>com.sun.star.sdb.XSingleSelectQueryAnalyzer</code>
37 * interface methods :
38 * <ul>
39 *  <li><code>getQuery()</code></li>
40 *  <li><code>setQuery()</code></li>
41 *  <li><code>getFilter()</code></li>
42 *  <li><code>getStructuredFilter()</code></li>
43 *  <li><code>getGroup()</code></li>
44 *  <li><code>getGroupColumns()</code></li>
45 *  <li><code>getHavingClause()</code></li>
46 *  <li><code>getStructuredHavingClause()</code></li>
47 *  <li><code>getOrder()</code></li>
48 *  <li><code>getOrderColumns()</code></li>
49 
50 * </ul> <p>
51 * @see com.sun.star.sdb.XSingleSelectQueryAnalyzer
52 */
53 public class _XSingleSelectQueryAnalyzer extends MultiMethodTest {
54 
55     // oObj filled by MultiMethodTest
56     public XSingleSelectQueryAnalyzer oObj = null ;
57 
58     private String queryString = "SELECT * FROM \"biblio\"";
59 
60     private XSingleSelectQueryComposer xComposer = null;
61 
62     /**
63      * Receives the object relations:
64     * <ul>
65     *  <li><code>XSingleSelectQueryComposer xCompoer</code></li>
66     * </ul> <p>
67      * @see com.sun.star.sdb.XSingleSelectQueryComposer
68      */
before()69     protected void before() {
70 
71         xComposer = (XSingleSelectQueryComposer)
72                       UnoRuntime.queryInterface(XSingleSelectQueryComposer.class,
73                       tEnv.getObjRelation("xComposer"));
74 
75         if (xComposer == null) {
76             throw new StatusException(Status.failed(
77            "Couldn't get object relation 'xComposer'. Test must be modified"));
78 
79         }
80 
81     }
82     /**
83     * call <code>setQuery()</code> once with valid query, once with invalid
84     * query. Has ok if only on sceond call <code>SQLException</code> was thrwon
85     */
_setQuery()86     public void _setQuery() {
87 
88         try{
89             oObj.setQuery("This is an invalid SQL query");
90         } catch (com.sun.star.sdbc.SQLException e){
91             log.println("expected Exception. ");
92         }
93 
94         try{
95             oObj.setQuery(queryString);
96         } catch (com.sun.star.sdbc.SQLException e){
97             log.println("unexpected Exception: " + e.toString());
98             tRes.tested("setQuery()", false);
99         }
100         tRes.tested("setQuery()", true);
101     }
102 
103     /**
104     * checks of the returned value of <code>getQuery()</code>
105     * equals the string which was set by <code>setQuery()</code>
106     * <p>
107     * required methods:
108     *<ul>
109     * <li><code>setQuery</code></li>
110     *</ul>
111     */
_getQuery()112     public void _getQuery() {
113         this.requiredMethod("setQuery()");
114 
115         boolean res = false;
116 
117         res = oObj.getQuery().equals(queryString);
118 
119         tRes.tested("getQuery()", res);
120     }
121 
122 
123     /**
124     * Object relation <code>xComposer</code> set a filter. This filter
125     * must returned while calling <code>getFilter</code>
126     */
_getFilter()127     public void _getFilter() {
128         try{
129             String filter = "\"Identifier\" = 'BOR02b'";
130             xComposer.setFilter(filter);
131             tRes.tested("getFilter()", (oObj.getFilter().equals(filter)));
132 
133         } catch (com.sun.star.sdbc.SQLException e){
134             log.println("unexpected Exception: " + e.toString());
135             tRes.tested("getFilter()", false);
136         }
137     }
138 
139     /**
140     * Object relation <code>xComposer</code> set a complex filter with method
141     . <code>setFilter</code>. Then <code>getStructuredFilter</code> returns a
142     * sequenze of <code>PropertyValue</code> which was set with method
143     * <code>setStructuredFilter</code> from <xComposer>.
144     * Then test has ok status if <getFilter> returns the complex filter.
145     * <p>
146     * required methods:
147     *<ul>
148     * <li><code>setQuery</code></li>
149     * <li><code>getFilter</code></li>
150     *</ul>
151     */
_getStructuredFilter()152     public void _getStructuredFilter() {
153         requiredMethod("setQuery()");
154         requiredMethod("getFilter()");
155         try{
156             oObj.setQuery("SELECT \"Identifier\", \"Type\", \"Address\" FROM \"biblio\" \"biblio\"");
157             String complexFilter = "( \"Identifier\" = '1' AND \"Type\" = '4' ) OR ( \"Identifier\" = '2' AND \"Type\" = '5' ) OR ( \"Identifier\" = '3' AND \"Type\" = '6' AND \"Address\" = '7' ) OR ( \"Address\" = '8' ) OR ( \"Type\" = '9' )";
158             xComposer.setFilter(complexFilter);
159             PropertyValue[][] aStructuredFilter = oObj.getStructuredFilter();
160             xComposer.setFilter("");
161             xComposer.setStructuredFilter(aStructuredFilter);
162             tRes.tested("getStructuredFilter()", (oObj.getFilter().equals(complexFilter)));
163 
164         } catch (com.sun.star.sdbc.SQLException e){
165             log.println("unexpected Exception: " + e.toString());
166             tRes.tested("getStructuredFilter()", false);
167         } catch (com.sun.star.lang.IllegalArgumentException e){
168             log.println("unexpected Exception: " + e.toString());
169             tRes.tested("getStructuredFilter()", false);
170         }
171     }
172 
173     /**
174     * Object relation <code>xComposer</code> set a goup. This group
175     * must returned while calling <code>getGroup</code>
176     */
_getGroup()177     public void _getGroup() {
178         try{
179             String group = "\"Identifier\"";
180             xComposer.setGroup(group);
181             tRes.tested("getGroup()", (oObj.getGroup().equals(group)));
182 
183         } catch (com.sun.star.sdbc.SQLException e){
184             log.println("unexpected Exception: " + e.toString());
185             tRes.tested("getGroup()", false);
186         }
187     }
188 
189     /**
190     * Method <code>getGroupColumns</code> retunrs a <code>XIndexAccess</code>
191     * Test has ok status if returned value is an useable <code>XIndexAccess</code>
192     */
_getGroupColumns()193     public void _getGroupColumns() {
194         try{
195            XIndexAccess xGroupColumns = oObj.getGroupColumns();
196 
197            tRes.tested("getGroupColumns()", (xGroupColumns != null &&
198                                             xGroupColumns.getCount() == 1 &&
199                                         xGroupColumns.getByIndex(0) != null));
200 
201         } catch (com.sun.star.lang.IndexOutOfBoundsException e){
202             log.println("unexpected Exception: " + e.toString());
203             tRes.tested("getGroupColumns()", false);
204         } catch (com.sun.star.lang.WrappedTargetException e){
205             log.println("unexpected Exception: " + e.toString());
206             tRes.tested("getGroupColumns()", false);
207         }
208     }
209 
210     /**
211     * Object relation <code>xComposer</code> set a clause. This clause
212     * must returned while calling <code>getHavingClause</code>
213     */
_getHavingClause()214     public void _getHavingClause() {
215         try{
216             String clause = "\"Identifier\" = 'BOR02b'";
217             xComposer.setHavingClause(clause);
218             tRes.tested("getHavingClause()", (
219                                         oObj.getHavingClause().equals(clause)));
220 
221         } catch (com.sun.star.sdbc.SQLException e){
222             log.println("unexpected Exception: " + e.toString());
223             tRes.tested("getHavingClause()", false);
224         }
225     }
226 
227     /**
228     * Object relation <code>xComposer</code> set a clause. This clause
229     * must returned while calling <code>getHavingClause</code>
230     * <p>
231     * required methods:
232     *<ul>
233     * <li><code>setQuery</code></li>
234     * <li><code>getFilter</code></li>
235     * <li><code>getStructuredFilter</code></li>
236     *</ul>
237     */
_getStructuredHavingClause()238     public void _getStructuredHavingClause() {
239         requiredMethod("setQuery()");
240         requiredMethod("getFilter()");
241         executeMethod("getStructuredFilter()");
242         String complexFilter = "( \"Identifier\" = '1' AND \"Type\" = '4' ) OR ( \"Identifier\" = '2' AND \"Type\" = '5' ) OR ( \"Identifier\" = '3' AND \"Type\" = '6' AND \"Address\" = '7' ) OR ( \"Address\" = '8' ) OR ( \"Type\" = '9' )";
243 
244         try{
245            xComposer.setHavingClause(complexFilter);
246            PropertyValue[][] aStructuredHaving = oObj.getStructuredHavingClause();
247            xComposer.setHavingClause("");
248            xComposer.setStructuredHavingClause(aStructuredHaving);
249            tRes.tested("getStructuredHavingClause()",
250                                 (oObj.getHavingClause().equals(complexFilter)));
251 
252         } catch (com.sun.star.sdbc.SQLException e){
253             log.println("unexpected Exception: " + e.toString());
254             tRes.tested("getStructuredHavingClause()", false);
255         }
256     }
257 
258     /**
259     * Object relation <code>xComposer</code> set an order. This order
260     * must returned while calling <code>getOrder</code>
261     */
_getOrder()262     public void _getOrder() {
263         try{
264             String order = "\"Identifier\"";
265             xComposer.setOrder(order);
266             tRes.tested("getOrder()", (oObj.getOrder().equals(order)));
267 
268         } catch (com.sun.star.sdbc.SQLException e){
269             log.println("unexpected Exception: " + e.toString());
270             tRes.tested("getOrder()", false);
271         }
272     }
273 
274     /**
275     * Method <code>getGroupColumns</code> retunrs a <code>XIndexAccess</code>
276     * Test has ok status if returned value is an useable <code>XIndexAccess</code>
277     */
_getOrderColumns()278     public void _getOrderColumns() {
279         try{
280             XIndexAccess xOrderColumns = oObj.getOrderColumns();
281             tRes.tested("getOrderColumns()", (xOrderColumns != null &&
282                                               xOrderColumns.getCount() == 1 &&
283                                          xOrderColumns.getByIndex(0) != null));
284 
285         } catch (com.sun.star.lang.IndexOutOfBoundsException e){
286             log.println("unexpected Exception: " + e.toString());
287             tRes.tested("getOrderColumns()", false);
288         } catch (com.sun.star.lang.WrappedTargetException e){
289             log.println("unexpected Exception: " + e.toString());
290             tRes.tested("getOrderColumns()", false);
291         }
292     }
293 
294 
295 }  // finish class _XSingleSelectQueryAnalyzer
296