bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / ifc / connection / _XConnector.java
blobdca84f120954b5c650a1c6a2d9edaf16dc5503e8
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 lib.MultiMethodTest;
22 import lib.StatusException;
24 import com.sun.star.connection.XAcceptor;
25 import com.sun.star.connection.XConnection;
26 import com.sun.star.connection.XConnector;
27 import com.sun.star.lang.XMultiServiceFactory;
28 import com.sun.star.uno.UnoRuntime;
29 import com.sun.star.uno.XInterface;
31 /**
32 * Tests methods of <code>XConnector</code> interface. <p>
33 * Required relations :
34 * <ul>
35 * <li> <code>'XConnector.connectStr'</code> : String variable. Has
36 * the following format :
37 * <code>'socket,host=<SOHost>,port=<UniquePort>' where <SOHost> is
38 * the host where StarOffice is started. This string must be passed
39 * as parameter to <code>accept()</code> method. </li>
40 * <ul> <p>
41 * This test <b>can not</b> be run in multiply threads.
43 public class _XConnector extends MultiMethodTest {
45 /**
46 * Calls <code>accept()</code> method in a separate thread.
47 * Then stores exception thrown by call if it occurred, or
48 * return value.
50 protected class AcceptorThread extends Thread {
51 /**
52 * the acceptor
54 private XAcceptor acc = null ;
55 /**
56 * If exception occurred during method call it is
57 * stored in this field.
59 public Exception ex = null ;
60 /**
61 * If method call returns some value it stores in this field.
63 public XConnection acceptedCall = null ;
65 /**
66 * Gets an object which can call <code>accept</code> method.
68 public AcceptorThread(XAcceptor acc) {
69 this.acc = acc ;
72 /**
73 * Call <code>accept()</code> method.
75 public void run() {
76 try {
77 acceptedCall = acc.accept(connectString) ;
78 } catch (com.sun.star.lang.IllegalArgumentException e) {
79 ex = e ;
80 } catch (com.sun.star.connection.ConnectionSetupException e) {
81 ex = e ;
82 } catch (com.sun.star.connection.AlreadyAcceptingException e) {
83 ex = e ;
88 public XConnector oObj = null;
89 protected String connectString = null ;
91 /**
92 * Retrieves object relation.
94 public void before() throws StatusException {
95 connectString = (String)
96 tEnv.getObjRelation("XConnector.connectStr") ;
97 if (connectString == null)
98 throw new StatusException("No object relation found",
99 new NullPointerException()) ;
103 * Thread with acceptor is created, and it starts listening.
104 * The main thread tries to connect to acceptor. Acception thread must
105 * return and a valid connection must be returned by Acceptor. <p>
108 public void _connect() {
109 boolean result = true ;
110 AcceptorThread acceptorThread = null;
111 XAcceptor xAcceptor = null ;
112 XConnection aCon = null;
113 XInterface x = null;
115 // create the acceptor
116 try {
117 x = (XInterface) (
118 (XMultiServiceFactory)tParam.getMSF()).createInstance
119 ("com.sun.star.connection.Acceptor") ;
120 } catch (com.sun.star.uno.Exception e) {
121 e.printStackTrace(log) ;
122 throw new StatusException("Can't create service", e) ;
125 xAcceptor = UnoRuntime.queryInterface(XAcceptor.class, x);
127 acceptorThread = new AcceptorThread(xAcceptor) ;
128 acceptorThread.start() ;
130 try {
131 Thread.sleep(500);
133 catch (java.lang.InterruptedException e) {}
135 // connect to acceptor
136 try {
137 aCon = oObj.connect(connectString);
139 if (aCon == null)
140 log.println("Connector returned: null") ;
141 else
142 log.println("Connector returned: " + aCon.getDescription()) ;
144 try {
145 acceptorThread.join(30 * 1000) ;
146 } catch(InterruptedException e) {}
148 // connection not established
149 if (acceptorThread.isAlive()) {
151 result = false ;
152 log.println("Method call hasn't returned") ;
154 if (acceptorThread.acceptedCall == null)
155 log.println("Acceptor returned : null") ;
156 else
157 log.println("Acceptor returned : " +
158 acceptorThread.acceptedCall.getDescription()) ;
159 } else {
160 if (acceptorThread.ex != null) {
161 log.println("Exception occurred in accept() thread :") ;
162 acceptorThread.ex.printStackTrace(log) ;
165 if (acceptorThread.acceptedCall == null)
166 log.println("Method returned : null") ;
167 else
168 log.println("Method returned : " +
169 acceptorThread.acceptedCall.getDescription()) ;
171 result &= acceptorThread.acceptedCall != null ;
173 } catch (com.sun.star.connection.ConnectionSetupException e) {
174 e.printStackTrace(log) ;
175 result = false ;
176 } catch (com.sun.star.connection.NoConnectException e) {
177 e.printStackTrace(log) ;
178 result = false ;
179 } finally {
180 acceptorThread.acc.stopAccepting();
181 if (acceptorThread.isAlive()) {
182 acceptorThread.interrupt();
186 tRes.tested("connect()", result) ;