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