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 .
21 import lib
.MultiMethodTest
;
22 import lib
.StatusException
;
24 import com
.sun
.star
.bridge
.XBridge
;
25 import com
.sun
.star
.bridge
.XBridgeFactory
;
26 import com
.sun
.star
.connection
.XAcceptor
;
27 import com
.sun
.star
.connection
.XConnection
;
28 import com
.sun
.star
.connection
.XConnector
;
29 import com
.sun
.star
.uno
.UnoRuntime
;
30 import com
.sun
.star
.uno
.XInterface
;
33 * Tests <code>com.sun.star.bridge.XBridgeFactory</code>
36 * <li><code> createBridge()</code></li>
37 * <li><code> getBridge()</code></li>
38 * <li><code> getExistingBridges()</code></li>
40 * @see com.sun.star.bridge.XBridgeFactory
42 public class _XBridgeFactory
extends MultiMethodTest
{
44 public XBridgeFactory oObj
= null;
46 private String bridgeName
= null ;
48 AcceptorThread acceptorThread
= null;
51 * Interrupts the acceptor after test is finished
54 protected void after() {
55 acceptorThread
.acc
.stopAccepting();
56 if (acceptorThread
.isAlive()) {
57 acceptorThread
.interrupt();
61 * Calls <code>accept()</code> method in a separate thread.
62 * Then stores exception thrown by call if it occurred, or
65 protected class AcceptorThread
extends Thread
{
69 private final XAcceptor acc
;
71 * If exception occurred during method call it is
72 * stored in this field.
74 public Exception ex
= null ;
76 * If method call returns some value it stores in this field.
78 public XConnection acceptedCall
= null ;
81 * Gets an object which can call <code>accept</code> method.
83 public AcceptorThread(XAcceptor acc
) {
88 * Call <code>accept()</code> method.
93 acceptedCall
= acc
.accept(connectString
);
94 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
96 } catch (com
.sun
.star
.connection
.ConnectionSetupException e
) {
98 } catch (com
.sun
.star
.connection
.AlreadyAcceptingException e
) {
105 * Variable to make bridge names unique in different threads.
107 public static int uniqueSuffix
= 0 ;
109 * Object for synchronizing <code>uniqueSuffix</code> increment.
111 public static Object synchFlag
= new Object() ;
115 public String connectString
;
118 * Gets array of existing bridges. <p>
119 * Has <b>OK</b> status if method returns not null.
121 public void _getExistingBridges() {
123 XBridge
[] bridges
= oObj
.getExistingBridges() ;
125 log
.println("Existing bridges :") ;
126 for (int i
= 0; i
< bridges
.length
; i
++)
127 log
.println(" " + bridges
[i
].getDescription()) ;
129 if (bridges
.length
> 0) bridgeName
= bridges
[0].getName() ;
131 tRes
.tested("getExistingBridges()", true) ;
135 * First creates connection with StarOffice process, using environment
136 * property <code>'CONNECTION_STRING'</code>. Then create bridge with unique name
137 * using protocol specified in environment as <code>'PROTOCOL'</code>
138 * property. After that bridge is disposed. <p>
139 * Has <b>OK</b> status if value returned is not null
140 * and no exceptions were thrown.<p>
142 public void _createBridge() {
143 XBridge bridge
= null;
144 XConnection conn
= null ;
145 boolean result
= false ;
147 // first creating a connection
149 XInterface x
= (XInterface
)
150 tParam
.getMSF().createInstance
151 ("com.sun.star.connection.Connector") ;
153 XConnector xCntr
= UnoRuntime
.queryInterface
154 (XConnector
.class, x
) ;
156 x
= (XInterface
) tParam
.getMSF().createInstance
157 ("com.sun.star.connection.Acceptor") ;
159 XAcceptor xAccptr
= UnoRuntime
.queryInterface(
161 connectString
= (String
)tEnv
.getObjRelation("CNNCTSTR");
162 acceptorThread
= new AcceptorThread(xAccptr
) ;
163 acceptorThread
.start();
165 util
.utils
.shortWait();
166 conn
= xCntr
.connect(connectString
) ;
168 } catch (com
.sun
.star
.uno
.Exception e
) {
169 e
.printStackTrace(log
) ;
170 throw new StatusException("Can't create connection", e
);
174 String protocol
= (String
) tParam
.get("PROTOCOL") ;
175 if (protocol
== null) protocol
= "urp" ;
178 synchronized (synchFlag
) {
179 brName
= "MyBridge" + (uniqueSuffix
++) ;
182 log
.println("Creating bridge with name " + brName
) ;
184 bridge
= oObj
.createBridge(brName
,
185 protocol
, conn
, null) ;
188 result
= bridge
!= null ;
189 } catch (com
.sun
.star
.bridge
.BridgeExistsException e
) {
190 log
.println("Exception while bridge creating :" + e
) ;
191 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
192 log
.println("Exception while bridge creating :" + e
) ;
195 tRes
.tested("createBridge()", result
) ;
199 * Gets bridge by name and checks the bridge name returned. <p>
200 * The following method tests are to be executed before :
202 * <li> <code>getExestingBridges</code> : to have some bridge name
205 * Has <b>OK</b> status if bridge successfully returned and its name
206 * equals to name passed as parameter.
208 public void _getBridge() {
209 executeMethod("getExistingBridges()") ;
211 if (bridgeName
== null) {
212 log
.println("No name for getting the bridge") ;
216 XBridge br
= oObj
.getBridge(bridgeName
) ;
218 tRes
.tested("getBridge()", br
!= null &&
219 bridgeName
.equals(br
.getName())) ;