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