merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / ifc / bridge / _XBridgeFactory.java
blob1706c0fb9ba200a076c8e1c811d43ac23d8f2fc8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XBridgeFactory.java,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package ifc.bridge;
33 import lib.MultiMethodTest;
34 import lib.StatusException;
36 import com.sun.star.bridge.XBridge;
37 import com.sun.star.bridge.XBridgeFactory;
38 import com.sun.star.connection.XAcceptor;
39 import com.sun.star.connection.XConnection;
40 import com.sun.star.connection.XConnector;
41 import com.sun.star.lang.XMultiServiceFactory;
42 import com.sun.star.uno.UnoRuntime;
43 import com.sun.star.uno.XInterface;
45 /**
46 * Tests <code>com.sun.star.bridge.XBridgeFactory</code>
47 * interface methods :
48 * <ul>
49 * <li><code> createBridge()</code></li>
50 * <li><code> getBridge()</code></li>
51 * <li><code> getExistingBridges()</code></li>
52 * </ul> <p>
53 * @see com.sun.star.bridge.XBridgeFactory
55 public class _XBridgeFactory extends MultiMethodTest {
57 public XBridgeFactory oObj = null;
59 private String bridgeName = null ;
61 AcceptorThread acceptorThread = null;
63 /**
64 * Interrupts the acceptor after test is finished
66 protected void after() {
67 acceptorThread.acc.stopAccepting();
68 if (acceptorThread.isAlive()) {
69 acceptorThread.interrupt();
72 /**
73 * Calls <code>accept()</code> method in a separate thread.
74 * Then stores exception thrown by call if it occured, or
75 * return value.
77 protected class AcceptorThread extends Thread {
78 /**
79 * the acceptor
81 private XAcceptor acc = null ;
82 /**
83 * If exception occured during method call it is
84 * stored in this field.
86 public Exception ex = null ;
87 /**
88 * If method call returns some value it stores in this field.
90 public XConnection acceptedCall = null ;
92 /**
93 * Gets an object which can call <code>accept</code> method.
95 public AcceptorThread(XAcceptor acc) {
96 this.acc = acc ;
99 /**
100 * Call <code>accept()</code> method.
102 public void run() {
103 try {
104 acceptedCall = acc.accept(connectString);
105 } catch (com.sun.star.lang.IllegalArgumentException e) {
106 ex = e ;
107 } catch (com.sun.star.connection.ConnectionSetupException e) {
108 ex = e ;
109 } catch (com.sun.star.connection.AlreadyAcceptingException e) {
110 ex = e ;
116 * Variable to make bridge names unique in different threads.
118 public static int uniqueSuffix = 0 ;
120 * Object for synchronizing <code>uniqueSuffix</code> increment.
122 public static Object synchFlag = new Object() ;
124 * Connection string
126 public String connectString;
129 * Gets array of existing bridges. <p>
130 * Has <b>OK</b> status if method returns not null.
132 public void _getExistingBridges() {
134 XBridge[] bridges = oObj.getExistingBridges() ;
136 log.println("Existing bridges :") ;
137 for (int i = 0; i < bridges.length; i++)
138 log.println(" " + bridges[i].getDescription()) ;
140 if (bridges.length > 0) bridgeName = bridges[0].getName() ;
142 tRes.tested("getExistingBridges()", bridges != null) ;
146 * First creates connection with StarOffice process, using environment
147 * property <code>'CNCSTR'</code>. Then cerates bridge with unique name
148 * using protocol specified in environment as <code>'PROTOCOL'</code>
149 * property. After that bridge is disposed. <p>
150 * Has <b>OK</b> status if value returned is not null
151 * and no exceptions were thrown.<p>
153 public void _createBridge() {
154 XBridge bridge = null;
155 XConnection conn = null ;
156 boolean result = false ;
158 // first creating a connection
159 try {
160 XInterface x = (XInterface)
161 ((XMultiServiceFactory)tParam.getMSF()).createInstance
162 ("com.sun.star.connection.Connector") ;
164 XConnector xCntr = (XConnector) UnoRuntime.queryInterface
165 (XConnector.class, x) ;
167 x = (XInterface) ((XMultiServiceFactory)tParam.getMSF()).createInstance
168 ("com.sun.star.connection.Acceptor") ;
170 XAcceptor xAccptr = (XAcceptor)UnoRuntime.queryInterface(
171 XAcceptor.class, x);
172 connectString = (String)tEnv.getObjRelation("CNNCTSTR");
173 acceptorThread = new AcceptorThread(xAccptr) ;
174 acceptorThread.start();
176 try {
177 Thread.sleep(500);
179 catch (java.lang.InterruptedException e) {}
180 conn = xCntr.connect(connectString) ;
182 } catch (com.sun.star.uno.Exception e) {
183 e.printStackTrace(log) ;
184 throw new StatusException("Can't create connection", e);
187 try {
188 String protocol = (String) tParam.get("PROTOCOL") ;
189 if (protocol == null) protocol = "urp" ;
191 String brName ;
192 synchronized (synchFlag) {
193 brName = "MyBridge" + (uniqueSuffix++) ;
196 log.println("Creating bridge with name " + brName) ;
198 bridge = oObj.createBridge(brName,
199 protocol, conn, null) ;
202 result = bridge != null ;
203 } catch (com.sun.star.bridge.BridgeExistsException e) {
204 log.println("Exception while bridge creating :" + e) ;
205 } catch (com.sun.star.lang.IllegalArgumentException e) {
206 log.println("Exception while bridge creating :" + e) ;
209 tRes.tested("createBridge()", result) ;
213 * Gets bridge by name and checks the bridge name returned. <p>
214 * The following method tests are to be executed before :
215 * <ul>
216 * <li> <code>getExestingBridges</code> : to have some bridge name
217 * to retrieve </li>
218 * </ul> <p>
219 * Has <b>OK</b> status if bridge successfully returned and it's name
220 * equals to name passed as parameter.
222 public void _getBridge() {
223 executeMethod("getExistingBridges()") ;
225 if (bridgeName == null) {
226 log.println("No name for getting the bridge") ;
227 return ;
230 XBridge br = oObj.getBridge(bridgeName) ;
232 tRes.tested("getBridge()", br != null &&
233 bridgeName.equals(br.getName())) ;