bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / ifc / bridge / _XBridgeFactory.java
blob9e0a8e661752d7e3f0821fa7b1009245a23bbb79
1 /*
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 ifc.bridge;
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;
33 /**
34 * Tests <code>com.sun.star.bridge.XBridgeFactory</code>
35 * interface methods :
36 * <ul>
37 * <li><code> createBridge()</code></li>
38 * <li><code> getBridge()</code></li>
39 * <li><code> getExistingBridges()</code></li>
40 * </ul> <p>
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;
51 /**
52 * Interrupts the acceptor after test is finished
54 protected void after() {
55 acceptorThread.acc.stopAccepting();
56 if (acceptorThread.isAlive()) {
57 acceptorThread.interrupt();
60 /**
61 * Calls <code>accept()</code> method in a separate thread.
62 * Then stores exception thrown by call if it occurred, or
63 * return value.
65 protected class AcceptorThread extends Thread {
66 /**
67 * the acceptor
69 private XAcceptor acc = null ;
70 /**
71 * If exception occurred during method call it is
72 * stored in this field.
74 public Exception ex = null ;
75 /**
76 * If method call returns some value it stores in this field.
78 public XConnection acceptedCall = null ;
80 /**
81 * Gets an object which can call <code>accept</code> method.
83 public AcceptorThread(XAcceptor acc) {
84 this.acc = acc ;
87 /**
88 * Call <code>accept()</code> method.
90 public void run() {
91 try {
92 acceptedCall = acc.accept(connectString);
93 } catch (com.sun.star.lang.IllegalArgumentException e) {
94 ex = e ;
95 } catch (com.sun.star.connection.ConnectionSetupException e) {
96 ex = e ;
97 } catch (com.sun.star.connection.AlreadyAcceptingException e) {
98 ex = 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() ;
112 * Connection string
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
147 try {
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(
159 XAcceptor.class, x);
160 connectString = (String)tEnv.getObjRelation("CNNCTSTR");
161 acceptorThread = new AcceptorThread(xAccptr) ;
162 acceptorThread.start();
164 try {
165 Thread.sleep(500);
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);
175 try {
176 String protocol = (String) tParam.get("PROTOCOL") ;
177 if (protocol == null) protocol = "urp" ;
179 String brName ;
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 :
203 * <ul>
204 * <li> <code>getExestingBridges</code> : to have some bridge name
205 * to retrieve </li>
206 * </ul> <p>
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") ;
215 return ;
218 XBridge br = oObj.getBridge(bridgeName) ;
220 tRes.tested("getBridge()", br != null &&
221 bridgeName.equals(br.getName())) ;