1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XAcceptor.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package ifc
.connection
;
33 import java
.io
.PrintWriter
;
35 import lib
.MultiMethodTest
;
36 import lib
.StatusException
;
38 import com
.sun
.star
.connection
.XAcceptor
;
39 import com
.sun
.star
.connection
.XConnection
;
40 import com
.sun
.star
.connection
.XConnector
;
41 import com
.sun
.star
.lang
.XMultiServiceFactory
;
42 import com
.sun
.star
.uno
.UnoRuntime
;
43 import com
.sun
.star
.uno
.XInterface
;
46 * Tests methods of <code>XAcceptor</code> interface. <p>
47 * Required relations :
49 * <li> <code>'XAcceptor.connectStr'</code> : String variable. Has
50 * the following format :
51 * <code>'socket,host=<SOHost>,port=<UniquePort>' where <SOHost> is
52 * the host where StarOffice is started. This string must be passed
53 * as parameter to <code>accept()</code> method. </li>
55 * This test <b>can not</b> be run in multiply threads.
57 public class _XAcceptor
extends MultiMethodTest
{
59 protected PrintWriter log_
;
62 * Calls <code>accept()</code> method in a separate thread.
63 * Then stores exception thrown by call if it occured, or
66 protected class AcceptorThread
extends Thread
{
68 * If exception occured during method call it is
69 * stored in this field.
71 public Exception ex
= null ;
72 private XAcceptor acc
= null ;
74 * If method call returns some value it stores in this field.
76 public XConnection acceptedCall
= null ;
79 * Creates object which can call <code>accept</code> method
80 * of the Acceptor object specified.
82 public AcceptorThread(XAcceptor acc
) {
87 * Call <code>accept()</code> method.
91 acceptedCall
= acc
.accept(connectString
) ;
92 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
94 } catch (com
.sun
.star
.connection
.ConnectionSetupException e
) {
96 } catch (com
.sun
.star
.connection
.AlreadyAcceptingException e
) {
102 public XAcceptor oObj
= null;
103 protected String connectString
= null ;
106 * Retrieves object relation.
108 public void before() throws StatusException
{
109 connectString
= (String
)
110 tEnv
.getObjRelation("XAcceptor.connectStr") ;
114 if (connectString
== null)
115 throw new StatusException("No object relation found",
116 new NullPointerException()) ;
120 * First part : Thread with acceptor created, and it starts listening.
121 * The main thread tries to connect to acceptor. Acception thread must
122 * return and valid connection must be returned by Acceptor. <p>
124 * Second part : Trying to create second acceptor which listen on
125 * the same port. Calling <code>accept()</code> method of the second
126 * Acceptor must rise appropriate exception. <p>
128 * Has OK status if both test parts executed properly.
130 public void _accept() {
131 boolean result
= true ;
132 AcceptorThread acception
= null,
133 dupAcception
= null ;
134 XAcceptor dupAcceptor
= null ;
135 XConnector xConnector
= null ;
137 // creating services requierd
139 Object oConnector
= ((XMultiServiceFactory
)tParam
.getMSF()).
140 createInstance("com.sun.star.connection.Connector") ;
142 xConnector
= (XConnector
) UnoRuntime
.queryInterface
143 (XConnector
.class, oConnector
) ;
145 XInterface acceptor
= (XInterface
) ((XMultiServiceFactory
)
146 tParam
.getMSF()).createInstance
147 ("com.sun.star.connection.Acceptor") ;
149 dupAcceptor
= (XAcceptor
) UnoRuntime
.queryInterface
150 (XAcceptor
.class, acceptor
) ;
151 } catch (com
.sun
.star
.uno
.Exception e
) {
152 e
.printStackTrace(log
) ;
153 throw new StatusException("Can't create service", e
) ;
156 // Testing connection to the acceptor
158 acception
= new AcceptorThread(oObj
) ;
164 catch (java
.lang
.InterruptedException e
) {}
166 XConnection con
= xConnector
.connect(connectString
) ;
169 log
.println("Connector returned : null") ;
171 log
.println("Connector returned : " + con
.getDescription()) ;
174 acception
.join(5 * 1000) ;
175 } catch(InterruptedException e
) {}
177 if (acception
.isAlive()) {
180 log
.println("Method call haven't returned") ;
182 if (acception
.acceptedCall
== null)
183 log
.println("Acceptor returned : null") ;
185 log
.println("Acceptor returned : " +
186 acception
.acceptedCall
.getDescription()) ;
188 if (acception
.ex
!= null) {
189 log
.println("Exception occured in accept() thread :") ;
190 acception
.ex
.printStackTrace(log
) ;
193 if (acception
.acceptedCall
== null)
194 log
.println("Method returned : null") ;
196 log
.println("Method returned : " +
197 acception
.acceptedCall
.getDescription()) ;
199 result
&= acception
.acceptedCall
!= null ;
201 } catch (com
.sun
.star
.connection
.ConnectionSetupException e
) {
202 e
.printStackTrace(log
) ;
204 } catch (com
.sun
.star
.connection
.NoConnectException e
) {
205 e
.printStackTrace(log
) ;
208 oObj
.stopAccepting();
209 if (acception
.isAlive()) {
210 acception
.interrupt();
214 // duplicate acceptor test
215 // creating the additional acceptor which listens
218 log
.println("___ Testing for accepting on the same port ...") ;
221 dupAcception
= new AcceptorThread(dupAcceptor
) ;
222 dupAcception
.start() ;
225 dupAcception
.join(1 * 1000) ;
226 } catch(InterruptedException e
) {}
229 if (dupAcception
.isAlive()) {
230 log
.println("Duplicate acceptor is listening ...") ;
232 // now trying to accept on the same port as additional
234 acception
= new AcceptorThread(oObj
) ;
238 acception
.join(3 * 1000) ;
239 } catch(InterruptedException e
) {}
241 if (acception
.isAlive()) {
242 oObj
.stopAccepting() ;
243 acception
.interrupt() ;
245 log
.println("Acceptor with the same port must cause"+
246 " an error but didn't") ;
249 log
.println("Accepted call = " + acception
.acceptedCall
) ;
250 if (acception
.ex
== null) {
252 log
.println("No exception was thrown when trying"+
253 " to listen on the same port") ;
255 if (acception
.ex
instanceof
256 com
.sun
.star
.connection
.AlreadyAcceptingException
||
257 acception
.ex
instanceof
258 com
.sun
.star
.connection
.ConnectionSetupException
) {
260 log
.println("Rigth exception was thrown when trying"+
261 " to listen on the same port") ;
264 log
.println("Wrong exception was thrown when trying"+
265 " to listen on the same port :") ;
266 acception
.ex
.printStackTrace(log
) ;
272 dupAcceptor
.stopAccepting() ;
273 if (dupAcception
.isAlive()) {
274 dupAcception
.interrupt() ;
278 tRes
.tested("accept()", result
) ;
282 * Starts thread with Acceptor and then calls <code>stopListening</code>
284 * Has OK status if <code>accept</code> method successfully returns and
285 * rises no exceptions.
287 public void _stopAccepting() {
288 boolean result
= true ;
291 AcceptorThread acception
= new AcceptorThread(oObj
) ;
295 oObj
.stopAccepting() ;
298 acception
.join(3 * 1000) ;
299 } catch (InterruptedException e
) {}
301 if (acception
.isAlive()) {
302 acception
.interrupt() ;
305 log
.println("Method call haven't returned") ;
308 if (acception
.ex
!= null) {
309 log
.println("Exception occured in accept() thread :") ;
310 acception
.ex
.printStackTrace(log
) ;
316 if (acception
.acceptedCall
== null)
317 log
.println("accept() returned : null") ;
319 log
.println("accept() returned : " +
320 acception
.acceptedCall
.getDescription()) ;
323 tRes
.tested("stopAccepting()", result
) ;