bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _remotebridge / uno / various.java
blob3a44607ead1e5953f855c94a8fd8f86ddf47ec12
1 /*
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 mod._remotebridge.uno;
21 import com.sun.star.bridge.XBridgeFactory;
22 import com.sun.star.bridge.XInstanceProvider;
23 import com.sun.star.connection.XAcceptor;
24 import com.sun.star.connection.XConnection;
25 import com.sun.star.connection.XConnector;
26 import com.sun.star.lang.XComponent;
27 import com.sun.star.lang.XMultiServiceFactory;
28 import com.sun.star.uno.Exception;
29 import com.sun.star.uno.UnoRuntime;
30 import com.sun.star.uno.XInterface;
31 import java.io.PrintWriter;
32 import lib.StatusException;
33 import lib.TestCase;
34 import lib.TestEnvironment;
35 import lib.TestParameters;
38 /**
39 * Test for object which is represented by service
40 * <code>com.sun.star.bridge.Bridge</code>. <p>
41 * Object implements the following interfaces :
42 * <ul>
43 * <li> <code>com::sun::star::lang::XInitialization</code></li>
44 * <li> <code>com::sun::star::lang::XComponent</code></li>
45 * <li> <code>com::sun::star::bridge::XBridge</code></li>
46 * </ul>
47 * This object test <b> is NOT </b> designed to be run in several
48 * threads concurently.
49 * @see com.sun.star.lang.XInitialization
50 * @see com.sun.star.lang.XComponent
51 * @see com.sun.star.bridge.XBridge
52 * @see com.sun.star.bridge.Bridge
53 * @see ifc.lang._XInitialization
54 * @see ifc.lang._XComponent
55 * @see ifc.bridge._XBridge
57 public class various extends TestCase {
59 /**
60 * String for establishing a connection
62 protected String connectString = null ;
64 /**
65 * Choose the first port after <code>basePort</code>
66 * which is free.
68 protected static final int basePort = 50000;
69 private int curPort = 50000;
71 private XAcceptor xAcctr;
72 private XConnector xCntr;
73 private XBridgeFactory xBrdgFctr;
74 private AcceptorThread accThread;
76 public XInterface bridge = null;
78 /**
79 * Implementation of interface XInstanceProvider
81 * @see com.sun.star.bridge.XInstanceProvider
83 protected class MyInstanceProvider implements XInstanceProvider {
84 /**
85 * a MultiServiceFactory for creating instances
87 * @see com.sun.star.lang.MultiServiceFactory
89 private XMultiServiceFactory xMSF = null;
91 /**
92 * Construct object with a MultiServiceFactory
94 * @see com.sun.star.lang.MultiServiceFactory
96 public MyInstanceProvider(XMultiServiceFactory xMSF) {
97 this.xMSF = xMSF;
101 * get an instance by name
103 public Object getInstance(String aInstanceName)
104 throws com.sun.star.container.NoSuchElementException
106 System.out.println("######## Try to get "+aInstanceName);
107 try {
108 return xMSF.createInstance(aInstanceName);
110 catch(com.sun.star.uno.Exception e) {
111 throw new StatusException("Unexpected exception", e);
117 * Calls <code>accept()</code> method in a separate thread.
118 * Then stores exception thrown by call if it occurred, or
119 * return value.
121 protected class AcceptorThread extends Thread {
123 * If exception occurred during method call it is
124 * stored in this field.
126 public Exception ex = null ;
127 private XAcceptor acc = null ;
128 private XInstanceProvider xInstProv = null ;
129 private XBridgeFactory xBrdgFctr = null;
131 * If method call returns some value it stores in this field.
133 public XConnection acceptedCall = null ;
136 * Creates object which can call <code>accept</code> method
137 * of the Acceptor object specified.
139 public AcceptorThread(XAcceptor acc, XInstanceProvider xInstProv,
140 XBridgeFactory xBrdgFctr) {
141 this.acc = acc ;
142 this.xInstProv = xInstProv;
143 this.xBrdgFctr = xBrdgFctr;
147 * Call <code>accept()</code> method and establish a bridge with an
148 * instance provider
150 public void run() {
151 try {
152 acceptedCall = acc.accept(connectString) ;
153 xBrdgFctr.createBridge("MyBridge", "urp",
154 acceptedCall, xInstProv);
155 } catch (com.sun.star.connection.ConnectionSetupException e) {
156 ex = e ;
157 } catch (com.sun.star.connection.AlreadyAcceptingException e) {
158 ex = e ;
159 } catch (com.sun.star.bridge.BridgeExistsException e) {
160 ex = e ;
165 private final boolean[] bridgeDisposed = new boolean[1] ;
168 * Creating a Testenvironment for the interfaces to be tested.
169 * Creates an instance of the service
170 * <code>com.sun.star.bridge.Bridge</code>.
171 * Object relations created :
172 * <ul>
173 * <li> <code>'XInitialization.args'</code> for
174 * {@link ifc.lang._XInitialization} and
175 * {@link ifc.bridge._XBridge} : contains arguments
176 * for <code>initialize()</code> method test.</li>
177 * </ul>
179 protected TestEnvironment createTestEnvironment(TestParameters tParam,
180 PrintWriter log) {
181 XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
183 try {
184 XInterface xInt = (XInterface)xMSF.createInstance(
185 "com.sun.star.bridge.Bridge");
187 TestEnvironment tEnv = new TestEnvironment(xInt);
188 // creating arguments for XInitialization
189 // first, creating a connection
190 // connection string
191 String cncstr = (String) tParam.get("CNCSTR") ;
192 int idx = cncstr.indexOf("host=") + 5 ;
194 // select the port
195 // curPort; //utils.getNextFreePort(basePort);
196 log.println("Choose Port nr: " + curPort);
198 connectString = "socket,host=" +
199 cncstr.substring(idx, cncstr.indexOf(",", idx)) +
200 ",port=" + curPort;
202 // create acceptor
203 XInterface oAcctr = (XInterface)xMSF.createInstance(
204 "com.sun.star.connection.Acceptor") ;
206 xAcctr = UnoRuntime.queryInterface(
207 XAcceptor.class, oAcctr);
208 // create connector
209 XInterface oCntr = (XInterface)xMSF.createInstance(
210 "com.sun.star.connection.Connector") ;
211 xCntr = UnoRuntime.queryInterface(
212 XConnector.class, oCntr);
214 // create bridge factory
215 XInterface oBrdg = (XInterface)xMSF.createInstance(
216 "com.sun.star.bridge.BridgeFactory") ;
217 xBrdgFctr = UnoRuntime.queryInterface(XBridgeFactory.class, oBrdg);
219 // create own implementation of XInstanceProvider
220 XInstanceProvider xInstProv = new MyInstanceProvider(xMSF);
221 // create waiting acceptor thread
222 accThread = new AcceptorThread(xAcctr, xInstProv, xBrdgFctr);
223 accThread.start();
224 // let the thread sleep
225 try {
226 Thread.sleep(500);
228 catch (java.lang.InterruptedException e) {}
230 // establish the connection
231 XConnection xConnection = xCntr.connect(connectString);
233 String protocol = "urp";
234 String bridgeName = protocol + ":" + connectString;
236 /* bridgeDisposed[0] = false ;
237 XComponent xComp = (XComponent)UnoRuntime.queryInterface(
238 XComponent.class, xInt);
239 final PrintWriter logF = log;
240 xComp.addEventListener(new XEventListener() {
241 public void disposing(EventObject ev) {
242 bridgeDisposed[0] = true ;
243 logF.println("The bridge Disposed.");
247 tEnv.addObjRelation("XInitialization.args", new Object[] {
248 bridgeName, protocol, xConnection, null});
250 bridge = tEnv.getTestObject();
252 return tEnv;
253 } catch (com.sun.star.uno.Exception e) {
254 e.printStackTrace(log);
255 throw new StatusException("Unexpected exception", e);
260 * Stop the acceptor thread and dispose the bridge
262 protected void cleanup(TestParameters Param, PrintWriter log) {
264 System.out.println("++++++++ cleanup");
265 xAcctr.stopAccepting();
266 if (accThread.isAlive()) {
267 accThread.interrupt();
269 XComponent xComp = UnoRuntime.queryInterface(
270 XComponent.class, xAcctr);
271 if (xComp != null)
272 xComp.dispose();
273 xComp = UnoRuntime.queryInterface(
274 XComponent.class, xCntr);
275 if (xComp != null)
276 xComp.dispose();
277 xComp = UnoRuntime.queryInterface(
278 XComponent.class, xBrdgFctr);
279 if (xComp != null)
280 xComp.dispose();
282 xComp = UnoRuntime.queryInterface(
283 XComponent.class, bridge);
284 if (xComp != null) {
285 System.out.println("######## Dispose bridge");
286 bridgeDisposed[0] = true;
287 xComp.dispose();
288 // wait for dispose
289 try {
290 Thread.sleep(5000);
292 catch(java.lang.InterruptedException e) {