xref: /AOO42X/main/qadevOOo/tests/java/ifc/connection/_XAcceptor.java (revision 4fd1f73c4b49c45d0e8de944a2a1800acf7734df)
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 package ifc.connection;
23 
24 import java.io.PrintWriter;
25 
26 import lib.MultiMethodTest;
27 import lib.StatusException;
28 
29 import com.sun.star.connection.XAcceptor;
30 import com.sun.star.connection.XConnection;
31 import com.sun.star.connection.XConnector;
32 import com.sun.star.lang.XMultiServiceFactory;
33 import com.sun.star.uno.UnoRuntime;
34 import com.sun.star.uno.XInterface;
35 
36 /**
37 * Tests methods of <code>XAcceptor</code> interface. <p>
38 * Required relations :
39 * <ul>
40 * <li> <code>'XAcceptor.connectStr'</code> : String variable. Has
41 *   the following format :
42 *   <code>'socket,host=<SOHost>,port=<UniquePort>' where <SOHost> is
43 *   the host where StarOffice is started. This string must be passed
44 *   as parameter to <code>accept()</code> method. </li>
45 * <ul> <p>
46 * This test <b>can not</b> be run in multiply threads.
47 */
48 public class _XAcceptor extends MultiMethodTest {
49 
50     protected PrintWriter log_ ;
51 
52     /**
53     * Calls <code>accept()</code> method in a separate thread.
54     * Then stores exception thrown by call if it occurred, or
55     * return value.
56     */
57     protected class AcceptorThread extends Thread {
58         /**
59         * If exception occurred during method call it is
60         * stored in this field.
61         */
62         public Exception ex = null ;
63         private XAcceptor acc = null ;
64         /**
65         * If method call returns some value it stores in this field.
66         */
67         public XConnection acceptedCall = null ;
68 
69         /**
70         * Creates object which can call <code>accept</code> method
71         * of the Acceptor object specified.
72         */
73         public AcceptorThread(XAcceptor acc) {
74             this.acc = acc ;
75         }
76 
77         /**
78         * Call <code>accept()</code> method.
79         */
80         public void run() {
81             try {
82                 acceptedCall = acc.accept(connectString) ;
83             } catch (com.sun.star.lang.IllegalArgumentException e) {
84                 ex = e ;
85             } catch (com.sun.star.connection.ConnectionSetupException e) {
86                 ex = e ;
87             } catch (com.sun.star.connection.AlreadyAcceptingException e) {
88                 ex = e ;
89             }
90         }
91     }
92 
93     public XAcceptor oObj = null;
94     protected String connectString = null ;
95 
96     /**
97     * Retrieves object relation.
98     */
99     public void before() throws StatusException {
100         connectString = (String)
101             tEnv.getObjRelation("XAcceptor.connectStr") ;
102 
103         log_ = log ;
104 
105         if (connectString == null)
106             throw new StatusException("No object relation found",
107                 new NullPointerException()) ;
108     }
109 
110     /**
111     * First part : Thread with acceptor created, and it starts listening.
112     * The main thread tries to connect to acceptor. Acception thread must
113     * return and valid connection must be returned by Acceptor. <p>
114     *
115     * Second part : Trying to create second acceptor which listen on
116     * the same port. Calling <code>accept()</code> method of the second
117     * Acceptor must rise appropriate exception. <p>
118     *
119     * Has OK status if both test parts executed properly.
120     */
121     public void _accept() {
122         boolean result = true ;
123         AcceptorThread acception = null,
124                        dupAcception = null ;
125         XAcceptor dupAcceptor = null ;
126         XConnector xConnector = null ;
127 
128         // creating services required
129         try {
130             Object oConnector = ((XMultiServiceFactory)tParam.getMSF()).
131                 createInstance("com.sun.star.connection.Connector") ;
132 
133             xConnector = (XConnector) UnoRuntime.queryInterface
134                 (XConnector.class, oConnector) ;
135 
136             XInterface acceptor = (XInterface) ((XMultiServiceFactory)
137                 tParam.getMSF()).createInstance
138                 ("com.sun.star.connection.Acceptor") ;
139 
140             dupAcceptor = (XAcceptor) UnoRuntime.queryInterface
141                 (XAcceptor.class, acceptor) ;
142         } catch (com.sun.star.uno.Exception e) {
143             e.printStackTrace(log) ;
144             throw new StatusException("Can't create service", e) ;
145         }
146 
147         // Testing connection to the acceptor
148         try {
149             acception = new AcceptorThread(oObj) ;
150             acception.start() ;
151 
152             try {
153                 Thread.sleep(500);
154             }
155             catch (java.lang.InterruptedException e) {}
156 
157             XConnection con = xConnector.connect(connectString) ;
158 
159             if (con == null)
160                 log.println("Connector returned : null") ;
161             else
162                 log.println("Connector returned : " + con.getDescription()) ;
163 
164             try {
165                 acception.join(5 * 1000) ;
166             } catch(InterruptedException e) {}
167 
168             if (acception.isAlive()) {
169 
170                 result = false ;
171                 log.println("Method call haven't returned") ;
172 
173                 if (acception.acceptedCall == null)
174                     log.println("Acceptor returned : null") ;
175                 else
176                     log.println("Acceptor returned : " +
177                         acception.acceptedCall.getDescription()) ;
178             } else {
179                 if (acception.ex != null) {
180                     log.println("Exception occurred in accept() thread :") ;
181                     acception.ex.printStackTrace(log) ;
182                 }
183 
184                 if (acception.acceptedCall == null)
185                     log.println("Method returned : null") ;
186                 else
187                     log.println("Method returned : " +
188                         acception.acceptedCall.getDescription()) ;
189 
190                 result &= acception.acceptedCall != null ;
191             }
192         } catch (com.sun.star.connection.ConnectionSetupException e) {
193             e.printStackTrace(log) ;
194             result =  false ;
195         } catch (com.sun.star.connection.NoConnectException e) {
196             e.printStackTrace(log) ;
197             result =  false ;
198         } finally {
199             oObj.stopAccepting();
200             if (acception.isAlive()) {
201                 acception.interrupt();
202             }
203         }
204 
205         // duplicate acceptor test
206         // creating the additional acceptor which listens
207         // on the same port
208 
209         log.println("___ Testing for accepting on the same port ...") ;
210 
211         try {
212             dupAcception = new AcceptorThread(dupAcceptor) ;
213             dupAcception.start() ;
214 
215             try {
216                 dupAcception.join(1 * 1000) ;
217             } catch(InterruptedException e) {}
218 
219 
220             if (dupAcception.isAlive()) {
221                 log.println("Duplicate acceptor is listening ...") ;
222 
223                 // now trying to accept on the same port as additional
224                 // acceptor
225                 acception = new AcceptorThread(oObj) ;
226                 acception.start() ;
227 
228                 try {
229                     acception.join(3 * 1000) ;
230                 } catch(InterruptedException e) {}
231 
232                 if (acception.isAlive()) {
233                     oObj.stopAccepting() ;
234                     acception.interrupt() ;
235 
236                     log.println("Acceptor with the same port must cause"+
237                     " an error but didn't") ;
238                     result = false ;
239                 } else {
240                     log.println("Accepted call = " + acception.acceptedCall) ;
241                     if (acception.ex == null) {
242                         //result = false ;
243                         log.println("No exception was thrown when trying"+
244                          " to listen on the same port") ;
245                     } else {
246                         if (acception.ex instanceof
247                             com.sun.star.connection.AlreadyAcceptingException ||
248                             acception.ex instanceof
249                             com.sun.star.connection.ConnectionSetupException) {
250 
251                             log.println("Right exception was thrown when trying"+
252                             " to listen on the same port") ;
253                         } else {
254                             result = false ;
255                             log.println("Wrong exception was thrown when trying"+
256                             " to listen on the same port :") ;
257                             acception.ex.printStackTrace(log) ;
258                         }
259                     }
260                 }
261             }
262         } finally {
263             dupAcceptor.stopAccepting() ;
264             if (dupAcception.isAlive()) {
265                 dupAcception.interrupt() ;
266             }
267         }
268 
269         tRes.tested("accept()", result) ;
270     }
271 
272     /**
273     * Starts thread with Acceptor and then calls <code>stopListening</code>
274     * method. <p>
275     * Has OK status if <code>accept</code> method successfully returns and
276     * rises no exceptions.
277     */
278     public void _stopAccepting() {
279         boolean result = true ;
280 
281 
282         AcceptorThread acception = new AcceptorThread(oObj) ;
283 
284         acception.start() ;
285 
286         oObj.stopAccepting() ;
287 
288         try {
289             acception.join(3 * 1000) ;
290         } catch (InterruptedException e) {}
291 
292         if (acception.isAlive()) {
293             acception.interrupt() ;
294 
295             result = false ;
296             log.println("Method call haven't returned") ;
297 
298         } else {
299             if (acception.ex != null) {
300                 log.println("Exception occurred in accept() thread :") ;
301                 acception.ex.printStackTrace(log) ;
302                 result = false ;
303             } else {
304                 result = true ;
305             }
306 
307             if (acception.acceptedCall == null)
308                 log.println("accept() returned : null") ;
309             else
310                 log.println("accept() returned : " +
311                     acception.acceptedCall.getDescription()) ;
312         }
313 
314         tRes.tested("stopAccepting()", result) ;
315     }
316 }
317