bump product version to 5.0.4.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _remotebridge / various.java
blobbe245feebf5eb2e7a042e72cf5c669f39175ea9a
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;
21 import java.io.PrintWriter;
23 import lib.StatusException;
24 import lib.TestCase;
25 import lib.TestEnvironment;
26 import lib.TestParameters;
28 import com.sun.star.bridge.XBridgeFactory;
29 import com.sun.star.bridge.XInstanceProvider;
30 import com.sun.star.connection.XAcceptor;
31 import com.sun.star.connection.XConnection;
32 import com.sun.star.connection.XConnector;
33 import com.sun.star.lang.XComponent;
34 import com.sun.star.lang.XMultiServiceFactory;
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.uno.XInterface;
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 static final 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 private class MyInstanceProvider implements XInstanceProvider {
84 /**
85 * a MultiServiceFactory for creating instances
87 * @see com.sun.star.lang.MultiServiceFactory
89 private final XMultiServiceFactory xMSF;
91 /**
92 * Construct object with a MultiServiceFactory
94 * @see com.sun.star.lang.MultiServiceFactory
96 private 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 private class AcceptorThread extends Thread {
122 private final XAcceptor acc;
125 * Creates object which can call <code>accept</code> method
126 * of the Acceptor object specified.
128 private AcceptorThread(XAcceptor acc) {
129 this.acc = acc ;
133 * Call <code>accept()</code> method and establish a bridge with an
134 * instance provider
136 @Override
137 public void run() {
138 try {
139 acc.accept(connectString) ;
140 } catch (com.sun.star.connection.ConnectionSetupException e) {
141 } catch (com.sun.star.connection.AlreadyAcceptingException e) {
146 private final boolean[] bridgeDisposed = new boolean[1] ;
149 * Creating a Testenvironment for the interfaces to be tested.
150 * Creates an instance of the service
151 * <code>com.sun.star.bridge.Bridge</code>.
152 * Object relations created :
153 * <ul>
154 * <li> <code>'XInitialization.args'</code> for
155 * {@link ifc.lang._XInitialization} and
156 * {@link ifc.bridge._XBridge} : contains arguments
157 * for <code>initialize()</code> method test.</li>
158 * </ul>
160 @Override
161 protected TestEnvironment createTestEnvironment(TestParameters tParam,
162 PrintWriter log) {
163 XMultiServiceFactory xMSF = tParam.getMSF();
165 try {
166 XInterface xInt = (XInterface)xMSF.createInstance(
167 "com.sun.star.bridge.Bridge");
169 TestEnvironment tEnv = new TestEnvironment(xInt);
170 // creating arguments for XInitialization
171 // first, creating a connection
172 // connection string
173 String cncstr = (String) tParam.get("CONNECTION_STRING") ;
174 int idx = cncstr.indexOf("host=") + 5 ;
176 // select the port
177 log.println("Choose Port nr: " + curPort);
179 connectString = "socket,host=" +
180 cncstr.substring(idx, cncstr.indexOf(",", idx)) +
181 ",port=" + curPort;
183 // create acceptor
184 XInterface oAcctr = (XInterface)xMSF.createInstance(
185 "com.sun.star.connection.Acceptor") ;
187 xAcctr = UnoRuntime.queryInterface(
188 XAcceptor.class, oAcctr);
189 // create connector
190 XInterface oCntr = (XInterface)xMSF.createInstance(
191 "com.sun.star.connection.Connector") ;
192 xCntr = UnoRuntime.queryInterface(
193 XConnector.class, oCntr);
195 // create bridge factory
196 XInterface oBrdg = (XInterface)xMSF.createInstance(
197 "com.sun.star.bridge.BridgeFactory") ;
198 xBrdgFctr = UnoRuntime.queryInterface(XBridgeFactory.class, oBrdg);
200 // create own implementation of XInstanceProvider
201 new MyInstanceProvider(xMSF);
202 // create waiting acceptor thread
203 accThread = new AcceptorThread(xAcctr);
204 accThread.start();
205 // let the thread sleep
206 util.utils.pause(500);
208 // establish the connection
209 XConnection xConnection = xCntr.connect(connectString);
211 String protocol = "urp";
212 String bridgeName = protocol + ":" + connectString;
214 /* bridgeDisposed[0] = false ;
215 XComponent xComp = (XComponent)UnoRuntime.queryInterface(
216 XComponent.class, xInt);
217 final PrintWriter logF = log;
218 xComp.addEventListener(new XEventListener() {
219 public void disposing(EventObject ev) {
220 bridgeDisposed[0] = true ;
221 logF.println("The bridge Disposed.");
225 tEnv.addObjRelation("XInitialization.args", new Object[] {
226 bridgeName, protocol, xConnection, null});
228 bridge = tEnv.getTestObject();
230 return tEnv;
231 } catch (com.sun.star.uno.Exception e) {
232 e.printStackTrace(log);
233 throw new StatusException("Unexpected exception", e);
238 * Stop the acceptor thread and dispose the bridge
240 @Override
241 protected void cleanup(TestParameters Param, PrintWriter log) {
243 System.out.println("++++++++ cleanup");
244 xAcctr.stopAccepting();
245 if (accThread.isAlive()) {
246 accThread.interrupt();
248 XComponent xComp = UnoRuntime.queryInterface(
249 XComponent.class, xAcctr);
250 if (xComp != null)
251 xComp.dispose();
252 xComp = UnoRuntime.queryInterface(
253 XComponent.class, xCntr);
254 if (xComp != null)
255 xComp.dispose();
256 xComp = UnoRuntime.queryInterface(
257 XComponent.class, xBrdgFctr);
258 if (xComp != null)
259 xComp.dispose();
261 xComp = UnoRuntime.queryInterface(
262 XComponent.class, bridge);
263 if (xComp != null) {
264 System.out.println("######## Dispose bridge");
265 bridgeDisposed[0] = true;
266 xComp.dispose();
267 // wait for dispose
268 util.utils.pause(5000);