merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / mod / _remotebridge.uno / various.java
blob75c3e37240913fa7784851b8e7896e7ec7af6b1c
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: various.java,v $
10 * $Revision: 1.4 $
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 mod._remotebridge.uno;
33 import com.sun.star.bridge.XBridge;
34 import com.sun.star.bridge.XBridgeFactory;
35 import com.sun.star.bridge.XInstanceProvider;
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.XComponent;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.uno.Exception;
42 import com.sun.star.uno.UnoRuntime;
43 import com.sun.star.uno.XInterface;
44 import java.io.PrintWriter;
45 import lib.StatusException;
46 import lib.TestCase;
47 import lib.TestEnvironment;
48 import lib.TestParameters;
51 /**
52 * Test for object which is represented by service
53 * <code>com.sun.star.bridge.Bridge</code>. <p>
54 * Object implements the following interfaces :
55 * <ul>
56 * <li> <code>com::sun::star::lang::XInitialization</code></li>
57 * <li> <code>com::sun::star::lang::XComponent</code></li>
58 * <li> <code>com::sun::star::bridge::XBridge</code></li>
59 * </ul>
60 * This object test <b> is NOT </b> designed to be run in several
61 * threads concurently.
62 * @see com.sun.star.lang.XInitialization
63 * @see com.sun.star.lang.XComponent
64 * @see com.sun.star.bridge.XBridge
65 * @see com.sun.star.bridge.Bridge
66 * @see ifc.lang._XInitialization
67 * @see ifc.lang._XComponent
68 * @see ifc.bridge._XBridge
70 public class various extends TestCase {
72 /**
73 * String for establishing a connection
75 protected String connectString = null ;
77 /**
78 * Choose the first port after <code>basePort</code>
79 * which is free.
81 protected static final int basePort = 50000;
82 private int curPort = 50000;
84 private XAcceptor xAcctr;
85 private XConnector xCntr;
86 private XBridgeFactory xBrdgFctr;
87 private AcceptorThread accThread;
89 public XInterface bridge = null;
91 /**
92 * Implementation of interface XInstanceProvider
94 * @see com.sun.star.bridge.XInstanceProvider
96 protected class MyInstanceProvider implements XInstanceProvider {
97 /**
98 * a MultiServiceFactory for creating instances
100 * @see com.sun.star.lang.MultiServiceFactory
102 private XMultiServiceFactory xMSF = null;
105 * Construct object with a MultiServiceFactory
107 * @see com.sun.star.lang.MultiServiceFactory
109 public MyInstanceProvider(XMultiServiceFactory xMSF) {
110 this.xMSF = xMSF;
114 * get an instance by name
116 public Object getInstance(String aInstanceName)
117 throws com.sun.star.container.NoSuchElementException
119 System.out.println("######## Try to get "+aInstanceName);
120 try {
121 return xMSF.createInstance(aInstanceName);
123 catch(com.sun.star.uno.Exception e) {
124 throw new StatusException("Unexpected exception", e);
130 * Calls <code>accept()</code> method in a separate thread.
131 * Then stores exception thrown by call if it occured, or
132 * return value.
134 protected class AcceptorThread extends Thread {
136 * If exception occured during method call it is
137 * stored in this field.
139 public Exception ex = null ;
140 private XAcceptor acc = null ;
141 private XInstanceProvider xInstProv = null ;
142 private XBridgeFactory xBrdgFctr = null;
144 * If method call returns some value it stores in this field.
146 public XConnection acceptedCall = null ;
149 * Creates object which can call <code>accept</code> method
150 * of the Acceptor object specified.
152 public AcceptorThread(XAcceptor acc, XInstanceProvider xInstProv,
153 XBridgeFactory xBrdgFctr) {
154 this.acc = acc ;
155 this.xInstProv = xInstProv;
156 this.xBrdgFctr = xBrdgFctr;
160 * Call <code>accept()</code> method and establish a bridge with an
161 * instance provider
163 public void run() {
164 try {
165 acceptedCall = acc.accept(connectString) ;
166 XBridge xBridge = xBrdgFctr.createBridge("MyBridge", "urp",
167 acceptedCall, xInstProv);
168 } catch (com.sun.star.lang.IllegalArgumentException e) {
169 ex = e ;
170 } catch (com.sun.star.connection.ConnectionSetupException e) {
171 ex = e ;
172 } catch (com.sun.star.connection.AlreadyAcceptingException e) {
173 ex = e ;
174 } catch (com.sun.star.bridge.BridgeExistsException e) {
175 ex = e ;
180 private final boolean[] bridgeDisposed = new boolean[1] ;
183 * Creating a Testenvironment for the interfaces to be tested.
184 * Creates an instance of the service
185 * <code>com.sun.star.bridge.Bridge</code>.
186 * Object relations created :
187 * <ul>
188 * <li> <code>'XInitialization.args'</code> for
189 * {@link ifc.lang._XInitialization} and
190 * {@link ifc.bridge._XBridge} : contains arguments
191 * for <code>initialize()</code> method test.</li>
192 * </ul>
194 protected TestEnvironment createTestEnvironment(TestParameters tParam,
195 PrintWriter log) {
196 XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
198 try {
199 XInterface xInt = (XInterface)xMSF.createInstance(
200 "com.sun.star.bridge.Bridge");
202 TestEnvironment tEnv = new TestEnvironment(xInt);
203 // creating arguments for XInitialization
204 // first, creating a connection
205 // connection string
206 String cncstr = (String) tParam.get("CNCSTR") ;
207 int idx = cncstr.indexOf("host=") + 5 ;
209 // select the port
210 // curPort; //utils.getNextFreePort(basePort);
211 log.println("Choose Port nr: " + curPort);
213 connectString = "socket,host=" +
214 cncstr.substring(idx, cncstr.indexOf(",", idx)) +
215 ",port=" + curPort;
217 // create acceptor
218 XInterface oAcctr = (XInterface)xMSF.createInstance(
219 "com.sun.star.connection.Acceptor") ;
221 xAcctr = (XAcceptor)UnoRuntime.queryInterface(
222 XAcceptor.class, oAcctr);
223 // create connector
224 XInterface oCntr = (XInterface)xMSF.createInstance(
225 "com.sun.star.connection.Connector") ;
226 xCntr = (XConnector)UnoRuntime.queryInterface(
227 XConnector.class, oCntr);
229 // create bridge factory
230 XInterface oBrdg = (XInterface)xMSF.createInstance(
231 "com.sun.star.bridge.BridgeFactory") ;
232 xBrdgFctr = (XBridgeFactory)
233 UnoRuntime.queryInterface(XBridgeFactory.class, oBrdg);
235 // create own implementation of XInstanceProvider
236 XInstanceProvider xInstProv = new MyInstanceProvider(xMSF);
237 // create waiting acceptor thread
238 accThread = new AcceptorThread(xAcctr, xInstProv, xBrdgFctr);
239 accThread.start();
240 // let the thread sleep
241 try {
242 Thread.sleep(500);
244 catch (java.lang.InterruptedException e) {}
246 // establish the connection
247 XConnection xConnection = xCntr.connect(connectString);
249 String protocol = "urp";
250 String bridgeName = protocol + ":" + connectString;
252 /* bridgeDisposed[0] = false ;
253 XComponent xComp = (XComponent)UnoRuntime.queryInterface(
254 XComponent.class, xInt);
255 final PrintWriter logF = log;
256 xComp.addEventListener(new XEventListener() {
257 public void disposing(EventObject ev) {
258 bridgeDisposed[0] = true ;
259 logF.println("The bridge Disposed.");
263 tEnv.addObjRelation("XInitialization.args", new Object[] {
264 bridgeName, protocol, xConnection, null});
266 bridge = tEnv.getTestObject();
268 return tEnv;
269 } catch (com.sun.star.uno.Exception e) {
270 e.printStackTrace(log);
271 throw new StatusException("Unexpected exception", e);
276 * Stop the acceptor thread and dispose the bridge
278 protected void cleanup(TestParameters Param, PrintWriter log) {
280 System.out.println("++++++++ cleanup");
281 xAcctr.stopAccepting();
282 if (accThread.isAlive()) {
283 accThread.interrupt();
285 XComponent xComp = (XComponent)UnoRuntime.queryInterface(
286 XComponent.class, xAcctr);
287 if (xComp != null)
288 xComp.dispose();
289 xComp = (XComponent)UnoRuntime.queryInterface(
290 XComponent.class, xCntr);
291 if (xComp != null)
292 xComp.dispose();
293 xComp = (XComponent)UnoRuntime.queryInterface(
294 XComponent.class, xBrdgFctr);
295 if (xComp != null)
296 xComp.dispose();
298 xComp = (XComponent)UnoRuntime.queryInterface(
299 XComponent.class, bridge);
300 if (xComp != null) {
301 System.out.println("######## Dispose bridge");
302 bridgeDisposed[0] = true;
303 xComp.dispose();
304 // wait for dispose
305 try {
306 Thread.sleep(5000);
308 catch(java.lang.InterruptedException e) {