1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XUpdateBroadcaster.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import lib
.MultiMethodTest
;
34 import lib
.StatusException
;
36 import com
.sun
.star
.form
.XUpdateBroadcaster
;
37 import com
.sun
.star
.form
.XUpdateListener
;
38 import com
.sun
.star
.lang
.EventObject
;
41 * Testing <code>com.sun.star.form.XUpdateBroadcaster</code>
44 * <li><code> addUpdateListener()</code></li>
45 * <li><code> removeUpdateListener()</code></li>
47 * This test needs the following object relations :
49 * <li> <code>'XUpdateBroadcaster.Checker'</code> : <code>
50 * _XUpdateBroadcaster.UpdateChecker</code> interface implementation
51 * which can update, commit data and check if the data was successfully
54 * Test is <b> NOT </b> multithread compilant. <p>
55 * @see com.sun.star.form.XUpdateBroadcaster
57 public class _XUpdateBroadcaster
extends MultiMethodTest
{
59 public XUpdateBroadcaster oObj
= null;
60 UpdateChecker checker
= null ;
63 * Interface for relation. Updating, commiting and checking
64 * if data was commited is object dependent behaviour.
66 public static interface UpdateChecker
{
68 * Method must make some data update in the object tested.
70 public void update() throws com
.sun
.star
.uno
.Exception
;
72 * Method must commit data change made by method <code>update</code>.
74 public void commit() throws com
.sun
.star
.uno
.Exception
;
76 * Checks if the data commited by <code>commit</code> method
77 * became permanent in data source.
78 * @return <code>true</code> if data was commited.
80 public boolean wasCommited() throws com
.sun
.star
.uno
.Exception
;
84 * Retrieves object relations.
85 * @throws StatusException If one of relations not found.
87 public void before() {
88 checker
= (UpdateChecker
)
89 tEnv
.getObjRelation("XUpdateBroadcaster.Checker") ;
90 if (checker
== null) {
91 log
.println("Relation not found") ;
92 throw new StatusException("Relation not found",
93 new NullPointerException("Relation not found")) ;
98 * Listener implementation, which can accept or reject update
99 * requests and store event calls.
101 protected class TestListener
implements XUpdateListener
{
103 * Indicates must listener approve update requests or not.
105 public boolean approve
= false ;
107 * Indicates that <code>approveUpdate</code> method was called.
109 public boolean approveCalled
= false ;
111 * Indicates that <code>updated</code> method was called.
113 public boolean updateCalled
= false ;
119 approveCalled
= false ;
120 updateCalled
= false ;
122 public void disposing(EventObject ev
) {}
123 public boolean approveUpdate(EventObject ev
) {
124 approveCalled
= true ;
127 public void updated(EventObject ev
) {
128 updateCalled
= true ;
132 private TestListener listener
= new TestListener();
135 * The listener methods calls are checked twice with approving
136 * and rejecting updates. <p>
137 * Has <b>OK</b> status if on update rejected only <code>
138 * approveUpdate</code> listener method was called, and if
139 * on update approved <code>approveUpdate</code> and
140 * <code>updated</code> methods called, and data was commited
143 public void _addUpdateListener() {
144 boolean bResult
= true;
146 oObj
.addUpdateListener(listener
) ;
153 boolean commited
= checker
.wasCommited() ;
157 bResult
= listener
.approveCalled
&&
158 ! listener
.updateCalled
&&
161 log
.println("Calling with no approving : approveUpdate() was " +
162 (listener
.approveCalled ?
"":"NOT")+" called, updated() was "+
163 (listener
.updateCalled ?
"":"NOT")+" called, the value was " +
164 (commited ?
"" : "NOT") + " commited.") ;
169 listener
.approve
= true ;
175 commited
= checker
.wasCommited() ;
179 log
.println("Calling with approving : approveUpdate() was " +
180 (listener
.approveCalled ?
"":"NOT")+" called, updated() was "+
181 (listener
.updateCalled ?
"":"NOT")+" called, the value was "+
182 (commited ?
"" : "NOT") + " commited.") ;
184 bResult
= listener
.approveCalled
&&
185 listener
.updateCalled
&&
187 } catch (com
.sun
.star
.uno
.Exception e
) {
189 e
.printStackTrace(log
) ;
192 tRes
.tested("addUpdateListener()", bResult
);
196 * Removes listener, updates data, and checks if no listener
197 * methods were called. <p>
198 * Has <b> OK </b> status if after listener removing no of its methods
200 * The following method tests are to be completed successfully before :
202 * <li> <code> addUpdateListener </code> : to have a listener added.</li>
205 public void _removeUpdateListener() {
206 requiredMethod("addUpdateListener()");
207 boolean bResult
= true;
210 listener
.approve
= true ;
212 oObj
.removeUpdateListener(listener
);
221 bResult
= ! listener
.approveCalled
&&
222 ! listener
.updateCalled
;
224 catch (com
.sun
.star
.uno
.Exception e
) {
225 log
.println("Exception occured during removeUpdateListener()");
226 e
.printStackTrace(log
);
230 tRes
.tested("removeUpdateListener()", bResult
);
233 private void shortWait() {
237 catch (InterruptedException ex
) {
243 * Forces environment recreation.
245 protected void after() {
246 disposeEnvironment();