1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
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
;
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
45 public class _XChangesNotifier
extends MultiMethodTest
{
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;
57 * Own implementation of the XChangesListener interface
58 * @see com.sun.star.util.XChangesListener
60 private static class MyChangesListener
implements XChangesListener
{
61 /** Just lo a call of the listener **/
62 boolean bChangesOccurred
= false;
64 /** A change did occur
65 * @param changesEvent The event.
67 public void changesOccurred(com
.sun
.star
.util
.ChangesEvent changesEvent
) {
68 bChangesOccurred
= true;
71 /** Disposing of the listener
72 * @param eventObject The event.
74 public void disposing(com
.sun
.star
.lang
.EventObject eventObject
) {
75 bChangesOccurred
= true;
82 bChangesOccurred
= false;
86 * Has the listener been called?
87 * @return True, if the listener has been called.
89 public boolean didChangesOccur() {
90 return bChangesOccurred
;
95 * Before the test: get the 'XChangesNotifier.ChangesBatch' object relation
96 * and create the listener.
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");
104 xProp
= (XPropertySet
)tEnv
.getObjRelation("XChangesNotifier.PropertySet");
106 if (originalElement
== null && xProp
!= null)
107 originalElement
= xProp
.getPropertyValue(elementName
);
109 catch(com
.sun
.star
.uno
.Exception e
) {
110 throw new StatusException("Could not get property '" + elementName
+ "'.", e
);
113 // or get an XNameReplace
114 xNameReplace
= (XNameReplace
)tEnv
.getObjRelation("XChangesNotifier.NameReplace");
116 if (originalElement
== null && xNameReplace
!= null)
117 originalElement
= xNameReplace
.getByName(elementName
);
119 catch(com
.sun
.star
.uno
.Exception e
) {
120 throw new StatusException("Could not get element by name '" + elementName
+ "'.", e
);
123 if (changeElement
== null || originalElement
== null || elementName
== null || (xProp
== null && xNameReplace
== null) || xBatch
== null) {
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'":""
132 throw new StatusException("Some needed object relations are missing.", new Exception());
135 xListener
= new _XChangesNotifier
.MyChangesListener();
138 /** test addChangesListener **/
139 public void _addChangesListener() {
140 oObj
.addChangesListener(xListener
);
141 tRes
.tested("addChangesListener()", true);
144 /** test removeChangesListener **/
145 public void _removeChangesListener() {
146 requiredMethod("addChangesListener()");
147 boolean result
= true;
148 result
&= commitChanges();
149 result
&= xListener
.didChangesOccur();
151 log
.println("Listener has not been called.");
152 oObj
.removeChangesListener(xListener
);
154 result
&= redoChanges();
155 boolean result2
= xListener
.didChangesOccur();
157 log
.println("Removed listener has been called.");
159 tRes
.tested("removeChangesListener()", result
&& !result2
);
163 * Commit a change, using an implementation of the XChangesBatch interface.
164 * @return true, if changing worked.
166 private boolean commitChanges() {
167 if (!executeChange(changeElement
)) return false;
168 if (!xBatch
.hasPendingChanges()) return false;
170 xBatch
.commitChanges();
172 catch(com
.sun
.star
.lang
.WrappedTargetException e
) {
173 e
.printStackTrace((PrintWriter
)log
);
180 * Redo the change, using an implementation of the XChangesBatch interface.
181 * @return true, if changing worked.
183 private boolean redoChanges() {
184 if (!executeChange(originalElement
)) return false;
185 if (!xBatch
.hasPendingChanges()) return false;
187 xBatch
.commitChanges();
189 catch(com
.sun
.star
.lang
.WrappedTargetException e
) {
190 e
.printStackTrace((PrintWriter
)log
);
197 * Execute the change, use XPropertySet or XNameReplace
198 * @return False, if changing did throw an exception.
200 private boolean executeChange(Object element
) throws StatusException
{
203 xProp
.setPropertyValue(elementName
, element
);
205 catch(com
.sun
.star
.uno
.Exception e
) {
206 e
.printStackTrace((PrintWriter
)log
);
210 else if (xNameReplace
!= null) {
212 xNameReplace
.replaceByName(elementName
, element
);
214 catch(com
.sun
.star
.uno
.Exception e
) {
215 e
.printStackTrace((PrintWriter
)log
);