Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / qadevOOo / tests / java / ifc / util / _XChangesNotifier.java
blob1aed93a6b2c8f0231f0ec1d8245303b6ba7f01db
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 ************************************************************************/
28 package ifc.util;
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;
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
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;
56 /**
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.
66 **/
67 public void changesOccurred(com.sun.star.util.ChangesEvent changesEvent) {
68 bChangesOccurred = true;
71 /** Disposing of the listener
72 * @param eventObject The event.
73 **/
74 public void disposing(com.sun.star.lang.EventObject eventObject) {
75 bChangesOccurred = true;
78 /**
79 * Reset the listener
81 public void reset() {
82 bChangesOccurred = false;
85 /**
86 * Has the listener been called?
87 * @return True, if the listener has been called.
89 public boolean didChangesOccur() {
90 return bChangesOccurred;
94 /**
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");
105 try {
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");
115 try {
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) {
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'":""
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();
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.");
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;
169 try {
170 xBatch.commitChanges();
172 catch(com.sun.star.lang.WrappedTargetException e) {
173 e.printStackTrace((PrintWriter)log);
174 return false;
176 return true;
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;
186 try {
187 xBatch.commitChanges();
189 catch(com.sun.star.lang.WrappedTargetException e) {
190 e.printStackTrace((PrintWriter)log);
191 return false;
193 return true;
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 {
201 if (xProp != null) {
202 try {
203 xProp.setPropertyValue(elementName, element);
205 catch(com.sun.star.uno.Exception e) {
206 e.printStackTrace((PrintWriter)log);
207 return false;
210 else if (xNameReplace != null) {
211 try {
212 xNameReplace.replaceByName(elementName, element);
214 catch(com.sun.star.uno.Exception e) {
215 e.printStackTrace((PrintWriter)log);
216 return false;
219 return true;