Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / mod / _sw / SwXTextTable.java
blobfe44aaccdd965f9a757f6a2bb0c8f1e204dd76d0
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 mod._sw;
20 import java.io.PrintWriter;
22 import lib.TestCase;
23 import lib.TestEnvironment;
24 import lib.TestParameters;
25 import util.SOfficeFactory;
26 import util.ValueComparer;
28 import com.sun.star.beans.XPropertySet;
29 import com.sun.star.table.XCell;
30 import com.sun.star.text.XTextContent;
31 import com.sun.star.text.XTextDocument;
32 import com.sun.star.text.XTextRange;
33 import com.sun.star.text.XTextTable;
34 import com.sun.star.uno.UnoRuntime;
37 /**
38 * this class tests the implementation of listed interfaces
39 * @see com.sun.star.beans.XPropertySet
40 * @see com.sun.star.container.XNamed
41 * @see com.sun.star.lang.XComponent
42 * @see com.sun.star.text.TextContent
43 * @see com.sun.star.text.XTextContent
44 * @see com.sun.star.text.XTextTable
46 public class SwXTextTable extends TestCase {
47 SOfficeFactory SOF;
48 XTextDocument xTextDoc;
50 @Override
51 protected void initialize(TestParameters tParam, PrintWriter log) throws Exception {
52 SOF = SOfficeFactory.getFactory( tParam.getMSF());
54 log.println("creating a textdocument");
55 xTextDoc = SOF.createTextDoc(null);
58 @Override
59 protected void cleanup(TestParameters tParam, PrintWriter log) {
60 log.println(" disposing xTextDoc ");
61 util.DesktopTools.closeDoc(xTextDoc);
64 /**
65 this method creates the testenvironment
66 @param tParam TestParameters class which contains additional
67 test parameters
68 @param log PrintWriter class to log the test state and result
70 @return TestEnvironment class
72 @see TestParameters
73 @see PrintWriter
75 @Override
76 protected TestEnvironment createTestEnvironment(TestParameters tParam,
77 PrintWriter log) {
78 // creation of testobject here
79 XTextTable oObj = null;
80 TestEnvironment tEnv = null;
81 Object instance = null;
83 oObj = SOfficeFactory.createTextTable(xTextDoc);
84 instance = SOfficeFactory.createTextTable(xTextDoc);
86 int nRow = 6;
87 int nCol = 2;
88 oObj.initialize(nRow, nCol);
90 log.println("Creating instance...");
91 tEnv = new TestEnvironment(oObj);
93 if (SOfficeFactory.getTableCollection(xTextDoc).getCount() == 0) {
94 SOfficeFactory.insertTextContent(xTextDoc, oObj);
97 XPropertySet props = UnoRuntime.queryInterface(
98 XPropertySet.class, oObj);
100 try {
101 props.setPropertyValue("ChartRowAsLabel", Boolean.TRUE);
102 props.setPropertyValue("ChartColumnAsLabel", Boolean.TRUE);
103 } catch (Exception e) {
104 e.printStackTrace(log);
108 //Adding relation for util.XTextTable
109 tEnv.addObjRelation("NROW", Integer.valueOf(nRow));
110 tEnv.addObjRelation("NCOL", Integer.valueOf(nCol));
112 //Adding ObjRelation for XCellRangeData
113 Object[][] newData = new Object[6][];
114 Object[] line = new Object[2];
115 line[0] = Double.valueOf(1.5);
116 line[1] = Double.valueOf(2.5);
117 newData[0] = line;
118 newData[1] = line;
119 newData[2] = line;
120 newData[3] = line;
121 newData[4] = line;
122 newData[5] = line;
123 tEnv.addObjRelation("NewData", newData);
125 //Adding relation for util.XSortable
126 final PrintWriter finalLog = log;
127 final XTextTable oTable = oObj;
128 tEnv.addObjRelation("SORTCHECKER",
129 new ifc.util._XSortable.XSortChecker() {
130 PrintWriter out = finalLog;
132 public void setPrintWriter(PrintWriter log) {
133 out = log;
136 public void prepareToSort() {
137 oTable.getCellByName("A1").setValue(4);
139 XTextRange textRange = UnoRuntime.queryInterface(
140 XTextRange.class,
141 oTable.getCellByName("A2"));
142 textRange.setString("b");
143 oTable.getCellByName("A3").setValue(3);
144 textRange = UnoRuntime.queryInterface(
145 XTextRange.class,
146 oTable.getCellByName("A4"));
147 textRange.setString("a");
148 oTable.getCellByName("A5").setValue(23);
149 textRange = UnoRuntime.queryInterface(
150 XTextRange.class,
151 oTable.getCellByName("A6"));
152 textRange.setString("ab");
156 public boolean checkSort(boolean isSortNumbering,
157 boolean isSortAscending) {
158 out.println("Sort checking...");
160 boolean res = false;
161 String[] value = new String[6];
163 for (int i = 0; i < 6; i++) {
164 XCell cell = oTable.getCellByName("A" + (i + 1));
165 XTextRange textRange = UnoRuntime.queryInterface(
166 XTextRange.class, cell);
167 value[i] = textRange.getString();
170 if (isSortNumbering) {
171 if (isSortAscending) {
172 String[] rightVal = { "3", "4", "23" };
173 String[] vals = { value[3], value[4], value[5] };
174 res = ValueComparer.equalValue(vals, rightVal);
176 if (!res) {
177 out.println("Expected: 3, 4, 23");
178 out.println("Getting: " + value[3] + ", " +
179 value[4] + ", " + value[5]);
181 } else {
182 String[] rightVal = { "23", "4", "3" };
183 String[] vals = { value[0], value[1], value[2] };
184 res = ValueComparer.equalValue(vals, rightVal);
186 if (!res) {
187 out.println("Expected: 23, 4, 3");
188 out.println("Getting: " + value[1] + ", " +
189 value[2] + ", " + value[3]);
192 } else {
193 if (isSortAscending) {
194 String[] rightVal = { "23", "3", "4", "a", "ab", "b" };
195 res = ValueComparer.equalValue(value, rightVal);
197 if (!res) {
198 out.println("Expected: 23, 3, 4, a, ab, b");
199 out.println("Getting: " + value[0] + ", " +
200 value[1] + ", " + value[2] +
201 ", " + value[3] + ", " +
202 value[4] + ", " + value[5]);
204 } else {
205 String[] rightVal = { "b", "ab", "a", "4", "3", "23" };
206 res = ValueComparer.equalValue(value, rightVal);
208 if (!res) {
209 out.println("Expected: b, ab, a, 4, 3, 23");
210 out.println("Getting: " + value[0] + ", " +
211 value[1] + ", " + value[2] +
212 ", " + value[3] + ", " +
213 value[4] + ", " + value[5]);
218 if (res) {
219 out.println("Sorted correctly");
220 } else {
221 out.println("Sorted incorrectly");
224 return res;
228 tEnv.addObjRelation("CONTENT",
229 UnoRuntime.queryInterface(
230 XTextContent.class, instance));
231 tEnv.addObjRelation("RANGE", xTextDoc.getText().createTextCursor());
233 return tEnv;
235 } // finish class SwXTextTable