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: _XConnector.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 lib
.MultiMethodTest
;
34 import lib
.StatusException
;
36 import com
.sun
.star
.connection
.XAcceptor
;
37 import com
.sun
.star
.connection
.XConnection
;
38 import com
.sun
.star
.connection
.XConnector
;
39 import com
.sun
.star
.lang
.XMultiServiceFactory
;
40 import com
.sun
.star
.uno
.UnoRuntime
;
41 import com
.sun
.star
.uno
.XInterface
;
44 * Tests methods of <code>XConnector</code> interface. <p>
45 * Required relations :
47 * <li> <code>'XConnector.connectStr'</code> : String variable. Has
48 * the following format :
49 * <code>'socket,host=<SOHost>,port=<UniquePort>' where <SOHost> is
50 * the host where StarOffice is started. This string must be passed
51 * as parameter to <code>accept()</code> method. </li>
53 * This test <b>can not</b> be run in multiply threads.
55 public class _XConnector
extends MultiMethodTest
{
58 * Calls <code>accept()</code> method in a separate thread.
59 * Then stores exception thrown by call if it occured, or
62 protected class AcceptorThread
extends Thread
{
66 private XAcceptor acc
= null ;
68 * If exception occured during method call it is
69 * stored in this field.
71 public Exception ex
= null ;
73 * If method call returns some value it stores in this field.
75 public XConnection acceptedCall
= null ;
78 * Gets an object which can call <code>accept</code> method.
80 public AcceptorThread(XAcceptor acc
) {
85 * Call <code>accept()</code> method.
89 acceptedCall
= acc
.accept(connectString
) ;
90 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
92 } catch (com
.sun
.star
.connection
.ConnectionSetupException e
) {
94 } catch (com
.sun
.star
.connection
.AlreadyAcceptingException e
) {
100 public XConnector oObj
= null;
101 protected String connectString
= null ;
104 * Retrieves object relation.
106 public void before() throws StatusException
{
107 connectString
= (String
)
108 tEnv
.getObjRelation("XConnector.connectStr") ;
109 if (connectString
== null)
110 throw new StatusException("No object relation found",
111 new NullPointerException()) ;
115 * Thread with acceptor is created, and it starts listening.
116 * The main thread tries to connect to acceptor. Acception thread must
117 * return and a valid connection must be returned by Acceptor. <p>
120 public void _connect() {
121 boolean result
= true ;
122 AcceptorThread acceptorThread
= null;
123 XAcceptor xAcceptor
= null ;
124 XConnection aCon
= null;
127 // create the acceptor
130 (XMultiServiceFactory
)tParam
.getMSF()).createInstance
131 ("com.sun.star.connection.Acceptor") ;
132 } catch (com
.sun
.star
.uno
.Exception e
) {
133 e
.printStackTrace(log
) ;
134 throw new StatusException("Can't create service", e
) ;
137 xAcceptor
= (XAcceptor
)UnoRuntime
.queryInterface(XAcceptor
.class, x
);
139 acceptorThread
= new AcceptorThread(xAcceptor
) ;
140 acceptorThread
.start() ;
145 catch (java
.lang
.InterruptedException e
) {}
147 // connect to acceptor
149 aCon
= oObj
.connect(connectString
);
152 log
.println("Connector returned: null") ;
154 log
.println("Connector returned: " + aCon
.getDescription()) ;
157 acceptorThread
.join(30 * 1000) ;
158 } catch(InterruptedException e
) {}
160 // connection not established
161 if (acceptorThread
.isAlive()) {
164 log
.println("Method call hasn't returned") ;
166 if (acceptorThread
.acceptedCall
== null)
167 log
.println("Acceptor returned : null") ;
169 log
.println("Acceptor returned : " +
170 acceptorThread
.acceptedCall
.getDescription()) ;
172 if (acceptorThread
.ex
!= null) {
173 log
.println("Exception occured in accept() thread :") ;
174 acceptorThread
.ex
.printStackTrace(log
) ;
177 if (acceptorThread
.acceptedCall
== null)
178 log
.println("Method returned : null") ;
180 log
.println("Method returned : " +
181 acceptorThread
.acceptedCall
.getDescription()) ;
183 result
&= acceptorThread
.acceptedCall
!= null ;
185 } catch (com
.sun
.star
.connection
.ConnectionSetupException e
) {
186 e
.printStackTrace(log
) ;
188 } catch (com
.sun
.star
.connection
.NoConnectException e
) {
189 e
.printStackTrace(log
) ;
192 acceptorThread
.acc
.stopAccepting();
193 if (acceptorThread
.isAlive()) {
194 acceptorThread
.interrupt();
198 tRes
.tested("connect()", result
) ;