Debian Codegraph @

framework/qa/complex/path_settings/PathSettingsTest.java

raw
1 /*
2 * This file is part of the LibreOffice project.
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 *
8 * This file incorporates work covered by the following license notice:
9 *
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
17 */
18 package complex.path_settings;
19
20 import com.sun.star.beans.Property;
21 import com.sun.star.beans.UnknownPropertyException;
22 import com.sun.star.beans.XFastPropertySet;
23 import com.sun.star.beans.XPropertySet;
24 import com.sun.star.beans.XPropertiesChangeListener;
25 import com.sun.star.beans.XPropertyChangeListener;
26 import com.sun.star.beans.XVetoableChangeListener;
27 import com.sun.star.lang.WrappedTargetException;
28 import com.sun.star.uno.UnoRuntime;
29 import com.sun.star.uno.AnyConverter;
30 import com.sun.star.util.thePathSettings;
31
32 // ---------- junit imports -----------------
33 import java.util.ArrayList;
34 import java.util.StringTokenizer;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.openoffice.test.OfficeConnection;
40 import static org.junit.Assert.*;
41
42
43 public class PathSettingsTest
44 {
45
46 // the test object: an instance of the tested service
47 private static Object aPathSettings = null;
48 // the properties of the service
49 private static Property[] xPropertyInfoOfPathSettings = null;
50 private static String[] aPathSettingNames = null;
51 private static String[] availablePropNames = new String[]
52 {
53 "Addin",
54 "AutoCorrect",
55 "AutoText",
56 "Backup",
57 "Basic",
58 "Bitmap",
59 "Config",
60 "Dictionary",
61 "Favorite",
62 "Filter",
63 "Fingerprint",
64 "Gallery",
65 "Graphic",
66 "Help",
67 "Linguistic",
68 "Module",
69 "Palette",
70 "Plugin",
71 "Storage",
72 "Temp",
73 "Template",
74 "UIConfig",
75 "UserConfig",
76 "Work",
77 };
78 // every path name from availablePropNames exist in this characteristics
79 // name
80 // name_internal
81 // name_user
82 // name_writable
83 private static String[] availablePropNameExtensions = new String[]
84 {
85 "",
86 "_internal",
87 "_user",
88 "_writable"
89 };
90 private static String[] aPathSettingValues = null;
91 ArrayList<Property> aListOfWorkingProperty;
92
93 /**
94 * Initialize before the tests start: this has to be done only once.
95 * This methods sets the 'aPathSettings' and 'xPropertyInfoOfPathSettings' variables.
96 */
97 @Before
98 public void before() throws Exception
99 {
100 try
101 {
102 aPathSettings = thePathSettings.get(connection.getComponentContext());
103 assertNotNull("Can't instantiate com.sun.star.util.thePathSettings.", aPathSettings);
104 util.dbg.getSuppServices(aPathSettings);
105 final XPropertySet xPropSet_of_PathSettings = UnoRuntime.queryInterface(XPropertySet.class, aPathSettings);
106
107 xPropertyInfoOfPathSettings = xPropSet_of_PathSettings.getPropertySetInfo().getProperties();
108 aPathSettingNames = new String[xPropertyInfoOfPathSettings.length];
109 aPathSettingValues = new String[xPropertyInfoOfPathSettings.length];
110
111 aListOfWorkingProperty = new ArrayList<Property>();
112
113 // get initial values and create new ones
114 for (int i = 0; i < xPropertyInfoOfPathSettings.length; i++)
115 {
116 final String sName = xPropertyInfoOfPathSettings[i].Name;
117 aPathSettingNames[i] = sName;
118 Object o = xPropSet_of_PathSettings.getPropertyValue(sName);
119
120 String sValue = convertToString(o);
121 aPathSettingValues[i] = sValue;
122 aListOfWorkingProperty.add(xPropertyInfoOfPathSettings[i]);
123 }
124 }
125 catch (com.sun.star.uno.Exception e)
126 {
127 System.out.println(e.getClass().getName());
128 System.out.println("Message: " + e.getMessage());
129 }
130 }
131
132 private String convertToString(Object o)
133 {
134 String sValue = "";
135 try
136 {
137 if (AnyConverter.isString(o))
138 {
139 sValue = AnyConverter.toString(o);
140 }
141 else if (AnyConverter.isArray(o))
142 {
143 Object oValueList = AnyConverter.toArray(o);
144 String[] aValueList = (String[]) oValueList;
145 String sValues = "";
146 for (int j = 0; j < aValueList.length; j++)
147 {
148 if (sValues.length() > 0)
149 {
150 sValues += ";";
151 }
152 sValues += aValueList[j];
153 }
154 sValue = sValues;
155 }
156 else
157 {
158 System.out.println("Can't convert Object to String");
159 }
160 }
161 catch (com.sun.star.lang.IllegalArgumentException e)
162 {
163 /* ignore */
164 }
165 return sValue;
166 }
167
168 /**
169 * Simple existence test, if this fails, the Lists must update
170 */
171 @Test
172 public void checkInternalListConsistence()
173 {
174 // check if all Properties are in the internal test list
175 for (int i = 0; i < xPropertyInfoOfPathSettings.length; i++)
176 {
177 final String sName = xPropertyInfoOfPathSettings[i].Name;
178 boolean bOccur = checkIfNameExistsInList(sName, availablePropNames, availablePropNameExtensions);
179 assertTrue("TEST IS WRONG, Name:='" + sName + "' doesn't exist in internal Test list.", bOccur);
180 }
181
182 // check if all properties in the internal list also exist in real life
183 for (int i = 0; i < availablePropNames.length; i++)
184 {
185 final String aListName = availablePropNames[i];
186 for (int j = 0; j < availablePropNameExtensions.length; j++)
187 {
188 final String aSubListName = availablePropNameExtensions[j];
189 final String aName = aListName + aSubListName;
190 boolean bOccur = checkIfNameExistsInList(aName, aPathSettingNames, new String[]
191 {
192 ""
193 } /* list must not empty! */);
194 assertTrue("TEST IS WRONG, Name:='" + aName + "' from the internal test list do not occur in real life path settings.", bOccur);
195 }
196 }
197 }
198
199 /**
200 * Simple O(n^n) check if a given String (_sNameMustOccur) exist in the given list(+SubList) values.
201 * @param _sNameMustOccur
202 * @param _aList
203 * @param _aSubList
204 * @return true, if name occur
205 */
206 private boolean checkIfNameExistsInList(String _sNameMustOccur, String[] _aList, String[] _aSubList)
207 {
208 for (int i = 0; i < _aList.length; i++)
209 {
210 final String aListName = _aList[i];
211 for (int j = 0; j < _aSubList.length; j++)
212 {
213 final String aSubListName = _aSubList[j];
214 final String aName = aListName + aSubListName;
215 if (aName.equals(_sNameMustOccur))
216 {
217 return true;
218 }
219 }
220 }
221 return false;
222 }
223
224 /**
225 * Shows the path settings
226 */
227 @Test
228 public void showPathSettings() throws UnknownPropertyException, WrappedTargetException
229 {
230 System.out.println("\n---- All properties ----");
231 final XPropertySet xPropSet_of_PathSettings = UnoRuntime.queryInterface(XPropertySet.class, aPathSettings);
232
233 for (int i = 0; i < aListOfWorkingProperty.size(); i++)
234 {
235 final String sName = aListOfWorkingProperty.get(i).Name;
236 System.out.print(sName);
237 Object o = xPropSet_of_PathSettings.getPropertyValue(sName);
238
239 try
240 {
241 AnyConverter.toString(o);
242 }
243 catch (com.sun.star.lang.IllegalArgumentException e)
244 {
245 }
246 System.out.println();
247 }
248 System.out.println("---- Finish showing properties ----\n");
249 }
250
251 private boolean checkPaths(Object _o, Object _o2)
252 {
253 String sLeftPath = "";
254 String sRightPath = "";
255 if (AnyConverter.isArray(_o))
256 {
257 try
258 {
259 Object oValues = AnyConverter.toArray(_o);
260 sLeftPath = convertToString(oValues);
261 }
262 catch (com.sun.star.lang.IllegalArgumentException e)
263 {
264 }
265 }
266 else if (AnyConverter.isString(_o))
267 {
268 try
269 {
270 sLeftPath = AnyConverter.toString(_o);
271 }
272 catch (com.sun.star.lang.IllegalArgumentException e)
273 {
274 }
275 }
276
277 if (AnyConverter.isArray(_o2))
278 {
279 try
280 {
281 Object oValues = AnyConverter.toArray(_o2);
282 sRightPath = convertToString(oValues);
283 }
284 catch (com.sun.star.lang.IllegalArgumentException e)
285 {
286 }
287 }
288 else if (AnyConverter.isString(_o2))
289 {
290 try
291 {
292 sRightPath = AnyConverter.toString(_o2);
293 }
294 catch (com.sun.star.lang.IllegalArgumentException e)
295 {
296 }
297 }
298 return checkPaths(sLeftPath, sRightPath);
299 }
300
301 /**
302 * Check 2 given paths if the _aOtherPath exists in _aPath, _aPath could be a list separated by ';'
303 * @param _aPath
304 * @param _aOtherPath
305 * @return true, if _aOtherPath found
306 */
307 private boolean checkPaths(String _aPath, String _aOtherPath)
308 {
309 if (_aOtherPath.contains(";"))
310 {
311 StringTokenizer aToken = new StringTokenizer(_aOtherPath, ";");
312 int nCount = 0;
313 int nFound = 0;
314 while (aToken.hasMoreElements())
315 {
316 String sPath = (String)aToken.nextElement();
317 nCount ++;
318 if (checkPaths(_aPath, sPath))
319 {
320 nFound++;
321 }
322 }
323 if (nFound == nCount)
324 {
325 return true;
326 }
327 }
328 else if(_aPath.contains(";"))
329 {
330 StringTokenizer aToken = new StringTokenizer(_aPath, ";");
331 while (aToken.hasMoreElements())
332 {
333 String sToken = (String)aToken.nextElement();
334 if (sToken.equals(_aOtherPath))
335 {
336 return true;
337 }
338 }
339 return false;
340 }
341 else if (_aPath.equals(_aOtherPath))
342 {
343 return true;
344 }
345 return false;
346 }
347
348 /**
349 * This tests the XFastPropertySet interface implementation.
350 */
351 @Test
352 public void checkXFastPropertySet()
353 {
354 System.out.println("---- Testing the XFastPropertySet interface ----");
355
356
357 // do for all properties
358 // xPropertyInfoOfPathSettings.length
359 for (int i = 0; i < aListOfWorkingProperty.size(); i++)
360 {
361 final Property property = aListOfWorkingProperty.get(i); // xPropertyInfoOfPathSettings[i];
362 String name = property.Name;
363 // get property name and initial value
364 System.out.println("Test property with name: " + name);
365 boolean bResult;
366 if (name.endsWith("_writable"))
367 {
368 bResult = checkStringProperty(property);
369 }
370 else if (name.endsWith("_user"))
371 {
372 bResult = checkStringListProperty(property);
373 }
374 else if (name.endsWith("_internal"))
375 {
376 bResult = checkStringListProperty(property);
377 }
378 else
379 {
380 // old path settings
381 bResult = checkStringProperty(property);
382 }
383 System.out.print(" Test of property " + name + " finished");
384 if (bResult)
385 {
386 System.out.println(" [ok]");
387 }
388 else
389 {
390 System.out.println(" [FAILED]");
391 }
392 System.out.println();
393 }
394 System.out.println("---- Test of XFastPropertySet finished ----\n");
395 }
396
397 private boolean checkStringListProperty(Property property)
398 {
399 // creating instances
400 boolean bResult = true;
401 XFastPropertySet xFPS = UnoRuntime.queryInterface(XFastPropertySet.class, aPathSettings);
402
403 String name = property.Name;
404 int handle = property.Handle;
405
406 Object oValue;
407 try
408 {
409 oValue = xFPS.getFastPropertyValue(handle);
410 }
411 catch (UnknownPropertyException ex)
412 {
413 return false;
414 }
415 catch (WrappedTargetException ex)
416 {
417 return false;
418 }
419
420 if (!AnyConverter.isArray(oValue))
421 {
422 System.out.println(" Internal error, type wrong. PathSetting property with name:" + name + " should be an array.");
423 return false;
424 }
425
426 String val;
427 try
428 {
429 Object oValues = AnyConverter.toArray(oValue);
430
431
432 final String[] aValues = (String[])oValues;
433
434 // aNewValues contains a deep copy of aValues
435 String[] aNewValues = new String[aValues.length];
436 System.arraycopy(aValues, 0, aNewValues, 0, aValues.length);
437 if (aValues.length > 0)
438 {
439 val = aValues[0];
440 }
441 else
442 {
443 val = null;
444 aNewValues = new String[1]; // create a String list
445 }
446 System.out.println(" Property has initial value: '" + val + "'");
447
448 // set to a new correct value
449 String newVal = changeToCorrectValue(val);
450 assertFalse("newVal must not equal val.", newVal.equals(val));
451
452 System.out.println(" Try to change to a correct value '" + newVal + "'");
453 aNewValues[0] = newVal;
454
455 try
456 {
457 try
458 {
459 xFPS.setFastPropertyValue(handle, aNewValues);
460 }
461 catch (com.sun.star.lang.WrappedTargetException e)
462 {
463 System.out.println(" FAIL: setFastPropertyValue(handle:=" + handle + ", name:='" + name + "')" + e.getMessage());
464 bResult = false;
465 }
466
467 // Property_internal can't change we will not arrive beyond this line
468
469 // check the change
470 Object oObj = xFPS.getFastPropertyValue(handle);
471 if (!checkPaths(oObj, aNewValues))
472 {
473 System.out.println(" FAIL: Did not change value on property " + name + ".");
474 bResult = false;
475 }
476
477 // set back to initial setting
478 System.out.println(" Try to check");
479 try
480 {
481 xFPS.setFastPropertyValue(handle, oValue);
482 }
483 catch (com.sun.star.beans.PropertyVetoException e)
484 {
485 // should not occur
486 System.out.println(" FAIL: PropertyVetoException caught: " + e.getMessage());
487 bResult = false;
488 }
489 }
490 catch (com.sun.star.beans.PropertyVetoException e)
491 {
492 if (!name.endsWith("_internal"))
493 {
494 // should not occur
495 System.out.println(" FAIL: PropertyVetoException caught: " + e.getMessage());
496 bResult = false;
497 }
498 else
499 {
500 System.out.println(" OK: PropertyVetoException caught: " + e.getMessage() + " it seems not allowed to change internal values.");
501 }
502 }
503
504 // check if changed
505 Object checkVal3 = xFPS.getFastPropertyValue(handle);
506 if (!checkPaths(checkVal3, oValues))
507 {
508 System.out.println(" FAIL: Can't change value back to original on property " + name);
509 bResult = false;
510 }
511 }
512 catch (com.sun.star.uno.Exception e)
513 {
514 System.out.println(" FAIL: getFastPropertyValue(handle:=" + handle + ", name:='" + name + "')" + e.getMessage());
515 bResult = false;
516 }
517 return bResult;
518 }
519
520 private boolean checkStringProperty(Property property)
521 {
522 boolean bResult = true;
523 XFastPropertySet xFPS = UnoRuntime.queryInterface(XFastPropertySet.class, aPathSettings);
524 String name = property.Name;
525 int handle = property.Handle;
526 Object oValue;
527 try
528 {
529 oValue = xFPS.getFastPropertyValue(handle);
530 }
531 catch (UnknownPropertyException ex)
532 {
533 return false;
534 }
535 catch (WrappedTargetException ex)
536 {
537 return false;
538 }
539
540
541 try
542 {
543 String val = "";
544 val = AnyConverter.toString(oValue);
545 System.out.println(" Property has initial value: '" + val + "'");
546
547 // set to a new correct value
548 String newVal = changeToCorrectValue(val);
549 System.out.println(" Try to change to a correct value '" + newVal + "'");
550 xFPS.setFastPropertyValue(handle, newVal);
551
552 // check the change
553 String checkVal = (String) xFPS.getFastPropertyValue(handle);
554 if (!checkPaths(checkVal, newVal))
555 {
556 System.out.println(" FAIL: Did not change value on property " + name + ".");
557 bResult = false;
558 }
559 /*
560 * Change the given String to an incorrect path URL.
561 */
562 newVal = "fileblablabla";
563 System.out.println(" Try to change to incorrect value '" + newVal + "'");
564 try
565 {
566 xFPS.setFastPropertyValue(handle, newVal);
567 }
568 catch (com.sun.star.lang.IllegalArgumentException e)
569 {
570 System.out.println(" Correctly thrown Exception caught.");
571 }
572
573 // check if changed
574 String checkVal2 = (String) xFPS.getFastPropertyValue(handle);
575 if (!checkPaths(checkVal2, checkVal))
576 {
577 System.out.println(" FAIL: Value did change on property " + name + " though it should not have.");
578 bResult = false;
579 }
580 else
581 {
582 System.out.println(" OK: Incorrect value was not set.");
583 }
584 // set back to initial setting
585 System.out.println(" Set back to initial value.");
586 try
587 {
588 xFPS.setFastPropertyValue(handle, val);
589 }
590 catch (com.sun.star.lang.IllegalArgumentException e)
591 {
592 System.out.println(" IllegalArgumentException caught: " + e.getMessage());
593 bResult = false;
594 }
595 // check if changed
596 String checkVal3 = (String) xFPS.getFastPropertyValue(handle);
597 if (!checkVal3.equals(val))
598 {
599 if (!checkPaths(checkVal3, val))
600 {
601 System.out.println(" FAIL: Can't change value back to original on property " + name);
602 System.out.println(" Value is: " + checkVal3);
603
604 bResult = false;
605 }
606 else
607 {
608 System.out.println(" OK: the pathsettings contains the original path.");
609 System.out.println(" Value is: " + checkVal3);
610 System.out.println(" Value should be: " + val);
611 }
612 }
613 else
614 {
615 System.out.println(" OK: Change value back to original on property " + name);
616 }
617 }
618 catch (com.sun.star.uno.Exception e)
619 {
620 System.out.println(" FAIL: getFastPropertyValue(handle:=" + handle + ", name:='" + name + "')" + e.getMessage());
621 bResult = false;
622 }
623 return bResult;
624 }
625
626
627 // ____________________
628 /**
629 * This tests the XPropertySet interface implementation.
630 */
631
632 /**
633 * Change the given String to a correct path URL.
634 * @return The changed path URL.
635 */
636 private String changeToCorrectValue(String path)
637 {
638 // the simplest possibility
639 if (path == null || path.equals(""))
640 {
641 String sTempDir = System.getProperty("java.io.tmpdir");
642 sTempDir = util.utils.getFullURL(sTempDir);
643 return sTempDir; // "file:///tmp";
644 }
645 return graphical.FileHelper.appendPath(path, "tmp");
646 }
647
648 /**
649 * Listener implementation which sets a flag when
650 * listener was called.
651 */
652 public class MyChangeListener implements XPropertiesChangeListener,
653 XPropertyChangeListener,
654 XVetoableChangeListener
655 {
656
657 private boolean propChanged = false;
658 private boolean propertiesChanged = false;
659 private boolean vetoableChanged = false;
660
661 public void propertiesChange(
662 com.sun.star.beans.PropertyChangeEvent[] e)
663 {
664 propertiesChanged = true;
665 }
666
667 public void vetoableChange(com.sun.star.beans.PropertyChangeEvent pE)
668 throws com.sun.star.beans.PropertyVetoException
669 {
670 vetoableChanged = true;
671 }
672
673 public void propertyChange(com.sun.star.beans.PropertyChangeEvent pE)
674 {
675 propChanged = true;
676 }
677
678 public void disposing(com.sun.star.lang.EventObject eventObject)
679 {
680 }
681
682 public void resetListener()
683 {
684 propChanged = false;
685 propertiesChanged = false;
686 vetoableChanged = false;
687 }
688
689 public boolean changePropertyEventFired()
690 {
691 return propChanged;
692 }
693
694 public boolean changePropertiesEventFired()
695 {
696 return propertiesChanged;
697 }
698
699 public boolean vetoableChangeEventFired()
700 {
701 return vetoableChanged;
702 }
703 }
704
705 // setup and close connections
706 @BeforeClass
707 public static void setUpConnection() throws Exception
708 {
709 System.out.println("setUpConnection()");
710 connection.setUp();
711 }
712
713 @AfterClass
714 public static void tearDownConnection()
715 throws InterruptedException, com.sun.star.uno.Exception
716 {
717 System.out.println("tearDownConnection()");
718 connection.tearDown();
719 }
720 private static final OfficeConnection connection = new OfficeConnection();
721 }