Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / text / _BaseIndex.java
blob006796724ad80ccf444439cb7eb48ea64ca9fe85
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.text;
20 import com.sun.star.beans.PropertyValue;
21 import com.sun.star.container.XIndexAccess;
22 import com.sun.star.container.XIndexReplace;
23 import com.sun.star.text.XTextColumns;
24 import com.sun.star.uno.UnoRuntime;
26 import lib.MultiPropertyTest;
29 /**
30 * Testing <code>com.sun.star.text.BaseIndex</code>
31 * service properties :
32 * <ul>
33 * <li><code> Title</code></li>
34 * <li><code> IsProtected</code></li>
35 * <li><code> ParaStyleHeading</code></li>
36 * <li><code> ParaStyleLevel1</code></li>
37 * <li><code> ParaStyleLevel2</code></li>
38 * <li><code> ParaStyleLevel3</code></li>
39 * <li><code> ParaStyleLevel4</code></li>
40 * <li><code> ParaStyleLevel5</code></li>
41 * <li><code> ParaStyleLevel6</code></li>
42 * <li><code> ParaStyleLevel7</code></li>
43 * <li><code> ParaStyleLevel8</code></li>
44 * <li><code> ParaStyleLevel9</code></li>
45 * <li><code> ParaStyleLevel10</code></li>
46 * <li><code> ParaStyleSeparator</code></li>
47 * <li><code> TextColumns</code></li>
48 * <li><code> BackGraphicURL</code></li>
49 * <li><code> BackGraphicFilter</code></li>
50 * <li><code> BackGraphicLocation</code></li>
51 * <li><code> BackTransparent</code></li>
52 * <li><code> LevelFormat</code></li>
53 * <li><code> CreateFromChapter</code></li>
54 * </ul> <p>
55 * Properties testing is automated by <code>lib.MultiPropertyTest</code>.
56 * @see com.sun.star.text.BaseIndex
58 public class _BaseIndex extends MultiPropertyTest {
59 /**
60 * Redefined method returns object, that contains changed property value.
62 protected PropertyTester CustomTester = new PropertyTester() {
63 @Override
64 protected Object getNewValue(String propName, Object oldValue) {
65 XTextColumns TC = UnoRuntime.queryInterface(
66 XTextColumns.class, oldValue);
67 TC.setColumnCount((short) (TC.getColumnCount() + (short) 1));
69 return TC;
73 /**
74 * New value must be defined for this property.
76 public void _TextColumns() {
77 log.println(
78 "Testing property 'TextColumns' with custom property tester");
79 testProperty("TextColumns", CustomTester);
82 /**
83 * The value of this property is a collection of document index
84 * level formats. This property is tested in the following manner :
85 * the property value (a collection) is obtained, the first element
86 * of this collection is replaced with new non-empty array
87 * (<code>PropertyValue[][]</code>) with some properties set.
88 * After that the collection is set back as property value. <p>
90 * Comparing of set and got <code>PropertyValue</code> arrays
91 * is difficult because values can be changed after setting
92 * by service implementation. <p>
94 * Has <b>OK</b> status if the collection again gotten, has a
95 * new first element (i.e. lengths of the old array and the array
96 * get are different or their contents differ).
98 public void _LevelFormat() {
99 log.println(
100 "Testing property 'LevelFormat' with custom property tester");
101 testProperty("LevelFormat",
102 new PropertyTester() {
103 PropertyValue[][] newVal = null;
104 PropertyValue[][] oldVal = null;
106 @Override
107 protected Object getNewValue(String propName, Object oldValue) {
108 XIndexReplace indProp = UnoRuntime.queryInterface(
109 XIndexReplace.class, oldValue);
111 try {
112 oldVal = (PropertyValue[][]) indProp.getByIndex(0);
114 log.println("Get:");
115 printLevelFormatProperty(oldValue);
117 newVal = new PropertyValue[1][2];
119 for (int i = 0; i < newVal[0].length; i++) {
120 newVal[0][i] = new PropertyValue();
123 newVal[0][1].Name = "TokenType";
124 newVal[0][1].Value = "TokenEntryText";
125 newVal[0][0].Name = "Text";
126 newVal[0][0].Value = "BaseIndex";
128 indProp.replaceByIndex(0, newVal);
129 } catch (com.sun.star.lang.WrappedTargetException e) {
130 log.println("Exception occurred while testing LevelFormat");
131 e.printStackTrace(log);
132 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
133 log.println("Exception occurred while testing LevelFormat");
134 e.printStackTrace(log);
135 } catch (com.sun.star.lang.IllegalArgumentException e) {
136 log.println("Exception occurred while testing LevelFormat");
137 e.printStackTrace(log);
140 return indProp;
143 @Override
144 protected void checkResult(String propName, Object oldValue,
145 Object newValue, Object resValue,
146 Exception exception)
147 throws Exception {
149 super.checkResult(propName, oldValue, newValue, resValue, exception);
151 PropertyValue[][] res = (PropertyValue[][]) UnoRuntime.queryInterface(
152 XIndexAccess.class,
153 resValue).getByIndex(0);
155 log.println("Result:");
156 printLevelFormatProperty(resValue);
158 boolean result = (res.length != oldVal.length) ||
159 !util.ValueComparer.equalValue(res, oldVal);
161 tRes.tested(propName, result);
167 * Outputs full description of 'LevelFormat' property
168 * value into <code>log</code>.
170 private void printLevelFormatProperty(Object value) {
171 XIndexReplace indProp = UnoRuntime.queryInterface(
172 XIndexReplace.class, value);
173 PropertyValue[][] val = null;
175 try {
176 log.println(" \u0421ollection has " + indProp.getCount() +
177 " elements : ");
179 for (int i = 0; i < indProp.getCount(); i++) {
180 val = (PropertyValue[][]) indProp.getByIndex(i);
182 log.println(" " + i + ": has " + val.length + " levels :");
184 for (int j = 0; j < val.length; j++) {
185 log.println(" " + j + " level :");
187 for (int k = 0; k < val[j].length; k++) {
188 log.println(" " + val[j][k].Name + "=" +
189 val[j][k].Value);
193 } catch (com.sun.star.lang.WrappedTargetException e) {
194 log.println("Exception occurred while printing LevelFormat");
195 e.printStackTrace(log);
196 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
197 log.println("Exception occurred while printing LevelFormat");
198 e.printStackTrace(log);
201 } // finish class _NumberingRules