1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package mod._acceptor;
29 
30 import java.io.PrintWriter;
31 
32 import lib.StatusException;
33 import lib.TestCase;
34 import lib.TestEnvironment;
35 import lib.TestParameters;
36 import util.utils;
37 
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.uno.XInterface;
40 
41 /**
42 * Here <code>com.sun.star.connection.Acceptor</code> service is tested.<p>
43 * Test allows to run object tests in several threads concurently.
44 * @see com.sun.star.connection.Acceptor
45 * @see com.sun.star.connection.XAcceptor
46 * @see com.sun.star.connection.XConnector
47 * @see ifc.connection._XAcceptor
48 */
49 public class Acceptor extends TestCase {
50 
51 
52     /**
53     * Acceptor chooses the first port after <code>basePort</code>
54     * which is free.
55     */
56     protected static final int basePort = 10000;
57     private int curPort ;
58     private static String sOfficeHost = null ;
59 
60     /**
61     * Retrieves host name where StarOffice is started from test
62     * parameter <code>'CNCSTR'</code>.
63     */
64     public void initialize( TestParameters tParam, PrintWriter log ) {
65         String cncstr = (String) tParam.get("CNCSTR") ;
66         int idx = cncstr.indexOf("host=") + 5 ;
67         sOfficeHost = cncstr.substring(idx, cncstr.indexOf(",", idx)) ;
68     }
69 
70     /**
71      * Creating a Testenvironment for the interfaces to be tested. <p>
72      * Creates <code>Acceptor</code> service and passed as relation
73      * connection string where port for accepting is unique among
74      * different object test threads. <p>
75      * The following object relations are created :
76      * <ul>
77      * <li> <code>'XAcceptor.connectStr'</code> : String variable for
78      *   <code>XAcceptor</code> interface test. Has the following format :
79      *   <code>'socket,host=<SOHost>,port=<UniquePort>' where <SOHost> is
80      *   the host where StarOffice is started. </li>
81      * <li> <code>'Acceptor.Port'</code> : Integer value which specifies
82      *   port on which Acceptor must listen, and which is required
83      *   when disposing environment, to free this port number. </li>
84      * <ul>
85      */
86     public synchronized TestEnvironment createTestEnvironment(
87         TestParameters Param, PrintWriter log ) throws StatusException {
88 
89         XInterface oObj = null;
90         XInterface acceptor = null;
91 
92         try {
93             acceptor = (XInterface)
94                 ((XMultiServiceFactory)Param.getMSF()).createInstance
95                 ("com.sun.star.connection.Acceptor");
96         } catch (com.sun.star.uno.Exception e) {
97             throw new StatusException("Can't create object environment", e) ;
98         }
99 
100         // select the port
101         curPort = utils.getNextFreePort(basePort);
102         log.println("Choose Port nr: " + curPort);
103         oObj = acceptor;
104 
105         TestEnvironment tEnv = new TestEnvironment(oObj) ;
106 
107         // adding connection string as relation
108         tEnv.addObjRelation("XAcceptor.connectStr",
109             "socket,host=" + sOfficeHost + ",port=" + curPort) ;
110 
111         // adding port number for freeing it.
112         tEnv.addObjRelation("Acceptor.Port", new Integer(curPort)) ;
113 
114         return tEnv ;
115     }
116 
117     /**
118     * Just clears flag which indicates that port is free now.
119     */
120     public synchronized void disposeTestEnvironment( TestEnvironment tEnv,
121             TestParameters tParam) {
122 
123         curPort = ((Integer)tEnv.getObjRelation("Acceptor.Port")).intValue();
124     }
125 }
126 
127 
128