Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / util / _XSortable.java
blobe43c93c16ad99c6fbd3e170bb3b8c4b25356d129
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 .
18 package ifc.util;
20 import java.io.PrintWriter;
22 import lib.MultiMethodTest;
23 import lib.Status;
24 import lib.StatusException;
26 import com.sun.star.beans.PropertyValue;
27 import com.sun.star.table.TableSortField;
28 import com.sun.star.util.XSortable;
31 /**
32 * Testing <code>com.sun.star.util.XSortable</code>
33 * interface methods :
34 * <ul>
35 * <li><code> createSortDescriptor()</code></li>
36 * <li><code> sort()</code></li>
37 * </ul> <p>
38 * This test needs the following object relations :
39 * <ul>
40 * <li> <code>'SORTCHECKER'</code> : <code>
41 * _XSortable.XSortChecker</code> interface implementation
42 * </li>
43 * <ul><p>
44 * Test is <b> NOT </b> multithread compliant. <p>
45 * @see com.sun.star.util.XSortable
47 public class _XSortable extends MultiMethodTest {
48 // oObj filled by MultiMethodTest
49 public XSortable oObj = null;
50 XSortChecker checker = null;
51 PropertyValue[] oPV = null;
53 @Override
54 protected void before() {
55 checker = (XSortChecker) tEnv.getObjRelation("SORTCHECKER");
57 if (checker == null) {
58 throw new StatusException(Status.failed(
59 "Couldn't get relation 'SORTCHECKER'"));
62 checker.setPrintWriter(log);
65 /**
66 * Test calls the method. <p>
67 * Has <b> OK </b> status if the length of the returned array
68 * is greater than zero. <p>
70 public void _createSortDescriptor() {
71 boolean bResult = false;
73 log.println("test for createSortDescriptor() ");
74 oPV = oObj.createSortDescriptor();
76 if (oPV.length > 0) {
77 bResult = true;
79 for (int k = 0; k < oPV.length; k++) {
80 log.println("DescriptorProperty " + k + ": Name=" +
81 oPV[k].Name + "; Value=" + oPV[k].Value);
83 if (oPV[k].Name.equals("SortFields")) {
84 TableSortField[] tsf = (TableSortField[]) oPV[k].Value;
86 for (int l = 0; l < tsf.length; l++) {
87 log.println("\t isAscending: " +
88 tsf[l].IsAscending);
89 log.println("\t IsCaseSensitive: " +
90 tsf[l].IsCaseSensitive);
91 log.println("\t CollatorAlgorithm: " +
92 tsf[l].CollatorAlgorithm);
98 log.println("Found " + oPV.length + " PropertyValues");
99 tRes.tested("createSortDescriptor()", bResult);
103 * Test calls the method using descriptor created before as
104 * parameter. <p>
105 * Has <b> OK </b> status if the method successfully returns
106 * and no exceptions were thrown. <p>
107 * The following method tests are to be completed successfully before :
108 * <ul>
109 * <li> <code> createSortDescriptor() </code> : to have a descriptor
110 * for sort. </li>
111 * </ul>
113 public void _sort() {
115 checker.prepareToSort();
117 log.println(
118 "############## Sort algorithm: Alphanumeric Order: Ascending");
119 modifyDescriptor(false, true);
120 oObj.sort(oPV);
122 boolean res = checker.checkSort(false, true);
123 log.println(
124 "############################################################");
126 log.println(
127 "############# Sort algorithm: Alphanumeric Order: Descending");
128 modifyDescriptor(false, false);
129 oObj.sort(oPV);
130 res = checker.checkSort(false, false);
131 log.println(
132 "############################################################");
134 log.println(
135 "################# Sort algorithm: Numeric Order: Ascending");
136 modifyDescriptor(true, true);
137 oObj.sort(oPV);
138 res = checker.checkSort(true, true);
139 log.println(
140 "############################################################");
142 log.println(
143 "################## Sort algorithm: Numeric Order: Descending");
144 modifyDescriptor(true, false);
145 oObj.sort(oPV);
146 res = checker.checkSort(true, false);
147 log.println(
148 "############################################################");
150 tRes.tested("sort()", res);
153 protected void modifyDescriptor(boolean isSortNumeric,
154 boolean isSortAscending) {
155 for (int i = 0; i < oPV.length; i++) {
156 if (oPV[i].Name.equals("SortFields")) {
157 TableSortField[] TableFields = (TableSortField[]) oPV[i].Value;
159 if (TableFields.length == 0) {
160 TableFields = new TableSortField[1];
161 TableFields[0] = new TableSortField();
164 for (int k = 0; k < TableFields.length; k++) {
165 TableFields[k].IsAscending = isSortAscending;
167 if (isSortNumeric) {
168 TableFields[k].FieldType = com.sun.star.table.TableSortFieldType.NUMERIC;
169 TableFields[k].CollatorAlgorithm = "numeric";
170 } else {
171 TableFields[k].FieldType = com.sun.star.table.TableSortFieldType.ALPHANUMERIC;
172 TableFields[k].CollatorAlgorithm = "alphanumeric";
176 oPV[i].Value = TableFields;
179 if (oPV[i].Name.equals("isSortInTable")) {
180 oPV[i].Value = Boolean.TRUE;
183 if (oPV[i].Name.equals("IsSortColumns")) {
184 oPV[i].Value = Boolean.FALSE;
188 log.println("Modified sort descriptor: ");
190 if (oPV.length > 0) {
191 for (int k = 0; k < oPV.length; k++) {
192 log.println("DescriptorProperty " + k + ": Name=" +
193 oPV[k].Name + "; Value=" + oPV[k].Value);
195 if (oPV[k].Name.equals("SortFields")) {
196 TableSortField[] tsf = (TableSortField[]) oPV[k].Value;
198 for (int l = 0; l < tsf.length; l++) {
199 log.println("\t isAscending: " +
200 tsf[l].IsAscending);
201 log.println("\t IsCaseSensitive: " +
202 tsf[l].IsCaseSensitive);
203 log.println("\t CollatorAlgorithm: " +
204 tsf[l].CollatorAlgorithm);
212 * The interface for sort checking.
214 public interface XSortChecker {
215 void prepareToSort();
217 boolean checkSort(boolean isSortNumbering,
218 boolean isSortAscending);
220 void setPrintWriter(PrintWriter log);
224 * Forces environment recreation.
226 @Override
227 protected void after() {
228 disposeEnvironment();
231 } // finish class _XSortable