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>XConnector</code> interface. <p>
32 * Required relations :
34 * <li> <code>'XConnector.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 _XConnector
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
{
53 private final XAcceptor acc
;
55 * If exception occurred during method call it is
56 * stored in this field.
58 public Exception ex
= null ;
60 * If method call returns some value it stores in this field.
62 public XConnection acceptedCall
= null ;
65 * Gets an object which can call <code>accept</code> method.
67 public AcceptorThread(XAcceptor acc
) {
72 * 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.
95 public void before() throws StatusException
{
96 connectString
= (String
)
97 tEnv
.getObjRelation("XConnector.connectStr") ;
98 if (connectString
== null)
99 throw new StatusException("No object relation found",
100 new NullPointerException()) ;
104 * Thread with acceptor is created, and it starts listening.
105 * The main thread tries to connect to acceptor. Acception thread must
106 * return and a valid connection must be returned by Acceptor. <p>
109 public void _connect() {
110 boolean result
= true ;
111 AcceptorThread acceptorThread
= null;
112 XAcceptor xAcceptor
= null ;
113 XConnection aCon
= null;
116 // create the acceptor
118 x
= (XInterface
) 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() ;
130 util
.utils
.shortWait();
132 // connect to acceptor
134 aCon
= oObj
.connect(connectString
);
137 log
.println("Connector returned: null") ;
139 log
.println("Connector returned: " + aCon
.getDescription()) ;
142 acceptorThread
.join(30 * 1000) ;
143 } catch(InterruptedException e
) {}
145 // connection not established
146 if (acceptorThread
.isAlive()) {
149 log
.println("Method call hasn't returned") ;
151 if (acceptorThread
.acceptedCall
== null)
152 log
.println("Acceptor returned : null") ;
154 log
.println("Acceptor returned : " +
155 acceptorThread
.acceptedCall
.getDescription()) ;
157 if (acceptorThread
.ex
!= null) {
158 log
.println("Exception occurred in accept() thread :") ;
159 acceptorThread
.ex
.printStackTrace(log
) ;
162 if (acceptorThread
.acceptedCall
== null)
163 log
.println("Method returned : null") ;
165 log
.println("Method returned : " +
166 acceptorThread
.acceptedCall
.getDescription()) ;
168 result
&= acceptorThread
.acceptedCall
!= null ;
170 } catch (com
.sun
.star
.connection
.ConnectionSetupException e
) {
171 e
.printStackTrace(log
) ;
173 } catch (com
.sun
.star
.connection
.NoConnectException e
) {
174 e
.printStackTrace(log
) ;
177 acceptorThread
.acc
.stopAccepting();
178 if (acceptorThread
.isAlive()) {
179 acceptorThread
.interrupt();
183 tRes
.tested("connect()", result
) ;