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 
28 package ifc.util;
29 
30 import com.sun.star.beans.XPropertySet;
31 import com.sun.star.container.XNameReplace;
32 import com.sun.star.util.XChangesBatch;
33 import com.sun.star.util.XChangesListener;
34 import com.sun.star.util.XChangesNotifier;
35 import java.io.PrintWriter;
36 import lib.StatusException;
37 import lib.MultiMethodTest;
38 
39 /**
40  * Test the XChangesNotifier interface. To produce some changes,
41  * XChangesBatch is used.
42  * @see com.sun.star.util.XChangesNotifier
43  * @see com.sun.star.util.XChangesBatch
44  */
45 public class _XChangesNotifier extends MultiMethodTest {
46 
47     public XChangesNotifier oObj = null;
48     private XChangesBatch xBatch = null;
49     private Object changeElement = null;
50     private Object originalElement = null;
51     private String elementName = null;
52     private XPropertySet xProp = null;
53     private XNameReplace xNameReplace = null;
54     private _XChangesNotifier.MyChangesListener xListener = null;
55 
56     /**
57      * Own implementation of the XChangesListener interface
58      * @see com.sun.star.util.XChangesListener
59      */
60     private static class MyChangesListener implements XChangesListener {
61         /** Just lo a call of the listener **/
62         boolean bChangesOccured = false;
63 
64         /** A change did occur
65          * @param changesEvent The event.
66          **/
67         public void changesOccurred(com.sun.star.util.ChangesEvent changesEvent) {
68             bChangesOccured = true;
69         }
70 
71         /** Disposing of the listener
72          * @param eventObject The event.
73          **/
74         public void disposing(com.sun.star.lang.EventObject eventObject) {
75             bChangesOccured = true;
76         }
77 
78         /**
79          * Reset the listener
80          */
81         public void reset() {
82             bChangesOccured = false;
83         }
84 
85         /**
86          * Has the listener been called?
87          * @return True, if the listener has been called.
88          */
89         public boolean didChangesOccur() {
90             return bChangesOccured;
91         }
92     }
93 
94     /**
95      * Before the test: get the 'XChangesNotifier.ChangesBatch' object relation
96      * and create the listener.
97      */
98     protected void before() {
99         xBatch = (XChangesBatch)tEnv.getObjRelation("XChangesNotifier.ChangesBatch");
100         changeElement = tEnv.getObjRelation("XChangesNotifier.ChangeElement");
101         originalElement = tEnv.getObjRelation("XChangesNotifier.OriginalElement");
102         elementName = (String)tEnv.getObjRelation("XChangesNotifier.PropertyName");
103 
104         xProp = (XPropertySet)tEnv.getObjRelation("XChangesNotifier.PropertySet");
105         try {
106             if (originalElement == null && xProp != null)
107                 originalElement = xProp.getPropertyValue(elementName);
108         }
109         catch(com.sun.star.uno.Exception e) {
110             throw new StatusException("Could not get property '" + elementName + "'.", e);
111         }
112 
113         // or get an XNameReplace
114         xNameReplace = (XNameReplace)tEnv.getObjRelation("XChangesNotifier.NameReplace");
115         try {
116             if (originalElement == null && xNameReplace != null)
117                 originalElement = xNameReplace.getByName(elementName);
118         }
119         catch(com.sun.star.uno.Exception e) {
120             throw new StatusException("Could not get element by name '" + elementName + "'.", e);
121         }
122 
123         if (changeElement == null || originalElement == null || elementName == null || (xProp == null && xNameReplace == null) || xBatch == null) {
124             log.println(
125                 changeElement == null?"Missing property 'XChangesNotifier.ChangeElement'\n":"" +
126                 originalElement == null?"Missing property 'XChangesNotifier.OriginalElement'\n":"" +
127                 elementName == null?"Missing property 'XChangesNotifier.PropertyName'\n":"" +
128                 xProp == null?"Missing property 'XChangesNotifier.PropertySet'":"" +
129                 xNameReplace == null?"Missing property 'XChangesNotifier.NameReplace'":"" +
130                 xBatch == null?"Missing property 'XChangesNotifier.ChangesBatch'":""
131             );
132             throw new StatusException("Some needed object relations are missing.", new Exception());
133         }
134 
135         xListener = new _XChangesNotifier.MyChangesListener();
136     }
137 
138     /** test addChangesListener **/
139     public void _addChangesListener() {
140         oObj.addChangesListener(xListener);
141         tRes.tested("addChangesListener()", true);
142     }
143 
144     /** test removeChangesListener **/
145     public void _removeChangesListener() {
146         requiredMethod("addChangesListener()");
147         boolean result = true;
148         result &= commitChanges();
149         result &= xListener.didChangesOccur();
150         if (!result)
151             log.println("Listener has not been called.");
152         oObj.removeChangesListener(xListener);
153         xListener.reset();
154         result &= redoChanges();
155         boolean result2 = xListener.didChangesOccur();
156         if (result2)
157             log.println("Removed listener has been called.");
158 
159         tRes.tested("removeChangesListener()", result && !result2);
160     }
161 
162     /**
163      * Commit a change, using an implementation of the XChangesBatch interface.
164      * @return true, if changing worked.
165      */
166     private boolean commitChanges() {
167         if (!executeChange(changeElement)) return false;
168         if (!xBatch.hasPendingChanges()) return false;
169         try {
170             xBatch.commitChanges();
171         }
172         catch(com.sun.star.lang.WrappedTargetException e) {
173             e.printStackTrace((PrintWriter)log);
174             return false;
175         }
176         return true;
177     }
178 
179     /**
180      * Redo the change, using an implementation of the XChangesBatch interface.
181      * @return true, if changing worked.
182      */
183     private boolean redoChanges() {
184         if (!executeChange(originalElement)) return false;
185         if (!xBatch.hasPendingChanges()) return false;
186         try {
187             xBatch.commitChanges();
188         }
189         catch(com.sun.star.lang.WrappedTargetException e) {
190             e.printStackTrace((PrintWriter)log);
191             return false;
192         }
193         return true;
194     }
195 
196     /**
197      * Execute the change, use XPropertySet or XNameReplace
198      * @return False, if changing did throw an exception.
199      */
200     private boolean executeChange(Object element) throws StatusException {
201         if (xProp != null) {
202             try {
203                 xProp.setPropertyValue(elementName, element);
204             }
205             catch(com.sun.star.uno.Exception e) {
206                 e.printStackTrace((PrintWriter)log);
207                 return false;
208             }
209         }
210         else if (xNameReplace != null) {
211             try {
212                 xNameReplace.replaceByName(elementName, element);
213             }
214             catch(com.sun.star.uno.Exception e) {
215                 e.printStackTrace((PrintWriter)log);
216                 return false;
217             }
218         }
219         return true;
220     }
221 
222 }
223