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