1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 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. 14 * 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). 20 * 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. 25 * 26 ************************************************************************/ 27 28 package ifc.bridge; 29 30 import lib.MultiMethodTest; 31 import lib.StatusException; 32 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; 41 42 /** 43 * Tests <code>com.sun.star.bridge.XBridgeFactory</code> 44 * interface methods : 45 * <ul> 46 * <li><code> createBridge()</code></li> 47 * <li><code> getBridge()</code></li> 48 * <li><code> getExistingBridges()</code></li> 49 * </ul> <p> 50 * @see com.sun.star.bridge.XBridgeFactory 51 */ 52 public class _XBridgeFactory extends MultiMethodTest { 53 54 public XBridgeFactory oObj = null; 55 56 private String bridgeName = null ; 57 58 AcceptorThread acceptorThread = null; 59 60 /** 61 * Interrupts the acceptor after test is finished 62 */ 63 protected void after() { 64 acceptorThread.acc.stopAccepting(); 65 if (acceptorThread.isAlive()) { 66 acceptorThread.interrupt(); 67 } 68 } 69 /** 70 * Calls <code>accept()</code> method in a separate thread. 71 * Then stores exception thrown by call if it occured, or 72 * return value. 73 */ 74 protected class AcceptorThread extends Thread { 75 /** 76 * the acceptor 77 */ 78 private XAcceptor acc = null ; 79 /** 80 * If exception occured during method call it is 81 * stored in this field. 82 */ 83 public Exception ex = null ; 84 /** 85 * If method call returns some value it stores in this field. 86 */ 87 public XConnection acceptedCall = null ; 88 89 /** 90 * Gets an object which can call <code>accept</code> method. 91 */ 92 public AcceptorThread(XAcceptor acc) { 93 this.acc = acc ; 94 } 95 96 /** 97 * Call <code>accept()</code> method. 98 */ 99 public void run() { 100 try { 101 acceptedCall = acc.accept(connectString); 102 } catch (com.sun.star.lang.IllegalArgumentException e) { 103 ex = e ; 104 } catch (com.sun.star.connection.ConnectionSetupException e) { 105 ex = e ; 106 } catch (com.sun.star.connection.AlreadyAcceptingException e) { 107 ex = e ; 108 } 109 } 110 } 111 112 /** 113 * Variable to make bridge names unique in different threads. 114 */ 115 public static int uniqueSuffix = 0 ; 116 /** 117 * Object for synchronizing <code>uniqueSuffix</code> increment. 118 */ 119 public static Object synchFlag = new Object() ; 120 /** 121 * Connection string 122 */ 123 public String connectString; 124 125 /** 126 * Gets array of existing bridges. <p> 127 * Has <b>OK</b> status if method returns not null. 128 */ 129 public void _getExistingBridges() { 130 131 XBridge[] bridges = oObj.getExistingBridges() ; 132 133 log.println("Existing bridges :") ; 134 for (int i = 0; i < bridges.length; i++) 135 log.println(" " + bridges[i].getDescription()) ; 136 137 if (bridges.length > 0) bridgeName = bridges[0].getName() ; 138 139 tRes.tested("getExistingBridges()", bridges != null) ; 140 } 141 142 /** 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> 149 */ 150 public void _createBridge() { 151 XBridge bridge = null; 152 XConnection conn = null ; 153 boolean result = false ; 154 155 // first creating a connection 156 try { 157 XInterface x = (XInterface) 158 ((XMultiServiceFactory)tParam.getMSF()).createInstance 159 ("com.sun.star.connection.Connector") ; 160 161 XConnector xCntr = (XConnector) UnoRuntime.queryInterface 162 (XConnector.class, x) ; 163 164 x = (XInterface) ((XMultiServiceFactory)tParam.getMSF()).createInstance 165 ("com.sun.star.connection.Acceptor") ; 166 167 XAcceptor xAccptr = (XAcceptor)UnoRuntime.queryInterface( 168 XAcceptor.class, x); 169 connectString = (String)tEnv.getObjRelation("CNNCTSTR"); 170 acceptorThread = new AcceptorThread(xAccptr) ; 171 acceptorThread.start(); 172 173 try { 174 Thread.sleep(500); 175 } 176 catch (java.lang.InterruptedException e) {} 177 conn = xCntr.connect(connectString) ; 178 179 } catch (com.sun.star.uno.Exception e) { 180 e.printStackTrace(log) ; 181 throw new StatusException("Can't create connection", e); 182 } 183 184 try { 185 String protocol = (String) tParam.get("PROTOCOL") ; 186 if (protocol == null) protocol = "urp" ; 187 188 String brName ; 189 synchronized (synchFlag) { 190 brName = "MyBridge" + (uniqueSuffix++) ; 191 } 192 193 log.println("Creating bridge with name " + brName) ; 194 195 bridge = oObj.createBridge(brName, 196 protocol, conn, null) ; 197 198 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) ; 204 } 205 206 tRes.tested("createBridge()", result) ; 207 } 208 209 /** 210 * Gets bridge by name and checks the bridge name returned. <p> 211 * The following method tests are to be executed before : 212 * <ul> 213 * <li> <code>getExestingBridges</code> : to have some bridge name 214 * to retrieve </li> 215 * </ul> <p> 216 * Has <b>OK</b> status if bridge successfully returned and it's name 217 * equals to name passed as parameter. 218 */ 219 public void _getBridge() { 220 executeMethod("getExistingBridges()") ; 221 222 if (bridgeName == null) { 223 log.println("No name for getting the bridge") ; 224 return ; 225 } 226 227 XBridge br = oObj.getBridge(bridgeName) ; 228 229 tRes.tested("getBridge()", br != null && 230 bridgeName.equals(br.getName())) ; 231 } 232 } 233 234