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
.lang
.XMultiServiceFactory
;
28 import com
.sun
.star
.uno
.UnoRuntime
;
29 import com
.sun
.star
.uno
.XInterface
;
32 * Tests methods of <code>XConnector</code> interface. <p>
33 * Required relations :
35 * <li> <code>'XConnector.connectStr'</code> : String variable. Has
36 * the following format :
37 * <code>'socket,host=<SOHost>,port=<UniquePort>' where <SOHost> is
38 * the host where StarOffice is started. This string must be passed
39 * as parameter to <code>accept()</code> method. </li>
41 * This test <b>can not</b> be run in multiply threads.
43 public class _XConnector
extends MultiMethodTest
{
46 * Calls <code>accept()</code> method in a separate thread.
47 * Then stores exception thrown by call if it occurred, or
50 protected class AcceptorThread
extends Thread
{
54 private XAcceptor acc
= null ;
56 * If exception occurred during method call it is
57 * stored in this field.
59 public Exception ex
= null ;
61 * If method call returns some value it stores in this field.
63 public XConnection acceptedCall
= null ;
66 * Gets an object which can call <code>accept</code> method.
68 public AcceptorThread(XAcceptor acc
) {
73 * Call <code>accept()</code> method.
77 acceptedCall
= acc
.accept(connectString
) ;
78 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
80 } catch (com
.sun
.star
.connection
.ConnectionSetupException e
) {
82 } catch (com
.sun
.star
.connection
.AlreadyAcceptingException e
) {
88 public XConnector oObj
= null;
89 protected String connectString
= null ;
92 * Retrieves object relation.
94 public void before() throws StatusException
{
95 connectString
= (String
)
96 tEnv
.getObjRelation("XConnector.connectStr") ;
97 if (connectString
== null)
98 throw new StatusException("No object relation found",
99 new NullPointerException()) ;
103 * Thread with acceptor is created, and it starts listening.
104 * The main thread tries to connect to acceptor. Acception thread must
105 * return and a valid connection must be returned by Acceptor. <p>
108 public void _connect() {
109 boolean result
= true ;
110 AcceptorThread acceptorThread
= null;
111 XAcceptor xAcceptor
= null ;
112 XConnection aCon
= null;
115 // create the acceptor
118 (XMultiServiceFactory
)tParam
.getMSF()).createInstance
119 ("com.sun.star.connection.Acceptor") ;
120 } catch (com
.sun
.star
.uno
.Exception e
) {
121 e
.printStackTrace(log
) ;
122 throw new StatusException("Can't create service", e
) ;
125 xAcceptor
= UnoRuntime
.queryInterface(XAcceptor
.class, x
);
127 acceptorThread
= new AcceptorThread(xAcceptor
) ;
128 acceptorThread
.start() ;
133 catch (java
.lang
.InterruptedException e
) {}
135 // connect to acceptor
137 aCon
= oObj
.connect(connectString
);
140 log
.println("Connector returned: null") ;
142 log
.println("Connector returned: " + aCon
.getDescription()) ;
145 acceptorThread
.join(30 * 1000) ;
146 } catch(InterruptedException e
) {}
148 // connection not established
149 if (acceptorThread
.isAlive()) {
152 log
.println("Method call hasn't returned") ;
154 if (acceptorThread
.acceptedCall
== null)
155 log
.println("Acceptor returned : null") ;
157 log
.println("Acceptor returned : " +
158 acceptorThread
.acceptedCall
.getDescription()) ;
160 if (acceptorThread
.ex
!= null) {
161 log
.println("Exception occurred in accept() thread :") ;
162 acceptorThread
.ex
.printStackTrace(log
) ;
165 if (acceptorThread
.acceptedCall
== null)
166 log
.println("Method returned : null") ;
168 log
.println("Method returned : " +
169 acceptorThread
.acceptedCall
.getDescription()) ;
171 result
&= acceptorThread
.acceptedCall
!= null ;
173 } catch (com
.sun
.star
.connection
.ConnectionSetupException e
) {
174 e
.printStackTrace(log
) ;
176 } catch (com
.sun
.star
.connection
.NoConnectException e
) {
177 e
.printStackTrace(log
) ;
180 acceptorThread
.acc
.stopAccepting();
181 if (acceptorThread
.isAlive()) {
182 acceptorThread
.interrupt();
186 tRes
.tested("connect()", result
) ;