2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package ifc
.connection
;
21 import java
.io
.PrintWriter
;
23 import lib
.MultiMethodTest
;
24 import lib
.StatusException
;
26 import com
.sun
.star
.connection
.XAcceptor
;
27 import com
.sun
.star
.connection
.XConnection
;
28 import com
.sun
.star
.connection
.XConnector
;
29 import com
.sun
.star
.uno
.UnoRuntime
;
30 import com
.sun
.star
.uno
.XInterface
;
33 * Tests methods of <code>XAcceptor</code> interface. <p>
34 * Required relations :
36 * <li> <code>'XAcceptor.connectStr'</code> : String variable. Has
37 * the following format :
38 * <code>'socket,host=<SOHost>,port=<UniquePort>' where <SOHost> is
39 * the host where StarOffice is started. This string must be passed
40 * as parameter to <code>accept()</code> method. </li>
42 * This test <b>can not</b> be run in multiply threads.
44 public class _XAcceptor
extends MultiMethodTest
{
46 protected PrintWriter log_
;
49 * Calls <code>accept()</code> method in a separate thread.
50 * Then stores exception thrown by call if it occurred, or
53 protected class AcceptorThread
extends Thread
{
55 * If exception occurred during method call it is
56 * stored in this field.
58 public Exception ex
= null ;
59 private final XAcceptor acc
;
61 * If method call returns some value it stores in this field.
63 public XConnection acceptedCall
= null ;
66 * Creates object which can call <code>accept</code> method
67 * of the Acceptor object specified.
69 public AcceptorThread(XAcceptor acc
) {
74 * Call <code>accept()</code> method.
79 acceptedCall
= acc
.accept(connectString
) ;
80 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
82 } catch (com
.sun
.star
.connection
.ConnectionSetupException e
) {
84 } catch (com
.sun
.star
.connection
.AlreadyAcceptingException e
) {
90 public XAcceptor oObj
= null;
91 protected String connectString
= null ;
94 * Retrieves object relation.
97 public void before() throws StatusException
{
98 connectString
= (String
)
99 tEnv
.getObjRelation("XAcceptor.connectStr") ;
103 if (connectString
== null)
104 throw new StatusException("No object relation found",
105 new NullPointerException()) ;
109 * First part : Thread with acceptor created, and it starts listening.
110 * The main thread tries to connect to acceptor. Acception thread must
111 * return and valid connection must be returned by Acceptor. <p>
113 * Second part : Trying to create second acceptor which listen on
114 * the same port. Calling <code>accept()</code> method of the second
115 * Acceptor must rise appropriate exception. <p>
117 * Has OK status if both test parts executed properly.
119 public void _accept() {
120 boolean result
= true ;
121 AcceptorThread acception
= null,
122 dupAcception
= null ;
123 XAcceptor dupAcceptor
= null ;
124 XConnector xConnector
= null ;
126 // creating services requierd
128 Object oConnector
= (tParam
.getMSF()).
129 createInstance("com.sun.star.connection.Connector") ;
131 xConnector
= UnoRuntime
.queryInterface
132 (XConnector
.class, oConnector
) ;
134 XInterface acceptor
= (XInterface
) tParam
.getMSF().createInstance
135 ("com.sun.star.connection.Acceptor") ;
137 dupAcceptor
= UnoRuntime
.queryInterface
138 (XAcceptor
.class, acceptor
) ;
139 } catch (com
.sun
.star
.uno
.Exception e
) {
140 e
.printStackTrace(log
) ;
141 throw new StatusException("Can't create service", e
) ;
144 // Testing connection to the acceptor
146 acception
= new AcceptorThread(oObj
) ;
149 util
.utils
.shortWait();
151 XConnection con
= xConnector
.connect(connectString
) ;
154 log
.println("Connector returned : null") ;
156 log
.println("Connector returned : " + con
.getDescription()) ;
159 acception
.join(5 * 1000) ;
160 } catch(InterruptedException e
) {}
162 if (acception
.isAlive()) {
165 log
.println("Method call haven't returned") ;
167 if (acception
.acceptedCall
== null)
168 log
.println("Acceptor returned : null") ;
170 log
.println("Acceptor returned : " +
171 acception
.acceptedCall
.getDescription()) ;
173 if (acception
.ex
!= null) {
174 log
.println("Exception occurred in accept() thread :") ;
175 acception
.ex
.printStackTrace(log
) ;
178 if (acception
.acceptedCall
== null)
179 log
.println("Method returned : null") ;
181 log
.println("Method returned : " +
182 acception
.acceptedCall
.getDescription()) ;
184 result
&= acception
.acceptedCall
!= null ;
186 } catch (com
.sun
.star
.connection
.ConnectionSetupException e
) {
187 e
.printStackTrace(log
) ;
189 } catch (com
.sun
.star
.connection
.NoConnectException e
) {
190 e
.printStackTrace(log
) ;
193 oObj
.stopAccepting();
194 if (acception
.isAlive()) {
195 acception
.interrupt();
199 // duplicate acceptor test
200 // creating the additional acceptor which listens
203 log
.println("___ Testing for accepting on the same port ...") ;
206 dupAcception
= new AcceptorThread(dupAcceptor
) ;
207 dupAcception
.start() ;
210 dupAcception
.join(1 * 1000) ;
211 } catch(InterruptedException e
) {}
214 if (dupAcception
.isAlive()) {
215 log
.println("Duplicate acceptor is listening ...") ;
217 // now trying to accept on the same port as additional
219 acception
= new AcceptorThread(oObj
) ;
223 acception
.join(3 * 1000) ;
224 } catch(InterruptedException e
) {}
226 if (acception
.isAlive()) {
227 oObj
.stopAccepting() ;
228 acception
.interrupt() ;
230 log
.println("Acceptor with the same port must cause"+
231 " an error but didn't") ;
234 log
.println("Accepted call = " + acception
.acceptedCall
) ;
235 if (acception
.ex
== null) {
237 log
.println("No exception was thrown when trying"+
238 " to listen on the same port") ;
240 if (acception
.ex
instanceof
241 com
.sun
.star
.connection
.AlreadyAcceptingException
||
242 acception
.ex
instanceof
243 com
.sun
.star
.connection
.ConnectionSetupException
) {
245 log
.println("Right exception was thrown when trying"+
246 " to listen on the same port") ;
249 log
.println("Wrong exception was thrown when trying"+
250 " to listen on the same port :") ;
251 acception
.ex
.printStackTrace(log
) ;
257 dupAcceptor
.stopAccepting() ;
258 if (dupAcception
.isAlive()) {
259 dupAcception
.interrupt() ;
263 tRes
.tested("accept()", result
) ;
267 * Starts thread with Acceptor and then calls <code>stopListening</code>
269 * Has OK status if <code>accept</code> method successfully returns and
270 * rises no exceptions.
272 public void _stopAccepting() {
273 boolean result
= true ;
276 AcceptorThread acception
= new AcceptorThread(oObj
) ;
280 oObj
.stopAccepting() ;
283 acception
.join(3 * 1000) ;
284 } catch (InterruptedException e
) {}
286 if (acception
.isAlive()) {
287 acception
.interrupt() ;
290 log
.println("Method call haven't returned") ;
293 if (acception
.ex
!= null) {
294 log
.println("Exception occurred in accept() thread :") ;
295 acception
.ex
.printStackTrace(log
) ;
301 if (acception
.acceptedCall
== null)
302 log
.println("accept() returned : null") ;
304 log
.println("accept() returned : " +
305 acception
.acceptedCall
.getDescription()) ;
308 tRes
.tested("stopAccepting()", result
) ;