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 * Retrieves 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 * sequence 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 set 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 clause which was set 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 string 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' )";
363 oObj
.setHavingClause(complexFilter
);
364 PropertyValue
[][] aStructuredHaving
=
365 xQueryAna
.getStructuredHavingClause();
366 oObj
.setHavingClause("");
367 oObj
.setStructuredHavingClause(aStructuredHaving
);
368 tRes
.tested("setStructuredHavingClause()",
369 xQueryAna
.getHavingClause().equals(complexFilter
));
371 } catch (SQLException e
){
372 log
.println("unexpected Exception: " + e
.toString());
373 tRes
.tested("setStructuredHavingClause()", false);
378 * First object relation <code>xProp</code> was used as parameter. Relation
379 * <code>xQueryAna</code> was used to check if relation <code>colName</code>
381 * Second an empty <code>XPropertySet</code> was given as parameter. An
382 * <code>SQLException</code> must be thrown.
384 public void _appendHavingClauseByColumn() {
388 xReSet
.beforeFirst();
390 catch (SQLException e
)
392 log
.println("unexpected Exception: " + e
.toString());
393 tRes
.tested("appendHavingClauseByColumn()", false);
397 oObj
.appendHavingClauseByColumn(xProp
, true,SQLFilterOperator
.EQUAL
);
398 log
.println("expected Exception was not thrown");
399 tRes
.tested("appendHavingClauseByColumn()", false);
403 catch (com
.sun
.star
.uno
.RuntimeException e
)
405 log
.println("expected Exception: " + e
.toString());
408 catch (SQLException e
)
410 log
.println("unexpected Exception: " + e
.toString());
411 tRes
.tested("appendHavingClauseByColumn()", false);
412 } catch (com
.sun
.star
.lang
.WrappedTargetException e
)
414 log
.println("unexpected Exception: " + e
.toString());
415 tRes
.tested("appendHavingClauseByColumn()", false);
422 catch (SQLException e
)
424 log
.println("unexpected Exception: " + e
.toString());
425 tRes
.tested("appendHavingClauseByColumn()", false);
429 oObj
.appendHavingClauseByColumn(xProp
, true,SQLFilterOperator
.EQUAL
);
430 log
.println("appendHavingClauseByColumn: " + xQueryAna
.getFilter());
431 ok
= ok
&& (xQueryAna
.getFilter().indexOf(colName
) > 0);
433 } catch (SQLException e
){
434 log
.println("unexpected Exception: " + e
.toString());
435 tRes
.tested("appendHavingClauseByColumn()", false);
436 } catch (com
.sun
.star
.lang
.WrappedTargetException e
)
438 log
.println("unexpected Exception: " + e
.toString());
439 tRes
.tested("appendHavingClauseByColumn()", false);
443 XPropertySet dummy
= null;
444 oObj
.appendHavingClauseByColumn(dummy
, true,SQLFilterOperator
.EQUAL
);
445 log
.println("expected Exception was not thrown");
446 tRes
.tested("appendHavingClauseByColumn()", false);
448 } catch (SQLException e
){
449 log
.println("expected Exception");
451 } catch (com
.sun
.star
.lang
.WrappedTargetException e
)
453 log
.println("unexpected Exception: " + e
.toString());
454 tRes
.tested("appendHavingClauseByColumn()", false);
460 xReSet
.beforeFirst();
462 catch (SQLException e
)
464 log
.println("unexpected Exception: " + e
.toString());
465 tRes
.tested("appendHavingClauseByColumn()", false);
467 tRes
.tested("appendHavingClauseByColumn()", ok
);
471 * First object relation <code>xProp</code> was set as parameter. Relation
472 * <code>xQueryAna</code> was used to check if relation <code>colName</code>
474 * Second an empty <code>XPropertySet</code> was given as parameter. An
475 * <code>SQLException</code> must be thrown.
477 public void _appendOrderByColumn() {
481 oObj
.appendOrderByColumn(xProp
, true);
482 log
.println("appendOrderByColumn: " + xQueryAna
.getFilter());
483 ok
= ok
&& (xQueryAna
.getFilter().indexOf(colName
) > 0);
485 } catch (SQLException e
){
486 log
.println("unexpected Exception: " + e
.toString());
487 tRes
.tested("appendOrderByColumn()", false);
490 XPropertySet dummy
= null;
491 oObj
.appendOrderByColumn(dummy
, true);
492 log
.println("expected Exception was not thrown");
493 tRes
.tested("appendOrderByColumn()", false);
495 } catch (SQLException e
){
496 log
.println("expected Exception");
499 tRes
.tested("appendOrderByColumn()", ok
);
504 * Method <code>getOrder</code> from object relation <code>xQueryAna</code>
505 * checks the order which was set while calling <code>setOrder</code>
507 public void _setOrder() {
509 String order
= "\"Identifier\"";
510 oObj
.setOrder(order
);
511 tRes
.tested("setOrder()", xQueryAna
.getOrder().equals(order
));
513 } catch (SQLException e
){
514 log
.println("unexpected Exception: " + e
.toString());
515 tRes
.tested("setOrder()", false);
521 } // finish class _XSingleSelectQueryComposer