merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / ifc / document / _Settings.java
blob41efcd6945a969607d8c7b24731bef80ce3f2837
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _Settings.java,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package ifc.document;
32 import com.sun.star.beans.Property;
33 import com.sun.star.beans.PropertyAttribute;
34 import com.sun.star.i18n.XForbiddenCharacters;
35 import com.sun.star.uno.UnoRuntime;
36 import java.lang.reflect.Method;
38 //import java.awt.print.PrinterJob;
40 //import javax.print.PrintService;
42 import lib.MultiPropertyTest;
43 import lib.Status;
44 import lib.StatusException;
48 * Generic test for all properties contained in this service
50 public class _Settings extends MultiPropertyTest {
52 /**
53 * This property accepts only values in a range of 1-3
54 * @see com.sun.star.document.PrinterIndependentLayout
56 public void _PrinterIndependentLayout() {
57 try{
58 Short oldVal = (Short) oObj.getPropertyValue("PrinterIndependentLayout");
59 Short newVal = oldVal.intValue() == 1 ? new Short("3") : new Short("1");
62 testProperty("PrinterIndependentLayout", oldVal, newVal);
64 } catch (com.sun.star.beans.UnknownPropertyException e) {
65 throw new StatusException(Status.failed("the property 'PrinterIndependentLayout' is unkown."));
66 } catch (com.sun.star.lang.WrappedTargetException e) {
67 throw new StatusException(Status.failed("the property 'PrinterIndependentLayout' could not be tested."));
71 public void _PrinterName() {
72 Object[] oServices = null;
73 Exception ex = null;
75 try {
76 Class cPrinterJob = Class.forName("java.awt.print.PrinterJob");
77 Method lookupMethod = cPrinterJob.getDeclaredMethod("lookupPrintServices", new Class[0]);
78 Object retValue = lookupMethod.invoke(cPrinterJob, new Object[0]);
79 oServices = (Object[])retValue;
81 catch(java.lang.ClassNotFoundException e) {
82 ex = e;
84 catch(java.lang.NoSuchMethodException e) {
85 ex = e;
87 catch(java.lang.IllegalAccessException e) {
88 ex = e;
90 catch(java.lang.reflect.InvocationTargetException e) {
91 ex = e;
94 if (ex != null) {
95 // get Java version:
96 String javaVersion = System.getProperty("java.version");
97 throw new StatusException(Status.failed(
98 "Cannot execute test with current Java version (Java 1.4 required) " +
99 javaVersion + ": " + ex.getMessage()));
101 // PrintService[] services = PrinterJob.lookupPrintServices();
103 if (oServices.length > 1) {
104 testProperty("PrinterName", getPrinterNameWithReflection(oServices[0]),
105 getPrinterNameWithReflection(oServices[1]));
106 } else {
107 log.println(
108 "checking this property needs at least two printers to be installed on your system");
109 throw new StatusException(Status.failed(
110 "only one printer installed so I can't change it"));
114 public void _ForbiddenCharacters() {
115 boolean res = true;
117 try {
118 //check if it is read only as specified
119 res &= isReadOnly("ForbiddenCharacters");
121 if (!isReadOnly("ForbiddenCharacters")) {
122 log.println(
123 "The Property 'ForbiddenCharacters' isn't readOnly as specified");
126 //check if the property has the right type
127 Object pValue = oObj.getPropertyValue("ForbiddenCharacters");
128 XForbiddenCharacters fc = (XForbiddenCharacters) UnoRuntime.queryInterface(
129 XForbiddenCharacters.class,
130 pValue);
131 res &= (fc != null);
132 } catch (com.sun.star.beans.UnknownPropertyException e) {
133 log.println(
134 "Exception while checking property 'ForbiddenCharacters' " +
135 e.getMessage());
136 } catch (com.sun.star.lang.WrappedTargetException e) {
137 log.println(
138 "Exception while checking property 'ForbiddenCharacters' " +
139 e.getMessage());
142 tRes.tested("ForbiddenCharacters", res);
145 protected boolean isReadOnly(String PropertyName) {
146 boolean res = false;
147 Property[] props = oObj.getPropertySetInfo().getProperties();
149 for (int i = 0; i < props.length; i++) {
150 if (props[i].Name.equals(PropertyName)) {
151 res = ((props[i].Attributes & PropertyAttribute.READONLY) != 0);
155 return res;
158 private String getPrinterNameWithReflection(Object pService) {
159 String pName = null;
160 try {
161 Class cPrintService = Class.forName("javax.print.PrintService");
162 Method getNameMethod = cPrintService.getDeclaredMethod("getName", new Class[0]);
163 Object retValue = getNameMethod.invoke(pService, new Object[0]);
164 pName = (String)retValue;
166 // ignore all excptions: we already ran into one of these if Java is too old
167 catch(java.lang.ClassNotFoundException e) {
169 catch(java.lang.NoSuchMethodException e) {
171 catch(java.lang.IllegalAccessException e) {
173 catch(java.lang.reflect.InvocationTargetException e) {
175 return pName;