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._acceptor.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 * Here <code>com.sun.star.connection.Acceptor</code> service is tested.<p>
37 * Test allows to run object tests in several threads concurently.
38 * @see com.sun.star.connection.Acceptor
39 * @see com.sun.star.connection.XAcceptor
40 * @see com.sun.star.connection.XConnector
41 * @see ifc.connection._XAcceptor
42 */
43 public class Acceptor extends TestCase {
44     /**
45     * Acceptor chooses the first port after <code>basePort</code>
46     * which is free.
47     */
48     protected static final int basePort = 10000;
49     private int curPort ;
50     private static String sOfficeHost = null ;
51 
52     /**
53     * Retrieves host name where StarOffice is started from test
54     * parameter <code>'CNCSTR'</code>.
55     */
initialize( TestParameters tParam, PrintWriter log )56     public void initialize( TestParameters tParam, PrintWriter log ) {
57         String cncstr = (String) tParam.get("CNCSTR") ;
58         int idx = cncstr.indexOf("host=") + 5 ;
59         sOfficeHost = cncstr.substring(idx, cncstr.indexOf(",", idx)) ;
60     }
61 
62     /**
63      * Creating a Testenvironment for the interfaces to be tested. <p>
64      * Creates <code>Acceptor</code> service and passed as relation
65      * connection string where port for accepting is unique among
66      * different object test threads. <p>
67      * The following object relations are created :
68      * <ul>
69      * <li> <code>'XAcceptor.connectStr'</code> : String variable for
70      *   <code>XAcceptor</code> interface test. Has the following format :
71      *   <code>'socket,host=<SOHost>,port=<UniquePort>' where <SOHost> is
72      *   the host where StarOffice is started. </li>
73      * <li> <code>'Acceptor.Port'</code> : Integer value which specifies
74      *   port on which Acceptor must listen, and which is required
75      *   when disposing environment, to free this port number. </li>
76      * <ul>
77      */
createTestEnvironment(TestParameters Param, PrintWriter log)78     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
79 
80         XInterface oObj = null;
81         XInterface acceptor = null;
82 
83         try {
84             acceptor = (XInterface)
85                 ((XMultiServiceFactory)Param.getMSF()).createInstance
86                 ("com.sun.star.connection.Acceptor");
87         } catch (com.sun.star.uno.Exception e) {
88             throw new StatusException("Can't create object environment", e) ;
89         }
90 
91         // select the port
92         curPort = utils.getNextFreePort(basePort);
93         log.println("Choose Port nr: " + curPort);
94         oObj = acceptor;
95 
96         TestEnvironment tEnv = new TestEnvironment(oObj) ;
97 
98         // adding connection string as relation
99         tEnv.addObjRelation("XAcceptor.connectStr",
100             "socket,host=" + sOfficeHost + ",port=" + curPort) ;
101 
102         // adding port number for freeing it.
103         tEnv.addObjRelation("Acceptor.Port", new Integer(curPort)) ;
104 
105         return tEnv ;
106     }
107 
108     /**
109     * Just clears flag which indicates that port is free now.
110     */
disposeTestEnvironment( TestEnvironment tEnv, TestParameters tParam)111     public synchronized void disposeTestEnvironment( TestEnvironment tEnv,
112             TestParameters tParam) {
113 
114         curPort = ((Integer)tEnv.getObjRelation("Acceptor.Port")).intValue();
115     }
116 }
117 
118 
119