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
.lang
.XMultiServiceFactory
;
30 import com
.sun
.star
.uno
.UnoRuntime
;
31 import com
.sun
.star
.uno
.XInterface
;
34 * Tests <code>com.sun.star.bridge.XBridgeFactory</code>
37 * <li><code> createBridge()</code></li>
38 * <li><code> getBridge()</code></li>
39 * <li><code> getExistingBridges()</code></li>
41 * @see com.sun.star.bridge.XBridgeFactory
43 public class _XBridgeFactory
extends MultiMethodTest
{
45 public XBridgeFactory oObj
= null;
47 private String bridgeName
= null ;
49 AcceptorThread acceptorThread
= null;
52 * 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 XAcceptor acc
= null ;
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.
92 acceptedCall
= acc
.accept(connectString
);
93 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
95 } catch (com
.sun
.star
.connection
.ConnectionSetupException e
) {
97 } catch (com
.sun
.star
.connection
.AlreadyAcceptingException e
) {
104 * Variable to make bridge names unique in different threads.
106 public static int uniqueSuffix
= 0 ;
108 * Object for synchronizing <code>uniqueSuffix</code> increment.
110 public static Object synchFlag
= new Object() ;
114 public String connectString
;
117 * Gets array of existing bridges. <p>
118 * Has <b>OK</b> status if method returns not null.
120 public void _getExistingBridges() {
122 XBridge
[] bridges
= oObj
.getExistingBridges() ;
124 log
.println("Existing bridges :") ;
125 for (int i
= 0; i
< bridges
.length
; i
++)
126 log
.println(" " + bridges
[i
].getDescription()) ;
128 if (bridges
.length
> 0) bridgeName
= bridges
[0].getName() ;
130 tRes
.tested("getExistingBridges()", bridges
!= null) ;
134 * First creates connection with StarOffice process, using environment
135 * property <code>'CNCSTR'</code>. Then cerates bridge with unique name
136 * using protocol specified in environment as <code>'PROTOCOL'</code>
137 * property. After that bridge is disposed. <p>
138 * Has <b>OK</b> status if value returned is not null
139 * and no exceptions were thrown.<p>
141 public void _createBridge() {
142 XBridge bridge
= null;
143 XConnection conn
= null ;
144 boolean result
= false ;
146 // first creating a connection
148 XInterface x
= (XInterface
)
149 ((XMultiServiceFactory
)tParam
.getMSF()).createInstance
150 ("com.sun.star.connection.Connector") ;
152 XConnector xCntr
= UnoRuntime
.queryInterface
153 (XConnector
.class, x
) ;
155 x
= (XInterface
) ((XMultiServiceFactory
)tParam
.getMSF()).createInstance
156 ("com.sun.star.connection.Acceptor") ;
158 XAcceptor xAccptr
= UnoRuntime
.queryInterface(
160 connectString
= (String
)tEnv
.getObjRelation("CNNCTSTR");
161 acceptorThread
= new AcceptorThread(xAccptr
) ;
162 acceptorThread
.start();
167 catch (java
.lang
.InterruptedException e
) {}
168 conn
= xCntr
.connect(connectString
) ;
170 } catch (com
.sun
.star
.uno
.Exception e
) {
171 e
.printStackTrace(log
) ;
172 throw new StatusException("Can't create connection", e
);
176 String protocol
= (String
) tParam
.get("PROTOCOL") ;
177 if (protocol
== null) protocol
= "urp" ;
180 synchronized (synchFlag
) {
181 brName
= "MyBridge" + (uniqueSuffix
++) ;
184 log
.println("Creating bridge with name " + brName
) ;
186 bridge
= oObj
.createBridge(brName
,
187 protocol
, conn
, null) ;
190 result
= bridge
!= null ;
191 } catch (com
.sun
.star
.bridge
.BridgeExistsException e
) {
192 log
.println("Exception while bridge creating :" + e
) ;
193 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
194 log
.println("Exception while bridge creating :" + e
) ;
197 tRes
.tested("createBridge()", result
) ;
201 * Gets bridge by name and checks the bridge name returned. <p>
202 * The following method tests are to be executed before :
204 * <li> <code>getExestingBridges</code> : to have some bridge name
207 * Has <b>OK</b> status if bridge successfully returned and it's name
208 * equals to name passed as parameter.
210 public void _getBridge() {
211 executeMethod("getExistingBridges()") ;
213 if (bridgeName
== null) {
214 log
.println("No name for getting the bridge") ;
218 XBridge br
= oObj
.getBridge(bridgeName
) ;
220 tRes
.tested("getBridge()", br
!= null &&
221 bridgeName
.equals(br
.getName())) ;