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.XSingleSelectQueryComposer;
27 import lib.MultiMethodTest;
28 import com.sun.star.sdb.XSingleSelectQueryAnalyzer;
29 import com.sun.star.uno.AnyConverter;
30 import com.sun.star.uno.UnoRuntime;
31 import lib.StatusException;
32 import lib.Status;
33 import com.sun.star.beans.PropertyValue;
34 import com.sun.star.beans.XPropertySet;
35 import com.sun.star.sdb.SQLFilterOperator;
36 
37 /**
38 * Testing <code>com.sun.star.sdb.XSingleSelectQueryComposer</code>
39 * interface methods :
40 * <ul>
41 *  <li><code>setFilter()</code></li>
42 *  <li><code>setStructuredFilter()</code></li>
43 *  <li><code>appendFilterByColumn()</code></li>
44 *  <li><code>appendGroupByColumn()</code></li>
45 *  <li><code>setGroup()</code></li>
46 *  <li><code>setHavingClause()</code></li>
47 *  <li><code>setStructuredHavingClause()</code></li>
48 *  <li><code>appendHavingClauseByColumn()</code></li>
49 *  <li><code>appendOrderByColumn()</code></li>
50 *  <li><code>setOrder()</code></li>
51 
52 * </ul> <p>
53 * @see com.sun.star.sdb.XSingleSelectQueryComposer
54 */
55 public class _XSingleSelectQueryComposer extends MultiMethodTest {
56 
57     // oObj filled by MultiMethodTest
58     public XSingleSelectQueryComposer oObj = null ;
59 
60     private String queryString = "SELECT * FROM \"biblio\"";
61 
62     private XSingleSelectQueryAnalyzer xQueryAna = null;
63 
64     private XPropertySet xProp = null;
65 
66     private String colName = null;
67 
68     /**
69      * Retcieves the object relations:
70     * <ul>
71     *  <li><code>XSingleSelectQueryAnalyzer xQueryAna</code></li>
72     *  <li><code>XPropertySet xProp</code></li>
73     *  <li><code>String colName</code></li>
74     * </ul> <p>
75      * @see com.sun.star.sdb.XSingleSelectQueryAnalyzer
76      * @see com.sun.star.beans.XPropertySet
77      */
78     protected void before() /* throws Exception*/ {
79 
80         xQueryAna = (XSingleSelectQueryAnalyzer)
81                       UnoRuntime.queryInterface(XSingleSelectQueryAnalyzer.class,
82                       tEnv.getObjRelation("xQueryAna"));
83 
84         if (xQueryAna == null) {
85             throw new StatusException(Status.failed(
86            "Couldn't get object relation 'xQueryAna'. Test must be modified"));
87 
88         }
89 
90         xProp = (XPropertySet)
91                       UnoRuntime.queryInterface(XPropertySet.class,
92                       tEnv.getObjRelation("xProp"));
93 
94         if (xProp == null) {
95             throw new StatusException(Status.failed(
96            "Couldn't get object relation 'xProp'. Test must be modified"));
97 
98         }
99 
100         try
101         {
102             colName = AnyConverter.toString(tEnv.getObjRelation("colName"));
103         }
104         catch (com.sun.star.lang.IllegalArgumentException e)
105         {
106             colName = null;
107         }
108 
109         if (colName == null) {
110             throw new StatusException(Status.failed(
111            "Couldn't get object relation 'colName'. Test must be modified"));
112 
113         }
114 
115     }
116 
117 
118     /**
119     * Object relation <code>xQueryAna</code> set a filter. This filter
120     * must returned while calling <code>getFilter</code>
121     */
122     public void _setFilter() {
123         try{
124             String filter = "\"Identifier\" = 'BOR02b'";
125             oObj.setFilter(filter);
126             tRes.tested("setFilter()", (xQueryAna.getFilter().equals(filter)));
127 
128         } catch (com.sun.star.sdbc.SQLException e){
129             log.println("unexpected Exception: " + e.toString());
130             tRes.tested("setFilter()", false);
131         }
132     }
133 
134     /**
135     * Object relation <code>xQueryAna</code> set a complex filter with method
136     . <code>setFilter</code>. Then <code>getStructuredFilter</code> returns a
137     * sequenze of <code>PropertyValue</code> which was set with method
138     * <code>setStructuredFilter</code> from <code>xQueryAna</code>.
139     * Then test has ok status if <code>getFilter</code> returns the complex filter.
140     */
141     public void _setStructuredFilter() {
142         requiredMethod("setFilter()");
143         try{
144             xQueryAna.setQuery("SELECT \"Identifier\", \"Type\", \"Address\" FROM \"biblio\" \"biblio\"");
145             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' )";
146             oObj.setFilter(complexFilter);
147             PropertyValue[][] aStructuredFilter = xQueryAna.getStructuredFilter();
148             oObj.setFilter("");
149             oObj.setStructuredFilter(aStructuredFilter);
150             tRes.tested("setStructuredFilter()", (xQueryAna.getFilter().equals(complexFilter)));
151 
152         } catch (com.sun.star.sdbc.SQLException e){
153             log.println("unexpected Exception: " + e.toString());
154             tRes.tested("setStructuredFilter()", false);
155         } catch (com.sun.star.lang.IllegalArgumentException e){
156             log.println("unexpected Exception: " + e.toString());
157             tRes.tested("setStructuredFilter()", false);
158         }
159     }
160 
161     /**
162     * At first the object relation <code>xProp</code> was set as parameter.
163     * Relation <code>xQueryAna</code> was used to check if realtion
164     * <code>colName</code> was found.
165     * Second an empty <code>XPropertySet</code> was used as parameter. A
166     * <code>com.sun.star.sdbc.SQLException</code> must be thrown.
167     */
168     public void _appendFilterByColumn() {
169         boolean ok = true;
170         try{
171 
172             oObj.appendFilterByColumn(xProp, true,SQLFilterOperator.EQUAL);
173             log.println("appendFilterByColumn: " + xQueryAna.getFilter());
174             ok = ok && (xQueryAna.getFilter().indexOf(colName) > 0);
175 
176         } catch (com.sun.star.sdbc.SQLException e){
177             log.println("unexpected Exception: " + e.toString());
178             tRes.tested("appendFilterByColumn()", false);
179         }
180 
181         try{
182 
183             oObj.appendFilterByColumn(xProp, false,SQLFilterOperator.EQUAL);
184             log.println("appendFilterByColumn: " + xQueryAna.getFilter());
185             ok = ok && (xQueryAna.getFilter().indexOf(colName) > 0);
186 
187         } catch (com.sun.star.sdbc.SQLException e){
188             log.println("unexpected Exception: " + e.toString());
189             tRes.tested("appendFilterByColumn()", false);
190         }
191 
192         try{
193             XPropertySet dummy = null;
194             oObj.appendFilterByColumn(dummy, true,SQLFilterOperator.EQUAL);
195             log.println("expected Exception was not thrown");
196             tRes.tested("appendFilterByColumn()", false);
197 
198         } catch (com.sun.star.sdbc.SQLException e){
199             log.println("expected Exception");
200             ok = ok && true;
201         }
202         tRes.tested("appendFilterByColumn()", ok);
203     }
204 
205     /**
206     * At first the object relation <code>xProp</code> was used as parameter.
207     * Relation <code>xQueryAna</code> was used to check if realtion
208     * <code>colName</code> was found.
209     * Second an empty <code>XPropertySet</code> was used as parameter. An
210     * <code>com.sun.star.sdbc.SQLException</code> must be thrown.
211     */
212     public void _appendGroupByColumn() {
213         boolean ok = true;
214         try{
215 
216             oObj.appendGroupByColumn(xProp);
217             log.println("appendGroupByColumn: " + xQueryAna.getFilter());
218             ok = ok && (xQueryAna.getFilter().indexOf(colName) > 0);
219 
220         } catch (com.sun.star.sdbc.SQLException e){
221             log.println("unexpected Exception: " + e.toString());
222             tRes.tested("appendGroupByColumn()", false);
223         }
224         try{
225             XPropertySet dummy = null;
226             oObj.appendGroupByColumn(dummy);
227             log.println("expected Exception was not thrown");
228             tRes.tested("appendGroupByColumn()", false);
229 
230         } catch (com.sun.star.sdbc.SQLException e){
231             log.println("expected Exception");
232             ok = ok && true;
233         }
234         tRes.tested("appendGroupByColumn()", ok);
235     }
236 
237     /**
238     * The group which was setted by <code>setGroup</code> must be returned
239     * while calling from object relation <code>XQueryAna</code>
240     * method <code>getGroup</code>
241     */
242     public void _setGroup() {
243         try{
244             String group = "\"Identifier\"";
245             oObj.setGroup(group);
246             tRes.tested("setGroup()", (xQueryAna.getGroup().equals(group)));
247 
248         } catch (com.sun.star.sdbc.SQLException e){
249             log.println("unexpected Exception: " + e.toString());
250             tRes.tested("setGroup()", false);
251         }
252     }
253 
254 
255     /**
256     * The cluase which was setted by <code>setHavingClause</code> must be returned
257     * while calling from object relation <code>XQueryAna</code>
258     * method <code>getHavingClause</code>
259     */
260     public void _setHavingClause() {
261         try{
262             String clause = "\"Identifier\" = 'BOR02b'";
263             oObj.setHavingClause(clause);
264             tRes.tested("setHavingClause()", (
265                                    xQueryAna.getHavingClause().equals(clause)));
266 
267         } catch (com.sun.star.sdbc.SQLException e){
268             log.println("unexpected Exception: " + e.toString());
269             tRes.tested("setHavingClause()", false);
270         }
271     }
272 
273     /**
274     * At first <code>setHavingClause</code> sets a complex clause.
275     * Then method <code>getStructuredHavingClause</code> from object relation
276     * <code>xQueryAna</code> returns a valid <code>PropertyValue[][]</code>
277     * Method <code>setHavingClause</code> was called with an empty sting to
278     * reset filter. Now <code>setStructuredHavingClause</code> with the valid
279     * <code>PropertyValue[][]</code> as parameter was called.
280     * Test is ok if <code>getHavingClause</code> from <code>xQueryAna</code>
281     * returns the complex clause from beginning.
282     * <p>
283     * required methods:
284     *<ul>
285     * <li><code>setHavingClause</code></li>
286     * <li><code>setStructuredFilter</code></li>
287     *</ul>
288     */
289     public void _setStructuredHavingClause() {
290         requiredMethod("setHavingClause()");
291         executeMethod("setStructuredFilter()");
292         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' )";
293 
294         try{
295            oObj.setHavingClause(complexFilter);
296            PropertyValue[][] aStructuredHaving =
297                                           xQueryAna.getStructuredHavingClause();
298            oObj.setHavingClause("");
299            oObj.setStructuredHavingClause(aStructuredHaving);
300            tRes.tested("setStructuredHavingClause()",
301                            (xQueryAna.getHavingClause().equals(complexFilter)));
302 
303         } catch (com.sun.star.sdbc.SQLException e){
304             log.println("unexpected Exception: " + e.toString());
305             tRes.tested("setStructuredHavingClause()", false);
306         }
307     }
308 
309     /**
310     * First object relation <code>xProp</code> was used as parameter. Relation
311     * <code>xQueryAna</code> was used to check if realtion <code>colName</code>
312     * was found.
313     * Second an empty <code>XPropertySet</code> was given as parameter. An
314     * <code>com.sun.star.sdbc.SQLException</code> must be thrown.
315     */
316     public void _appendHavingClauseByColumn() {
317         boolean ok = true;
318         try{
319 
320             oObj.appendHavingClauseByColumn(xProp, true,SQLFilterOperator.EQUAL);
321             log.println("appendHavingClauseByColumn: " + xQueryAna.getFilter());
322             ok = ok && (xQueryAna.getFilter().indexOf(colName) > 0);
323 
324         } catch (com.sun.star.sdbc.SQLException e){
325             log.println("unexpected Exception: " + e.toString());
326             tRes.tested("appendHavingClauseByColumn()", false);
327         }
328         try{
329             XPropertySet dummy = null;
330             oObj.appendHavingClauseByColumn(dummy, true,SQLFilterOperator.EQUAL);
331             log.println("expected Exception was not thrown");
332             tRes.tested("appendHavingClauseByColumn()", false);
333 
334         } catch (com.sun.star.sdbc.SQLException e){
335             log.println("expected Exception");
336             ok = ok && true;
337         }
338         tRes.tested("appendHavingClauseByColumn()", ok);
339     }
340 
341     /**
342     * First object relation <code>xProp</code> was set as parameter. Relation
343     * <code>xQueryAna</code> was used to check if realtion <code>colName</code>
344     * was found.
345     * Second an empty <code>XPropertySet</code> was given as parameter. An
346     * <code>com.sun.star.sdbc.SQLException</code> must be thrown.
347     */
348     public void _appendOrderByColumn() {
349         boolean ok = true;
350         try{
351 
352             oObj.appendOrderByColumn(xProp, true);
353             log.println("appendOrderByColumn: " + xQueryAna.getFilter());
354             ok = ok && (xQueryAna.getFilter().indexOf(colName) > 0);
355 
356         } catch (com.sun.star.sdbc.SQLException e){
357             log.println("unexpected Exception: " + e.toString());
358             tRes.tested("appendOrderByColumn()", false);
359         }
360         try{
361             XPropertySet dummy = null;
362             oObj.appendOrderByColumn(dummy, true);
363             log.println("expected Exception was not thrown");
364             tRes.tested("appendOrderByColumn()", false);
365 
366         } catch (com.sun.star.sdbc.SQLException e){
367             log.println("expected Exception");
368             ok = ok && true;
369         }
370         tRes.tested("appendOrderByColumn()", ok);
371     }
372 
373 
374     /**
375     * Method <code>getOrder</code> from object relation <code>xQueryAna</code>
376     * checks the order which was setted while calling <code>setOrder</code>
377     */
378     public void _setOrder() {
379         try{
380             String order = "\"Identifier\"";
381             oObj.setOrder(order);
382             tRes.tested("setOrder()", (xQueryAna.getOrder().equals(order)));
383 
384         } catch (com.sun.star.sdbc.SQLException e){
385             log.println("unexpected Exception: " + e.toString());
386             tRes.tested("setOrder()", false);
387         }
388     }
389 
390 
391 
392 }  // finish class _XSingleSelectQueryComposer
393