Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _remotebridge / uno / various.java
blobc62bf1ee13c8844abd37e8ba909eb4fc61e02f40
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.UnoRuntime;
29 import com.sun.star.uno.XInterface;
30 import java.io.PrintWriter;
31 import lib.StatusException;
32 import lib.TestCase;
33 import lib.TestEnvironment;
34 import lib.TestParameters;
37 /**
38 * Test for object which is represented by service
39 * <code>com.sun.star.bridge.Bridge</code>. <p>
40 * Object implements the following interfaces :
41 * <ul>
42 * <li> <code>com::sun::star::lang::XInitialization</code></li>
43 * <li> <code>com::sun::star::lang::XComponent</code></li>
44 * <li> <code>com::sun::star::bridge::XBridge</code></li>
45 * </ul>
46 * This object test <b> is NOT </b> designed to be run in several
47 * threads concurently.
48 * @see com.sun.star.lang.XInitialization
49 * @see com.sun.star.lang.XComponent
50 * @see com.sun.star.bridge.XBridge
51 * @see com.sun.star.bridge.Bridge
52 * @see ifc.lang._XInitialization
53 * @see ifc.lang._XComponent
54 * @see ifc.bridge._XBridge
56 public class various extends TestCase {
58 /**
59 * String for establishing a connection
61 protected String connectString = null ;
63 /**
64 * Choose the first port after <code>basePort</code>
65 * which is free.
67 protected static final int basePort = 50000;
68 private static final int curPort = 50000;
70 private XAcceptor xAcctr;
71 private XConnector xCntr;
72 private XBridgeFactory xBrdgFctr;
73 private AcceptorThread accThread;
75 public XInterface bridge = null;
77 /**
78 * Implementation of interface XInstanceProvider
80 * @see com.sun.star.bridge.XInstanceProvider
82 private class MyInstanceProvider implements XInstanceProvider {
83 /**
84 * a MultiServiceFactory for creating instances
86 * @see com.sun.star.lang.MultiServiceFactory
88 private final XMultiServiceFactory xMSF;
90 /**
91 * Construct object with a MultiServiceFactory
93 * @see com.sun.star.lang.MultiServiceFactory
95 private MyInstanceProvider(XMultiServiceFactory xMSF) {
96 this.xMSF = xMSF;
99 /**
100 * get an instance by name
102 public Object getInstance(String aInstanceName)
103 throws com.sun.star.container.NoSuchElementException
105 System.out.println("######## Try to get "+aInstanceName);
106 try {
107 return xMSF.createInstance(aInstanceName);
109 catch(com.sun.star.uno.Exception e) {
110 throw new StatusException("Unexpected exception", e);
116 * Calls <code>accept()</code> method in a separate thread.
117 * Then stores exception thrown by call if it occurred, or
118 * return value.
120 private class AcceptorThread extends Thread {
121 private final XAcceptor acc;
122 private final XInstanceProvider xInstProv;
123 private final XBridgeFactory xBrdgFctr;
125 * If method call returns some value it stores in this field.
127 private XConnection acceptedCall = null ;
130 * Creates object which can call <code>accept</code> method
131 * of the Acceptor object specified.
133 private AcceptorThread(XAcceptor acc, XInstanceProvider xInstProv,
134 XBridgeFactory xBrdgFctr) {
135 this.acc = acc ;
136 this.xInstProv = xInstProv;
137 this.xBrdgFctr = xBrdgFctr;
141 * Call <code>accept()</code> method and establish a bridge with an
142 * instance provider
144 @Override
145 public void run() {
146 try {
147 acceptedCall = acc.accept(connectString) ;
148 xBrdgFctr.createBridge("MyBridge", "urp",
149 acceptedCall, xInstProv);
150 } catch (com.sun.star.connection.ConnectionSetupException e) {
151 } catch (com.sun.star.connection.AlreadyAcceptingException e) {
152 } catch (com.sun.star.bridge.BridgeExistsException e) {
157 private final boolean[] bridgeDisposed = new boolean[1] ;
160 * Creating a Testenvironment for the interfaces to be tested.
161 * Creates an instance of the service
162 * <code>com.sun.star.bridge.Bridge</code>.
163 * Object relations created :
164 * <ul>
165 * <li> <code>'XInitialization.args'</code> for
166 * {@link ifc.lang._XInitialization} and
167 * {@link ifc.bridge._XBridge} : contains arguments
168 * for <code>initialize()</code> method test.</li>
169 * </ul>
171 @Override
172 protected TestEnvironment createTestEnvironment(TestParameters tParam,
173 PrintWriter log) {
174 XMultiServiceFactory xMSF = tParam.getMSF();
176 try {
177 XInterface xInt = (XInterface)xMSF.createInstance(
178 "com.sun.star.bridge.Bridge");
180 TestEnvironment tEnv = new TestEnvironment(xInt);
181 // creating arguments for XInitialization
182 // first, creating a connection
183 // connection string
184 String cncstr = (String) tParam.get("CONNECTION_STRING") ;
185 int idx = cncstr.indexOf("host=") + 5 ;
187 // select the port
188 log.println("Choose Port nr: " + curPort);
190 connectString = "socket,host=" +
191 cncstr.substring(idx, cncstr.indexOf(",", idx)) +
192 ",port=" + curPort;
194 // create acceptor
195 XInterface oAcctr = (XInterface)xMSF.createInstance(
196 "com.sun.star.connection.Acceptor") ;
198 xAcctr = UnoRuntime.queryInterface(
199 XAcceptor.class, oAcctr);
200 // create connector
201 XInterface oCntr = (XInterface)xMSF.createInstance(
202 "com.sun.star.connection.Connector") ;
203 xCntr = UnoRuntime.queryInterface(
204 XConnector.class, oCntr);
206 // create bridge factory
207 XInterface oBrdg = (XInterface)xMSF.createInstance(
208 "com.sun.star.bridge.BridgeFactory") ;
209 xBrdgFctr = UnoRuntime.queryInterface(XBridgeFactory.class, oBrdg);
211 // create own implementation of XInstanceProvider
212 XInstanceProvider xInstProv = new MyInstanceProvider(xMSF);
213 // create waiting acceptor thread
214 accThread = new AcceptorThread(xAcctr, xInstProv, xBrdgFctr);
215 accThread.start();
216 // let the thread sleep
217 util.utils.pause(500);
219 // establish the connection
220 XConnection xConnection = xCntr.connect(connectString);
222 String protocol = "urp";
223 String bridgeName = protocol + ":" + connectString;
225 /* bridgeDisposed[0] = false ;
226 XComponent xComp = (XComponent)UnoRuntime.queryInterface(
227 XComponent.class, xInt);
228 final PrintWriter logF = log;
229 xComp.addEventListener(new XEventListener() {
230 public void disposing(EventObject ev) {
231 bridgeDisposed[0] = true ;
232 logF.println("The bridge Disposed.");
236 tEnv.addObjRelation("XInitialization.args", new Object[] {
237 bridgeName, protocol, xConnection, null});
239 bridge = tEnv.getTestObject();
241 return tEnv;
242 } catch (com.sun.star.uno.Exception e) {
243 e.printStackTrace(log);
244 throw new StatusException("Unexpected exception", e);
249 * Stop the acceptor thread and dispose the bridge
251 @Override
252 protected void cleanup(TestParameters Param, PrintWriter log) {
254 System.out.println("++++++++ cleanup");
255 xAcctr.stopAccepting();
256 if (accThread.isAlive()) {
257 accThread.interrupt();
259 XComponent xComp = UnoRuntime.queryInterface(
260 XComponent.class, xAcctr);
261 if (xComp != null)
262 xComp.dispose();
263 xComp = UnoRuntime.queryInterface(
264 XComponent.class, xCntr);
265 if (xComp != null)
266 xComp.dispose();
267 xComp = UnoRuntime.queryInterface(
268 XComponent.class, xBrdgFctr);
269 if (xComp != null)
270 xComp.dispose();
272 xComp = UnoRuntime.queryInterface(
273 XComponent.class, bridge);
274 if (xComp != null) {
275 System.out.println("######## Dispose bridge");
276 bridgeDisposed[0] = true;
277 xComp.dispose();
278 // wait for dispose
279 util.utils.pause(5000);