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 
28 package ifc.beans;
29 
30 import com.sun.star.beans.PropertyValue;
31 import com.sun.star.beans.PropertyVetoException;
32 import com.sun.star.lang.WrappedTargetException;
33 import lib.MultiMethodTest;
34 import com.sun.star.beans.UnknownPropertyException;
35 import com.sun.star.beans.XPropertyAccess;
36 import lib.Status;
37 import lib.StatusException;
38 
39 /**
40  * Testing <code>com.sun.star.beans.XPropertyAccess</code>
41  * interface methods :
42  * <ul>
43  *  <li><code>getPropertyValues()</code></li>
44  *  <li><code>setPropertyValues()</code></li>
45  * </ul>
46  * @see com.sun.star.beans.XPropertyAccess
47  */
48 public class _XPropertyAccess extends MultiMethodTest {
49 
50     /**
51      * oObj filled by MultiMethodTest
52      */
53     public XPropertyAccess oObj = null;// oObj filled by MultiMethodTest
54 
55     /**
56      * object relation X<CODE>PropertyAccess.propertyToChange</CODE><br>
57      * This relation must be filled from the module. It contains a property which must
58      * be kind of String property, available at <CODE>getPropertyValues()</CODE> and changeable by
59      * <CODE>setPropertyValues()</CODE>
60      */
61     public PropertyValue propertyToChange = null;
62 
63     /**
64      * checks if the object relation <CODE>XPropertyAccess.propertyToChange</CODE>
65      * is available
66      */
67     public void before() {
68         propertyToChange = (PropertyValue) tEnv.getObjRelation("XPropertyAccess.propertyToChange");
69         if (propertyToChange == null) {
70             throw new StatusException(Status.failed("Object raltion 'XPropertyAccess.propertyToChange' is null"));
71         }
72     }
73 
74     /**
75      * Test calls the method and checks if the returned sequenze contanis a propterty which is named
76      * in the object relation <code>XPropertyAccess.propertyToChange</code>.
77      */
78     public void _getPropertyValues() {
79         PropertyValue[] properties = oObj.getPropertyValues();
80 
81         boolean ok = true;
82 
83         if (properties != null){
84 
85             boolean found = false;
86             for (int i=0; i < properties.length; i++){
87                 if (properties[i].Name.equals(propertyToChange.Name)) found = true;
88             }
89             if (! found){
90                 log.println("ERROR: could not find desired property '"+ propertyToChange.Name+"'");
91                 ok=false;
92             }
93 
94         } else {
95             log.println("ERROR: the method returned NULL");
96             ok =false;
97         }
98 
99         tRes.tested("getPropertyValues()", ok );
100         return;
101     }
102 
103     /**
104      * Test calls the method and checks if:
105      * <ul>
106      *    <li>the property given by the object relation
107      *    <CODE>XPropertyAccess.propertyToChange</CODE> has changed</LI>
108      *    <li><CODE>com.sun.star.lang.IllegalArgumentException</CODE> was thrown if a <CODE>Integer</CODE>
109      *    value was set to a <CODE>String</CODE> property</LI>
110      *    <li><CODE>com.sun.star.beans.UnknownPropertyException</CODE> was throen if an invalid property
111      *    was set</LI>
112      * </ul>
113      */
114     public void _setPropertyValues(){
115 
116         boolean ok = true;
117         boolean test = true;
118         boolean exp = false;
119 
120         try {
121             PropertyValue[] newProps = new PropertyValue[1];
122             newProps[0] = propertyToChange;
123 
124             log.println("try to set property vlaues given by object relation 'XPropertyAccess.propertyToChange'...");
125             oObj.setPropertyValues(newProps);
126 
127         } catch (UnknownPropertyException ex) {
128             log.println("ERROR: Exception was thrown while trying to set property value: " +
129                 ex.toString());
130             test = false;
131         } catch (PropertyVetoException ex) {
132             log.println("ERROR: Exception was thrown while trying to set property value: " +
133                 ex.toString());
134             test = false;
135         } catch (WrappedTargetException ex) {
136             log.println("ERROR: Exception was thrown while trying to set property value: " +
137                 ex.toString());
138             test = false;
139         } catch (com.sun.star.lang.IllegalArgumentException ex) {
140             log.println("ERROR: Exception was thrown while trying to set property value: " +
141                 ex.toString());
142             test = false;
143         }
144 
145         if ( test){
146             log.println("... OK");
147         }
148 
149         ok &= test;
150         test = false;
151         exp = false;
152         try {
153             log.println("try to set integer value to string property, " +
154                 "expect 'com.sun.star.lang.IllegalArgumentException'...");
155             PropertyValue[] newProps = new PropertyValue[1];
156             PropertyValue failedProp = new PropertyValue();
157             failedProp.Name = propertyToChange.Name;
158             failedProp.Value = new Integer(10);
159             newProps[0] = failedProp;
160             oObj.setPropertyValues(newProps);
161         } catch (PropertyVetoException ex) {
162             log.println("ERROR: unexptected exception was thrown while trying to set null value: " +
163                 ex.toString());
164             exp = true;
165         } catch (WrappedTargetException ex) {
166             log.println("ERROR: unexptected exception was thrown while trying to set null value: " +
167                 ex.toString());
168             exp = true;
169         } catch (com.sun.star.lang.IllegalArgumentException ex) {
170             log.println("OK: exptected exception was thrown while trying to set null value: " +
171                 ex.toString());
172             test = true;
173             exp = true;
174         } catch (UnknownPropertyException ex) {
175             log.println("ERROR: unexptected exception was thrown while trying to set null value: " +
176                 ex.toString());
177             exp = true;
178         }
179 
180         if (! exp){
181             log.println("FAILED: expected exception 'UnknownPropertyException' was not thrown");
182         } else {
183             if (test) log.println("... OK");
184         }
185 
186         ok &= test;
187         test = false;
188         exp = false;
189         try {
190 
191             log.println("try to set values with invalid property name. " +
192                 "Expect 'com.sun.star.beans.UnknownPropertyException'...");
193 
194             PropertyValue[] newProps = new PropertyValue[1];
195             PropertyValue newProp = new PropertyValue();
196             newProp.Name = "XPropertyAccess.InvalidPropertyName";
197             newProp.Value = "invalid property";
198             newProps[0] = newProp;
199 
200             oObj.setPropertyValues(newProps);
201 
202         } catch (WrappedTargetException ex) {
203             log.println("ERROR: unexptected exception was thrown while trying to set invalid value: " +
204                 ex.toString());
205             exp = true;
206         } catch (com.sun.star.lang.IllegalArgumentException ex) {
207             log.println("ERROR: unexptected exception was thrown while trying to set invalid value: " +
208                 ex.toString());
209             exp = true;
210         } catch (PropertyVetoException ex) {
211             log.println("ERROR: unexptected exception was thrown while trying to set invalid value: " +
212                 ex.toString());
213             exp = true;
214         } catch (UnknownPropertyException ex) {
215             log.println("OK: Exptected exception was thrown while trying to set invalid value: " +
216                 ex.toString());
217             exp = true;
218             test = true;
219         }
220 
221         ok &= test;
222 
223         if (! exp){
224             log.println("FAILED: expected exception 'UnknownPropertyException' was not thrown");
225         } else {
226             if (test) log.println("... OK");
227         }
228 
229         tRes.tested("setPropertyValues()", ok);
230         return;
231 
232     }
233 
234 }    /// finish class XPropertyAccess
235 
236 
237