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