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