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 ifc.connection;
25 
26 import java.io.PrintWriter;
27 
28 import lib.MultiMethodTest;
29 import lib.StatusException;
30 
31 import com.sun.star.connection.XAcceptor;
32 import com.sun.star.connection.XConnection;
33 import com.sun.star.connection.XConnector;
34 import com.sun.star.lang.XMultiServiceFactory;
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.uno.XInterface;
37 
38 /**
39 * Tests methods of <code>XAcceptor</code> interface. <p>
40 * Required relations :
41 * <ul>
42 * <li> <code>'XAcceptor.connectStr'</code> : String variable. Has
43 *   the following format :
44 *   <code>'socket,host=<SOHost>,port=<UniquePort>' where <SOHost> is
45 *   the host where StarOffice is started. This string must be passed
46 *   as parameter to <code>accept()</code> method. </li>
47 * <ul> <p>
48 * This test <b>can not</b> be run in multiply threads.
49 */
50 public class _XAcceptor extends MultiMethodTest {
51 
52     protected PrintWriter log_ ;
53 
54     /**
55     * Calls <code>accept()</code> method in a separate thread.
56     * Then stores exception thrown by call if it occurred, or
57     * return value.
58     */
59     protected class AcceptorThread extends Thread {
60         /**
61         * If exception occurred during method call it is
62         * stored in this field.
63         */
64         public Exception ex = null ;
65         private XAcceptor acc = null ;
66         /**
67         * If method call returns some value it stores in this field.
68         */
69         public XConnection acceptedCall = null ;
70 
71         /**
72         * Creates object which can call <code>accept</code> method
73         * of the Acceptor object specified.
74         */
AcceptorThread(XAcceptor acc)75         public AcceptorThread(XAcceptor acc) {
76             this.acc = acc ;
77         }
78 
79         /**
80         * Call <code>accept()</code> method.
81         */
run()82         public void run() {
83             try {
84                 acceptedCall = acc.accept(connectString) ;
85             } catch (com.sun.star.lang.IllegalArgumentException e) {
86                 ex = e ;
87             } catch (com.sun.star.connection.ConnectionSetupException e) {
88                 ex = e ;
89             } catch (com.sun.star.connection.AlreadyAcceptingException e) {
90                 ex = e ;
91             }
92         }
93     }
94 
95     public XAcceptor oObj = null;
96     protected String connectString = null ;
97 
98     /**
99     * Retrieves object relation.
100     */
before()101     public void before() throws StatusException {
102         connectString = (String)
103             tEnv.getObjRelation("XAcceptor.connectStr") ;
104 
105         log_ = log ;
106 
107         if (connectString == null)
108             throw new StatusException("No object relation found",
109                 new NullPointerException()) ;
110     }
111 
112     /**
113     * First part : Thread with acceptor created, and it starts listening.
114     * The main thread tries to connect to acceptor. Acception thread must
115     * return and valid connection must be returned by Acceptor. <p>
116     *
117     * Second part : Trying to create second acceptor which listen on
118     * the same port. Calling <code>accept()</code> method of the second
119     * Acceptor must rise appropriate exception. <p>
120     *
121     * Has OK status if both test parts executed properly.
122     */
_accept()123     public void _accept() {
124         boolean result = true ;
125         AcceptorThread acception = null,
126                        dupAcception = null ;
127         XAcceptor dupAcceptor = null ;
128         XConnector xConnector = null ;
129 
130         // creating services requierd
131         try {
132             Object oConnector = ((XMultiServiceFactory)tParam.getMSF()).
133                 createInstance("com.sun.star.connection.Connector") ;
134 
135             xConnector = (XConnector) UnoRuntime.queryInterface
136                 (XConnector.class, oConnector) ;
137 
138             XInterface acceptor = (XInterface) ((XMultiServiceFactory)
139                 tParam.getMSF()).createInstance
140                 ("com.sun.star.connection.Acceptor") ;
141 
142             dupAcceptor = (XAcceptor) UnoRuntime.queryInterface
143                 (XAcceptor.class, acceptor) ;
144         } catch (com.sun.star.uno.Exception e) {
145             e.printStackTrace(log) ;
146             throw new StatusException("Can't create service", e) ;
147         }
148 
149         // Testing connection to the acceptor
150         try {
151             acception = new AcceptorThread(oObj) ;
152             acception.start() ;
153 
154             try {
155                 Thread.sleep(500);
156             }
157             catch (java.lang.InterruptedException e) {}
158 
159             XConnection con = xConnector.connect(connectString) ;
160 
161             if (con == null)
162                 log.println("Connector returned : null") ;
163             else
164                 log.println("Connector returned : " + con.getDescription()) ;
165 
166             try {
167                 acception.join(5 * 1000) ;
168             } catch(InterruptedException e) {}
169 
170             if (acception.isAlive()) {
171 
172                 result = false ;
173                 log.println("Method call haven't returned") ;
174 
175                 if (acception.acceptedCall == null)
176                     log.println("Acceptor returned : null") ;
177                 else
178                     log.println("Acceptor returned : " +
179                         acception.acceptedCall.getDescription()) ;
180             } else {
181                 if (acception.ex != null) {
182                     log.println("Exception occurred in accept() thread :") ;
183                     acception.ex.printStackTrace(log) ;
184                 }
185 
186                 if (acception.acceptedCall == null)
187                     log.println("Method returned : null") ;
188                 else
189                     log.println("Method returned : " +
190                         acception.acceptedCall.getDescription()) ;
191 
192                 result &= acception.acceptedCall != null ;
193             }
194         } catch (com.sun.star.connection.ConnectionSetupException e) {
195             e.printStackTrace(log) ;
196             result =  false ;
197         } catch (com.sun.star.connection.NoConnectException e) {
198             e.printStackTrace(log) ;
199             result =  false ;
200         } finally {
201             oObj.stopAccepting();
202             if (acception.isAlive()) {
203                 acception.interrupt();
204             }
205         }
206 
207         // duplicate acceptor test
208         // creating the additional acceptor which listens
209         // on the same port
210 
211         log.println("___ Testing for accepting on the same port ...") ;
212 
213         try {
214             dupAcception = new AcceptorThread(dupAcceptor) ;
215             dupAcception.start() ;
216 
217             try {
218                 dupAcception.join(1 * 1000) ;
219             } catch(InterruptedException e) {}
220 
221 
222             if (dupAcception.isAlive()) {
223                 log.println("Duplicate acceptor is listening ...") ;
224 
225                 // now trying to accept on the same port as additional
226                 // acceptor
227                 acception = new AcceptorThread(oObj) ;
228                 acception.start() ;
229 
230                 try {
231                     acception.join(3 * 1000) ;
232                 } catch(InterruptedException e) {}
233 
234                 if (acception.isAlive()) {
235                     oObj.stopAccepting() ;
236                     acception.interrupt() ;
237 
238                     log.println("Acceptor with the same port must cause"+
239                     " an error but didn't") ;
240                     result = false ;
241                 } else {
242                     log.println("Accepted call = " + acception.acceptedCall) ;
243                     if (acception.ex == null) {
244                         //result = false ;
245                         log.println("No exception was thrown when trying"+
246                          " to listen on the same port") ;
247                     } else {
248                         if (acception.ex instanceof
249                             com.sun.star.connection.AlreadyAcceptingException ||
250                             acception.ex instanceof
251                             com.sun.star.connection.ConnectionSetupException) {
252 
253                             log.println("Rigth exception was thrown when trying"+
254                             " to listen on the same port") ;
255                         } else {
256                             result = false ;
257                             log.println("Wrong exception was thrown when trying"+
258                             " to listen on the same port :") ;
259                             acception.ex.printStackTrace(log) ;
260                         }
261                     }
262                 }
263             }
264         } finally {
265             dupAcceptor.stopAccepting() ;
266             if (dupAcception.isAlive()) {
267                 dupAcception.interrupt() ;
268             }
269         }
270 
271         tRes.tested("accept()", result) ;
272     }
273 
274     /**
275     * Starts thread with Acceptor and then calls <code>stopListening</code>
276     * method. <p>
277     * Has OK status if <code>accept</code> method successfully returns and
278     * rises no exceptions.
279     */
_stopAccepting()280     public void _stopAccepting() {
281         boolean result = true ;
282 
283 
284         AcceptorThread acception = new AcceptorThread(oObj) ;
285 
286         acception.start() ;
287 
288         oObj.stopAccepting() ;
289 
290         try {
291             acception.join(3 * 1000) ;
292         } catch (InterruptedException e) {}
293 
294         if (acception.isAlive()) {
295             acception.interrupt() ;
296 
297             result = false ;
298             log.println("Method call haven't returned") ;
299 
300         } else {
301             if (acception.ex != null) {
302                 log.println("Exception occurred in accept() thread :") ;
303                 acception.ex.printStackTrace(log) ;
304                 result = false ;
305             } else {
306                 result = true ;
307             }
308 
309             if (acception.acceptedCall == null)
310                 log.println("accept() returned : null") ;
311             else
312                 log.println("accept() returned : " +
313                     acception.acceptedCall.getDescription()) ;
314         }
315 
316         tRes.tested("stopAccepting()", result) ;
317     }
318 }
319 
320