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 package ifc.beans;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.beans.GetDirectPropertyTolerantResult;
26cdf0e10cSrcweir import com.sun.star.beans.GetPropertyTolerantResult;
27cdf0e10cSrcweir import com.sun.star.beans.Property;
28cdf0e10cSrcweir import com.sun.star.beans.PropertyAttribute;
29cdf0e10cSrcweir import com.sun.star.beans.PropertyState;
30cdf0e10cSrcweir import com.sun.star.beans.SetPropertyTolerantFailed;
31cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
32cdf0e10cSrcweir import com.sun.star.beans.XPropertyState;
33cdf0e10cSrcweir import com.sun.star.beans.XTolerantMultiPropertySet;
34cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir import java.util.ArrayList;
37cdf0e10cSrcweir import java.util.Collections;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir import lib.MultiMethodTest;
40cdf0e10cSrcweir import lib.Status;
41cdf0e10cSrcweir import lib.StatusException;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir import util.ValueChanger;
44cdf0e10cSrcweir import util.ValueComparer;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 
47cdf0e10cSrcweir public class _XTolerantMultiPropertySet extends MultiMethodTest {
48cdf0e10cSrcweir     public XTolerantMultiPropertySet oObj;
49cdf0e10cSrcweir     protected String[] namesOfDirectProperties = null;
50cdf0e10cSrcweir     protected String[] namesOfProperties = null;
51cdf0e10cSrcweir     protected Object[] valuesOfProperties = null;
52cdf0e10cSrcweir     protected Property[] properties = null;
53cdf0e10cSrcweir     protected XPropertyState pState = null;
54cdf0e10cSrcweir     protected XPropertySet PS = null;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     /*
58cdf0e10cSrcweir      * Queries XPropertySet from the given Component and gets XPropertySetInfo
59cdf0e10cSrcweir      * from it to get the PropertyNames available and their Values<br>
60cdf0e10cSrcweir      * Then queries XPropertyState from the given Component
61cdf0e10cSrcweir      * to get the direct properties<br>
62cdf0e10cSrcweir      * Throws a lib StatusException if the Component doesn't support XPropertySet or XPropertyState
63cdf0e10cSrcweir      */
before()64cdf0e10cSrcweir     public void before() {
65cdf0e10cSrcweir         PS = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
66cdf0e10cSrcweir                                                       tEnv.getTestObject());
67cdf0e10cSrcweir 
68cdf0e10cSrcweir         if (PS == null) {
69cdf0e10cSrcweir             throw new StatusException(Status.failed(
70cdf0e10cSrcweir                                               "Component doesn't provide the needed XPropertySet"));
71cdf0e10cSrcweir         }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir         pState = (XPropertyState) UnoRuntime.queryInterface(
74cdf0e10cSrcweir                          XPropertyState.class, tEnv.getTestObject());
75cdf0e10cSrcweir 
76cdf0e10cSrcweir         if (pState == null) {
77cdf0e10cSrcweir             throw new StatusException(Status.failed(
78cdf0e10cSrcweir                                               "Component doesn't provide the needed XPropertyState"));
79cdf0e10cSrcweir         }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir         properties = PS.getPropertySetInfo().getProperties();
82cdf0e10cSrcweir         namesOfProperties = getProperties();
83cdf0e10cSrcweir         valuesOfProperties = getPropertyValues(namesOfProperties);
84cdf0e10cSrcweir     }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     /*
87cdf0e10cSrcweir      * Calls the method getDirectPropertyValuesTolerant() and compares the resulting
88cdf0e10cSrcweir      * sequence with the one gained as direct values in the before() method.<br>
89cdf0e10cSrcweir      * Has OK state if both sequences equal.
90cdf0e10cSrcweir      */
_getDirectPropertyValuesTolerant()91cdf0e10cSrcweir     public void _getDirectPropertyValuesTolerant() {
92cdf0e10cSrcweir         namesOfDirectProperties = getDirectProperties(properties);
93cdf0e10cSrcweir 
94cdf0e10cSrcweir         GetDirectPropertyTolerantResult[] GDPR = oObj.getDirectPropertyValuesTolerant(
95cdf0e10cSrcweir                                                          namesOfProperties);
96cdf0e10cSrcweir 
97cdf0e10cSrcweir         boolean res = (GDPR.length == namesOfDirectProperties.length);
98cdf0e10cSrcweir 
99cdf0e10cSrcweir         if (!res) {
100cdf0e10cSrcweir             log.println("Found: ");
101cdf0e10cSrcweir 
102cdf0e10cSrcweir             for (int i = 0; i < GDPR.length; i++) {
103cdf0e10cSrcweir                 log.println("\t" + GDPR[i].Name);
104cdf0e10cSrcweir             }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir             log.println("Expected: ");
107cdf0e10cSrcweir 
108cdf0e10cSrcweir             for (int i = 0; i < namesOfDirectProperties.length; i++) {
109cdf0e10cSrcweir                 log.println("\t" + namesOfDirectProperties[i]);
110cdf0e10cSrcweir             }
111cdf0e10cSrcweir         } else {
112cdf0e10cSrcweir             for (int i = 0; i < GDPR.length; i++) {
113cdf0e10cSrcweir                 boolean localres = GDPR[i].Name.equals(
114cdf0e10cSrcweir                                            namesOfDirectProperties[i]);
115cdf0e10cSrcweir 
116cdf0e10cSrcweir                 if (!localres) {
117cdf0e10cSrcweir                     log.println("Found: ");
118cdf0e10cSrcweir                     log.println("\t" + GDPR[i].Name);
119cdf0e10cSrcweir                     log.println("Expected: ");
120cdf0e10cSrcweir                     log.println("\t" + namesOfDirectProperties[i]);
121cdf0e10cSrcweir                 }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir                 res &= localres;
124cdf0e10cSrcweir             }
125cdf0e10cSrcweir         }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         tRes.tested("getDirectPropertyValuesTolerant()", res);
128cdf0e10cSrcweir     }
129cdf0e10cSrcweir 
_getPropertyValuesTolerant()130cdf0e10cSrcweir     public void _getPropertyValuesTolerant() {
131cdf0e10cSrcweir         requiredMethod("getDirectPropertyValuesTolerant()");
132cdf0e10cSrcweir         GetPropertyTolerantResult[] GPR = oObj.getPropertyValuesTolerant(
133cdf0e10cSrcweir                                                   namesOfProperties);
134cdf0e10cSrcweir 
135cdf0e10cSrcweir         boolean res = (GPR.length == namesOfProperties.length);
136cdf0e10cSrcweir 
137cdf0e10cSrcweir         if (!res) {
138cdf0e10cSrcweir             log.println("Length of sequences differs");
139cdf0e10cSrcweir             log.println("Found: " + GPR.length);
140cdf0e10cSrcweir             log.println("Expected: " + namesOfProperties.length);
141cdf0e10cSrcweir         } else {
142cdf0e10cSrcweir             for (int i = 0; i < GPR.length; i++) {
143cdf0e10cSrcweir                 boolean localres = true;
144cdf0e10cSrcweir 
145cdf0e10cSrcweir                 if (!(GPR[i].Value instanceof com.sun.star.uno.Any)) {
146cdf0e10cSrcweir                     localres = ValueComparer.equalValue(GPR[i].Value,
147cdf0e10cSrcweir                                                         valuesOfProperties[i]);
148cdf0e10cSrcweir 
149cdf0e10cSrcweir                 }
150cdf0e10cSrcweir 
151cdf0e10cSrcweir                 if (!localres) {
152cdf0e10cSrcweir                     log.println("Values differ for : " +
153cdf0e10cSrcweir                                 namesOfProperties[i]);
154cdf0e10cSrcweir                     log.println("\t" + GPR[i].Value);
155cdf0e10cSrcweir                     log.println("Expected: ");
156cdf0e10cSrcweir                     log.println("\t" + valuesOfProperties[i]);
157cdf0e10cSrcweir                 }
158cdf0e10cSrcweir 
159cdf0e10cSrcweir                 res &= localres;
160cdf0e10cSrcweir             }
161cdf0e10cSrcweir         }
162cdf0e10cSrcweir 
163cdf0e10cSrcweir         tRes.tested("getPropertyValuesTolerant()", res);
164cdf0e10cSrcweir     }
165cdf0e10cSrcweir 
_setPropertyValuesTolerant()166cdf0e10cSrcweir     public void _setPropertyValuesTolerant() {
167cdf0e10cSrcweir         requiredMethod("getPropertyValuesTolerant()");
168cdf0e10cSrcweir 
169cdf0e10cSrcweir         SetPropertyTolerantFailed[] SPTF = null;
170cdf0e10cSrcweir 
171cdf0e10cSrcweir         try {
172cdf0e10cSrcweir             SPTF = oObj.setPropertyValuesTolerant(namesOfProperties,
173cdf0e10cSrcweir                                                   getNewValues(
174cdf0e10cSrcweir                                                           valuesOfProperties));
175cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException e) {
176cdf0e10cSrcweir             e.printStackTrace(log);
177cdf0e10cSrcweir         }
178cdf0e10cSrcweir 
179cdf0e10cSrcweir         //read only properties will throw a PropertyVetoExeption if they are set
180cdf0e10cSrcweir         int failures = 0;
181cdf0e10cSrcweir 
182cdf0e10cSrcweir         for (int k = 0; k < SPTF.length; k++) {
183cdf0e10cSrcweir             if (SPTF[k].Result == com.sun.star.beans.TolerantPropertySetResultType.PROPERTY_VETO) {
184cdf0e10cSrcweir                 failures++;
185cdf0e10cSrcweir             }
186cdf0e10cSrcweir         }
187cdf0e10cSrcweir 
188cdf0e10cSrcweir         int roProps = getCountOfReadOnlyProperties();
189cdf0e10cSrcweir 
190cdf0e10cSrcweir         boolean res = (failures == roProps);
191cdf0e10cSrcweir 
192cdf0e10cSrcweir         if (!res) {
193cdf0e10cSrcweir             log.println("Failures: " + failures);
194cdf0e10cSrcweir             log.println("Count of R/O properties: " + roProps);
195cdf0e10cSrcweir 
196cdf0e10cSrcweir             for (int i = 0; i < SPTF.length; i++) {
197cdf0e10cSrcweir                 if (SPTF[i].Result == com.sun.star.beans.TolerantPropertySetResultType.PROPERTY_VETO) {
198cdf0e10cSrcweir                     failures++;
199cdf0e10cSrcweir                     log.println("Failed for " + SPTF[i].Name);
200cdf0e10cSrcweir                     log.println("\t Result: " + SPTF[i].Result);
201cdf0e10cSrcweir                 }
202cdf0e10cSrcweir             }
203cdf0e10cSrcweir         } else {
204cdf0e10cSrcweir             for (int i = 0; i < SPTF.length; i++) {
205cdf0e10cSrcweir                 boolean localres = true;
206cdf0e10cSrcweir                 GetPropertyTolerantResult[] GPR = oObj.getPropertyValuesTolerant(
207cdf0e10cSrcweir                                                           namesOfProperties);
208cdf0e10cSrcweir 
209cdf0e10cSrcweir                 if ((!(GPR[i].Value instanceof com.sun.star.uno.Any)) &&
210cdf0e10cSrcweir                         (SPTF[i].Result == com.sun.star.beans.TolerantPropertySetResultType.SUCCESS)) {
211cdf0e10cSrcweir                     localres = ValueComparer.equalValue(GPR[i].Value,
212cdf0e10cSrcweir                                                         valuesOfProperties[i]);
213cdf0e10cSrcweir                 }
214cdf0e10cSrcweir 
215cdf0e10cSrcweir                 if (!localres) {
216cdf0e10cSrcweir                     log.println("Values differ for : " +
217cdf0e10cSrcweir                                 namesOfProperties[i]);
218cdf0e10cSrcweir                     log.println("\t" + GPR[i].Value);
219cdf0e10cSrcweir                     log.println("Expected: ");
220cdf0e10cSrcweir                     log.println("\t" + valuesOfProperties[i]);
221cdf0e10cSrcweir                 }
222cdf0e10cSrcweir 
223cdf0e10cSrcweir                 res &= localres;
224cdf0e10cSrcweir             }
225cdf0e10cSrcweir         }
226cdf0e10cSrcweir 
227cdf0e10cSrcweir         tRes.tested("setPropertyValuesTolerant()", res);
228cdf0e10cSrcweir     }
229cdf0e10cSrcweir 
230cdf0e10cSrcweir     /*
231cdf0e10cSrcweir      * This method returns a sorted list of property names
232cdf0e10cSrcweir      * contained in a given sequence of properties that additionally
233cdf0e10cSrcweir      * have the state DIRECT_VALUE
234cdf0e10cSrcweir      */
getDirectProperties(Property[] props)235cdf0e10cSrcweir     protected String[] getDirectProperties(Property[] props) {
236cdf0e10cSrcweir         ArrayList direct = new ArrayList();
237cdf0e10cSrcweir 
238cdf0e10cSrcweir         for (int i = 0; i < props.length; i++) {
239cdf0e10cSrcweir             String pName = props[i].Name;
240cdf0e10cSrcweir 
241cdf0e10cSrcweir             try {
242cdf0e10cSrcweir                 PropertyState state = pState.getPropertyState(pName);
243cdf0e10cSrcweir 
244cdf0e10cSrcweir                 if (state.equals(PropertyState.DIRECT_VALUE)) {
245cdf0e10cSrcweir                     if (isUsable(pName)) direct.add(pName);
246cdf0e10cSrcweir                 }
247cdf0e10cSrcweir             } catch (com.sun.star.beans.UnknownPropertyException e) {
248cdf0e10cSrcweir                 log.println("Property '" + pName + "'");
249cdf0e10cSrcweir             }
250cdf0e10cSrcweir         }
251cdf0e10cSrcweir 
252cdf0e10cSrcweir         Collections.sort(direct);
253cdf0e10cSrcweir 
254cdf0e10cSrcweir         Object[] obj = direct.toArray();
255cdf0e10cSrcweir         String[] ret = new String[obj.length];
256cdf0e10cSrcweir 
257cdf0e10cSrcweir         for (int i = 0; i < obj.length; i++) {
258cdf0e10cSrcweir             ret[i] = (String) obj[i];
259cdf0e10cSrcweir         }
260cdf0e10cSrcweir 
261cdf0e10cSrcweir         return ret;
262cdf0e10cSrcweir     }
263cdf0e10cSrcweir 
isUsable(String name)264cdf0e10cSrcweir     private boolean isUsable(String name) {
265cdf0e10cSrcweir         boolean isUsable=true;
266cdf0e10cSrcweir         if (name.startsWith("TextWriting")) isUsable = false;
267cdf0e10cSrcweir         if (name.startsWith("MetaFile")) isUsable = false;
268cdf0e10cSrcweir         return isUsable;
269cdf0e10cSrcweir     }
270cdf0e10cSrcweir 
271cdf0e10cSrcweir     /*
272cdf0e10cSrcweir      * This method returns a sorted list of property names
273cdf0e10cSrcweir      * contained in a given sequence of properties
274cdf0e10cSrcweir      */
getProperties()275cdf0e10cSrcweir     protected String[] getProperties() {
276cdf0e10cSrcweir         ArrayList names = new ArrayList();
277cdf0e10cSrcweir 
278cdf0e10cSrcweir         for (int i = 0; i < properties.length; i++) {
279cdf0e10cSrcweir             String pName = properties[i].Name;
280cdf0e10cSrcweir             if (isUsable(pName)) names.add(pName);
281cdf0e10cSrcweir         }
282cdf0e10cSrcweir 
283cdf0e10cSrcweir         Collections.sort(names);
284cdf0e10cSrcweir 
285cdf0e10cSrcweir         Object[] obj = names.toArray();
286cdf0e10cSrcweir         String[] ret = new String[obj.length];
287cdf0e10cSrcweir 
288cdf0e10cSrcweir         for (int i = 0; i < obj.length; i++) {
289cdf0e10cSrcweir             ret[i] = (String) obj[i];
290cdf0e10cSrcweir         }
291cdf0e10cSrcweir 
292cdf0e10cSrcweir         return ret;
293cdf0e10cSrcweir     }
294cdf0e10cSrcweir 
295cdf0e10cSrcweir     /*
296cdf0e10cSrcweir      * Returns the values of a given array of properties in an Object array
297cdf0e10cSrcweir      */
getPropertyValues(String[] propertyNames)298cdf0e10cSrcweir     protected Object[] getPropertyValues(String[] propertyNames) {
299cdf0e10cSrcweir         Object[] values = new Object[propertyNames.length];
300cdf0e10cSrcweir 
301cdf0e10cSrcweir         for (int i = 0; i < propertyNames.length; i++) {
302cdf0e10cSrcweir             try {
303cdf0e10cSrcweir                 values[i] = PS.getPropertyValue(propertyNames[i]);
304cdf0e10cSrcweir             } catch (com.sun.star.beans.UnknownPropertyException e) {
305cdf0e10cSrcweir                 e.printStackTrace(log);
306cdf0e10cSrcweir             } catch (com.sun.star.lang.WrappedTargetException e) {
307cdf0e10cSrcweir                 e.printStackTrace(log);
308cdf0e10cSrcweir             }
309cdf0e10cSrcweir         }
310cdf0e10cSrcweir 
311cdf0e10cSrcweir         return values;
312cdf0e10cSrcweir     }
313cdf0e10cSrcweir 
getCountOfReadOnlyProperties()314cdf0e10cSrcweir     protected int getCountOfReadOnlyProperties() {
315cdf0e10cSrcweir         int ro = 0;
316cdf0e10cSrcweir 
317cdf0e10cSrcweir         for (int i = 0; i < properties.length; i++) {
318cdf0e10cSrcweir             Property property = properties[i];
319cdf0e10cSrcweir             boolean isWritable = ((property.Attributes & PropertyAttribute.READONLY) == 0);
320cdf0e10cSrcweir 
321cdf0e10cSrcweir             if (!isWritable) {
322cdf0e10cSrcweir                 ro++;
323cdf0e10cSrcweir             }
324cdf0e10cSrcweir         }
325cdf0e10cSrcweir 
326cdf0e10cSrcweir         return ro;
327cdf0e10cSrcweir     }
328cdf0e10cSrcweir 
getNewValues(Object[] oldValues)329cdf0e10cSrcweir     protected Object[] getNewValues(Object[] oldValues) {
330cdf0e10cSrcweir         Object[] newValues = new Object[oldValues.length];
331cdf0e10cSrcweir 
332cdf0e10cSrcweir         for (int i = 0; i < oldValues.length; i++) {
333cdf0e10cSrcweir             if (oldValues[i] instanceof com.sun.star.uno.Any) {
334cdf0e10cSrcweir                 newValues[i] = oldValues[i];
335cdf0e10cSrcweir             } else {
336cdf0e10cSrcweir                 newValues[i] = ValueChanger.changePValue(oldValues[i]);
337cdf0e10cSrcweir             }
338cdf0e10cSrcweir         }
339cdf0e10cSrcweir 
340cdf0e10cSrcweir         return newValues;
341cdf0e10cSrcweir     }
342cdf0e10cSrcweir }