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