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
;
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
;
39 * Test for object which is represented by service
40 * <code>com.sun.star.bridge.Bridge</code>. <p>
41 * Object implements the following interfaces :
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>
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
{
60 * String for establishing a connection
62 protected String connectString
= null ;
65 * Choose the first port after <code>basePort</code>
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;
79 * Implementation of interface XInstanceProvider
81 * @see com.sun.star.bridge.XInstanceProvider
83 private class MyInstanceProvider
implements XInstanceProvider
{
85 * a MultiServiceFactory for creating instances
87 * @see com.sun.star.lang.MultiServiceFactory
89 private final XMultiServiceFactory xMSF
;
92 * Construct object with a MultiServiceFactory
94 * @see com.sun.star.lang.MultiServiceFactory
96 private MyInstanceProvider(XMultiServiceFactory 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
);
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
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
) {
133 * Call <code>accept()</code> method and establish a bridge with an
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 :
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>
161 protected TestEnvironment
createTestEnvironment(TestParameters tParam
,
163 XMultiServiceFactory xMSF
= tParam
.getMSF();
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
173 String cncstr
= (String
) tParam
.get("CONNECTION_STRING") ;
174 int idx
= cncstr
.indexOf("host=") + 5 ;
177 log
.println("Choose Port nr: " + curPort
);
179 connectString
= "socket,host=" +
180 cncstr
.substring(idx
, cncstr
.indexOf(",", idx
)) +
184 XInterface oAcctr
= (XInterface
)xMSF
.createInstance(
185 "com.sun.star.connection.Acceptor") ;
187 xAcctr
= UnoRuntime
.queryInterface(
188 XAcceptor
.class, oAcctr
);
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
);
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();
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
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
);
252 xComp
= UnoRuntime
.queryInterface(
253 XComponent
.class, xCntr
);
256 xComp
= UnoRuntime
.queryInterface(
257 XComponent
.class, xBrdgFctr
);
261 xComp
= UnoRuntime
.queryInterface(
262 XComponent
.class, bridge
);
264 System
.out
.println("######## Dispose bridge");
265 bridgeDisposed
[0] = true;
268 util
.utils
.pause(5000);