update credits
[LibreOffice.git] / qadevOOo / tests / java / ifc / connection / _XAcceptor.java
blob9a066723da4e7d932b9e7b8edb9e5fec9bb5e6c1
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.lang.XMultiServiceFactory;
30 import com.sun.star.uno.UnoRuntime;
31 import com.sun.star.uno.XInterface;
33 /**
34 * Tests methods of <code>XAcceptor</code> interface. <p>
35 * Required relations :
36 * <ul>
37 * <li> <code>'XAcceptor.connectStr'</code> : String variable. Has
38 * the following format :
39 * <code>'socket,host=<SOHost>,port=<UniquePort>' where <SOHost> is
40 * the host where StarOffice is started. This string must be passed
41 * as parameter to <code>accept()</code> method. </li>
42 * <ul> <p>
43 * This test <b>can not</b> be run in multiply threads.
45 public class _XAcceptor extends MultiMethodTest {
47 protected PrintWriter log_ ;
49 /**
50 * Calls <code>accept()</code> method in a separate thread.
51 * Then stores exception thrown by call if it occurred, or
52 * return value.
54 protected class AcceptorThread extends Thread {
55 /**
56 * If exception occurred during method call it is
57 * stored in this field.
59 public Exception ex = null ;
60 private XAcceptor acc = null ;
61 /**
62 * If method call returns some value it stores in this field.
64 public XConnection acceptedCall = null ;
66 /**
67 * Creates object which can call <code>accept</code> method
68 * of the Acceptor object specified.
70 public AcceptorThread(XAcceptor acc) {
71 this.acc = acc ;
74 /**
75 * Call <code>accept()</code> method.
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 public void before() throws StatusException {
97 connectString = (String)
98 tEnv.getObjRelation("XAcceptor.connectStr") ;
100 log_ = log ;
102 if (connectString == null)
103 throw new StatusException("No object relation found",
104 new NullPointerException()) ;
108 * First part : Thread with acceptor created, and it starts listening.
109 * The main thread tries to connect to acceptor. Acception thread must
110 * return and valid connection must be returned by Acceptor. <p>
112 * Second part : Trying to create second acceptor which listen on
113 * the same port. Calling <code>accept()</code> method of the second
114 * Acceptor must rise appropriate exception. <p>
116 * Has OK status if both test parts executed properly.
118 public void _accept() {
119 boolean result = true ;
120 AcceptorThread acception = null,
121 dupAcception = null ;
122 XAcceptor dupAcceptor = null ;
123 XConnector xConnector = null ;
125 // creating services requierd
126 try {
127 Object oConnector = ((XMultiServiceFactory)tParam.getMSF()).
128 createInstance("com.sun.star.connection.Connector") ;
130 xConnector = UnoRuntime.queryInterface
131 (XConnector.class, oConnector) ;
133 XInterface acceptor = (XInterface) ((XMultiServiceFactory)
134 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 try {
150 Thread.sleep(500);
152 catch (java.lang.InterruptedException e) {}
154 XConnection con = xConnector.connect(connectString) ;
156 if (con == null)
157 log.println("Connector returned : null") ;
158 else
159 log.println("Connector returned : " + con.getDescription()) ;
161 try {
162 acception.join(5 * 1000) ;
163 } catch(InterruptedException e) {}
165 if (acception.isAlive()) {
167 result = false ;
168 log.println("Method call haven't returned") ;
170 if (acception.acceptedCall == null)
171 log.println("Acceptor returned : null") ;
172 else
173 log.println("Acceptor returned : " +
174 acception.acceptedCall.getDescription()) ;
175 } else {
176 if (acception.ex != null) {
177 log.println("Exception occurred in accept() thread :") ;
178 acception.ex.printStackTrace(log) ;
181 if (acception.acceptedCall == null)
182 log.println("Method returned : null") ;
183 else
184 log.println("Method returned : " +
185 acception.acceptedCall.getDescription()) ;
187 result &= acception.acceptedCall != null ;
189 } catch (com.sun.star.connection.ConnectionSetupException e) {
190 e.printStackTrace(log) ;
191 result = false ;
192 } catch (com.sun.star.connection.NoConnectException e) {
193 e.printStackTrace(log) ;
194 result = false ;
195 } finally {
196 oObj.stopAccepting();
197 if (acception.isAlive()) {
198 acception.interrupt();
202 // duplicate acceptor test
203 // creating the additional acceptor which listens
204 // on the same port
206 log.println("___ Testing for accepting on the same port ...") ;
208 try {
209 dupAcception = new AcceptorThread(dupAcceptor) ;
210 dupAcception.start() ;
212 try {
213 dupAcception.join(1 * 1000) ;
214 } catch(InterruptedException e) {}
217 if (dupAcception.isAlive()) {
218 log.println("Duplicate acceptor is listening ...") ;
220 // now trying to accept on the same port as additional
221 // acceptor
222 acception = new AcceptorThread(oObj) ;
223 acception.start() ;
225 try {
226 acception.join(3 * 1000) ;
227 } catch(InterruptedException e) {}
229 if (acception.isAlive()) {
230 oObj.stopAccepting() ;
231 acception.interrupt() ;
233 log.println("Acceptor with the same port must cause"+
234 " an error but didn't") ;
235 result = false ;
236 } else {
237 log.println("Accepted call = " + acception.acceptedCall) ;
238 if (acception.ex == null) {
239 //result = false ;
240 log.println("No exception was thrown when trying"+
241 " to listen on the same port") ;
242 } else {
243 if (acception.ex instanceof
244 com.sun.star.connection.AlreadyAcceptingException ||
245 acception.ex instanceof
246 com.sun.star.connection.ConnectionSetupException) {
248 log.println("Rigth exception was thrown when trying"+
249 " to listen on the same port") ;
250 } else {
251 result = false ;
252 log.println("Wrong exception was thrown when trying"+
253 " to listen on the same port :") ;
254 acception.ex.printStackTrace(log) ;
259 } finally {
260 dupAcceptor.stopAccepting() ;
261 if (dupAcception.isAlive()) {
262 dupAcception.interrupt() ;
266 tRes.tested("accept()", result) ;
270 * Starts thread with Acceptor and then calls <code>stopListening</code>
271 * method. <p>
272 * Has OK status if <code>accept</code> method successfully returns and
273 * rises no exceptions.
275 public void _stopAccepting() {
276 boolean result = true ;
279 AcceptorThread acception = new AcceptorThread(oObj) ;
281 acception.start() ;
283 oObj.stopAccepting() ;
285 try {
286 acception.join(3 * 1000) ;
287 } catch (InterruptedException e) {}
289 if (acception.isAlive()) {
290 acception.interrupt() ;
292 result = false ;
293 log.println("Method call haven't returned") ;
295 } else {
296 if (acception.ex != null) {
297 log.println("Exception occurred in accept() thread :") ;
298 acception.ex.printStackTrace(log) ;
299 result = false ;
300 } else {
301 result = true ;
304 if (acception.acceptedCall == null)
305 log.println("accept() returned : null") ;
306 else
307 log.println("accept() returned : " +
308 acception.acceptedCall.getDescription()) ;
311 tRes.tested("stopAccepting()", result) ;