Update ooo320-m1
[ooovba.git] / qadevOOo / tests / java / ifc / connection / _XAcceptor.java
blobea920611ab1418ee195e86d1c0bf2e6fa7fd9363
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XAcceptor.java,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package ifc.connection;
33 import java.io.PrintWriter;
35 import lib.MultiMethodTest;
36 import lib.StatusException;
38 import com.sun.star.connection.XAcceptor;
39 import com.sun.star.connection.XConnection;
40 import com.sun.star.connection.XConnector;
41 import com.sun.star.lang.XMultiServiceFactory;
42 import com.sun.star.uno.UnoRuntime;
43 import com.sun.star.uno.XInterface;
45 /**
46 * Tests methods of <code>XAcceptor</code> interface. <p>
47 * Required relations :
48 * <ul>
49 * <li> <code>'XAcceptor.connectStr'</code> : String variable. Has
50 * the following format :
51 * <code>'socket,host=<SOHost>,port=<UniquePort>' where <SOHost> is
52 * the host where StarOffice is started. This string must be passed
53 * as parameter to <code>accept()</code> method. </li>
54 * <ul> <p>
55 * This test <b>can not</b> be run in multiply threads.
57 public class _XAcceptor extends MultiMethodTest {
59 protected PrintWriter log_ ;
61 /**
62 * Calls <code>accept()</code> method in a separate thread.
63 * Then stores exception thrown by call if it occured, or
64 * return value.
66 protected class AcceptorThread extends Thread {
67 /**
68 * If exception occured during method call it is
69 * stored in this field.
71 public Exception ex = null ;
72 private XAcceptor acc = null ;
73 /**
74 * If method call returns some value it stores in this field.
76 public XConnection acceptedCall = null ;
78 /**
79 * Creates object which can call <code>accept</code> method
80 * of the Acceptor object specified.
82 public AcceptorThread(XAcceptor acc) {
83 this.acc = acc ;
86 /**
87 * Call <code>accept()</code> method.
89 public void run() {
90 try {
91 acceptedCall = acc.accept(connectString) ;
92 } catch (com.sun.star.lang.IllegalArgumentException e) {
93 ex = e ;
94 } catch (com.sun.star.connection.ConnectionSetupException e) {
95 ex = e ;
96 } catch (com.sun.star.connection.AlreadyAcceptingException e) {
97 ex = e ;
102 public XAcceptor oObj = null;
103 protected String connectString = null ;
106 * Retrieves object relation.
108 public void before() throws StatusException {
109 connectString = (String)
110 tEnv.getObjRelation("XAcceptor.connectStr") ;
112 log_ = log ;
114 if (connectString == null)
115 throw new StatusException("No object relation found",
116 new NullPointerException()) ;
120 * First part : Thread with acceptor created, and it starts listening.
121 * The main thread tries to connect to acceptor. Acception thread must
122 * return and valid connection must be returned by Acceptor. <p>
124 * Second part : Trying to create second acceptor which listen on
125 * the same port. Calling <code>accept()</code> method of the second
126 * Acceptor must rise appropriate exception. <p>
128 * Has OK status if both test parts executed properly.
130 public void _accept() {
131 boolean result = true ;
132 AcceptorThread acception = null,
133 dupAcception = null ;
134 XAcceptor dupAcceptor = null ;
135 XConnector xConnector = null ;
137 // creating services requierd
138 try {
139 Object oConnector = ((XMultiServiceFactory)tParam.getMSF()).
140 createInstance("com.sun.star.connection.Connector") ;
142 xConnector = (XConnector) UnoRuntime.queryInterface
143 (XConnector.class, oConnector) ;
145 XInterface acceptor = (XInterface) ((XMultiServiceFactory)
146 tParam.getMSF()).createInstance
147 ("com.sun.star.connection.Acceptor") ;
149 dupAcceptor = (XAcceptor) UnoRuntime.queryInterface
150 (XAcceptor.class, acceptor) ;
151 } catch (com.sun.star.uno.Exception e) {
152 e.printStackTrace(log) ;
153 throw new StatusException("Can't create service", e) ;
156 // Testing connection to the acceptor
157 try {
158 acception = new AcceptorThread(oObj) ;
159 acception.start() ;
161 try {
162 Thread.sleep(500);
164 catch (java.lang.InterruptedException e) {}
166 XConnection con = xConnector.connect(connectString) ;
168 if (con == null)
169 log.println("Connector returned : null") ;
170 else
171 log.println("Connector returned : " + con.getDescription()) ;
173 try {
174 acception.join(5 * 1000) ;
175 } catch(InterruptedException e) {}
177 if (acception.isAlive()) {
179 result = false ;
180 log.println("Method call haven't returned") ;
182 if (acception.acceptedCall == null)
183 log.println("Acceptor returned : null") ;
184 else
185 log.println("Acceptor returned : " +
186 acception.acceptedCall.getDescription()) ;
187 } else {
188 if (acception.ex != null) {
189 log.println("Exception occured in accept() thread :") ;
190 acception.ex.printStackTrace(log) ;
193 if (acception.acceptedCall == null)
194 log.println("Method returned : null") ;
195 else
196 log.println("Method returned : " +
197 acception.acceptedCall.getDescription()) ;
199 result &= acception.acceptedCall != null ;
201 } catch (com.sun.star.connection.ConnectionSetupException e) {
202 e.printStackTrace(log) ;
203 result = false ;
204 } catch (com.sun.star.connection.NoConnectException e) {
205 e.printStackTrace(log) ;
206 result = false ;
207 } finally {
208 oObj.stopAccepting();
209 if (acception.isAlive()) {
210 acception.interrupt();
214 // duplicate acceptor test
215 // creating the additional acceptor which listens
216 // on the same port
218 log.println("___ Testing for accepting on the same port ...") ;
220 try {
221 dupAcception = new AcceptorThread(dupAcceptor) ;
222 dupAcception.start() ;
224 try {
225 dupAcception.join(1 * 1000) ;
226 } catch(InterruptedException e) {}
229 if (dupAcception.isAlive()) {
230 log.println("Duplicate acceptor is listening ...") ;
232 // now trying to accept on the same port as additional
233 // acceptor
234 acception = new AcceptorThread(oObj) ;
235 acception.start() ;
237 try {
238 acception.join(3 * 1000) ;
239 } catch(InterruptedException e) {}
241 if (acception.isAlive()) {
242 oObj.stopAccepting() ;
243 acception.interrupt() ;
245 log.println("Acceptor with the same port must cause"+
246 " an error but didn't") ;
247 result = false ;
248 } else {
249 log.println("Accepted call = " + acception.acceptedCall) ;
250 if (acception.ex == null) {
251 //result = false ;
252 log.println("No exception was thrown when trying"+
253 " to listen on the same port") ;
254 } else {
255 if (acception.ex instanceof
256 com.sun.star.connection.AlreadyAcceptingException ||
257 acception.ex instanceof
258 com.sun.star.connection.ConnectionSetupException) {
260 log.println("Rigth exception was thrown when trying"+
261 " to listen on the same port") ;
262 } else {
263 result = false ;
264 log.println("Wrong exception was thrown when trying"+
265 " to listen on the same port :") ;
266 acception.ex.printStackTrace(log) ;
271 } finally {
272 dupAcceptor.stopAccepting() ;
273 if (dupAcception.isAlive()) {
274 dupAcception.interrupt() ;
278 tRes.tested("accept()", result) ;
282 * Starts thread with Acceptor and then calls <code>stopListening</code>
283 * method. <p>
284 * Has OK status if <code>accept</code> method successfully returns and
285 * rises no exceptions.
287 public void _stopAccepting() {
288 boolean result = true ;
291 AcceptorThread acception = new AcceptorThread(oObj) ;
293 acception.start() ;
295 oObj.stopAccepting() ;
297 try {
298 acception.join(3 * 1000) ;
299 } catch (InterruptedException e) {}
301 if (acception.isAlive()) {
302 acception.interrupt() ;
304 result = false ;
305 log.println("Method call haven't returned") ;
307 } else {
308 if (acception.ex != null) {
309 log.println("Exception occured in accept() thread :") ;
310 acception.ex.printStackTrace(log) ;
311 result = false ;
312 } else {
313 result = true ;
316 if (acception.acceptedCall == null)
317 log.println("accept() returned : null") ;
318 else
319 log.println("accept() returned : " +
320 acception.acceptedCall.getDescription()) ;
323 tRes.tested("stopAccepting()", result) ;