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
.lang
.XMultiServiceFactory
;
30 import com
.sun
.star
.uno
.UnoRuntime
;
31 import com
.sun
.star
.uno
.XInterface
;
34 * Tests methods of <code>XAcceptor</code> interface. <p>
35 * Required relations :
37 * <li> <code>'XAcceptor.connectStr'</code> : String variable. Has
38 * the following format :
39 * <code>'socket,host=<SOHost>,port=<UniquePort>' where <SOHost> is
40 * the host where StarOffice is started. This string must be passed
41 * as parameter to <code>accept()</code> method. </li>
43 * This test <b>can not</b> be run in multiply threads.
45 public class _XAcceptor
extends MultiMethodTest
{
47 protected PrintWriter log_
;
50 * Calls <code>accept()</code> method in a separate thread.
51 * Then stores exception thrown by call if it occurred, or
54 protected class AcceptorThread
extends Thread
{
56 * If exception occurred during method call it is
57 * stored in this field.
59 public Exception ex
= null ;
60 private XAcceptor acc
= null ;
62 * If method call returns some value it stores in this field.
64 public XConnection acceptedCall
= null ;
67 * Creates object which can call <code>accept</code> method
68 * of the Acceptor object specified.
70 public AcceptorThread(XAcceptor acc
) {
75 * 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.
96 public void before() throws StatusException
{
97 connectString
= (String
)
98 tEnv
.getObjRelation("XAcceptor.connectStr") ;
102 if (connectString
== null)
103 throw new StatusException("No object relation found",
104 new NullPointerException()) ;
108 * First part : Thread with acceptor created, and it starts listening.
109 * The main thread tries to connect to acceptor. Acception thread must
110 * return and valid connection must be returned by Acceptor. <p>
112 * Second part : Trying to create second acceptor which listen on
113 * the same port. Calling <code>accept()</code> method of the second
114 * Acceptor must rise appropriate exception. <p>
116 * Has OK status if both test parts executed properly.
118 public void _accept() {
119 boolean result
= true ;
120 AcceptorThread acception
= null,
121 dupAcception
= null ;
122 XAcceptor dupAcceptor
= null ;
123 XConnector xConnector
= null ;
125 // creating services requierd
127 Object oConnector
= ((XMultiServiceFactory
)tParam
.getMSF()).
128 createInstance("com.sun.star.connection.Connector") ;
130 xConnector
= UnoRuntime
.queryInterface
131 (XConnector
.class, oConnector
) ;
133 XInterface acceptor
= (XInterface
) ((XMultiServiceFactory
)
134 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
) ;
152 catch (java
.lang
.InterruptedException e
) {}
154 XConnection con
= xConnector
.connect(connectString
) ;
157 log
.println("Connector returned : null") ;
159 log
.println("Connector returned : " + con
.getDescription()) ;
162 acception
.join(5 * 1000) ;
163 } catch(InterruptedException e
) {}
165 if (acception
.isAlive()) {
168 log
.println("Method call haven't returned") ;
170 if (acception
.acceptedCall
== null)
171 log
.println("Acceptor returned : null") ;
173 log
.println("Acceptor returned : " +
174 acception
.acceptedCall
.getDescription()) ;
176 if (acception
.ex
!= null) {
177 log
.println("Exception occurred in accept() thread :") ;
178 acception
.ex
.printStackTrace(log
) ;
181 if (acception
.acceptedCall
== null)
182 log
.println("Method returned : null") ;
184 log
.println("Method returned : " +
185 acception
.acceptedCall
.getDescription()) ;
187 result
&= acception
.acceptedCall
!= null ;
189 } catch (com
.sun
.star
.connection
.ConnectionSetupException e
) {
190 e
.printStackTrace(log
) ;
192 } catch (com
.sun
.star
.connection
.NoConnectException e
) {
193 e
.printStackTrace(log
) ;
196 oObj
.stopAccepting();
197 if (acception
.isAlive()) {
198 acception
.interrupt();
202 // duplicate acceptor test
203 // creating the additional acceptor which listens
206 log
.println("___ Testing for accepting on the same port ...") ;
209 dupAcception
= new AcceptorThread(dupAcceptor
) ;
210 dupAcception
.start() ;
213 dupAcception
.join(1 * 1000) ;
214 } catch(InterruptedException e
) {}
217 if (dupAcception
.isAlive()) {
218 log
.println("Duplicate acceptor is listening ...") ;
220 // now trying to accept on the same port as additional
222 acception
= new AcceptorThread(oObj
) ;
226 acception
.join(3 * 1000) ;
227 } catch(InterruptedException e
) {}
229 if (acception
.isAlive()) {
230 oObj
.stopAccepting() ;
231 acception
.interrupt() ;
233 log
.println("Acceptor with the same port must cause"+
234 " an error but didn't") ;
237 log
.println("Accepted call = " + acception
.acceptedCall
) ;
238 if (acception
.ex
== null) {
240 log
.println("No exception was thrown when trying"+
241 " to listen on the same port") ;
243 if (acception
.ex
instanceof
244 com
.sun
.star
.connection
.AlreadyAcceptingException
||
245 acception
.ex
instanceof
246 com
.sun
.star
.connection
.ConnectionSetupException
) {
248 log
.println("Rigth exception was thrown when trying"+
249 " to listen on the same port") ;
252 log
.println("Wrong exception was thrown when trying"+
253 " to listen on the same port :") ;
254 acception
.ex
.printStackTrace(log
) ;
260 dupAcceptor
.stopAccepting() ;
261 if (dupAcception
.isAlive()) {
262 dupAcception
.interrupt() ;
266 tRes
.tested("accept()", result
) ;
270 * Starts thread with Acceptor and then calls <code>stopListening</code>
272 * Has OK status if <code>accept</code> method successfully returns and
273 * rises no exceptions.
275 public void _stopAccepting() {
276 boolean result
= true ;
279 AcceptorThread acception
= new AcceptorThread(oObj
) ;
283 oObj
.stopAccepting() ;
286 acception
.join(3 * 1000) ;
287 } catch (InterruptedException e
) {}
289 if (acception
.isAlive()) {
290 acception
.interrupt() ;
293 log
.println("Method call haven't returned") ;
296 if (acception
.ex
!= null) {
297 log
.println("Exception occurred in accept() thread :") ;
298 acception
.ex
.printStackTrace(log
) ;
304 if (acception
.acceptedCall
== null)
305 log
.println("accept() returned : null") ;
307 log
.println("accept() returned : " +
308 acception
.acceptedCall
.getDescription()) ;
311 tRes
.tested("stopAccepting()", result
) ;