2 * This file is part of the LibreOffice project.
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/.
8 * This file incorporates work covered by the following license notice:
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 .
21 import com
.sun
.star
.sdb
.XSingleSelectQueryComposer
;
22 import lib
.MultiMethodTest
;
23 import com
.sun
.star
.sdb
.XSingleSelectQueryAnalyzer
;
24 import com
.sun
.star
.sdbc
.XResultSet
;
25 import com
.sun
.star
.uno
.AnyConverter
;
26 import com
.sun
.star
.uno
.UnoRuntime
;
27 import lib
.StatusException
;
29 import com
.sun
.star
.beans
.PropertyValue
;
30 import com
.sun
.star
.beans
.XPropertySet
;
31 import com
.sun
.star
.sdb
.SQLFilterOperator
;
32 import com
.sun
.star
.sdbc
.SQLException
;
35 * Testing <code>com.sun.star.sdb.XSingleSelectQueryComposer</code>
38 * <li><code>setFilter()</code></li>
39 * <li><code>setStructuredFilter()</code></li>
40 * <li><code>appendFilterByColumn()</code></li>
41 * <li><code>appendGroupByColumn()</code></li>
42 * <li><code>setGroup()</code></li>
43 * <li><code>setHavingClause()</code></li>
44 * <li><code>setStructuredHavingClause()</code></li>
45 * <li><code>appendHavingClauseByColumn()</code></li>
46 * <li><code>appendOrderByColumn()</code></li>
47 * <li><code>setOrder()</code></li>
50 * @see com.sun.star.sdb.XSingleSelectQueryComposer
52 public class _XSingleSelectQueryComposer
extends MultiMethodTest
{
54 // oObj filled by MultiMethodTest
55 public XSingleSelectQueryComposer oObj
= null ;
57 private XSingleSelectQueryAnalyzer xQueryAna
= null;
59 private XPropertySet xProp
= null;
61 private String colName
= null;
63 private XResultSet xReSet
= null;
66 * Retcieves the object relations:
68 * <li><code>XSingleSelectQueryAnalyzer xQueryAna</code></li>
69 * <li><code>XPropertySet xProp</code></li>
70 * <li><code>String colName</code></li>
72 * @see com.sun.star.sdb.XSingleSelectQueryAnalyzer
73 * @see com.sun.star.beans.XPropertySet
76 protected void before() /* throws Exception*/ {
78 xQueryAna
= UnoRuntime
.queryInterface(XSingleSelectQueryAnalyzer
.class,
79 tEnv
.getObjRelation("xQueryAna"));
81 if (xQueryAna
== null) {
82 throw new StatusException(Status
.failed(
83 "Couldn't get object relation 'xQueryAna'. Test must be modified"));
87 xProp
= UnoRuntime
.queryInterface(XPropertySet
.class,
88 tEnv
.getObjRelation("xProp"));
91 throw new StatusException(Status
.failed(
92 "Couldn't get object relation 'xProp'. Test must be modified"));
96 xReSet
= UnoRuntime
.queryInterface(XResultSet
.class,
97 tEnv
.getObjRelation("xResultSet"));
100 throw new StatusException(Status
.failed(
101 "Couldn't get object relation 'xResultSet'. Test must be modified"));
107 colName
= AnyConverter
.toString(tEnv
.getObjRelation("colName"));
109 catch (com
.sun
.star
.lang
.IllegalArgumentException e
)
114 if (colName
== null) {
115 throw new StatusException(Status
.failed(
116 "Couldn't get object relation 'colName'. Test must be modified"));
124 * Object relation <code>xQueryAna</code> set a filter. This filter
125 * must returned while calling <code>getFilter</code>
127 public void _setFilter() {
129 String filter
= "\"Identifier\" = 'BOR02b'";
130 oObj
.setFilter(filter
);
131 tRes
.tested("setFilter()", (xQueryAna
.getFilter().equals(filter
)));
133 } catch (SQLException e
){
134 log
.println("unexpected Exception: " + e
.toString());
135 tRes
.tested("setFilter()", false);
140 * Object relation <code>xQueryAna</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 <code>xQueryAna</code>.
144 * Then test has ok status if <code>getFilter</code> returns the complex filter.
146 public void _setStructuredFilter() {
147 requiredMethod("setFilter()");
149 xQueryAna
.setQuery("SELECT \"Identifier\", \"Type\", \"Address\" FROM \"biblio\" \"biblio\"");
150 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' )";
151 oObj
.setFilter(complexFilter
);
152 PropertyValue
[][] aStructuredFilter
= xQueryAna
.getStructuredFilter();
154 oObj
.setStructuredFilter(aStructuredFilter
);
155 tRes
.tested("setStructuredFilter()", (xQueryAna
.getFilter().equals(complexFilter
)));
157 } catch (SQLException e
){
158 log
.println("unexpected Exception: " + e
.toString());
159 tRes
.tested("setStructuredFilter()", false);
160 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
){
161 log
.println("unexpected Exception: " + e
.toString());
162 tRes
.tested("setStructuredFilter()", false);
167 * At first the object relation <code>xProp</code> was set as parameter.
168 * Relation <code>xQueryAna</code> was used to check if relation
169 * <code>colName</code> was found.
170 * Second an empty <code>XPropertySet</code> was used as parameter. A
171 * <code>SQLException</code> must be thrown.
173 public void _appendFilterByColumn() {
177 xReSet
.beforeFirst();
179 catch (SQLException e
)
181 log
.println("unexpected Exception: " + e
.toString());
182 tRes
.tested("appendFilterByColumn()", false);
186 oObj
.appendFilterByColumn(xProp
, true,SQLFilterOperator
.EQUAL
);
187 log
.println("expected Exception was not thrown");
188 tRes
.tested("appendFilterByColumn()", false);
191 catch (com
.sun
.star
.uno
.RuntimeException e
)
193 log
.println("expected Exception: " + e
.toString());
196 catch (SQLException e
)
198 log
.println("unexpected Exception: " + e
.toString());
199 tRes
.tested("appendFilterByColumn()", false);
201 catch (com
.sun
.star
.lang
.WrappedTargetException e
)
203 log
.println("unexpected Exception: " + e
.toString());
204 tRes
.tested("appendFilterByColumn()", false);
211 catch (SQLException e
)
213 log
.println("unexpected Exception: " + e
.toString());
214 tRes
.tested("appendFilterByColumn()", false);
218 oObj
.appendFilterByColumn(xProp
, true,SQLFilterOperator
.EQUAL
);
219 log
.println("appendFilterByColumn: " + xQueryAna
.getFilter());
220 ok
= ok
&& (xQueryAna
.getFilter().indexOf(colName
) > 0);
222 } catch (SQLException e
){
223 log
.println("unexpected Exception: " + e
.toString());
224 tRes
.tested("appendFilterByColumn()", false);
226 catch (com
.sun
.star
.lang
.WrappedTargetException e
)
228 log
.println("unexpected Exception: " + e
.toString());
229 tRes
.tested("appendFilterByColumn()", false);
234 oObj
.appendFilterByColumn(xProp
, false,SQLFilterOperator
.EQUAL
);
235 log
.println("appendFilterByColumn: " + xQueryAna
.getFilter());
236 ok
= ok
&& (xQueryAna
.getFilter().indexOf(colName
) > 0);
238 } catch (SQLException e
){
239 log
.println("unexpected Exception: " + e
.toString());
240 tRes
.tested("appendFilterByColumn()", false);
241 } catch (com
.sun
.star
.lang
.WrappedTargetException e
)
243 log
.println("unexpected Exception: " + e
.toString());
244 tRes
.tested("appendFilterByColumn()", false);
248 XPropertySet dummy
= null;
249 oObj
.appendFilterByColumn(dummy
, true,SQLFilterOperator
.EQUAL
);
250 log
.println("expected Exception was not thrown");
251 tRes
.tested("appendFilterByColumn()", false);
253 } catch (SQLException e
){
254 log
.println("expected Exception");
256 } catch (com
.sun
.star
.lang
.WrappedTargetException e
)
258 log
.println("unexpected Exception: " + e
.toString());
259 tRes
.tested("appendFilterByColumn()", false);
264 xReSet
.beforeFirst();
266 catch (SQLException e
)
268 log
.println("unexpected Exception: " + e
.toString());
269 tRes
.tested("appendFilterByColumn()", false);
271 tRes
.tested("appendFilterByColumn()", ok
);
275 * At first the object relation <code>xProp</code> was used as parameter.
276 * Relation <code>xQueryAna</code> was used to check if relation
277 * <code>colName</code> was found.
278 * Second an empty <code>XPropertySet</code> was used as parameter. An
279 * <code>SQLException</code> must be thrown.
281 public void _appendGroupByColumn() {
285 oObj
.appendGroupByColumn(xProp
);
286 log
.println("appendGroupByColumn: " + xQueryAna
.getFilter());
287 ok
= ok
&& (xQueryAna
.getFilter().indexOf(colName
) > 0);
289 } catch (SQLException e
){
290 log
.println("unexpected Exception: " + e
.toString());
291 tRes
.tested("appendGroupByColumn()", false);
294 XPropertySet dummy
= null;
295 oObj
.appendGroupByColumn(dummy
);
296 log
.println("expected Exception was not thrown");
297 tRes
.tested("appendGroupByColumn()", false);
299 } catch (SQLException e
){
300 log
.println("expected Exception");
303 tRes
.tested("appendGroupByColumn()", ok
);
307 * The group which was setted by <code>setGroup</code> must be returned
308 * while calling from object relation <code>XQueryAna</code>
309 * method <code>getGroup</code>
311 public void _setGroup() {
313 String group
= "\"Identifier\"";
314 oObj
.setGroup(group
);
315 tRes
.tested("setGroup()", (xQueryAna
.getGroup().equals(group
)));
317 } catch (SQLException e
){
318 log
.println("unexpected Exception: " + e
.toString());
319 tRes
.tested("setGroup()", false);
325 * The cluase which was setted by <code>setHavingClause</code> must be returned
326 * while calling from object relation <code>XQueryAna</code>
327 * method <code>getHavingClause</code>
329 public void _setHavingClause() {
331 String clause
= "\"Identifier\" = 'BOR02b'";
332 oObj
.setHavingClause(clause
);
333 tRes
.tested("setHavingClause()", (
334 xQueryAna
.getHavingClause().equals(clause
)));
336 } catch (SQLException e
){
337 log
.println("unexpected Exception: " + e
.toString());
338 tRes
.tested("setHavingClause()", false);
343 * At first <code>setHavingClause</code> sets a complex clause.
344 * Then method <code>getStructuredHavingClause</code> from object relation
345 * <code>xQueryAna</code> returns a valid <code>PropertyValue[][]</code>
346 * Method <code>setHavingClause</code> was called with an empty sting to
347 * reset filter. Now <code>setStructuredHavingClause</code> with the valid
348 * <code>PropertyValue[][]</code> as parameter was called.
349 * Test is ok if <code>getHavingClause</code> from <code>xQueryAna</code>
350 * returns the complex clause from beginning.
354 * <li><code>setHavingClause</code></li>
355 * <li><code>setStructuredFilter</code></li>
358 public void _setStructuredHavingClause() {
359 requiredMethod("setHavingClause()");
360 executeMethod("setStructuredFilter()");
361 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' )";
364 oObj
.setHavingClause(complexFilter
);
365 PropertyValue
[][] aStructuredHaving
=
366 xQueryAna
.getStructuredHavingClause();
367 oObj
.setHavingClause("");
368 oObj
.setStructuredHavingClause(aStructuredHaving
);
369 tRes
.tested("setStructuredHavingClause()",
370 (xQueryAna
.getHavingClause().equals(complexFilter
)));
372 } catch (SQLException e
){
373 log
.println("unexpected Exception: " + e
.toString());
374 tRes
.tested("setStructuredHavingClause()", false);
379 * First object relation <code>xProp</code> was used as parameter. Relation
380 * <code>xQueryAna</code> was used to check if relation <code>colName</code>
382 * Second an empty <code>XPropertySet</code> was given as parameter. An
383 * <code>SQLException</code> must be thrown.
385 public void _appendHavingClauseByColumn() {
389 xReSet
.beforeFirst();
391 catch (SQLException e
)
393 log
.println("unexpected Exception: " + e
.toString());
394 tRes
.tested("appendHavingClauseByColumn()", false);
398 oObj
.appendHavingClauseByColumn(xProp
, true,SQLFilterOperator
.EQUAL
);
399 log
.println("expected Exception was not thrown");
400 tRes
.tested("appendHavingClauseByColumn()", false);
404 catch (com
.sun
.star
.uno
.RuntimeException e
)
406 log
.println("expected Exception: " + e
.toString());
409 catch (SQLException e
)
411 log
.println("unexpected Exception: " + e
.toString());
412 tRes
.tested("appendHavingClauseByColumn()", false);
413 } catch (com
.sun
.star
.lang
.WrappedTargetException e
)
415 log
.println("unexpected Exception: " + e
.toString());
416 tRes
.tested("appendHavingClauseByColumn()", false);
423 catch (SQLException e
)
425 log
.println("unexpected Exception: " + e
.toString());
426 tRes
.tested("appendHavingClauseByColumn()", false);
430 oObj
.appendHavingClauseByColumn(xProp
, true,SQLFilterOperator
.EQUAL
);
431 log
.println("appendHavingClauseByColumn: " + xQueryAna
.getFilter());
432 ok
= ok
&& (xQueryAna
.getFilter().indexOf(colName
) > 0);
434 } catch (SQLException e
){
435 log
.println("unexpected Exception: " + e
.toString());
436 tRes
.tested("appendHavingClauseByColumn()", false);
437 } catch (com
.sun
.star
.lang
.WrappedTargetException e
)
439 log
.println("unexpected Exception: " + e
.toString());
440 tRes
.tested("appendHavingClauseByColumn()", false);
444 XPropertySet dummy
= null;
445 oObj
.appendHavingClauseByColumn(dummy
, true,SQLFilterOperator
.EQUAL
);
446 log
.println("expected Exception was not thrown");
447 tRes
.tested("appendHavingClauseByColumn()", false);
449 } catch (SQLException e
){
450 log
.println("expected Exception");
452 } catch (com
.sun
.star
.lang
.WrappedTargetException e
)
454 log
.println("unexpected Exception: " + e
.toString());
455 tRes
.tested("appendHavingClauseByColumn()", false);
461 xReSet
.beforeFirst();
463 catch (SQLException e
)
465 log
.println("unexpected Exception: " + e
.toString());
466 tRes
.tested("appendHavingClauseByColumn()", false);
468 tRes
.tested("appendHavingClauseByColumn()", ok
);
472 * First object relation <code>xProp</code> was set as parameter. Relation
473 * <code>xQueryAna</code> was used to check if relation <code>colName</code>
475 * Second an empty <code>XPropertySet</code> was given as parameter. An
476 * <code>SQLException</code> must be thrown.
478 public void _appendOrderByColumn() {
482 oObj
.appendOrderByColumn(xProp
, true);
483 log
.println("appendOrderByColumn: " + xQueryAna
.getFilter());
484 ok
= ok
&& (xQueryAna
.getFilter().indexOf(colName
) > 0);
486 } catch (SQLException e
){
487 log
.println("unexpected Exception: " + e
.toString());
488 tRes
.tested("appendOrderByColumn()", false);
491 XPropertySet dummy
= null;
492 oObj
.appendOrderByColumn(dummy
, true);
493 log
.println("expected Exception was not thrown");
494 tRes
.tested("appendOrderByColumn()", false);
496 } catch (SQLException e
){
497 log
.println("expected Exception");
500 tRes
.tested("appendOrderByColumn()", ok
);
505 * Method <code>getOrder</code> from object relation <code>xQueryAna</code>
506 * checks the order which was setted while calling <code>setOrder</code>
508 public void _setOrder() {
510 String order
= "\"Identifier\"";
511 oObj
.setOrder(order
);
512 tRes
.tested("setOrder()", (xQueryAna
.getOrder().equals(order
)));
514 } catch (SQLException e
){
515 log
.println("unexpected Exception: " + e
.toString());
516 tRes
.tested("setOrder()", false);
522 } // finish class _XSingleSelectQueryComposer