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 
24 package ifc.util;
25 
26 import com.sun.star.beans.XPropertySet;
27 import com.sun.star.container.XNameReplace;
28 import com.sun.star.util.ElementChange;
29 import lib.MultiMethodTest;
30 
31 import com.sun.star.util.XChangesBatch;
32 import lib.Status;
33 import lib.StatusException;
34 
35 public class _XChangesBatch extends MultiMethodTest {
36 
37     public XChangesBatch oObj;
38     private Object changeElement = null;
39     private Object originalElement = null;
40     private String elementName = null;
41     private XPropertySet xProp = null;
42     private XNameReplace xNameReplace = null;
43 
44     /**
45      * add a change that can be committed
46      */
before()47     protected void before() {
48         changeElement = tEnv.getObjRelation("XChangesBatch.ChangeElement");
49         originalElement = tEnv.getObjRelation("XChangesBatch.OriginalElement");
50         elementName = (String)tEnv.getObjRelation("XChangesBatch.PropertyName");
51 
52         // to do a change, get an XPropertySet
53         xProp = (XPropertySet)tEnv.getObjRelation("XChangesBatch.PropertySet");
54         try {
55             if (originalElement == null && xProp != null)
56                 originalElement = xProp.getPropertyValue(elementName);
57         }
58         catch(com.sun.star.uno.Exception e) {
59             throw new StatusException("Could not get property '" + elementName + "'.", e);
60         }
61 
62         // or get an XNameReplace
63         xNameReplace = (XNameReplace)tEnv.getObjRelation("XChangesBatch.NameReplace");
64         try {
65             if (originalElement == null && xNameReplace != null)
66                 originalElement = xNameReplace.getByName(elementName);
67         }
68         catch(com.sun.star.uno.Exception e) {
69             throw new StatusException("Could not get element by name '" + elementName + "'.", e);
70         }
71 
72         if (changeElement == null || originalElement == null || elementName == null || (xProp == null && xNameReplace == null)) {
73             log.println(
74                 changeElement == null?"Missing property 'XChangesBatch.ChangeElement'\n":"" +
75                 originalElement == null?"Missing property 'XChangesBatch.OriginalElement'\n":"" +
76                 elementName == null?"Missing property 'XChangesBatch.PropertyName'\n":"" +
77                 xProp == null?"Missing property 'XChangesBatch.PropertySet'":"" +
78                 xNameReplace == null?"Missing property 'XChangesBatch.NameReplace'":""
79             );
80             throw new StatusException("Some needed object relations are missing.", new Exception());
81         }
82     }
83 
_commitChanges()84     public void _commitChanges() {
85         requiredMethod("getPendingChanges()");
86         try {
87             log.println("Committing changes.");
88             oObj.commitChanges();
89         }
90         catch(com.sun.star.lang.WrappedTargetException e) {
91             tRes.tested("commitChanges()", Status.exception(e));
92             return;
93         }
94         try {
95             executeChange(originalElement);
96         }
97         catch(StatusException e) {
98             tRes.tested("hasPendingChanges()", Status.exception(e));
99             return;
100         }
101 
102         try {
103             log.println("Commit changes back.");
104             oObj.commitChanges();
105         }
106         catch(com.sun.star.lang.WrappedTargetException e) {
107             tRes.tested("commitChanges()", Status.exception(e));
108             return;
109         }
110         tRes.tested("commitChanges()", true);
111     }
112 
_getPendingChanges()113     public void _getPendingChanges() {
114         requiredMethod("hasPendingChanges()");
115         ElementChange[]changes = oObj.getPendingChanges();
116         if (changes == null) {
117             log.println("Returned changes was 'null'");
118             log.println("It should have been 1 change.");
119             tRes.tested("getPendingChanges()", false);
120         } else if (changes.length != 1) {
121             int amount = changes.length;
122             log.println("Found not the right number of changes: " + amount);
123             log.println("It should have been 1 change.");
124             for (int i=0; i<amount; i++) {
125                 System.out.println("Detailed Change " + i + " -> new Element: '" +
126                             changes[i].Element.toString() + "'  ReplacedElement: '" +
127                             changes[i].ReplacedElement.toString() + "'");
128             }
129             tRes.tested("getPendingChanges()", false);
130         }
131         else {
132             boolean result = changes[0].ReplacedElement.equals(originalElement);
133             result &= changes[0].Element.equals(changeElement);
134             tRes.tested("getPendingChanges()", result);
135         }
136     }
137 
_hasPendingChanges()138     public void _hasPendingChanges() {
139         try {
140             executeChange(changeElement);
141         }
142         catch(StatusException e) {
143             tRes.tested("hasPendingChanges()", Status.exception(e));
144             return;
145         }
146         boolean hasPendingChanges = oObj.hasPendingChanges();
147         tRes.tested("hasPendingChanges()", hasPendingChanges);
148     }
149 
executeChange(Object element)150     private void executeChange(Object element) throws StatusException {
151         if (xProp != null) {
152             try {
153                 xProp.setPropertyValue(elementName, element);
154             }
155             catch(com.sun.star.uno.Exception e) {
156                 throw new StatusException("Could not set property '" + elementName + "'.", e);
157             }
158         }
159         else if (xNameReplace != null) {
160             try {
161                 xNameReplace.replaceByName(elementName, element);
162             }
163             catch(com.sun.star.uno.Exception e) {
164                 throw new StatusException("Could not replace '" + elementName + "' by name.", e);
165             }
166         }
167     }
168 }
169