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