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 mod._bridgefac.uno;
25 
26 import com.sun.star.uno.XInterface;
27 import com.sun.star.lang.XMultiServiceFactory;
28 import java.io.PrintWriter;
29 import lib.StatusException;
30 import lib.TestCase;
31 import lib.TestEnvironment;
32 import lib.TestParameters;
33 import util.utils;
34 
35 /**
36 * Tests <code>com.sun.star.bridge.BridgeFactory</code>
37 * service. <p>
38 * @see com.sun.star.bridge.XBridgeFactory
39 * @see com.sun.star.lang.XComponent
40 * @see ifc.bridge._XBridgeFactory
41 * @see ifc.lang._XComponent
42 */
43 public class BridgeFactory extends TestCase {
44 
45     /**
46     * Retrieves host name where StarOffice is started from test
47     * parameter <code>'CNCSTR'</code>.
48     */
initialize( TestParameters tParam, PrintWriter log )49     protected void initialize( TestParameters tParam, PrintWriter log ) {
50         String cncstr = (String) tParam.get("CNCSTR") ;
51         int idx = cncstr.indexOf("host=") + 5 ;
52         sOfficeHost = cncstr.substring(idx, cncstr.indexOf(",", idx)) ;
53     }
54 
cleanup( TestParameters tParam, PrintWriter log )55     protected void cleanup( TestParameters tParam, PrintWriter log ) {
56     }
57 
58     /**
59     * Acceptor chooses the first port after <code>basePort</code>
60     * which is free.
61     */
62     protected static final int basePort = 50003 ;
63     private int curPort ;
64     private static String sOfficeHost = null ;
65 
66     /**
67      * Creating a Testenvironment for the interfaces to be tested.
68      * Just creates <code>com.sun.star.bridge.BridgeFactory</code>
69      * service as object to be tested.
70      */
createTestEnvironment(TestParameters Param, PrintWriter log)71     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
72 
73         XInterface oObj = null ;
74 
75         try {
76             oObj = (XInterface)
77                     ((XMultiServiceFactory)Param.getMSF()).createInstance
78                     ("com.sun.star.bridge.BridgeFactory") ;
79         } catch (com.sun.star.uno.Exception e) {
80             throw new StatusException("Can't create object environment", e) ;
81         }
82 
83         if (oObj == null)
84             throw new StatusException("Can't create service",
85                 new NullPointerException());
86 
87         TestEnvironment tEnv = new TestEnvironment(oObj) ;
88 
89         // select the port
90         curPort = utils.getNextFreePort(basePort);
91         log.println("Choose Port nr: " + curPort);
92 
93         // adding connection string as relation
94         tEnv.addObjRelation("CNNCTSTR",
95             "socket,host=" + sOfficeHost + ",port=" + curPort) ;
96 
97         // adding port number for freeing it.
98         tEnv.addObjRelation("Connector.Port", new Integer(curPort)) ;
99 
100         return tEnv ;
101     }
102 
103     /**
104     * Just clears flag which indicates that port is free now.
105     */
disposeTestEnvironment( TestEnvironment tEnv, TestParameters tParam)106     public synchronized void disposeTestEnvironment( TestEnvironment tEnv,
107             TestParameters tParam) {
108         curPort = ((Integer)tEnv.getObjRelation("Connector.Port")).intValue() ;
109     }
110 }
111 
112 
113