xref: /AOO41X/main/qadevOOo/tests/java/ifc/bridge/_XBridgeFactory.java (revision ef39d40d3f5e66cf3f035b3e93783012b340500d)
1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package ifc.bridge;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import lib.MultiMethodTest;
27cdf0e10cSrcweir import lib.StatusException;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir import com.sun.star.bridge.XBridge;
30cdf0e10cSrcweir import com.sun.star.bridge.XBridgeFactory;
31cdf0e10cSrcweir import com.sun.star.connection.XAcceptor;
32cdf0e10cSrcweir import com.sun.star.connection.XConnection;
33cdf0e10cSrcweir import com.sun.star.connection.XConnector;
34cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
35cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
36cdf0e10cSrcweir import com.sun.star.uno.XInterface;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir /**
39cdf0e10cSrcweir * Tests <code>com.sun.star.bridge.XBridgeFactory</code>
40cdf0e10cSrcweir * interface methods :
41cdf0e10cSrcweir * <ul>
42cdf0e10cSrcweir *  <li><code> createBridge()</code></li>
43cdf0e10cSrcweir *  <li><code> getBridge()</code></li>
44cdf0e10cSrcweir *  <li><code> getExistingBridges()</code></li>
45cdf0e10cSrcweir * </ul> <p>
46cdf0e10cSrcweir * @see com.sun.star.bridge.XBridgeFactory
47cdf0e10cSrcweir */
48cdf0e10cSrcweir public class _XBridgeFactory extends MultiMethodTest {
49cdf0e10cSrcweir 
50cdf0e10cSrcweir     public XBridgeFactory oObj = null;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir     private String bridgeName = null ;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir     AcceptorThread acceptorThread = null;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir     /**
57cdf0e10cSrcweir      * Interrupts the acceptor after test is finished
58cdf0e10cSrcweir      */
after()59cdf0e10cSrcweir     protected void after() {
60cdf0e10cSrcweir         acceptorThread.acc.stopAccepting();
61cdf0e10cSrcweir         if (acceptorThread.isAlive()) {
62cdf0e10cSrcweir             acceptorThread.interrupt();
63cdf0e10cSrcweir         }
64cdf0e10cSrcweir     }
65cdf0e10cSrcweir     /**
66cdf0e10cSrcweir     * Calls <code>accept()</code> method in a separate thread.
67cdf0e10cSrcweir     * Then stores exception thrown by call if it occured, or
68cdf0e10cSrcweir     * return value.
69cdf0e10cSrcweir     */
70cdf0e10cSrcweir     protected class AcceptorThread extends Thread {
71cdf0e10cSrcweir         /**
72cdf0e10cSrcweir          * the acceptor
73cdf0e10cSrcweir          */
74cdf0e10cSrcweir         private XAcceptor acc = null ;
75cdf0e10cSrcweir         /**
76cdf0e10cSrcweir         * If exception occured during method call it is
77cdf0e10cSrcweir         * stored in this field.
78cdf0e10cSrcweir         */
79cdf0e10cSrcweir         public Exception ex = null ;
80cdf0e10cSrcweir         /**
81cdf0e10cSrcweir         * If method call returns some value it stores in this field.
82cdf0e10cSrcweir         */
83cdf0e10cSrcweir         public XConnection acceptedCall = null ;
84cdf0e10cSrcweir 
85cdf0e10cSrcweir         /**
86cdf0e10cSrcweir         * Gets an object which can call <code>accept</code> method.
87cdf0e10cSrcweir         */
AcceptorThread(XAcceptor acc)88cdf0e10cSrcweir         public AcceptorThread(XAcceptor acc) {
89cdf0e10cSrcweir             this.acc = acc ;
90cdf0e10cSrcweir         }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir         /**
93cdf0e10cSrcweir         * Call <code>accept()</code> method.
94cdf0e10cSrcweir         */
run()95cdf0e10cSrcweir         public void run() {
96cdf0e10cSrcweir             try {
97cdf0e10cSrcweir                 acceptedCall = acc.accept(connectString);
98cdf0e10cSrcweir             } catch (com.sun.star.lang.IllegalArgumentException e) {
99cdf0e10cSrcweir                 ex = e ;
100cdf0e10cSrcweir             } catch (com.sun.star.connection.ConnectionSetupException e) {
101cdf0e10cSrcweir                 ex = e ;
102cdf0e10cSrcweir             } catch (com.sun.star.connection.AlreadyAcceptingException e) {
103cdf0e10cSrcweir                 ex = e ;
104cdf0e10cSrcweir             }
105cdf0e10cSrcweir         }
106cdf0e10cSrcweir     }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir     /**
109cdf0e10cSrcweir     * Variable to make bridge names unique in different threads.
110cdf0e10cSrcweir     */
111cdf0e10cSrcweir     public static int uniqueSuffix = 0 ;
112cdf0e10cSrcweir     /**
113cdf0e10cSrcweir     * Object for synchronizing <code>uniqueSuffix</code> increment.
114cdf0e10cSrcweir     */
115cdf0e10cSrcweir     public static Object synchFlag = new Object() ;
116cdf0e10cSrcweir     /**
117cdf0e10cSrcweir      * Connection string
118cdf0e10cSrcweir      */
119cdf0e10cSrcweir      public String connectString;
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     /**
122cdf0e10cSrcweir     * Gets array of existing bridges. <p>
123cdf0e10cSrcweir     * Has <b>OK</b> status if method returns not null.
124cdf0e10cSrcweir     */
_getExistingBridges()125cdf0e10cSrcweir     public void _getExistingBridges() {
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         XBridge[] bridges = oObj.getExistingBridges() ;
128cdf0e10cSrcweir 
129cdf0e10cSrcweir         log.println("Existing bridges :") ;
130cdf0e10cSrcweir         for (int i = 0; i < bridges.length; i++)
131cdf0e10cSrcweir             log.println("  " + bridges[i].getDescription()) ;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir         if (bridges.length > 0) bridgeName = bridges[0].getName() ;
134cdf0e10cSrcweir 
135cdf0e10cSrcweir         tRes.tested("getExistingBridges()", bridges != null) ;
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     /**
139cdf0e10cSrcweir     * First creates connection with StarOffice process, using environment
140cdf0e10cSrcweir     * property <code>'CNCSTR'</code>. Then cerates bridge with unique name
141cdf0e10cSrcweir     * using protocol specified in environment as <code>'PROTOCOL'</code>
142cdf0e10cSrcweir     * property. After that bridge is disposed. <p>
143cdf0e10cSrcweir     * Has <b>OK</b> status if value returned is not null
144cdf0e10cSrcweir     * and no exceptions were thrown.<p>
145cdf0e10cSrcweir     */
_createBridge()146cdf0e10cSrcweir     public void _createBridge() {
147cdf0e10cSrcweir         XBridge bridge = null;
148cdf0e10cSrcweir         XConnection conn = null ;
149cdf0e10cSrcweir         boolean result = false ;
150cdf0e10cSrcweir 
151cdf0e10cSrcweir         // first creating a connection
152cdf0e10cSrcweir         try {
153cdf0e10cSrcweir             XInterface x = (XInterface)
154cdf0e10cSrcweir                 ((XMultiServiceFactory)tParam.getMSF()).createInstance
155cdf0e10cSrcweir                 ("com.sun.star.connection.Connector") ;
156cdf0e10cSrcweir 
157cdf0e10cSrcweir             XConnector xCntr = (XConnector) UnoRuntime.queryInterface
158cdf0e10cSrcweir                 (XConnector.class, x) ;
159cdf0e10cSrcweir 
160cdf0e10cSrcweir             x = (XInterface) ((XMultiServiceFactory)tParam.getMSF()).createInstance
161cdf0e10cSrcweir                 ("com.sun.star.connection.Acceptor") ;
162cdf0e10cSrcweir 
163cdf0e10cSrcweir             XAcceptor xAccptr = (XAcceptor)UnoRuntime.queryInterface(
164cdf0e10cSrcweir                                                         XAcceptor.class, x);
165cdf0e10cSrcweir             connectString = (String)tEnv.getObjRelation("CNNCTSTR");
166cdf0e10cSrcweir             acceptorThread = new AcceptorThread(xAccptr) ;
167cdf0e10cSrcweir             acceptorThread.start();
168cdf0e10cSrcweir 
169cdf0e10cSrcweir             try {
170cdf0e10cSrcweir                 Thread.sleep(500);
171cdf0e10cSrcweir             }
172cdf0e10cSrcweir             catch (java.lang.InterruptedException e) {}
173cdf0e10cSrcweir             conn = xCntr.connect(connectString) ;
174cdf0e10cSrcweir 
175cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
176cdf0e10cSrcweir             e.printStackTrace(log) ;
177cdf0e10cSrcweir             throw new StatusException("Can't create connection", e);
178cdf0e10cSrcweir         }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir         try {
181cdf0e10cSrcweir             String protocol = (String) tParam.get("PROTOCOL") ;
182cdf0e10cSrcweir             if (protocol == null) protocol = "urp" ;
183cdf0e10cSrcweir 
184cdf0e10cSrcweir             String brName ;
185cdf0e10cSrcweir             synchronized (synchFlag) {
186cdf0e10cSrcweir                 brName = "MyBridge" + (uniqueSuffix++) ;
187cdf0e10cSrcweir             }
188cdf0e10cSrcweir 
189cdf0e10cSrcweir             log.println("Creating bridge with name " + brName) ;
190cdf0e10cSrcweir 
191cdf0e10cSrcweir             bridge = oObj.createBridge(brName,
192cdf0e10cSrcweir                 protocol, conn, null) ;
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 
195cdf0e10cSrcweir             result = bridge != null ;
196cdf0e10cSrcweir         } catch (com.sun.star.bridge.BridgeExistsException e) {
197cdf0e10cSrcweir             log.println("Exception while bridge creating :" + e) ;
198cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException e) {
199cdf0e10cSrcweir             log.println("Exception while bridge creating :" + e) ;
200cdf0e10cSrcweir         }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir         tRes.tested("createBridge()", result) ;
203cdf0e10cSrcweir     }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir     /**
206cdf0e10cSrcweir     * Gets bridge by name and checks the bridge name returned. <p>
207cdf0e10cSrcweir     * The following method tests are to be executed before :
208cdf0e10cSrcweir     * <ul>
209cdf0e10cSrcweir     * <li> <code>getExestingBridges</code> : to have some bridge name
210cdf0e10cSrcweir     *    to retrieve </li>
211cdf0e10cSrcweir     * </ul> <p>
212cdf0e10cSrcweir     * Has <b>OK</b> status if bridge successfully returned and it's name
213cdf0e10cSrcweir     * equals to name passed as parameter.
214cdf0e10cSrcweir     */
_getBridge()215cdf0e10cSrcweir     public void _getBridge() {
216cdf0e10cSrcweir         executeMethod("getExistingBridges()") ;
217cdf0e10cSrcweir 
218cdf0e10cSrcweir         if (bridgeName == null) {
219cdf0e10cSrcweir             log.println("No name for getting the bridge") ;
220cdf0e10cSrcweir             return ;
221cdf0e10cSrcweir         }
222cdf0e10cSrcweir 
223cdf0e10cSrcweir         XBridge br = oObj.getBridge(bridgeName) ;
224cdf0e10cSrcweir 
225cdf0e10cSrcweir         tRes.tested("getBridge()", br != null &&
226cdf0e10cSrcweir             bridgeName.equals(br.getName())) ;
227cdf0e10cSrcweir     }
228cdf0e10cSrcweir }
229cdf0e10cSrcweir 
230