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._connector.uno;
29 
30 import com.sun.star.uno.XInterface;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import java.io.PrintWriter;
33 import lib.StatusException;
34 import lib.TestCase;
35 import lib.TestEnvironment;
36 import lib.TestParameters;
37 import util.utils;
38 
39 /**
40 * Test for object which is represented by service
41 * <code>com.sun.star.connection.Connector</code>. <p>
42 * Object implements the following interfaces :
43 * <ul>
44 *  <li> <code>com::sun::star::connection::XConnector</code></li>
45 * </ul>
46 * Can be run in several threads.
47 * @see com.sun.star.connection.XConnector
48 * @see ifc.connection._XConnector
49 */
50 public class Connector extends TestCase {
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     protected 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     * Does nothing.
72     */
73     protected void cleanup( TestParameters tParam, PrintWriter log ) {
74 
75     }
76 
77     /**
78     * Creating a Testenvironment for the interfaces to be tested.
79     * Just creates service <code>com.sun.star.connection.Connector</code>
80     */
81     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
82 
83         XInterface oObj = null ;
84 
85         try {
86             XInterface connector = (XInterface)
87                 ((XMultiServiceFactory)Param.getMSF()).createInstance
88                                     ("com.sun.star.connection.Connector") ;
89 
90             oObj = connector ;
91         } catch (com.sun.star.uno.Exception e) {
92             e.printStackTrace(log);
93             throw new StatusException("Can't create object environment", e);
94         }
95 
96         TestEnvironment tEnv = new TestEnvironment(oObj) ;
97 
98         // select the port
99         curPort = utils.getNextFreePort(basePort);
100         log.println("Choose Port nr: " + curPort);
101 
102         // adding connection string as relation
103         tEnv.addObjRelation("XConnector.connectStr",
104             "socket,host=" + sOfficeHost + ",port=" + curPort) ;
105 
106         // adding port number for freeing it.
107         tEnv.addObjRelation("Connector.Port", new Integer(curPort)) ;
108 
109         return tEnv ;
110     }
111 
112     /**
113     * Just clears flag which indicates that port is free now.
114     */
115     public synchronized void disposeTestEnvironment( TestEnvironment tEnv,
116             TestParameters tParam) {
117 
118         curPort = ((Integer)tEnv.getObjRelation("Connector.Port")).intValue();
119     }
120 }
121 
122 
123