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