Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / style / _ParagraphProperties.java
blobaa3b173f5225ed0459e8db3a69f0c4a99aeaed97
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.style;
20 import ifc.text._NumberingLevel;
22 import java.util.HashMap;
24 import lib.MultiPropertyTest;
25 import share.LogWriter;
26 import util.utils;
28 import com.sun.star.beans.PropertyValue;
29 import com.sun.star.beans.UnknownPropertyException;
30 import com.sun.star.container.XIndexReplace;
31 import com.sun.star.container.XNameContainer;
32 import com.sun.star.lang.WrappedTargetException;
33 import com.sun.star.uno.AnyConverter;
34 import com.sun.star.uno.Type;
35 import com.sun.star.xml.AttributeData;
38 /**
39 * Testing <code>com.sun.star.style.ParagraphProperties</code>
40 * service properties :
41 * <ul>
42 * <li><code> ParaAdjust</code></li>
43 * <li><code> ParaLineSpacing</code></li>
44 * <li><code> ParaBackColor</code></li>
45 * <li><code> ParaBackTransparent</code></li>
46 * <li><code> ParaBackGraphicURL</code></li>
47 * <li><code> ParaBackGraphicFilter</code></li>
48 * <li><code> ParaBackGraphicLocation</code></li>
49 * <li><code> ParaLastLineAdjust</code></li>
50 * <li><code> ParaExpandSingleWord</code></li>
51 * <li><code> ParaLeftMargin</code></li>
52 * <li><code> ParaRightMargin</code></li>
53 * <li><code> ParaTopMargin</code></li>
54 * <li><code> ParaBottomMargin</code></li>
55 * <li><code> ParaLineNumberCount</code></li>
56 * <li><code> ParaLineNumberStartValue</code></li>
57 * <li><code> ParaIsHyphenation</code></li>
58 * <li><code> PageDescName</code></li>
59 * <li><code> PageNumberOffset</code></li>
60 * <li><code> ParaRegisterModeActive</code></li>
61 * <li><code> ParaTabStops</code></li>
62 * <li><code> ParaStyleName</code></li>
63 * <li><code> DropCapFormat</code></li>
64 * <li><code> DropCapWholeWord</code></li>
65 * <li><code> ParaKeepTogether</code></li>
66 * <li><code> ParaSplit</code></li>
67 * <li><code> NumberingLevel</code></li>
68 * <li><code> NumberingRules</code></li>
69 * <li><code> NumberingStartValue</code></li>
70 * <li><code> ParaIsNumberingRestart</code></li>
71 * <li><code> NumberingStyleName</code></li>
72 * <li><code> ParaOrphans</code></li>
73 * <li><code> ParaWidows</code></li>
74 * <li><code> ParaShadowFormat</code></li>
75 * <li><code> IsHangingPunctuation</code></li>
76 * <li><code> IsCharacterDistance</code></li>
77 * <li><code> IsForbiddenRules</code></li>
78 * <li><code> LeftBorder</code></li>
79 * <li><code> RightBorder</code></li>
80 * <li><code> TopBorder</code></li>
81 * <li><code> BottomBorder</code></li>
82 * <li><code> BorderDistance</code></li>
83 * <li><code> LeftBorderDistance</code></li>
84 * <li><code> RightBorderDistance</code></li>
85 * <li><code> TopBorderDistance</code></li>
86 * <li><code> BottomBorderDistance</code></li>
87 * </ul> <p>
88 * This test needs the following object relations :
89 * <ul>
90 * <li> <code>'NRULES'</code> : <b>optional</b>
91 * (service <code>com.sun.star.text.NumberingRules</code>):
92 * instance of the service which can be set as 'NumberingRules'
93 * property new value. If the relation doesn't then two
94 * different <code>NumberingRules</code> objects are tried
95 * to be obtained by setting different 'NumberingStyleName'
96 * property styles and getting 'NumberingRules' property values.</li>
97 * <ul> <p>
98 * Properties testing is automated by <code>lib.MultiPropertyTest</code>.
99 * @see com.sun.star.style.ParagraphProperties
101 public class _ParagraphProperties extends MultiPropertyTest {
103 * Custom tester for numbering style properties. Switches between
104 * 'Numbering 123' and 'Numbering ABC' styles.
106 protected PropertyTester NumberingStyleTester = new PropertyTester() {
107 @Override
108 protected Object getNewValue(String propName, Object oldValue)
109 throws java.lang.IllegalArgumentException {
110 if ((oldValue != null) && (oldValue.equals("Numbering 123"))) {
111 return "Numbering ABC";
112 } else {
113 return "Numbering 123";
119 * Custom tester for paragraph style properties. Switches between
120 * 'Salutation' and 'Heading' styles.
122 protected PropertyTester charStyleTester = new PropertyTester() {
123 @Override
124 protected Object getNewValue(String propName, Object oldValue) {
125 if (!utils.isVoid(oldValue) && (oldValue.equals("Example"))) {
126 return "Emphasis";
127 } else {
128 return "Example";
134 * Custom tester for paragraph style properties. Switches between
135 * 'Salutation' and 'Heading' styles.
137 protected PropertyTester ParaStyleTester = new PropertyTester() {
138 @Override
139 protected Object getNewValue(String propName, Object oldValue) {
140 if (!utils.isVoid(oldValue) && (oldValue.equals("Heading"))) {
141 return "Salutation";
142 } else {
143 return "Heading";
149 * Custom tester for PageDescName properties. Switches between
150 * 'HTML' and 'Standard' descriptor names.
152 protected PropertyTester PageDescTester = new PropertyTester() {
153 @Override
154 protected Object getNewValue(String propName, Object oldValue) {
155 if (!util.utils.isVoid(oldValue) &&
156 (oldValue.equals("Default"))) {
157 return "HTML";
158 } else {
159 return "Default";
165 * Custom tester for properties which have <code>short</code> type
166 * and can be void, so if they have void value, the new value must
167 * be specified. Switches between two different values.
169 protected PropertyTester ShortTester = new PropertyTester() {
170 @Override
171 protected Object getNewValue(String propName, Object oldValue) {
172 if ((oldValue != null) &&
173 (oldValue.equals(Short.valueOf((short) 0)))) {
174 return Short.valueOf((short) 2);
175 } else {
176 return Short.valueOf((short) 0);
182 * Custom tester for properties which have <code>boolean</code> type
183 * and can be void, so if they have void value, the new value must
184 * be specified. Switches between true and false.
186 protected PropertyTester BooleanTester = new PropertyTester() {
187 @Override
188 protected Object getNewValue(String propName, Object oldValue) {
189 if ((oldValue != null) &&
190 (oldValue.equals(Boolean.FALSE))) {
191 return Boolean.TRUE;
192 } else {
193 return Boolean.FALSE;
199 * Custom tester for properties which contains image URLs.
200 * Switches between two JPG images' URLs.
202 protected PropertyTester URLTester = new PropertyTester() {
203 @Override
204 protected Object getNewValue(String propName, Object oldValue) {
205 if (oldValue.equals(util.utils.getFullTestURL("space-metal.jpg"))) {
206 return util.utils.getFullTestURL("crazy-blue.jpg");
207 } else {
208 return util.utils.getFullTestURL("space-metal.jpg");
214 * Tested with custom property tester.
216 public void _NumberingStyleName() {
217 log.println("Testing with custom Property tester");
218 testProperty("NumberingStyleName", NumberingStyleTester);
222 * Tested with custom property tester.
224 public void _DropCapCharStyleName() {
225 log.println("Testing with custom Property tester");
226 testProperty("DropCapCharStyleName", charStyleTester);
230 * Tested with custom property tester.
232 public void _ParaStyleName() {
233 log.println("Testing with custom Property tester");
234 testProperty("ParaStyleName", ParaStyleTester);
238 * Tested with custom property tester.
240 public void _PageDescName() {
241 log.println("Testing with custom Property tester");
242 testProperty("PageDescName", PageDescTester);
246 * Tested with custom property tester. Before testing property
247 * <code>ParaAdjust</code> is setting to value <code>BLOCK</code>
248 * because setting the property <code>ParaLastLineAdjust</code>
249 * makes sense only in this case.
251 public void _ParaLastLineAdjust() {
252 log.println("Testing with custom Property tester");
254 try {
255 oObj.setPropertyValue("ParaAdjust",
256 com.sun.star.style.ParagraphAdjust.BLOCK);
257 } catch (com.sun.star.lang.WrappedTargetException e) {
258 log.println("Exception occurred setting property 'ParagraphAdjust'" + e);
259 } catch (com.sun.star.lang.IllegalArgumentException e) {
260 log.println("Exception occurred setting property 'ParagraphAdjust'" + e);
261 } catch (com.sun.star.beans.UnknownPropertyException e) {
262 log.println("Exception occurred setting property 'ParagraphAdjust'" + e);
263 } catch (com.sun.star.beans.PropertyVetoException e) {
264 log.println("Exception occurred setting property 'ParagraphAdjust'" + e);
267 testProperty("ParaLastLineAdjust", ShortTester);
271 * Tested with custom property tester.
273 public void _ParaBackGraphicURL() {
274 log.println("Testing with custom Property tester");
275 testProperty("ParaBackGraphicURL", URLTester);
279 * Tested with custom property tester. <p>
280 * The following property tests are to be completed successfully before :
281 * <ul>
282 * <li> <code> NumberingStyleName </code> : a numbering style must
283 * be set before testing this property </li>
284 * </ul>
286 public void _NumberingLevel() {
287 requiredMethod("NumberingStyleName");
288 log.println("Testing with custom Property tester");
289 testProperty("NumberingLevel", ShortTester);
293 * Tested with custom property tester. <p>
295 public void _ParaIsConnectBorder() {
297 log.println("Testing with custom Property tester");
298 testProperty("ParaIsConnectBorder", BooleanTester);
302 * Tested with custom property tester.
304 public void _ParaVertAlignment() {
305 log.println("Testing with custom Property tester");
306 testProperty("ParaVertAlignment", ShortTester);
310 * Tested with com.sun.star.text.NumberingLevel <p>
311 * The value of this property is a com.sun.star.container.XIndexReplace which is represented by
312 * com.sun.star.text.NumberingLevel.
313 * The following property tests are to be completed successfully before :
314 * <ul>
315 * <li> <code> NumberingStyleName </code> : a numbering style must
316 * be set before testing this property </li>
317 * </ul>
318 * @see com.sun.star.text.NumberlingLevel
319 * @see com.sun.star.container.XIndexReplace
320 * @see ifc.text._NumberingLevel
322 public void _NumberingRules() {
323 requiredMethod("NumberingStyleName");
325 XIndexReplace NumberingRules = null;
326 PropertyValue[] propertyValues = null;
327 try {
328 NumberingRules = (XIndexReplace) AnyConverter.toObject(
329 new Type(XIndexReplace.class), oObj.getPropertyValue("NumberingRules"));
330 } catch (com.sun.star.lang.IllegalArgumentException ex) {
331 log.println("could not get NumberingRules: "+ ex.toString() );
332 return;
333 } catch (UnknownPropertyException ex) {
334 log.println( "could not get NumberingRules: "+ ex.toString() );
335 return;
336 } catch (WrappedTargetException ex) {
337 log.println( "could not get NumberingRules: "+ ex.toString() );
338 return;
340 try {
341 propertyValues = (PropertyValue[]) NumberingRules.getByIndex(0);
343 } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
344 log.println( "could not get NumberlingLevel-Array from NumberingRules: "+ ex.toString() );
345 return;
346 } catch (WrappedTargetException ex) {
347 log.println( "could not get NumberlingLevel-Array from NumberingRules: "+ ex.toString() );
348 return;
351 _NumberingLevel numb = new _NumberingLevel((LogWriter)log, tParam, propertyValues);
353 boolean result = numb.testPropertyArray();
355 tRes.tested("NumberingRules", result);
358 public void _ParaUserDefinedAttributes() {
359 XNameContainer uda = null;
360 boolean res = false;
362 try {
363 try{
364 uda = (XNameContainer) AnyConverter.toObject(
365 new Type(XNameContainer.class),
366 oObj.getPropertyValue("ParaUserDefinedAttributes"));
367 } catch (com.sun.star.lang.IllegalArgumentException e){
368 log.println("ParaUserDefinedAttributes is empty.");
369 uda = new _ParagraphProperties.OwnUserDefinedAttributes();
371 AttributeData attr = new AttributeData();
372 attr.Namespace = "http://www.sun.com/staroffice/apitest/Cellprop";
373 attr.Type = "CDATA";
374 attr.Value = "true";
375 uda.insertByName("Cellprop:has-first-alien-attribute", attr);
377 uda.getElementNames();
378 oObj.setPropertyValue("ParaUserDefinedAttributes", uda);
379 uda = (XNameContainer) AnyConverter.toObject(
380 new Type(XNameContainer.class),
381 oObj.getPropertyValue("ParaUserDefinedAttributes"));
382 uda.getElementNames();
384 uda.getByName("Cellprop:has-first-alien-attribute");
385 res = true;
386 } catch (com.sun.star.beans.UnknownPropertyException upe) {
387 if (isOptional("ParaUserDefinedAttributes")) {
388 log.println("Property is optional and not supported");
389 res = true;
390 } else {
391 log.println("Don't know the Property 'ParaUserDefinedAttributes'");
393 } catch (com.sun.star.lang.WrappedTargetException wte) {
394 log.println(
395 "WrappedTargetException while getting Property 'ParaUserDefinedAttributes'");
396 } catch (com.sun.star.container.NoSuchElementException nee) {
397 log.println("added Element isn't part of the NameContainer");
398 } catch (com.sun.star.lang.IllegalArgumentException iae) {
399 log.println(
400 "IllegalArgumentException while getting Property 'ParaUserDefinedAttributes'");
401 } catch (com.sun.star.beans.PropertyVetoException pve) {
402 log.println(
403 "PropertyVetoException while getting Property 'ParaUserDefinedAttributes'");
404 } catch (com.sun.star.container.ElementExistException eee) {
405 log.println(
406 "ElementExistException while getting Property 'ParaUserDefinedAttributes'");
409 tRes.tested("ParaUserDefinedAttributes", res);
412 private static class OwnUserDefinedAttributes implements XNameContainer{
413 HashMap<String, Object> members = null;
416 public OwnUserDefinedAttributes() {
417 members = new HashMap<String, Object>();
420 public Object getByName(String str) throws com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException {
421 return members.get(str);
424 public String[] getElementNames() {
425 java.util.Set<String> keySet = members.keySet();
426 return keySet.toArray(new String[keySet.size()]);
429 public com.sun.star.uno.Type getElementType() {
430 String key = members.keySet().iterator().next();
431 Object o = members.get(key);
432 return new Type(o.getClass());
435 public boolean hasByName(String str) {
436 return members.get(str) != null;
439 public boolean hasElements() {
440 return members.size() > 0;
443 public void insertByName(String str, Object obj) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.container.ElementExistException, com.sun.star.lang.WrappedTargetException {
444 members.put(str, obj);
447 public void removeByName(String str) throws com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException {
448 members.remove(str);
451 public void replaceByName(String str, Object obj) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException {
452 members.put(str, obj);
456 } // finish class _ParagraphProperties