Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / ifc / connection / _XAcceptor.java
blobf0b5dc850fe0c83700c20d4df63f6de0beb4bfd7
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package ifc.connection;
21 import java.io.PrintWriter;
23 import lib.MultiMethodTest;
24 import lib.StatusException;
26 import com.sun.star.connection.XAcceptor;
27 import com.sun.star.connection.XConnection;
28 import com.sun.star.connection.XConnector;
29 import com.sun.star.uno.UnoRuntime;
30 import com.sun.star.uno.XInterface;
32 /**
33 * Tests methods of <code>XAcceptor</code> interface. <p>
34 * Required relations :
35 * <ul>
36 * <li> <code>'XAcceptor.connectStr'</code> : String variable. Has
37 * the following format :
38 * <code>'socket,host=<SOHost>,port=<UniquePort>' where <SOHost> is
39 * the host where StarOffice is started. This string must be passed
40 * as parameter to <code>accept()</code> method. </li>
41 * <ul> <p>
42 * This test <b>can not</b> be run in multiply threads.
44 public class _XAcceptor extends MultiMethodTest {
46 protected PrintWriter log_ ;
48 /**
49 * Calls <code>accept()</code> method in a separate thread.
50 * Then stores exception thrown by call if it occurred, or
51 * return value.
53 protected class AcceptorThread extends Thread {
54 /**
55 * If exception occurred during method call it is
56 * stored in this field.
58 public Exception ex = null ;
59 private final XAcceptor acc;
60 /**
61 * If method call returns some value it stores in this field.
63 public XConnection acceptedCall = null ;
65 /**
66 * Creates object which can call <code>accept</code> method
67 * of the Acceptor object specified.
69 public AcceptorThread(XAcceptor acc) {
70 this.acc = acc ;
73 /**
74 * Call <code>accept()</code> method.
76 @Override
77 public void run() {
78 try {
79 acceptedCall = acc.accept(connectString) ;
80 } catch (com.sun.star.lang.IllegalArgumentException e) {
81 ex = e ;
82 } catch (com.sun.star.connection.ConnectionSetupException e) {
83 ex = e ;
84 } catch (com.sun.star.connection.AlreadyAcceptingException e) {
85 ex = e ;
90 public XAcceptor oObj = null;
91 protected String connectString = null ;
93 /**
94 * Retrieves object relation.
96 @Override
97 public void before() throws StatusException {
98 connectString = (String)
99 tEnv.getObjRelation("XAcceptor.connectStr") ;
101 log_ = log ;
103 if (connectString == null)
104 throw new StatusException("No object relation found",
105 new NullPointerException()) ;
109 * First part : Thread with acceptor created, and it starts listening.
110 * The main thread tries to connect to acceptor. Acception thread must
111 * return and valid connection must be returned by Acceptor. <p>
113 * Second part : Trying to create second acceptor which listen on
114 * the same port. Calling <code>accept()</code> method of the second
115 * Acceptor must rise appropriate exception. <p>
117 * Has OK status if both test parts executed properly.
119 public void _accept() {
120 boolean result = true ;
121 AcceptorThread acception = null,
122 dupAcception = null ;
123 XAcceptor dupAcceptor = null ;
124 XConnector xConnector = null ;
126 // creating services requierd
127 try {
128 Object oConnector = (tParam.getMSF()).
129 createInstance("com.sun.star.connection.Connector") ;
131 xConnector = UnoRuntime.queryInterface
132 (XConnector.class, oConnector) ;
134 XInterface acceptor = (XInterface) tParam.getMSF().createInstance
135 ("com.sun.star.connection.Acceptor") ;
137 dupAcceptor = UnoRuntime.queryInterface
138 (XAcceptor.class, acceptor) ;
139 } catch (com.sun.star.uno.Exception e) {
140 e.printStackTrace(log) ;
141 throw new StatusException("Can't create service", e) ;
144 // Testing connection to the acceptor
145 try {
146 acception = new AcceptorThread(oObj) ;
147 acception.start() ;
149 util.utils.shortWait();
151 XConnection con = xConnector.connect(connectString) ;
153 if (con == null)
154 log.println("Connector returned : null") ;
155 else
156 log.println("Connector returned : " + con.getDescription()) ;
158 try {
159 acception.join(5 * 1000) ;
160 } catch(InterruptedException e) {}
162 if (acception.isAlive()) {
164 result = false ;
165 log.println("Method call haven't returned") ;
167 if (acception.acceptedCall == null)
168 log.println("Acceptor returned : null") ;
169 else
170 log.println("Acceptor returned : " +
171 acception.acceptedCall.getDescription()) ;
172 } else {
173 if (acception.ex != null) {
174 log.println("Exception occurred in accept() thread :") ;
175 acception.ex.printStackTrace(log) ;
178 if (acception.acceptedCall == null)
179 log.println("Method returned : null") ;
180 else
181 log.println("Method returned : " +
182 acception.acceptedCall.getDescription()) ;
184 result &= acception.acceptedCall != null ;
186 } catch (com.sun.star.connection.ConnectionSetupException e) {
187 e.printStackTrace(log) ;
188 result = false ;
189 } catch (com.sun.star.connection.NoConnectException e) {
190 e.printStackTrace(log) ;
191 result = false ;
192 } finally {
193 oObj.stopAccepting();
194 if (acception.isAlive()) {
195 acception.interrupt();
199 // duplicate acceptor test
200 // creating the additional acceptor which listens
201 // on the same port
203 log.println("___ Testing for accepting on the same port ...") ;
205 try {
206 dupAcception = new AcceptorThread(dupAcceptor) ;
207 dupAcception.start() ;
209 try {
210 dupAcception.join(1 * 1000) ;
211 } catch(InterruptedException e) {}
214 if (dupAcception.isAlive()) {
215 log.println("Duplicate acceptor is listening ...") ;
217 // now trying to accept on the same port as additional
218 // acceptor
219 acception = new AcceptorThread(oObj) ;
220 acception.start() ;
222 try {
223 acception.join(3 * 1000) ;
224 } catch(InterruptedException e) {}
226 if (acception.isAlive()) {
227 oObj.stopAccepting() ;
228 acception.interrupt() ;
230 log.println("Acceptor with the same port must cause"+
231 " an error but didn't") ;
232 result = false ;
233 } else {
234 log.println("Accepted call = " + acception.acceptedCall) ;
235 if (acception.ex == null) {
236 //result = false ;
237 log.println("No exception was thrown when trying"+
238 " to listen on the same port") ;
239 } else {
240 if (acception.ex instanceof
241 com.sun.star.connection.AlreadyAcceptingException ||
242 acception.ex instanceof
243 com.sun.star.connection.ConnectionSetupException) {
245 log.println("Right exception was thrown when trying"+
246 " to listen on the same port") ;
247 } else {
248 result = false ;
249 log.println("Wrong exception was thrown when trying"+
250 " to listen on the same port :") ;
251 acception.ex.printStackTrace(log) ;
256 } finally {
257 dupAcceptor.stopAccepting() ;
258 if (dupAcception.isAlive()) {
259 dupAcception.interrupt() ;
263 tRes.tested("accept()", result) ;
267 * Starts thread with Acceptor and then calls <code>stopListening</code>
268 * method. <p>
269 * Has OK status if <code>accept</code> method successfully returns and
270 * rises no exceptions.
272 public void _stopAccepting() {
273 boolean result = true ;
276 AcceptorThread acception = new AcceptorThread(oObj) ;
278 acception.start() ;
280 oObj.stopAccepting() ;
282 try {
283 acception.join(3 * 1000) ;
284 } catch (InterruptedException e) {}
286 if (acception.isAlive()) {
287 acception.interrupt() ;
289 result = false ;
290 log.println("Method call haven't returned") ;
292 } else {
293 if (acception.ex != null) {
294 log.println("Exception occurred in accept() thread :") ;
295 acception.ex.printStackTrace(log) ;
296 result = false ;
297 } else {
298 result = true ;
301 if (acception.acceptedCall == null)
302 log.println("accept() returned : null") ;
303 else
304 log.println("accept() returned : " +
305 acception.acceptedCall.getDescription()) ;
308 tRes.tested("stopAccepting()", result) ;