1ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ef39d40dSAndrew Rist  * distributed with this work for additional information
6ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10ef39d40dSAndrew Rist  *
11ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12ef39d40dSAndrew Rist  *
13ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18ef39d40dSAndrew Rist  * under the License.
19ef39d40dSAndrew Rist  *
20ef39d40dSAndrew Rist  *************************************************************/
21ef39d40dSAndrew Rist 
22ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package ifc.connection;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import lib.MultiMethodTest;
27cdf0e10cSrcweir import lib.StatusException;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir import com.sun.star.connection.XAcceptor;
30cdf0e10cSrcweir import com.sun.star.connection.XConnection;
31cdf0e10cSrcweir import com.sun.star.connection.XConnector;
32cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
33cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
34cdf0e10cSrcweir import com.sun.star.uno.XInterface;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir /**
37cdf0e10cSrcweir * Tests methods of <code>XConnector</code> interface. <p>
38cdf0e10cSrcweir * Required relations :
39cdf0e10cSrcweir * <ul>
40cdf0e10cSrcweir * <li> <code>'XConnector.connectStr'</code> : String variable. Has
41cdf0e10cSrcweir *   the following format :
42cdf0e10cSrcweir *   <code>'socket,host=<SOHost>,port=<UniquePort>' where <SOHost> is
43cdf0e10cSrcweir *   the host where StarOffice is started. This string must be passed
44cdf0e10cSrcweir *   as parameter to <code>accept()</code> method. </li>
45cdf0e10cSrcweir * <ul> <p>
46cdf0e10cSrcweir * This test <b>can not</b> be run in multiply threads.
47cdf0e10cSrcweir */
48cdf0e10cSrcweir public class _XConnector extends MultiMethodTest {
49cdf0e10cSrcweir 
50cdf0e10cSrcweir     /**
51cdf0e10cSrcweir     * Calls <code>accept()</code> method in a separate thread.
52*bb6af6bcSPedro Giffuni     * Then stores exception thrown by call if it occurred, or
53cdf0e10cSrcweir     * return value.
54cdf0e10cSrcweir     */
55cdf0e10cSrcweir     protected class AcceptorThread extends Thread {
56cdf0e10cSrcweir         /**
57cdf0e10cSrcweir          * the acceptor
58cdf0e10cSrcweir          */
59cdf0e10cSrcweir         private XAcceptor acc = null ;
60cdf0e10cSrcweir         /**
61*bb6af6bcSPedro Giffuni         * If exception occurred during method call it is
62cdf0e10cSrcweir         * stored in this field.
63cdf0e10cSrcweir         */
64cdf0e10cSrcweir         public Exception ex = null ;
65cdf0e10cSrcweir         /**
66cdf0e10cSrcweir         * If method call returns some value it stores in this field.
67cdf0e10cSrcweir         */
68cdf0e10cSrcweir         public XConnection acceptedCall = null ;
69cdf0e10cSrcweir 
70cdf0e10cSrcweir         /**
71cdf0e10cSrcweir         * Gets an object which can call <code>accept</code> method.
72cdf0e10cSrcweir         */
AcceptorThread(XAcceptor acc)73cdf0e10cSrcweir         public AcceptorThread(XAcceptor acc) {
74cdf0e10cSrcweir             this.acc = acc ;
75cdf0e10cSrcweir         }
76cdf0e10cSrcweir 
77cdf0e10cSrcweir         /**
78cdf0e10cSrcweir         * Call <code>accept()</code> method.
79cdf0e10cSrcweir         */
run()80cdf0e10cSrcweir         public void run() {
81cdf0e10cSrcweir             try {
82cdf0e10cSrcweir                 acceptedCall = acc.accept(connectString) ;
83cdf0e10cSrcweir             } catch (com.sun.star.lang.IllegalArgumentException e) {
84cdf0e10cSrcweir                 ex = e ;
85cdf0e10cSrcweir             } catch (com.sun.star.connection.ConnectionSetupException e) {
86cdf0e10cSrcweir                 ex = e ;
87cdf0e10cSrcweir             } catch (com.sun.star.connection.AlreadyAcceptingException e) {
88cdf0e10cSrcweir                 ex = e ;
89cdf0e10cSrcweir             }
90cdf0e10cSrcweir         }
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     public XConnector oObj = null;
94cdf0e10cSrcweir     protected String connectString = null ;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     /**
97cdf0e10cSrcweir     * Retrieves object relation.
98cdf0e10cSrcweir     */
before()99cdf0e10cSrcweir     public void before() throws StatusException {
100cdf0e10cSrcweir         connectString = (String)
101cdf0e10cSrcweir             tEnv.getObjRelation("XConnector.connectStr") ;
102cdf0e10cSrcweir         if (connectString == null)
103cdf0e10cSrcweir             throw new StatusException("No object relation found",
104cdf0e10cSrcweir                 new NullPointerException()) ;
105cdf0e10cSrcweir     }
106cdf0e10cSrcweir 
107cdf0e10cSrcweir     /**
108cdf0e10cSrcweir     * Thread with acceptor is created, and it starts listening.
109cdf0e10cSrcweir     * The main thread tries to connect to acceptor. Acception thread must
110cdf0e10cSrcweir     * return and a valid connection must be returned by Acceptor. <p>
111cdf0e10cSrcweir     *
112cdf0e10cSrcweir     */
_connect()113cdf0e10cSrcweir     public void _connect() {
114cdf0e10cSrcweir         boolean result = true ;
115cdf0e10cSrcweir         AcceptorThread acceptorThread = null;
116cdf0e10cSrcweir         XAcceptor xAcceptor = null ;
117cdf0e10cSrcweir         XConnection aCon = null;
118cdf0e10cSrcweir         XInterface x = null;
119cdf0e10cSrcweir 
120cdf0e10cSrcweir         // create the acceptor
121cdf0e10cSrcweir         try {
122cdf0e10cSrcweir             x = (XInterface) (
123cdf0e10cSrcweir                 (XMultiServiceFactory)tParam.getMSF()).createInstance
124cdf0e10cSrcweir                 ("com.sun.star.connection.Acceptor") ;
125cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
126cdf0e10cSrcweir             e.printStackTrace(log) ;
127cdf0e10cSrcweir             throw new StatusException("Can't create service", e) ;
128cdf0e10cSrcweir         }
129cdf0e10cSrcweir 
130cdf0e10cSrcweir         xAcceptor = (XAcceptor)UnoRuntime.queryInterface(XAcceptor.class, x);
131cdf0e10cSrcweir 
132cdf0e10cSrcweir         acceptorThread = new AcceptorThread(xAcceptor) ;
133cdf0e10cSrcweir         acceptorThread.start() ;
134cdf0e10cSrcweir 
135cdf0e10cSrcweir         try {
136cdf0e10cSrcweir             Thread.sleep(500);
137cdf0e10cSrcweir         }
138cdf0e10cSrcweir         catch (java.lang.InterruptedException e) {}
139cdf0e10cSrcweir 
140cdf0e10cSrcweir         // connect to acceptor
141cdf0e10cSrcweir         try {
142cdf0e10cSrcweir             aCon = oObj.connect(connectString);
143cdf0e10cSrcweir 
144cdf0e10cSrcweir             if (aCon == null)
145cdf0e10cSrcweir                 log.println("Connector returned: null") ;
146cdf0e10cSrcweir             else
147cdf0e10cSrcweir                 log.println("Connector returned: " + aCon.getDescription()) ;
148cdf0e10cSrcweir 
149cdf0e10cSrcweir             try {
150cdf0e10cSrcweir                 acceptorThread.join(30 * 1000) ;
151cdf0e10cSrcweir             } catch(InterruptedException e) {}
152cdf0e10cSrcweir 
153cdf0e10cSrcweir             // connection not established
154cdf0e10cSrcweir             if (acceptorThread.isAlive()) {
155cdf0e10cSrcweir 
156cdf0e10cSrcweir                 result = false ;
157cdf0e10cSrcweir                 log.println("Method call hasn't returned") ;
158cdf0e10cSrcweir 
159cdf0e10cSrcweir                 if (acceptorThread.acceptedCall == null)
160cdf0e10cSrcweir                     log.println("Acceptor returned : null") ;
161cdf0e10cSrcweir                 else
162cdf0e10cSrcweir                     log.println("Acceptor returned : " +
163cdf0e10cSrcweir                         acceptorThread.acceptedCall.getDescription()) ;
164cdf0e10cSrcweir             } else {
165cdf0e10cSrcweir                 if (acceptorThread.ex != null) {
166*bb6af6bcSPedro Giffuni                     log.println("Exception occurred in accept() thread :") ;
167cdf0e10cSrcweir                     acceptorThread.ex.printStackTrace(log) ;
168cdf0e10cSrcweir                 }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir                 if (acceptorThread.acceptedCall == null)
171cdf0e10cSrcweir                     log.println("Method returned : null") ;
172cdf0e10cSrcweir                 else
173cdf0e10cSrcweir                     log.println("Method returned : " +
174cdf0e10cSrcweir                         acceptorThread.acceptedCall.getDescription()) ;
175cdf0e10cSrcweir 
176cdf0e10cSrcweir                 result &= acceptorThread.acceptedCall != null ;
177cdf0e10cSrcweir             }
178cdf0e10cSrcweir         } catch (com.sun.star.connection.ConnectionSetupException e) {
179cdf0e10cSrcweir             e.printStackTrace(log) ;
180cdf0e10cSrcweir             result =  false ;
181cdf0e10cSrcweir         } catch (com.sun.star.connection.NoConnectException e) {
182cdf0e10cSrcweir             e.printStackTrace(log) ;
183cdf0e10cSrcweir             result =  false ;
184cdf0e10cSrcweir         } finally {
185cdf0e10cSrcweir             acceptorThread.acc.stopAccepting();
186cdf0e10cSrcweir             if (acceptorThread.isAlive()) {
187cdf0e10cSrcweir                 acceptorThread.interrupt();
188cdf0e10cSrcweir             }
189cdf0e10cSrcweir         }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir         tRes.tested("connect()", result) ;
192cdf0e10cSrcweir     }
193cdf0e10cSrcweir }
194cdf0e10cSrcweir 
195