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 ifc.connection;
29 
30 import lib.MultiMethodTest;
31 import lib.StatusException;
32 
33 import com.sun.star.connection.XAcceptor;
34 import com.sun.star.connection.XConnection;
35 import com.sun.star.connection.XConnector;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.uno.XInterface;
39 
40 /**
41 * Tests methods of <code>XConnector</code> interface. <p>
42 * Required relations :
43 * <ul>
44 * <li> <code>'XConnector.connectStr'</code> : String variable. Has
45 *   the following format :
46 *   <code>'socket,host=<SOHost>,port=<UniquePort>' where <SOHost> is
47 *   the host where StarOffice is started. This string must be passed
48 *   as parameter to <code>accept()</code> method. </li>
49 * <ul> <p>
50 * This test <b>can not</b> be run in multiply threads.
51 */
52 public class _XConnector extends MultiMethodTest {
53 
54     /**
55     * Calls <code>accept()</code> method in a separate thread.
56     * Then stores exception thrown by call if it occured, or
57     * return value.
58     */
59     protected class AcceptorThread extends Thread {
60         /**
61          * the acceptor
62          */
63         private XAcceptor acc = null ;
64         /**
65         * If exception occured during method call it is
66         * stored in this field.
67         */
68         public Exception ex = null ;
69         /**
70         * If method call returns some value it stores in this field.
71         */
72         public XConnection acceptedCall = null ;
73 
74         /**
75         * Gets an object which can call <code>accept</code> method.
76         */
77         public AcceptorThread(XAcceptor acc) {
78             this.acc = acc ;
79         }
80 
81         /**
82         * Call <code>accept()</code> method.
83         */
84         public void run() {
85             try {
86                 acceptedCall = acc.accept(connectString) ;
87             } catch (com.sun.star.lang.IllegalArgumentException e) {
88                 ex = e ;
89             } catch (com.sun.star.connection.ConnectionSetupException e) {
90                 ex = e ;
91             } catch (com.sun.star.connection.AlreadyAcceptingException e) {
92                 ex = e ;
93             }
94         }
95     }
96 
97     public XConnector oObj = null;
98     protected String connectString = null ;
99 
100     /**
101     * Retrieves object relation.
102     */
103     public void before() throws StatusException {
104         connectString = (String)
105             tEnv.getObjRelation("XConnector.connectStr") ;
106         if (connectString == null)
107             throw new StatusException("No object relation found",
108                 new NullPointerException()) ;
109     }
110 
111     /**
112     * Thread with acceptor is created, and it starts listening.
113     * The main thread tries to connect to acceptor. Acception thread must
114     * return and a valid connection must be returned by Acceptor. <p>
115     *
116     */
117     public void _connect() {
118         boolean result = true ;
119         AcceptorThread acceptorThread = null;
120         XAcceptor xAcceptor = null ;
121         XConnection aCon = null;
122         XInterface x = null;
123 
124         // create the acceptor
125         try {
126             x = (XInterface) (
127                 (XMultiServiceFactory)tParam.getMSF()).createInstance
128                 ("com.sun.star.connection.Acceptor") ;
129         } catch (com.sun.star.uno.Exception e) {
130             e.printStackTrace(log) ;
131             throw new StatusException("Can't create service", e) ;
132         }
133 
134         xAcceptor = (XAcceptor)UnoRuntime.queryInterface(XAcceptor.class, x);
135 
136         acceptorThread = new AcceptorThread(xAcceptor) ;
137         acceptorThread.start() ;
138 
139         try {
140             Thread.sleep(500);
141         }
142         catch (java.lang.InterruptedException e) {}
143 
144         // connect to acceptor
145         try {
146             aCon = oObj.connect(connectString);
147 
148             if (aCon == null)
149                 log.println("Connector returned: null") ;
150             else
151                 log.println("Connector returned: " + aCon.getDescription()) ;
152 
153             try {
154                 acceptorThread.join(30 * 1000) ;
155             } catch(InterruptedException e) {}
156 
157             // connection not established
158             if (acceptorThread.isAlive()) {
159 
160                 result = false ;
161                 log.println("Method call hasn't returned") ;
162 
163                 if (acceptorThread.acceptedCall == null)
164                     log.println("Acceptor returned : null") ;
165                 else
166                     log.println("Acceptor returned : " +
167                         acceptorThread.acceptedCall.getDescription()) ;
168             } else {
169                 if (acceptorThread.ex != null) {
170                     log.println("Exception occured in accept() thread :") ;
171                     acceptorThread.ex.printStackTrace(log) ;
172                 }
173 
174                 if (acceptorThread.acceptedCall == null)
175                     log.println("Method returned : null") ;
176                 else
177                     log.println("Method returned : " +
178                         acceptorThread.acceptedCall.getDescription()) ;
179 
180                 result &= acceptorThread.acceptedCall != null ;
181             }
182         } catch (com.sun.star.connection.ConnectionSetupException e) {
183             e.printStackTrace(log) ;
184             result =  false ;
185         } catch (com.sun.star.connection.NoConnectException e) {
186             e.printStackTrace(log) ;
187             result =  false ;
188         } finally {
189             acceptorThread.acc.stopAccepting();
190             if (acceptorThread.isAlive()) {
191                 acceptorThread.interrupt();
192             }
193         }
194 
195         tRes.tested("connect()", result) ;
196     }
197 }
198 
199