1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
30 import lib
.MultiMethodTest
;
31 import lib
.StatusException
;
33 import com
.sun
.star
.bridge
.XBridge
;
34 import com
.sun
.star
.bridge
.XBridgeFactory
;
35 import com
.sun
.star
.connection
.XAcceptor
;
36 import com
.sun
.star
.connection
.XConnection
;
37 import com
.sun
.star
.connection
.XConnector
;
38 import com
.sun
.star
.lang
.XMultiServiceFactory
;
39 import com
.sun
.star
.uno
.UnoRuntime
;
40 import com
.sun
.star
.uno
.XInterface
;
43 * Tests <code>com.sun.star.bridge.XBridgeFactory</code>
46 * <li><code> createBridge()</code></li>
47 * <li><code> getBridge()</code></li>
48 * <li><code> getExistingBridges()</code></li>
50 * @see com.sun.star.bridge.XBridgeFactory
52 public class _XBridgeFactory
extends MultiMethodTest
{
54 public XBridgeFactory oObj
= null;
56 private String bridgeName
= null ;
58 AcceptorThread acceptorThread
= null;
61 * Interrupts the acceptor after test is finished
63 protected void after() {
64 acceptorThread
.acc
.stopAccepting();
65 if (acceptorThread
.isAlive()) {
66 acceptorThread
.interrupt();
70 * Calls <code>accept()</code> method in a separate thread.
71 * Then stores exception thrown by call if it occurred, or
74 protected class AcceptorThread
extends Thread
{
78 private XAcceptor acc
= null ;
80 * If exception occurred during method call it is
81 * stored in this field.
83 public Exception ex
= null ;
85 * If method call returns some value it stores in this field.
87 public XConnection acceptedCall
= null ;
90 * Gets an object which can call <code>accept</code> method.
92 public AcceptorThread(XAcceptor acc
) {
97 * Call <code>accept()</code> method.
101 acceptedCall
= acc
.accept(connectString
);
102 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
104 } catch (com
.sun
.star
.connection
.ConnectionSetupException e
) {
106 } catch (com
.sun
.star
.connection
.AlreadyAcceptingException e
) {
113 * Variable to make bridge names unique in different threads.
115 public static int uniqueSuffix
= 0 ;
117 * Object for synchronizing <code>uniqueSuffix</code> increment.
119 public static Object synchFlag
= new Object() ;
123 public String connectString
;
126 * Gets array of existing bridges. <p>
127 * Has <b>OK</b> status if method returns not null.
129 public void _getExistingBridges() {
131 XBridge
[] bridges
= oObj
.getExistingBridges() ;
133 log
.println("Existing bridges :") ;
134 for (int i
= 0; i
< bridges
.length
; i
++)
135 log
.println(" " + bridges
[i
].getDescription()) ;
137 if (bridges
.length
> 0) bridgeName
= bridges
[0].getName() ;
139 tRes
.tested("getExistingBridges()", bridges
!= null) ;
143 * First creates connection with StarOffice process, using environment
144 * property <code>'CNCSTR'</code>. Then cerates bridge with unique name
145 * using protocol specified in environment as <code>'PROTOCOL'</code>
146 * property. After that bridge is disposed. <p>
147 * Has <b>OK</b> status if value returned is not null
148 * and no exceptions were thrown.<p>
150 public void _createBridge() {
151 XBridge bridge
= null;
152 XConnection conn
= null ;
153 boolean result
= false ;
155 // first creating a connection
157 XInterface x
= (XInterface
)
158 ((XMultiServiceFactory
)tParam
.getMSF()).createInstance
159 ("com.sun.star.connection.Connector") ;
161 XConnector xCntr
= (XConnector
) UnoRuntime
.queryInterface
162 (XConnector
.class, x
) ;
164 x
= (XInterface
) ((XMultiServiceFactory
)tParam
.getMSF()).createInstance
165 ("com.sun.star.connection.Acceptor") ;
167 XAcceptor xAccptr
= (XAcceptor
)UnoRuntime
.queryInterface(
169 connectString
= (String
)tEnv
.getObjRelation("CNNCTSTR");
170 acceptorThread
= new AcceptorThread(xAccptr
) ;
171 acceptorThread
.start();
176 catch (java
.lang
.InterruptedException e
) {}
177 conn
= xCntr
.connect(connectString
) ;
179 } catch (com
.sun
.star
.uno
.Exception e
) {
180 e
.printStackTrace(log
) ;
181 throw new StatusException("Can't create connection", e
);
185 String protocol
= (String
) tParam
.get("PROTOCOL") ;
186 if (protocol
== null) protocol
= "urp" ;
189 synchronized (synchFlag
) {
190 brName
= "MyBridge" + (uniqueSuffix
++) ;
193 log
.println("Creating bridge with name " + brName
) ;
195 bridge
= oObj
.createBridge(brName
,
196 protocol
, conn
, null) ;
199 result
= bridge
!= null ;
200 } catch (com
.sun
.star
.bridge
.BridgeExistsException e
) {
201 log
.println("Exception while bridge creating :" + e
) ;
202 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
203 log
.println("Exception while bridge creating :" + e
) ;
206 tRes
.tested("createBridge()", result
) ;
210 * Gets bridge by name and checks the bridge name returned. <p>
211 * The following method tests are to be executed before :
213 * <li> <code>getExestingBridges</code> : to have some bridge name
216 * Has <b>OK</b> status if bridge successfully returned and it's name
217 * equals to name passed as parameter.
219 public void _getBridge() {
220 executeMethod("getExistingBridges()") ;
222 if (bridgeName
== null) {
223 log
.println("No name for getting the bridge") ;
227 XBridge br
= oObj
.getBridge(bridgeName
) ;
229 tRes
.tested("getBridge()", br
!= null &&
230 bridgeName
.equals(br
.getName())) ;