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 lib
.MultiMethodTest
;
22 import lib
.StatusException
;
24 import com
.sun
.star
.connection
.XAcceptor
;
25 import com
.sun
.star
.connection
.XConnection
;
26 import com
.sun
.star
.connection
.XConnector
;
27 import com
.sun
.star
.uno
.UnoRuntime
;
28 import com
.sun
.star
.uno
.XInterface
;
31 * Tests methods of <code>XAcceptor</code> interface. <p>
32 * Required relations :
34 * <li> <code>'XAcceptor.connectStr'</code> : String variable. Has
35 * the following format :
36 * <code>'socket,host=<SOHost>,port=<UniquePort>' where <SOHost> is
37 * the host where StarOffice is started. This string must be passed
38 * as parameter to <code>accept()</code> method. </li>
40 * This test <b>can not</b> be run in multiply threads.
42 public class _XAcceptor
extends MultiMethodTest
{
45 * Calls <code>accept()</code> method in a separate thread.
46 * Then stores exception thrown by call if it occurred, or
49 protected class AcceptorThread
extends Thread
{
51 * If exception occurred during method call it is
52 * stored in this field.
54 public Exception ex
= null ;
55 private final XAcceptor acc
;
57 * If method call returns some value it stores in this field.
59 public XConnection acceptedCall
= null ;
62 * Creates object which can call <code>accept</code> method
63 * of the Acceptor object specified.
65 public AcceptorThread(XAcceptor acc
) {
70 * Call <code>accept()</code> method.
75 acceptedCall
= acc
.accept(connectString
) ;
76 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
78 } catch (com
.sun
.star
.connection
.ConnectionSetupException e
) {
80 } catch (com
.sun
.star
.connection
.AlreadyAcceptingException e
) {
86 public XAcceptor oObj
= null;
87 protected String connectString
= null ;
90 * Retrieves object relation.
93 public void before() throws StatusException
{
94 connectString
= (String
)
95 tEnv
.getObjRelation("XAcceptor.connectStr") ;
97 if (connectString
== null)
98 throw new StatusException("No object relation found",
99 new NullPointerException()) ;
103 * First part : Thread with acceptor created, and it starts listening.
104 * The main thread tries to connect to acceptor. Acception thread must
105 * return and valid connection must be returned by Acceptor. <p>
107 * Second part : Trying to create second acceptor which listen on
108 * the same port. Calling <code>accept()</code> method of the second
109 * Acceptor must rise appropriate exception. <p>
111 * Has OK status if both test parts executed properly.
113 public void _accept() {
114 boolean result
= true ;
115 AcceptorThread acception
= null,
116 dupAcception
= null ;
117 XAcceptor dupAcceptor
= null ;
118 XConnector xConnector
= null ;
120 // creating services required
122 Object oConnector
= tParam
.getMSF().createInstance("com.sun.star.connection.Connector") ;
124 xConnector
= UnoRuntime
.queryInterface
125 (XConnector
.class, oConnector
) ;
127 XInterface acceptor
= (XInterface
) tParam
.getMSF().createInstance
128 ("com.sun.star.connection.Acceptor") ;
130 dupAcceptor
= UnoRuntime
.queryInterface
131 (XAcceptor
.class, acceptor
) ;
132 } catch (com
.sun
.star
.uno
.Exception e
) {
133 e
.printStackTrace(log
) ;
134 throw new StatusException("Can't create service", e
) ;
137 // Testing connection to the acceptor
139 acception
= new AcceptorThread(oObj
) ;
142 util
.utils
.shortWait();
144 XConnection con
= xConnector
.connect(connectString
) ;
147 log
.println("Connector returned : null") ;
149 log
.println("Connector returned : " + con
.getDescription()) ;
152 acception
.join(5 * 1000) ;
153 } catch(InterruptedException e
) {}
155 if (acception
.isAlive()) {
158 log
.println("Method call haven't returned") ;
160 if (acception
.acceptedCall
== null)
161 log
.println("Acceptor returned : null") ;
163 log
.println("Acceptor returned : " +
164 acception
.acceptedCall
.getDescription()) ;
166 if (acception
.ex
!= null) {
167 log
.println("Exception occurred in accept() thread :") ;
168 acception
.ex
.printStackTrace(log
) ;
171 if (acception
.acceptedCall
== null)
172 log
.println("Method returned : null") ;
174 log
.println("Method returned : " +
175 acception
.acceptedCall
.getDescription()) ;
177 result
&= acception
.acceptedCall
!= null ;
179 } catch (com
.sun
.star
.connection
.ConnectionSetupException e
) {
180 e
.printStackTrace(log
) ;
182 } catch (com
.sun
.star
.connection
.NoConnectException e
) {
183 e
.printStackTrace(log
) ;
186 oObj
.stopAccepting();
187 if (acception
.isAlive()) {
188 acception
.interrupt();
192 // duplicate acceptor test
193 // creating the additional acceptor which listens
196 log
.println("___ Testing for accepting on the same port ...") ;
199 dupAcception
= new AcceptorThread(dupAcceptor
) ;
200 dupAcception
.start() ;
203 dupAcception
.join(1 * 1000) ;
204 } catch(InterruptedException e
) {}
207 if (dupAcception
.isAlive()) {
208 log
.println("Duplicate acceptor is listening ...") ;
210 // now trying to accept on the same port as additional
212 acception
= new AcceptorThread(oObj
) ;
216 acception
.join(3 * 1000) ;
217 } catch(InterruptedException e
) {}
219 if (acception
.isAlive()) {
220 oObj
.stopAccepting() ;
221 acception
.interrupt() ;
223 log
.println("Acceptor with the same port must cause"+
224 " an error but didn't") ;
227 log
.println("Accepted call = " + acception
.acceptedCall
) ;
228 if (acception
.ex
== null) {
230 log
.println("No exception was thrown when trying"+
231 " to listen on the same port") ;
233 if (acception
.ex
instanceof
234 com
.sun
.star
.connection
.AlreadyAcceptingException
||
235 acception
.ex
instanceof
236 com
.sun
.star
.connection
.ConnectionSetupException
) {
238 log
.println("Right exception was thrown when trying"+
239 " to listen on the same port") ;
242 log
.println("Wrong exception was thrown when trying"+
243 " to listen on the same port :") ;
244 acception
.ex
.printStackTrace(log
) ;
250 dupAcceptor
.stopAccepting() ;
251 if (dupAcception
.isAlive()) {
252 dupAcception
.interrupt() ;
256 tRes
.tested("accept()", result
) ;
260 * Starts thread with Acceptor and then calls <code>stopListening</code>
262 * Has OK status if <code>accept</code> method successfully returns and
263 * rises no exceptions.
265 public void _stopAccepting() {
266 boolean result
= true ;
269 AcceptorThread acception
= new AcceptorThread(oObj
) ;
273 oObj
.stopAccepting() ;
276 acception
.join(3 * 1000) ;
277 } catch (InterruptedException e
) {}
279 if (acception
.isAlive()) {
280 acception
.interrupt() ;
283 log
.println("Method call haven't returned") ;
286 if (acception
.ex
!= null) {
287 log
.println("Exception occurred in accept() thread :") ;
288 acception
.ex
.printStackTrace(log
) ;
294 if (acception
.acceptedCall
== null)
295 log
.println("accept() returned : null") ;
297 log
.println("accept() returned : " +
298 acception
.acceptedCall
.getDescription()) ;
301 tRes
.tested("stopAccepting()", result
) ;