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