Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / document / _Settings.java
bloba0b0f87b6b8c4a5d1add882fe36299f1f239e34f
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.document;
20 import com.sun.star.beans.Property;
21 import com.sun.star.beans.PropertyAttribute;
22 import com.sun.star.i18n.XForbiddenCharacters;
23 import com.sun.star.uno.UnoRuntime;
24 import java.lang.reflect.Method;
26 import lib.MultiPropertyTest;
27 import lib.Status;
28 import lib.StatusException;
32 * Generic test for all properties contained in this service
34 public class _Settings extends MultiPropertyTest {
36 /**
37 * This property accepts only values in a range of 1-3
38 * @see com.sun.star.document.PrinterIndependentLayout
40 public void _PrinterIndependentLayout() {
41 try{
42 Short oldVal = (Short) oObj.getPropertyValue("PrinterIndependentLayout");
43 Short newVal = oldVal.intValue() == 1 ? Short.valueOf("3") : Short.valueOf("1");
46 testProperty("PrinterIndependentLayout", oldVal, newVal);
48 } catch (com.sun.star.beans.UnknownPropertyException e) {
49 throw new StatusException(e, Status.failed("the property 'PrinterIndependentLayout' is unknown."));
50 } catch (com.sun.star.lang.WrappedTargetException e) {
51 throw new StatusException(e, Status.failed("the property 'PrinterIndependentLayout' could not be tested."));
55 public void _PrinterName() {
56 Object[] oServices = null;
57 Exception ex = null;
59 try {
60 Class<?> cPrinterJob = Class.forName("java.awt.print.PrinterJob");
61 Method lookupMethod = cPrinterJob.getDeclaredMethod("lookupPrintServices", new Class[0]);
62 Object retValue = lookupMethod.invoke(cPrinterJob, new Object[0]);
63 oServices = (Object[])retValue;
65 catch(java.lang.ClassNotFoundException e) {
66 ex = e;
68 catch(java.lang.NoSuchMethodException e) {
69 ex = e;
71 catch(java.lang.IllegalAccessException e) {
72 ex = e;
74 catch(java.lang.reflect.InvocationTargetException e) {
75 ex = e;
78 if (ex != null) {
79 // get Java version:
80 String javaVersion = System.getProperty("java.version");
81 throw new StatusException(Status.failed(
82 "Cannot execute test with current Java version (Java 1.4 required) " +
83 javaVersion + ": " + ex.getMessage()));
86 if (oServices.length > 1) {
87 testProperty("PrinterName", getPrinterNameWithReflection(oServices[0]),
88 getPrinterNameWithReflection(oServices[1]));
89 } else {
90 log.println(
91 "checking this property needs at least two printers to be installed on your system");
92 throw new StatusException(Status.failed(
93 "only one printer installed so I can't change it"));
97 public void _ForbiddenCharacters() {
98 boolean res = true;
100 try {
101 //check if it is read only as specified
102 res &= isReadOnly("ForbiddenCharacters");
104 if (!isReadOnly("ForbiddenCharacters")) {
105 log.println(
106 "The Property 'ForbiddenCharacters' isn't readOnly as specified");
109 //check if the property has the right type
110 Object pValue = oObj.getPropertyValue("ForbiddenCharacters");
111 XForbiddenCharacters fc = UnoRuntime.queryInterface(
112 XForbiddenCharacters.class,
113 pValue);
114 res &= (fc != null);
115 } catch (com.sun.star.beans.UnknownPropertyException e) {
116 log.println(
117 "Exception while checking property 'ForbiddenCharacters' " +
118 e.getMessage());
119 } catch (com.sun.star.lang.WrappedTargetException e) {
120 log.println(
121 "Exception while checking property 'ForbiddenCharacters' " +
122 e.getMessage());
125 tRes.tested("ForbiddenCharacters", res);
128 protected boolean isReadOnly(String PropertyName) {
129 boolean res = false;
130 Property[] props = oObj.getPropertySetInfo().getProperties();
132 for (int i = 0; i < props.length; i++) {
133 if (props[i].Name.equals(PropertyName)) {
134 res = ((props[i].Attributes & PropertyAttribute.READONLY) != 0);
138 return res;
141 private String getPrinterNameWithReflection(Object pService) {
142 String pName = null;
143 try {
144 Class<?> cPrintService = Class.forName("javax.print.PrintService");
145 Method getNameMethod = cPrintService.getDeclaredMethod("getName", new Class[0]);
146 Object retValue = getNameMethod.invoke(pService, new Object[0]);
147 pName = (String)retValue;
149 // ignore all exceptions: we already ran into one of these if Java is too old
150 catch(java.lang.ClassNotFoundException e) {
152 catch(java.lang.NoSuchMethodException e) {
154 catch(java.lang.IllegalAccessException e) {
156 catch(java.lang.reflect.InvocationTargetException e) {
158 return pName;