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.uno;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import com.sun.star.bridge.XBridge;
31*cdf0e10cSrcweir import com.sun.star.bridge.XBridgeFactory;
32*cdf0e10cSrcweir import com.sun.star.bridge.XInstanceProvider;
33*cdf0e10cSrcweir import com.sun.star.connection.XAcceptor;
34*cdf0e10cSrcweir import com.sun.star.connection.XConnection;
35*cdf0e10cSrcweir import com.sun.star.connection.XConnector;
36*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
37*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
38*cdf0e10cSrcweir import com.sun.star.uno.Exception;
39*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
40*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
41*cdf0e10cSrcweir import java.io.PrintWriter;
42*cdf0e10cSrcweir import lib.StatusException;
43*cdf0e10cSrcweir import lib.TestCase;
44*cdf0e10cSrcweir import lib.TestEnvironment;
45*cdf0e10cSrcweir import lib.TestParameters;
46*cdf0e10cSrcweir 
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                 xBrdgFctr.createBridge("MyBridge", "urp",
164*cdf0e10cSrcweir                                             acceptedCall, xInstProv);
165*cdf0e10cSrcweir             } catch (com.sun.star.lang.IllegalArgumentException e) {
166*cdf0e10cSrcweir                 ex = e ;
167*cdf0e10cSrcweir             } catch (com.sun.star.connection.ConnectionSetupException e) {
168*cdf0e10cSrcweir                 ex = e ;
169*cdf0e10cSrcweir             } catch (com.sun.star.connection.AlreadyAcceptingException e) {
170*cdf0e10cSrcweir                 ex = e ;
171*cdf0e10cSrcweir             } catch (com.sun.star.bridge.BridgeExistsException e) {
172*cdf0e10cSrcweir                 ex = e ;
173*cdf0e10cSrcweir             }
174*cdf0e10cSrcweir         }
175*cdf0e10cSrcweir     }
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir     private final boolean[] bridgeDisposed = new boolean[1] ;
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir     /**
180*cdf0e10cSrcweir     * Creating a Testenvironment for the interfaces to be tested.
181*cdf0e10cSrcweir     * Creates an instance of the service
182*cdf0e10cSrcweir     * <code>com.sun.star.bridge.Bridge</code>.
183*cdf0e10cSrcweir     *     Object relations created :
184*cdf0e10cSrcweir     * <ul>
185*cdf0e10cSrcweir     *  <li> <code>'XInitialization.args'</code> for
186*cdf0e10cSrcweir     *   {@link ifc.lang._XInitialization} and
187*cdf0e10cSrcweir     *   {@link ifc.bridge._XBridge} : contains arguments
188*cdf0e10cSrcweir     *   for <code>initialize()</code> method test.</li>
189*cdf0e10cSrcweir     * </ul>
190*cdf0e10cSrcweir     */
191*cdf0e10cSrcweir     protected TestEnvironment createTestEnvironment(TestParameters tParam,
192*cdf0e10cSrcweir             PrintWriter log) {
193*cdf0e10cSrcweir         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir         try {
196*cdf0e10cSrcweir             XInterface xInt = (XInterface)xMSF.createInstance(
197*cdf0e10cSrcweir                     "com.sun.star.bridge.Bridge");
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir             TestEnvironment tEnv = new TestEnvironment(xInt);
200*cdf0e10cSrcweir             // creating arguments for XInitialization
201*cdf0e10cSrcweir             // first, creating a connection
202*cdf0e10cSrcweir             // connection string
203*cdf0e10cSrcweir             String cncstr = (String) tParam.get("CNCSTR") ;
204*cdf0e10cSrcweir             int idx = cncstr.indexOf("host=") + 5 ;
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir             // select the port
207*cdf0e10cSrcweir //            curPort; //utils.getNextFreePort(basePort);
208*cdf0e10cSrcweir             log.println("Choose Port nr: " + curPort);
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir             connectString = "socket,host=" +
211*cdf0e10cSrcweir                     cncstr.substring(idx, cncstr.indexOf(",", idx)) +
212*cdf0e10cSrcweir                     ",port=" + curPort;
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir             // create acceptor
215*cdf0e10cSrcweir             XInterface oAcctr = (XInterface)xMSF.createInstance(
216*cdf0e10cSrcweir                     "com.sun.star.connection.Acceptor") ;
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir             xAcctr = (XAcceptor)UnoRuntime.queryInterface(
219*cdf0e10cSrcweir                     XAcceptor.class, oAcctr);
220*cdf0e10cSrcweir             // create connector
221*cdf0e10cSrcweir             XInterface oCntr = (XInterface)xMSF.createInstance(
222*cdf0e10cSrcweir                     "com.sun.star.connection.Connector") ;
223*cdf0e10cSrcweir             xCntr = (XConnector)UnoRuntime.queryInterface(
224*cdf0e10cSrcweir                     XConnector.class, oCntr);
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir             // create bridge factory
227*cdf0e10cSrcweir             XInterface oBrdg = (XInterface)xMSF.createInstance(
228*cdf0e10cSrcweir                     "com.sun.star.bridge.BridgeFactory") ;
229*cdf0e10cSrcweir             xBrdgFctr = (XBridgeFactory)
230*cdf0e10cSrcweir                         UnoRuntime.queryInterface(XBridgeFactory.class, oBrdg);
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir             // create own implementation of XInstanceProvider
233*cdf0e10cSrcweir             XInstanceProvider xInstProv = new MyInstanceProvider(xMSF);
234*cdf0e10cSrcweir             // create waiting acceptor thread
235*cdf0e10cSrcweir             accThread = new AcceptorThread(xAcctr, xInstProv, xBrdgFctr);
236*cdf0e10cSrcweir             accThread.start();
237*cdf0e10cSrcweir             // let the thread sleep
238*cdf0e10cSrcweir             try {
239*cdf0e10cSrcweir                 Thread.sleep(500);
240*cdf0e10cSrcweir             }
241*cdf0e10cSrcweir             catch (java.lang.InterruptedException e) {}
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir             // establish the connection
244*cdf0e10cSrcweir             XConnection xConnection = xCntr.connect(connectString);
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir             String protocol = "urp";
247*cdf0e10cSrcweir             String bridgeName = protocol + ":" + connectString;
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir /*            bridgeDisposed[0] = false ;
250*cdf0e10cSrcweir             XComponent xComp = (XComponent)UnoRuntime.queryInterface(
251*cdf0e10cSrcweir                 XComponent.class, xInt);
252*cdf0e10cSrcweir             final PrintWriter logF = log;
253*cdf0e10cSrcweir             xComp.addEventListener(new XEventListener() {
254*cdf0e10cSrcweir                 public void disposing(EventObject ev) {
255*cdf0e10cSrcweir                     bridgeDisposed[0] = true ;
256*cdf0e10cSrcweir                     logF.println("The bridge Disposed.");
257*cdf0e10cSrcweir                 }
258*cdf0e10cSrcweir             });
259*cdf0e10cSrcweir */
260*cdf0e10cSrcweir             tEnv.addObjRelation("XInitialization.args", new Object[] {
261*cdf0e10cSrcweir                     bridgeName, protocol, xConnection, null});
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir             bridge = tEnv.getTestObject();
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir             return tEnv;
266*cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
267*cdf0e10cSrcweir             e.printStackTrace(log);
268*cdf0e10cSrcweir             throw new StatusException("Unexpected exception", e);
269*cdf0e10cSrcweir         }
270*cdf0e10cSrcweir     }
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir     /**
273*cdf0e10cSrcweir      * Stop the acceptor thread and dispose the bridge
274*cdf0e10cSrcweir      */
275*cdf0e10cSrcweir     protected void cleanup(TestParameters Param, PrintWriter log) {
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir         System.out.println("++++++++ cleanup");
278*cdf0e10cSrcweir         xAcctr.stopAccepting();
279*cdf0e10cSrcweir         if (accThread.isAlive()) {
280*cdf0e10cSrcweir             accThread.interrupt();
281*cdf0e10cSrcweir         }
282*cdf0e10cSrcweir         XComponent xComp = (XComponent)UnoRuntime.queryInterface(
283*cdf0e10cSrcweir                 XComponent.class, xAcctr);
284*cdf0e10cSrcweir         if (xComp != null)
285*cdf0e10cSrcweir             xComp.dispose();
286*cdf0e10cSrcweir         xComp = (XComponent)UnoRuntime.queryInterface(
287*cdf0e10cSrcweir                 XComponent.class, xCntr);
288*cdf0e10cSrcweir         if (xComp != null)
289*cdf0e10cSrcweir             xComp.dispose();
290*cdf0e10cSrcweir         xComp = (XComponent)UnoRuntime.queryInterface(
291*cdf0e10cSrcweir                 XComponent.class, xBrdgFctr);
292*cdf0e10cSrcweir         if (xComp != null)
293*cdf0e10cSrcweir             xComp.dispose();
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir         xComp = (XComponent)UnoRuntime.queryInterface(
296*cdf0e10cSrcweir                 XComponent.class, bridge);
297*cdf0e10cSrcweir         if (xComp != null) {
298*cdf0e10cSrcweir             System.out.println("######## Dispose bridge");
299*cdf0e10cSrcweir             bridgeDisposed[0] = true;
300*cdf0e10cSrcweir             xComp.dispose();
301*cdf0e10cSrcweir             // wait for dispose
302*cdf0e10cSrcweir             try {
303*cdf0e10cSrcweir                 Thread.sleep(5000);
304*cdf0e10cSrcweir             }
305*cdf0e10cSrcweir             catch(java.lang.InterruptedException e) {
306*cdf0e10cSrcweir             }
307*cdf0e10cSrcweir         }
308*cdf0e10cSrcweir     }
309*cdf0e10cSrcweir }
310