1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package ifc.document;
24 
25 import com.sun.star.beans.Property;
26 import com.sun.star.beans.PropertyAttribute;
27 import com.sun.star.i18n.XForbiddenCharacters;
28 import com.sun.star.uno.UnoRuntime;
29 import java.lang.reflect.Method;
30 
31 //import java.awt.print.PrinterJob;
32 
33 //import javax.print.PrintService;
34 
35 import lib.MultiPropertyTest;
36 import lib.Status;
37 import lib.StatusException;
38 
39 
40 /*
41  * Generic test for all properties contained in this service
42  */
43 public class _Settings extends MultiPropertyTest {
44 
45     /**
46      * This property accepts only values in a range of 1-3
47      * @see com.sun.star.document.PrinterIndependentLayout
48      */
_PrinterIndependentLayout()49     public void _PrinterIndependentLayout() {
50         try{
51             Short oldVal = (Short) oObj.getPropertyValue("PrinterIndependentLayout");
52             Short newVal = oldVal.intValue() == 1 ?  new Short("3") : new Short("1");
53 
54 
55             testProperty("PrinterIndependentLayout", oldVal, newVal);
56 
57         } catch (com.sun.star.beans.UnknownPropertyException e) {
58             throw new StatusException(Status.failed("the property 'PrinterIndependentLayout' is unknown."));
59         } catch (com.sun.star.lang.WrappedTargetException e) {
60             throw new StatusException(Status.failed("the property 'PrinterIndependentLayout' could not be tested."));
61         }
62     }
63 
_PrinterName()64     public void _PrinterName() {
65         Object[] oServices = null;
66         Exception ex = null;
67 
68         try {
69             Class cPrinterJob = Class.forName("java.awt.print.PrinterJob");
70             Method lookupMethod = cPrinterJob.getDeclaredMethod("lookupPrintServices", new Class[0]);
71             Object retValue = lookupMethod.invoke(cPrinterJob, new Object[0]);
72             oServices = (Object[])retValue;
73         }
74         catch(java.lang.ClassNotFoundException e) {
75             ex = e;
76         }
77         catch(java.lang.NoSuchMethodException e) {
78             ex = e;
79         }
80         catch(java.lang.IllegalAccessException e) {
81             ex = e;
82         }
83         catch(java.lang.reflect.InvocationTargetException e) {
84             ex = e;
85         }
86 
87         if (ex != null) {
88             // get Java version:
89             String javaVersion = System.getProperty("java.version");
90             throw new StatusException(Status.failed(
91                 "Cannot execute test with current Java version (Java 1.4 required) " +
92                 javaVersion + ": " + ex.getMessage()));
93         }
94 //        PrintService[] services = PrinterJob.lookupPrintServices();
95 
96         if (oServices.length > 1) {
97             testProperty("PrinterName", getPrinterNameWithReflection(oServices[0]),
98                             getPrinterNameWithReflection(oServices[1]));
99         } else {
100             log.println(
101                     "checking this property needs at least two printers to be installed on your system");
102             throw new StatusException(Status.failed(
103                                               "only one printer installed so I can't change it"));
104         }
105     }
106 
_ForbiddenCharacters()107     public void _ForbiddenCharacters() {
108         boolean res = true;
109 
110         try {
111             //check if it is read only as specified
112             res &= isReadOnly("ForbiddenCharacters");
113 
114             if (!isReadOnly("ForbiddenCharacters")) {
115                 log.println(
116                         "The Property 'ForbiddenCharacters' isn't readOnly as specified");
117             }
118 
119             //check if the property has the right type
120             Object pValue = oObj.getPropertyValue("ForbiddenCharacters");
121             XForbiddenCharacters fc = (XForbiddenCharacters) UnoRuntime.queryInterface(
122                                               XForbiddenCharacters.class,
123                                               pValue);
124             res &= (fc != null);
125         } catch (com.sun.star.beans.UnknownPropertyException e) {
126             log.println(
127                     "Exception while checking property 'ForbiddenCharacters' " +
128                     e.getMessage());
129         } catch (com.sun.star.lang.WrappedTargetException e) {
130             log.println(
131                     "Exception while checking property 'ForbiddenCharacters' " +
132                     e.getMessage());
133         }
134 
135         tRes.tested("ForbiddenCharacters", res);
136     }
137 
isReadOnly(String PropertyName)138     protected boolean isReadOnly(String PropertyName) {
139         boolean res = false;
140         Property[] props = oObj.getPropertySetInfo().getProperties();
141 
142         for (int i = 0; i < props.length; i++) {
143             if (props[i].Name.equals(PropertyName)) {
144                 res = ((props[i].Attributes & PropertyAttribute.READONLY) != 0);
145             }
146         }
147 
148         return res;
149     }
150 
getPrinterNameWithReflection(Object pService)151     private String getPrinterNameWithReflection(Object pService) {
152         String pName = null;
153         try {
154             Class cPrintService = Class.forName("javax.print.PrintService");
155             Method getNameMethod = cPrintService.getDeclaredMethod("getName", new Class[0]);
156             Object retValue = getNameMethod.invoke(pService, new Object[0]);
157             pName = (String)retValue;
158         }
159         // ignore all excptions: we already ran into one of these if Java is too old
160         catch(java.lang.ClassNotFoundException e) {
161         }
162         catch(java.lang.NoSuchMethodException e) {
163         }
164         catch(java.lang.IllegalAccessException e) {
165         }
166         catch(java.lang.reflect.InvocationTargetException e) {
167         }
168         return pName;
169     }
170 }
171