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
;
24 import lib
.TestEnvironment
;
25 import lib
.TestParameters
;
27 import com
.sun
.star
.bridge
.XBridgeFactory
;
28 import com
.sun
.star
.connection
.XAcceptor
;
29 import com
.sun
.star
.connection
.XConnection
;
30 import com
.sun
.star
.connection
.XConnector
;
31 import com
.sun
.star
.lang
.XComponent
;
32 import com
.sun
.star
.lang
.XMultiServiceFactory
;
33 import com
.sun
.star
.uno
.UnoRuntime
;
34 import com
.sun
.star
.uno
.XInterface
;
37 * Test for object which is represented by service
38 * <code>com.sun.star.bridge.Bridge</code>. <p>
39 * Object implements the following interfaces :
41 * <li> <code>com::sun::star::lang::XInitialization</code></li>
42 * <li> <code>com::sun::star::lang::XComponent</code></li>
43 * <li> <code>com::sun::star::bridge::XBridge</code></li>
45 * This object test <b> is NOT </b> designed to be run in several
46 * threads concurrently.
47 * @see com.sun.star.lang.XInitialization
48 * @see com.sun.star.lang.XComponent
49 * @see com.sun.star.bridge.XBridge
50 * @see com.sun.star.bridge.Bridge
51 * @see ifc.lang._XInitialization
52 * @see ifc.lang._XComponent
53 * @see ifc.bridge._XBridge
55 public class various
extends TestCase
{
58 * String for establishing a connection
60 protected String connectString
= null ;
63 * Choose the first port after <code>basePort</code>
66 protected static final int basePort
= 50000;
67 private static final int curPort
= 50000;
69 private XAcceptor xAcctr
;
70 private XConnector xCntr
;
71 private XBridgeFactory xBrdgFctr
;
72 private AcceptorThread accThread
;
74 public XInterface bridge
= null;
77 * Calls <code>accept()</code> method in a separate thread.
78 * Then stores exception thrown by call if it occurred, or
81 private class AcceptorThread
extends Thread
{
82 private final XAcceptor acc
;
85 * Creates object which can call <code>accept</code> method
86 * of the Acceptor object specified.
88 private AcceptorThread(XAcceptor acc
) {
93 * Call <code>accept()</code> method and establish a bridge with an
99 acc
.accept(connectString
) ;
100 } catch (com
.sun
.star
.connection
.ConnectionSetupException e
) {
101 } catch (com
.sun
.star
.connection
.AlreadyAcceptingException e
) {
106 private final boolean[] bridgeDisposed
= new boolean[1] ;
109 * Creating a TestEnvironment for the interfaces to be tested.
110 * Creates an instance of the service
111 * <code>com.sun.star.bridge.Bridge</code>.
112 * Object relations created :
114 * <li> <code>'XInitialization.args'</code> for
115 * {@link ifc.lang._XInitialization} and
116 * {@link ifc.bridge._XBridge} : contains arguments
117 * for <code>initialize()</code> method test.</li>
121 protected TestEnvironment
createTestEnvironment(TestParameters tParam
,
122 PrintWriter log
) throws Exception
{
123 XMultiServiceFactory xMSF
= tParam
.getMSF();
125 XInterface xInt
= (XInterface
)xMSF
.createInstance(
126 "com.sun.star.bridge.Bridge");
128 TestEnvironment tEnv
= new TestEnvironment(xInt
);
129 // creating arguments for XInitialization
130 // first, creating a connection
132 String cncstr
= (String
) tParam
.get("CONNECTION_STRING") ;
133 int idx
= cncstr
.indexOf("host=") + 5 ;
136 log
.println("Choose Port nr: " + curPort
);
138 connectString
= "socket,host=" +
139 cncstr
.substring(idx
, cncstr
.indexOf(",", idx
)) +
143 XInterface oAcctr
= (XInterface
)xMSF
.createInstance(
144 "com.sun.star.connection.Acceptor") ;
146 xAcctr
= UnoRuntime
.queryInterface(
147 XAcceptor
.class, oAcctr
);
149 XInterface oCntr
= (XInterface
)xMSF
.createInstance(
150 "com.sun.star.connection.Connector") ;
151 xCntr
= UnoRuntime
.queryInterface(
152 XConnector
.class, oCntr
);
154 // create bridge factory
155 XInterface oBrdg
= (XInterface
)xMSF
.createInstance(
156 "com.sun.star.bridge.BridgeFactory") ;
157 xBrdgFctr
= UnoRuntime
.queryInterface(XBridgeFactory
.class, oBrdg
);
159 // create waiting acceptor thread
160 accThread
= new AcceptorThread(xAcctr
);
162 // let the thread sleep
163 util
.utils
.pause(500);
165 // establish the connection
166 XConnection xConnection
= xCntr
.connect(connectString
);
168 String protocol
= "urp";
169 String bridgeName
= protocol
+ ":" + connectString
;
171 tEnv
.addObjRelation("XInitialization.args", new Object
[] {
172 bridgeName
, protocol
, xConnection
, null});
174 bridge
= tEnv
.getTestObject();
180 * Stop the acceptor thread and dispose the bridge
183 protected void cleanup(TestParameters Param
, PrintWriter log
) {
185 System
.out
.println("++++++++ cleanup");
186 xAcctr
.stopAccepting();
187 if (accThread
.isAlive()) {
188 accThread
.interrupt();
190 XComponent xComp
= UnoRuntime
.queryInterface(
191 XComponent
.class, xAcctr
);
194 xComp
= UnoRuntime
.queryInterface(
195 XComponent
.class, xCntr
);
198 xComp
= UnoRuntime
.queryInterface(
199 XComponent
.class, xBrdgFctr
);
203 xComp
= UnoRuntime
.queryInterface(
204 XComponent
.class, bridge
);
206 System
.out
.println("######## Dispose bridge");
207 bridgeDisposed
[0] = true;
210 util
.utils
.pause(5000);