Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / sdb / _XSingleSelectQueryComposer.java
blob53b31f6931f52851c2934c9f31bdb1534a39fc12
1 /*
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 .
19 package ifc.sdb;
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;
28 import lib.Status;
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;
34 /**
35 * Testing <code>com.sun.star.sdb.XSingleSelectQueryComposer</code>
36 * interface methods :
37 * <ul>
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>
49 * </ul> <p>
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;
65 /**
66 * Retrieves the object relations:
67 * <ul>
68 * <li><code>XSingleSelectQueryAnalyzer xQueryAna</code></li>
69 * <li><code>XPropertySet xProp</code></li>
70 * <li><code>String colName</code></li>
71 * </ul> <p>
72 * @see com.sun.star.sdb.XSingleSelectQueryAnalyzer
73 * @see com.sun.star.beans.XPropertySet
75 @Override
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"));
90 if (xProp == null) {
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"));
99 if (xReSet == null) {
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)
111 colName = null;
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() {
128 try{
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()");
148 try{
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();
153 oObj.setFilter("");
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() {
174 boolean ok = true;
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);
189 ok = false;
191 catch (com.sun.star.uno.RuntimeException e)
193 log.println("expected Exception: " + e.toString());
194 ok = ok && true;
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);
209 xReSet.first();
211 catch (SQLException e)
213 log.println("unexpected Exception: " + e.toString());
214 tRes.tested("appendFilterByColumn()", false);
216 try{
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);
232 try{
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);
247 try{
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");
255 ok = ok && true;
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() {
282 boolean ok = true;
283 try{
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);
293 try{
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");
301 ok = ok && true;
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() {
312 try{
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() {
330 try{
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.
351 * <p>
352 * required methods:
353 *<ul>
354 * <li><code>setHavingClause</code></li>
355 * <li><code>setStructuredFilter</code></li>
356 *</ul>
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' )";
362 try{
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>
380 * was found.
381 * Second an empty <code>XPropertySet</code> was given as parameter. An
382 * <code>SQLException</code> must be thrown.
384 public void _appendHavingClauseByColumn() {
385 boolean ok = true;
388 xReSet.beforeFirst();
390 catch (SQLException e)
392 log.println("unexpected Exception: " + e.toString());
393 tRes.tested("appendHavingClauseByColumn()", false);
395 try{
397 oObj.appendHavingClauseByColumn(xProp, true,SQLFilterOperator.EQUAL);
398 log.println("expected Exception was not thrown");
399 tRes.tested("appendHavingClauseByColumn()", false);
400 ok = false;
403 catch (com.sun.star.uno.RuntimeException e)
405 log.println("expected Exception: " + e.toString());
406 ok = ok && true;
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);
420 xReSet.first();
422 catch (SQLException e)
424 log.println("unexpected Exception: " + e.toString());
425 tRes.tested("appendHavingClauseByColumn()", false);
427 try{
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);
442 try{
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");
450 ok = ok && true;
451 } catch (com.sun.star.lang.WrappedTargetException e)
453 log.println("unexpected Exception: " + e.toString());
454 tRes.tested("appendHavingClauseByColumn()", false);
457 // cleanup
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>
473 * was found.
474 * Second an empty <code>XPropertySet</code> was given as parameter. An
475 * <code>SQLException</code> must be thrown.
477 public void _appendOrderByColumn() {
478 boolean ok = true;
479 try{
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);
489 try{
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");
497 ok = ok && true;
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() {
508 try{
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