Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / qadevOOo / tests / java / ifc / connection / _XAcceptor.java
blobb9e43d8eca697dcad0535ff4fdb2bf8b2ceae5d8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 package ifc.connection;
30 import java.io.PrintWriter;
32 import lib.MultiMethodTest;
33 import lib.StatusException;
35 import com.sun.star.connection.XAcceptor;
36 import com.sun.star.connection.XConnection;
37 import com.sun.star.connection.XConnector;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.uno.XInterface;
42 /**
43 * Tests methods of <code>XAcceptor</code> interface. <p>
44 * Required relations :
45 * <ul>
46 * <li> <code>'XAcceptor.connectStr'</code> : String variable. Has
47 * the following format :
48 * <code>'socket,host=<SOHost>,port=<UniquePort>' where <SOHost> is
49 * the host where StarOffice is started. This string must be passed
50 * as parameter to <code>accept()</code> method. </li>
51 * <ul> <p>
52 * This test <b>can not</b> be run in multiply threads.
54 public class _XAcceptor extends MultiMethodTest {
56 protected PrintWriter log_ ;
58 /**
59 * Calls <code>accept()</code> method in a separate thread.
60 * Then stores exception thrown by call if it occurred, or
61 * return value.
63 protected class AcceptorThread extends Thread {
64 /**
65 * If exception occurred during method call it is
66 * stored in this field.
68 public Exception ex = null ;
69 private XAcceptor acc = null ;
70 /**
71 * If method call returns some value it stores in this field.
73 public XConnection acceptedCall = null ;
75 /**
76 * Creates object which can call <code>accept</code> method
77 * of the Acceptor object specified.
79 public AcceptorThread(XAcceptor acc) {
80 this.acc = acc ;
83 /**
84 * Call <code>accept()</code> method.
86 public void run() {
87 try {
88 acceptedCall = acc.accept(connectString) ;
89 } catch (com.sun.star.lang.IllegalArgumentException e) {
90 ex = e ;
91 } catch (com.sun.star.connection.ConnectionSetupException e) {
92 ex = e ;
93 } catch (com.sun.star.connection.AlreadyAcceptingException e) {
94 ex = e ;
99 public XAcceptor oObj = null;
100 protected String connectString = null ;
103 * Retrieves object relation.
105 public void before() throws StatusException {
106 connectString = (String)
107 tEnv.getObjRelation("XAcceptor.connectStr") ;
109 log_ = log ;
111 if (connectString == null)
112 throw new StatusException("No object relation found",
113 new NullPointerException()) ;
117 * First part : Thread with acceptor created, and it starts listening.
118 * The main thread tries to connect to acceptor. Acception thread must
119 * return and valid connection must be returned by Acceptor. <p>
121 * Second part : Trying to create second acceptor which listen on
122 * the same port. Calling <code>accept()</code> method of the second
123 * Acceptor must rise appropriate exception. <p>
125 * Has OK status if both test parts executed properly.
127 public void _accept() {
128 boolean result = true ;
129 AcceptorThread acception = null,
130 dupAcception = null ;
131 XAcceptor dupAcceptor = null ;
132 XConnector xConnector = null ;
134 // creating services requierd
135 try {
136 Object oConnector = ((XMultiServiceFactory)tParam.getMSF()).
137 createInstance("com.sun.star.connection.Connector") ;
139 xConnector = (XConnector) UnoRuntime.queryInterface
140 (XConnector.class, oConnector) ;
142 XInterface acceptor = (XInterface) ((XMultiServiceFactory)
143 tParam.getMSF()).createInstance
144 ("com.sun.star.connection.Acceptor") ;
146 dupAcceptor = (XAcceptor) UnoRuntime.queryInterface
147 (XAcceptor.class, acceptor) ;
148 } catch (com.sun.star.uno.Exception e) {
149 e.printStackTrace(log) ;
150 throw new StatusException("Can't create service", e) ;
153 // Testing connection to the acceptor
154 try {
155 acception = new AcceptorThread(oObj) ;
156 acception.start() ;
158 try {
159 Thread.sleep(500);
161 catch (java.lang.InterruptedException e) {}
163 XConnection con = xConnector.connect(connectString) ;
165 if (con == null)
166 log.println("Connector returned : null") ;
167 else
168 log.println("Connector returned : " + con.getDescription()) ;
170 try {
171 acception.join(5 * 1000) ;
172 } catch(InterruptedException e) {}
174 if (acception.isAlive()) {
176 result = false ;
177 log.println("Method call haven't returned") ;
179 if (acception.acceptedCall == null)
180 log.println("Acceptor returned : null") ;
181 else
182 log.println("Acceptor returned : " +
183 acception.acceptedCall.getDescription()) ;
184 } else {
185 if (acception.ex != null) {
186 log.println("Exception occurred in accept() thread :") ;
187 acception.ex.printStackTrace(log) ;
190 if (acception.acceptedCall == null)
191 log.println("Method returned : null") ;
192 else
193 log.println("Method returned : " +
194 acception.acceptedCall.getDescription()) ;
196 result &= acception.acceptedCall != null ;
198 } catch (com.sun.star.connection.ConnectionSetupException e) {
199 e.printStackTrace(log) ;
200 result = false ;
201 } catch (com.sun.star.connection.NoConnectException e) {
202 e.printStackTrace(log) ;
203 result = false ;
204 } finally {
205 oObj.stopAccepting();
206 if (acception.isAlive()) {
207 acception.interrupt();
211 // duplicate acceptor test
212 // creating the additional acceptor which listens
213 // on the same port
215 log.println("___ Testing for accepting on the same port ...") ;
217 try {
218 dupAcception = new AcceptorThread(dupAcceptor) ;
219 dupAcception.start() ;
221 try {
222 dupAcception.join(1 * 1000) ;
223 } catch(InterruptedException e) {}
226 if (dupAcception.isAlive()) {
227 log.println("Duplicate acceptor is listening ...") ;
229 // now trying to accept on the same port as additional
230 // acceptor
231 acception = new AcceptorThread(oObj) ;
232 acception.start() ;
234 try {
235 acception.join(3 * 1000) ;
236 } catch(InterruptedException e) {}
238 if (acception.isAlive()) {
239 oObj.stopAccepting() ;
240 acception.interrupt() ;
242 log.println("Acceptor with the same port must cause"+
243 " an error but didn't") ;
244 result = false ;
245 } else {
246 log.println("Accepted call = " + acception.acceptedCall) ;
247 if (acception.ex == null) {
248 //result = false ;
249 log.println("No exception was thrown when trying"+
250 " to listen on the same port") ;
251 } else {
252 if (acception.ex instanceof
253 com.sun.star.connection.AlreadyAcceptingException ||
254 acception.ex instanceof
255 com.sun.star.connection.ConnectionSetupException) {
257 log.println("Rigth exception was thrown when trying"+
258 " to listen on the same port") ;
259 } else {
260 result = false ;
261 log.println("Wrong exception was thrown when trying"+
262 " to listen on the same port :") ;
263 acception.ex.printStackTrace(log) ;
268 } finally {
269 dupAcceptor.stopAccepting() ;
270 if (dupAcception.isAlive()) {
271 dupAcception.interrupt() ;
275 tRes.tested("accept()", result) ;
279 * Starts thread with Acceptor and then calls <code>stopListening</code>
280 * method. <p>
281 * Has OK status if <code>accept</code> method successfully returns and
282 * rises no exceptions.
284 public void _stopAccepting() {
285 boolean result = true ;
288 AcceptorThread acception = new AcceptorThread(oObj) ;
290 acception.start() ;
292 oObj.stopAccepting() ;
294 try {
295 acception.join(3 * 1000) ;
296 } catch (InterruptedException e) {}
298 if (acception.isAlive()) {
299 acception.interrupt() ;
301 result = false ;
302 log.println("Method call haven't returned") ;
304 } else {
305 if (acception.ex != null) {
306 log.println("Exception occurred in accept() thread :") ;
307 acception.ex.printStackTrace(log) ;
308 result = false ;
309 } else {
310 result = true ;
313 if (acception.acceptedCall == null)
314 log.println("accept() returned : null") ;
315 else
316 log.println("accept() returned : " +
317 acception.acceptedCall.getDescription()) ;
320 tRes.tested("stopAccepting()", result) ;