Update ooo320-m1
[ooovba.git] / qadevOOo / tests / java / ifc / connection / _XConnector.java
blobd77252e14c8480eb1bf82f2c155abcb8e6e95d11
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XConnector.java,v $
10 * $Revision: 1.5 $
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;
43 /**
44 * Tests methods of <code>XConnector</code> interface. <p>
45 * Required relations :
46 * <ul>
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>
52 * <ul> <p>
53 * This test <b>can not</b> be run in multiply threads.
55 public class _XConnector extends MultiMethodTest {
57 /**
58 * Calls <code>accept()</code> method in a separate thread.
59 * Then stores exception thrown by call if it occured, or
60 * return value.
62 protected class AcceptorThread extends Thread {
63 /**
64 * the acceptor
66 private XAcceptor acc = null ;
67 /**
68 * If exception occured during method call it is
69 * stored in this field.
71 public Exception ex = null ;
72 /**
73 * If method call returns some value it stores in this field.
75 public XConnection acceptedCall = null ;
77 /**
78 * Gets an object which can call <code>accept</code> method.
80 public AcceptorThread(XAcceptor acc) {
81 this.acc = acc ;
84 /**
85 * Call <code>accept()</code> method.
87 public void run() {
88 try {
89 acceptedCall = acc.accept(connectString) ;
90 } catch (com.sun.star.lang.IllegalArgumentException e) {
91 ex = e ;
92 } catch (com.sun.star.connection.ConnectionSetupException e) {
93 ex = e ;
94 } catch (com.sun.star.connection.AlreadyAcceptingException e) {
95 ex = 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;
125 XInterface x = null;
127 // create the acceptor
128 try {
129 x = (XInterface) (
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() ;
142 try {
143 Thread.sleep(500);
145 catch (java.lang.InterruptedException e) {}
147 // connect to acceptor
148 try {
149 aCon = oObj.connect(connectString);
151 if (aCon == null)
152 log.println("Connector returned: null") ;
153 else
154 log.println("Connector returned: " + aCon.getDescription()) ;
156 try {
157 acceptorThread.join(30 * 1000) ;
158 } catch(InterruptedException e) {}
160 // connection not established
161 if (acceptorThread.isAlive()) {
163 result = false ;
164 log.println("Method call hasn't returned") ;
166 if (acceptorThread.acceptedCall == null)
167 log.println("Acceptor returned : null") ;
168 else
169 log.println("Acceptor returned : " +
170 acceptorThread.acceptedCall.getDescription()) ;
171 } else {
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") ;
179 else
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) ;
187 result = false ;
188 } catch (com.sun.star.connection.NoConnectException e) {
189 e.printStackTrace(log) ;
190 result = false ;
191 } finally {
192 acceptorThread.acc.stopAccepting();
193 if (acceptorThread.isAlive()) {
194 acceptorThread.interrupt();
198 tRes.tested("connect()", result) ;