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 .
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
;
28 import lib
.StatusException
;
32 * Generic test for all properties contained in this service
34 public class _Settings
extends MultiPropertyTest
{
37 * This property accepts only values in a range of 1-3
38 * @see com.sun.star.document.PrinterIndependentLayout
40 public void _PrinterIndependentLayout() {
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;
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
) {
68 catch(java
.lang
.NoSuchMethodException e
) {
71 catch(java
.lang
.IllegalAccessException e
) {
74 catch(java
.lang
.reflect
.InvocationTargetException e
) {
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]));
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() {
101 //check if it is read only as specified
102 res
&= isReadOnly("ForbiddenCharacters");
104 if (!isReadOnly("ForbiddenCharacters")) {
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,
115 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
117 "Exception while checking property 'ForbiddenCharacters' " +
119 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
121 "Exception while checking property 'ForbiddenCharacters' " +
125 tRes
.tested("ForbiddenCharacters", res
);
128 protected boolean isReadOnly(String PropertyName
) {
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);
141 private String
getPrinterNameWithReflection(Object pService
) {
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
) {