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