1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir package mod._remotebridge; 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir import java.io.PrintWriter; 31*cdf0e10cSrcweir 32*cdf0e10cSrcweir import lib.StatusException; 33*cdf0e10cSrcweir import lib.TestCase; 34*cdf0e10cSrcweir import lib.TestEnvironment; 35*cdf0e10cSrcweir import lib.TestParameters; 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir import com.sun.star.bridge.XBridgeFactory; 38*cdf0e10cSrcweir import com.sun.star.bridge.XInstanceProvider; 39*cdf0e10cSrcweir import com.sun.star.connection.XAcceptor; 40*cdf0e10cSrcweir import com.sun.star.connection.XConnection; 41*cdf0e10cSrcweir import com.sun.star.connection.XConnector; 42*cdf0e10cSrcweir import com.sun.star.lang.XComponent; 43*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 44*cdf0e10cSrcweir import com.sun.star.uno.Exception; 45*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 46*cdf0e10cSrcweir import com.sun.star.uno.XInterface; 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir /** 49*cdf0e10cSrcweir * Test for object which is represented by service 50*cdf0e10cSrcweir * <code>com.sun.star.bridge.Bridge</code>. <p> 51*cdf0e10cSrcweir * Object implements the following interfaces : 52*cdf0e10cSrcweir * <ul> 53*cdf0e10cSrcweir * <li> <code>com::sun::star::lang::XInitialization</code></li> 54*cdf0e10cSrcweir * <li> <code>com::sun::star::lang::XComponent</code></li> 55*cdf0e10cSrcweir * <li> <code>com::sun::star::bridge::XBridge</code></li> 56*cdf0e10cSrcweir * </ul> 57*cdf0e10cSrcweir * This object test <b> is NOT </b> designed to be run in several 58*cdf0e10cSrcweir * threads concurently. 59*cdf0e10cSrcweir * @see com.sun.star.lang.XInitialization 60*cdf0e10cSrcweir * @see com.sun.star.lang.XComponent 61*cdf0e10cSrcweir * @see com.sun.star.bridge.XBridge 62*cdf0e10cSrcweir * @see com.sun.star.bridge.Bridge 63*cdf0e10cSrcweir * @see ifc.lang._XInitialization 64*cdf0e10cSrcweir * @see ifc.lang._XComponent 65*cdf0e10cSrcweir * @see ifc.bridge._XBridge 66*cdf0e10cSrcweir */ 67*cdf0e10cSrcweir public class various extends TestCase { 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir /** 70*cdf0e10cSrcweir * String for establishing a connection 71*cdf0e10cSrcweir */ 72*cdf0e10cSrcweir protected String connectString = null ; 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir /** 75*cdf0e10cSrcweir * Choose the first port after <code>basePort</code> 76*cdf0e10cSrcweir * which is free. 77*cdf0e10cSrcweir */ 78*cdf0e10cSrcweir protected static final int basePort = 50000; 79*cdf0e10cSrcweir private int curPort = 50000; 80*cdf0e10cSrcweir 81*cdf0e10cSrcweir private XAcceptor xAcctr; 82*cdf0e10cSrcweir private XConnector xCntr; 83*cdf0e10cSrcweir private XBridgeFactory xBrdgFctr; 84*cdf0e10cSrcweir private AcceptorThread accThread; 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir public XInterface bridge = null; 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir /** 89*cdf0e10cSrcweir * Implementation of interface XInstanceProvider 90*cdf0e10cSrcweir * 91*cdf0e10cSrcweir * @see com.sun.star.bridge.XInstanceProvider 92*cdf0e10cSrcweir */ 93*cdf0e10cSrcweir protected class MyInstanceProvider implements XInstanceProvider { 94*cdf0e10cSrcweir /** 95*cdf0e10cSrcweir * a MultiServiceFactory for creating instances 96*cdf0e10cSrcweir * 97*cdf0e10cSrcweir * @see com.sun.star.lang.MultiServiceFactory 98*cdf0e10cSrcweir */ 99*cdf0e10cSrcweir private XMultiServiceFactory xMSF = null; 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir /** 102*cdf0e10cSrcweir * Construct object with a MultiServiceFactory 103*cdf0e10cSrcweir * 104*cdf0e10cSrcweir * @see com.sun.star.lang.MultiServiceFactory 105*cdf0e10cSrcweir */ 106*cdf0e10cSrcweir public MyInstanceProvider(XMultiServiceFactory xMSF) { 107*cdf0e10cSrcweir this.xMSF = xMSF; 108*cdf0e10cSrcweir } 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir /** 111*cdf0e10cSrcweir * get an instance by name 112*cdf0e10cSrcweir */ 113*cdf0e10cSrcweir public Object getInstance(String aInstanceName) 114*cdf0e10cSrcweir throws com.sun.star.container.NoSuchElementException 115*cdf0e10cSrcweir { 116*cdf0e10cSrcweir System.out.println("######## Try to get "+aInstanceName); 117*cdf0e10cSrcweir try { 118*cdf0e10cSrcweir return xMSF.createInstance(aInstanceName); 119*cdf0e10cSrcweir } 120*cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) { 121*cdf0e10cSrcweir throw new StatusException("Unexpected exception", e); 122*cdf0e10cSrcweir } 123*cdf0e10cSrcweir } 124*cdf0e10cSrcweir } 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir /** 127*cdf0e10cSrcweir * Calls <code>accept()</code> method in a separate thread. 128*cdf0e10cSrcweir * Then stores exception thrown by call if it occured, or 129*cdf0e10cSrcweir * return value. 130*cdf0e10cSrcweir */ 131*cdf0e10cSrcweir protected class AcceptorThread extends Thread { 132*cdf0e10cSrcweir /** 133*cdf0e10cSrcweir * If exception occured during method call it is 134*cdf0e10cSrcweir * stored in this field. 135*cdf0e10cSrcweir */ 136*cdf0e10cSrcweir public Exception ex = null ; 137*cdf0e10cSrcweir private XAcceptor acc = null ; 138*cdf0e10cSrcweir private XInstanceProvider xInstProv = null ; 139*cdf0e10cSrcweir private XBridgeFactory xBrdgFctr = null; 140*cdf0e10cSrcweir /** 141*cdf0e10cSrcweir * If method call returns some value it stores in this field. 142*cdf0e10cSrcweir */ 143*cdf0e10cSrcweir public XConnection acceptedCall = null ; 144*cdf0e10cSrcweir 145*cdf0e10cSrcweir /** 146*cdf0e10cSrcweir * Creates object which can call <code>accept</code> method 147*cdf0e10cSrcweir * of the Acceptor object specified. 148*cdf0e10cSrcweir */ 149*cdf0e10cSrcweir public AcceptorThread(XAcceptor acc, XInstanceProvider xInstProv, 150*cdf0e10cSrcweir XBridgeFactory xBrdgFctr) { 151*cdf0e10cSrcweir this.acc = acc ; 152*cdf0e10cSrcweir this.xInstProv = xInstProv; 153*cdf0e10cSrcweir this.xBrdgFctr = xBrdgFctr; 154*cdf0e10cSrcweir } 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir /** 157*cdf0e10cSrcweir * Call <code>accept()</code> method and establish a bridge with an 158*cdf0e10cSrcweir * instance provider 159*cdf0e10cSrcweir */ 160*cdf0e10cSrcweir public void run() { 161*cdf0e10cSrcweir try { 162*cdf0e10cSrcweir acceptedCall = acc.accept(connectString) ; 163*cdf0e10cSrcweir } catch (com.sun.star.lang.IllegalArgumentException e) { 164*cdf0e10cSrcweir ex = e ; 165*cdf0e10cSrcweir } catch (com.sun.star.connection.ConnectionSetupException e) { 166*cdf0e10cSrcweir ex = e ; 167*cdf0e10cSrcweir } catch (com.sun.star.connection.AlreadyAcceptingException e) { 168*cdf0e10cSrcweir ex = e ; 169*cdf0e10cSrcweir } 170*cdf0e10cSrcweir } 171*cdf0e10cSrcweir } 172*cdf0e10cSrcweir 173*cdf0e10cSrcweir private final boolean[] bridgeDisposed = new boolean[1] ; 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir /** 176*cdf0e10cSrcweir * Creating a Testenvironment for the interfaces to be tested. 177*cdf0e10cSrcweir * Creates an instance of the service 178*cdf0e10cSrcweir * <code>com.sun.star.bridge.Bridge</code>. 179*cdf0e10cSrcweir * Object relations created : 180*cdf0e10cSrcweir * <ul> 181*cdf0e10cSrcweir * <li> <code>'XInitialization.args'</code> for 182*cdf0e10cSrcweir * {@link ifc.lang._XInitialization} and 183*cdf0e10cSrcweir * {@link ifc.bridge._XBridge} : contains arguments 184*cdf0e10cSrcweir * for <code>initialize()</code> method test.</li> 185*cdf0e10cSrcweir * </ul> 186*cdf0e10cSrcweir */ 187*cdf0e10cSrcweir protected TestEnvironment createTestEnvironment(TestParameters tParam, 188*cdf0e10cSrcweir PrintWriter log) { 189*cdf0e10cSrcweir XMultiServiceFactory xMSF = (XMultiServiceFactory) tParam.getMSF(); 190*cdf0e10cSrcweir 191*cdf0e10cSrcweir try { 192*cdf0e10cSrcweir XInterface xInt = (XInterface)xMSF.createInstance( 193*cdf0e10cSrcweir "com.sun.star.bridge.Bridge"); 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir TestEnvironment tEnv = new TestEnvironment(xInt); 196*cdf0e10cSrcweir // creating arguments for XInitialization 197*cdf0e10cSrcweir // first, creating a connection 198*cdf0e10cSrcweir // connection string 199*cdf0e10cSrcweir String cncstr = (String) tParam.get("CNCSTR") ; 200*cdf0e10cSrcweir int idx = cncstr.indexOf("host=") + 5 ; 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir // select the port 203*cdf0e10cSrcweir // curPort; //utils.getNextFreePort(basePort); 204*cdf0e10cSrcweir log.println("Choose Port nr: " + curPort); 205*cdf0e10cSrcweir 206*cdf0e10cSrcweir connectString = "socket,host=" + 207*cdf0e10cSrcweir cncstr.substring(idx, cncstr.indexOf(",", idx)) + 208*cdf0e10cSrcweir ",port=" + curPort; 209*cdf0e10cSrcweir 210*cdf0e10cSrcweir // create acceptor 211*cdf0e10cSrcweir XInterface oAcctr = (XInterface)xMSF.createInstance( 212*cdf0e10cSrcweir "com.sun.star.connection.Acceptor") ; 213*cdf0e10cSrcweir 214*cdf0e10cSrcweir xAcctr = (XAcceptor)UnoRuntime.queryInterface( 215*cdf0e10cSrcweir XAcceptor.class, oAcctr); 216*cdf0e10cSrcweir // create connector 217*cdf0e10cSrcweir XInterface oCntr = (XInterface)xMSF.createInstance( 218*cdf0e10cSrcweir "com.sun.star.connection.Connector") ; 219*cdf0e10cSrcweir xCntr = (XConnector)UnoRuntime.queryInterface( 220*cdf0e10cSrcweir XConnector.class, oCntr); 221*cdf0e10cSrcweir 222*cdf0e10cSrcweir // create bridge factory 223*cdf0e10cSrcweir XInterface oBrdg = (XInterface)xMSF.createInstance( 224*cdf0e10cSrcweir "com.sun.star.bridge.BridgeFactory") ; 225*cdf0e10cSrcweir xBrdgFctr = (XBridgeFactory) 226*cdf0e10cSrcweir UnoRuntime.queryInterface(XBridgeFactory.class, oBrdg); 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir // create own implementation of XInstanceProvider 229*cdf0e10cSrcweir XInstanceProvider xInstProv = new MyInstanceProvider(xMSF); 230*cdf0e10cSrcweir // create waiting acceptor thread 231*cdf0e10cSrcweir accThread = new AcceptorThread(xAcctr, xInstProv, xBrdgFctr); 232*cdf0e10cSrcweir accThread.start(); 233*cdf0e10cSrcweir // let the thread sleep 234*cdf0e10cSrcweir try { 235*cdf0e10cSrcweir Thread.sleep(500); 236*cdf0e10cSrcweir } 237*cdf0e10cSrcweir catch (java.lang.InterruptedException e) {} 238*cdf0e10cSrcweir 239*cdf0e10cSrcweir // establish the connection 240*cdf0e10cSrcweir XConnection xConnection = xCntr.connect(connectString); 241*cdf0e10cSrcweir 242*cdf0e10cSrcweir String protocol = "urp"; 243*cdf0e10cSrcweir String bridgeName = protocol + ":" + connectString; 244*cdf0e10cSrcweir 245*cdf0e10cSrcweir /* bridgeDisposed[0] = false ; 246*cdf0e10cSrcweir XComponent xComp = (XComponent)UnoRuntime.queryInterface( 247*cdf0e10cSrcweir XComponent.class, xInt); 248*cdf0e10cSrcweir final PrintWriter logF = log; 249*cdf0e10cSrcweir xComp.addEventListener(new XEventListener() { 250*cdf0e10cSrcweir public void disposing(EventObject ev) { 251*cdf0e10cSrcweir bridgeDisposed[0] = true ; 252*cdf0e10cSrcweir logF.println("The bridge Disposed."); 253*cdf0e10cSrcweir } 254*cdf0e10cSrcweir }); 255*cdf0e10cSrcweir */ 256*cdf0e10cSrcweir tEnv.addObjRelation("XInitialization.args", new Object[] { 257*cdf0e10cSrcweir bridgeName, protocol, xConnection, null}); 258*cdf0e10cSrcweir 259*cdf0e10cSrcweir bridge = tEnv.getTestObject(); 260*cdf0e10cSrcweir 261*cdf0e10cSrcweir return tEnv; 262*cdf0e10cSrcweir } catch (com.sun.star.uno.Exception e) { 263*cdf0e10cSrcweir e.printStackTrace(log); 264*cdf0e10cSrcweir throw new StatusException("Unexpected exception", e); 265*cdf0e10cSrcweir } 266*cdf0e10cSrcweir } 267*cdf0e10cSrcweir 268*cdf0e10cSrcweir /** 269*cdf0e10cSrcweir * Stop the acceptor thread and dispose the bridge 270*cdf0e10cSrcweir */ 271*cdf0e10cSrcweir protected void cleanup(TestParameters Param, PrintWriter log) { 272*cdf0e10cSrcweir 273*cdf0e10cSrcweir System.out.println("++++++++ cleanup"); 274*cdf0e10cSrcweir xAcctr.stopAccepting(); 275*cdf0e10cSrcweir if (accThread.isAlive()) { 276*cdf0e10cSrcweir accThread.interrupt(); 277*cdf0e10cSrcweir } 278*cdf0e10cSrcweir XComponent xComp = (XComponent)UnoRuntime.queryInterface( 279*cdf0e10cSrcweir XComponent.class, xAcctr); 280*cdf0e10cSrcweir if (xComp != null) 281*cdf0e10cSrcweir xComp.dispose(); 282*cdf0e10cSrcweir xComp = (XComponent)UnoRuntime.queryInterface( 283*cdf0e10cSrcweir XComponent.class, xCntr); 284*cdf0e10cSrcweir if (xComp != null) 285*cdf0e10cSrcweir xComp.dispose(); 286*cdf0e10cSrcweir xComp = (XComponent)UnoRuntime.queryInterface( 287*cdf0e10cSrcweir XComponent.class, xBrdgFctr); 288*cdf0e10cSrcweir if (xComp != null) 289*cdf0e10cSrcweir xComp.dispose(); 290*cdf0e10cSrcweir 291*cdf0e10cSrcweir xComp = (XComponent)UnoRuntime.queryInterface( 292*cdf0e10cSrcweir XComponent.class, bridge); 293*cdf0e10cSrcweir if (xComp != null) { 294*cdf0e10cSrcweir System.out.println("######## Dispose bridge"); 295*cdf0e10cSrcweir bridgeDisposed[0] = true; 296*cdf0e10cSrcweir xComp.dispose(); 297*cdf0e10cSrcweir // wait for dispose 298*cdf0e10cSrcweir try { 299*cdf0e10cSrcweir Thread.sleep(5000); 300*cdf0e10cSrcweir } 301*cdf0e10cSrcweir catch(java.lang.InterruptedException e) { 302*cdf0e10cSrcweir } 303*cdf0e10cSrcweir } 304*cdf0e10cSrcweir } 305*cdf0e10cSrcweir } 306