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.io;
29 
30 import lib.MultiMethodTest;
31 import lib.Status;
32 import lib.StatusException;
33 
34 import com.sun.star.io.XActiveDataControl;
35 import com.sun.star.io.XStreamListener;
36 import com.sun.star.lang.EventObject;
37 
38 /**
39  * Testing <code>com.sun.star.io.XActiveDataControl</code>
40  * interface methods :
41  * <ul>
42  *  <li><code> addListener()</code></li>
43  *  <li><code> removeListener()</code></li>
44  *  <li><code> start()</code></li>
45  *  <li><code> terminate()</code></li>
46  * </ul> <p>
47  *
48  * Tests <code>XActiveDataControl</code> interface. First, it registers a listener
49  * and performs <code>start()</code> and <code>terminate()</code> calls. The
50  * events received in the listener are analyzed to verify the result.<p>
51  *
52  * @see com.sun.star.io.XActiveDataControl
53  */
54 public class _XActiveDataControl extends MultiMethodTest {
55 
56     /**
57      * Contains the object under test.
58      */
59     public XActiveDataControl oObj = null;
60 
61     /**
62      * Indicates that the <code>XStreamListener.started()</code> method has
63      * been called.
64      */
65     private boolean startCalled = false;
66 
67     /**
68      * Indicates that the <code>XStreamListener.terminated()</code> method has
69      * been called.
70      */
71     private boolean terminateCalled = false;
72 
73     /**
74      * Indicates that the <code>XEventListener.closed()</code> method has
75      * been called.
76      */
77     private boolean closeCalled = false;
78 
79     /**
80      * Indicates that the <code>XStreamListener.error()</code> method has
81      * been called.
82      */
83     private boolean errorCalled = false;
84 
85     /**
86      * Contains the error, if <code>XStreamListener.error(Object error)</code>
87      * method was called.
88      */
89     private Object error;
90 
91     /**
92      * Indicates that the <code>XEventListener.disposing()</code> method has
93      * been called.
94      */
95     private boolean smthngElseCalled = false;
96 
97     /**
98      * The listener is used to verify results of the methods.
99      */
100     private TestStreamListener listener = new TestStreamListener();
101 
102     /**
103      * XStreamListener implementation. Sets variables
104      * (<cod>estartedCalled</code>, <code>terminatedCalled</code>, etc.) to
105      * <tt>true</tt> if the appropriate method was called (for example, if
106      * <code>started()</code> was called, the <code>startedCalled</code>
107      * field is set).
108      */
109     private class TestStreamListener implements XStreamListener {
110         public void started() {
111             startCalled = true ;
112         }
113         public void terminated() {
114             terminateCalled = true ;
115         }
116         public void error(Object e) {
117             error = e;
118             errorCalled = true ;
119         }
120         public void closed() {
121             closeCalled = true ;
122         }
123         public void disposing(EventObject e) {
124             smthngElseCalled = true ;
125         }
126 
127     }
128 
129     /**
130      * Tests <code>addListener()</code>. The verification is performed later, in
131      * <code>_terminate()</code> method.
132      */
133     public void _addListener() {
134         oObj.addListener(listener);
135     }
136 
137     /**
138      * Starts the data activity (e.g. data pump). Verifictation is performed
139      * later, in <code>_terminate()</code> method.
140      */
141     public void _start() {
142         executeMethod("addListener()");
143 
144         oObj.start();
145 
146         // waiting a little bit for data transfered
147         try {
148             Thread.sleep(200);
149         } catch (InterruptedException e) {
150             e.printStackTrace(log) ;
151             throw new StatusException(Status.failed(e.getMessage()));
152         }
153     }
154 
155     /**
156      * Tests <code>removeListener()</code>. Before, it ensures that other
157      * tests are perforemed and that <code>addListener()</code> is okay. Then,
158      * calls <code>XActiveDataControl.start()</code> and checks that no method
159      * of the listener was called.
160      */
161     public void _removeListener() {
162         // performing other tests before, so, that don't break them
163         try {
164             executeMethod("terminate()");
165         } catch (StatusException e) {
166             // the result doesn't matter
167         }
168 
169         // check that addListener() is okay
170         requiredMethod("addListener()");
171 
172         // clearing previous records
173         startCalled = false;
174         terminateCalled = false;
175         errorCalled = false;
176         error = null;
177         smthngElseCalled = false;
178 
179         // removing the listener
180         oObj.removeListener(listener);
181 
182         // starting the activity
183         oObj.start();
184 
185         // wait a little bit to allow for listeners to be called
186         try {
187             Thread.sleep(200);
188         } catch (InterruptedException e) {
189             e.printStackTrace(log) ;
190             throw new StatusException(Status.failed(e.getMessage()));
191         }
192 
193         // check that no removed listener's method was called
194         tRes.tested("removeListener()",!startCalled &&
195             !terminateCalled && !errorCalled && !smthngElseCalled) ;
196     }
197 
198     /**
199      * Tests <code>terminate()</code>. First, ensures that <code>start()</code>
200      * has been called. Then, verifies <code>start()</code>,
201      * <code>addListener()</code> and <code>terminate()</code> results, by
202      * checking that the appropriate listener's methods have been called.
203      */
204     public void _terminate() {
205         // ensuring that the activity has been started
206         executeMethod("start()");
207 
208         // terminating the activity
209         oObj.terminate();
210 
211         // waiting a little bit for listeners to be called
212         try {
213             Thread.sleep(200);
214         } catch (InterruptedException e) {
215             e.printStackTrace(log) ;
216             throw new StatusException(Status.failed(e.getMessage()));
217         }
218 
219         // check, if any error occured
220         if (errorCalled) {
221             Status.failed("Unexpected error");
222             log.println("Unexpected error " + error);
223             ((Exception)error).printStackTrace(log);
224         }
225 
226         // verification of start() method - startedCalled method should be
227         // called
228         if (!tRes.tested("start()", startCalled)) {
229             log.println("XStreamListener.started() was not called()");
230         }
231 
232         // check that any listener method is called
233         tRes.tested("addListener()", startCalled ||
234                 terminateCalled || errorCalled || smthngElseCalled);
235 
236         // checking that terminated() has been called or streams were closed
237         // before terminate() call, in this case termination has no sense.
238         tRes.tested("terminate()", terminateCalled || closeCalled);
239     }
240 
241     /**
242      * Disposes the test environment, since it is used.
243      */
244     public void after() {
245         this.disposeEnvironment();
246     }
247 }
248 
249 
250