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