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 10cdf0e10cSrcweir * 11ef39d40dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 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. 19cdf0e10cSrcweir * 20ef39d40dSAndrew Rist *************************************************************/ 21ef39d40dSAndrew Rist 22cdf0e10cSrcweir package ifc.connection; 23cdf0e10cSrcweir 24cdf0e10cSrcweir import java.io.PrintWriter; 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>XAcceptor</code> interface. <p> 38cdf0e10cSrcweir * Required relations : 39cdf0e10cSrcweir * <ul> 40cdf0e10cSrcweir * <li> <code>'XAcceptor.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 _XAcceptor extends MultiMethodTest { 49cdf0e10cSrcweir 50cdf0e10cSrcweir protected PrintWriter log_ ; 51cdf0e10cSrcweir 52cdf0e10cSrcweir /** 53cdf0e10cSrcweir * Calls <code>accept()</code> method in a separate thread. 54bb6af6bcSPedro Giffuni * Then stores exception thrown by call if it occurred, or 55cdf0e10cSrcweir * return value. 56cdf0e10cSrcweir */ 57cdf0e10cSrcweir protected class AcceptorThread extends Thread { 58cdf0e10cSrcweir /** 59bb6af6bcSPedro Giffuni * If exception occurred during method call it is 60cdf0e10cSrcweir * stored in this field. 61cdf0e10cSrcweir */ 62cdf0e10cSrcweir public Exception ex = null ; 63cdf0e10cSrcweir private XAcceptor acc = null ; 64cdf0e10cSrcweir /** 65cdf0e10cSrcweir * If method call returns some value it stores in this field. 66cdf0e10cSrcweir */ 67cdf0e10cSrcweir public XConnection acceptedCall = null ; 68cdf0e10cSrcweir 69cdf0e10cSrcweir /** 70cdf0e10cSrcweir * Creates object which can call <code>accept</code> method 71cdf0e10cSrcweir * of the Acceptor object specified. 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 XAcceptor 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("XAcceptor.connectStr") ; 102cdf0e10cSrcweir 103cdf0e10cSrcweir log_ = log ; 104cdf0e10cSrcweir 105cdf0e10cSrcweir if (connectString == null) 106cdf0e10cSrcweir throw new StatusException("No object relation found", 107cdf0e10cSrcweir new NullPointerException()) ; 108cdf0e10cSrcweir } 109cdf0e10cSrcweir 110cdf0e10cSrcweir /** 111cdf0e10cSrcweir * First part : Thread with acceptor created, and it starts listening. 112cdf0e10cSrcweir * The main thread tries to connect to acceptor. Acception thread must 113cdf0e10cSrcweir * return and valid connection must be returned by Acceptor. <p> 114cdf0e10cSrcweir * 115cdf0e10cSrcweir * Second part : Trying to create second acceptor which listen on 116cdf0e10cSrcweir * the same port. Calling <code>accept()</code> method of the second 117cdf0e10cSrcweir * Acceptor must rise appropriate exception. <p> 118cdf0e10cSrcweir * 119cdf0e10cSrcweir * Has OK status if both test parts executed properly. 120cdf0e10cSrcweir */ _accept()121cdf0e10cSrcweir public void _accept() { 122cdf0e10cSrcweir boolean result = true ; 123cdf0e10cSrcweir AcceptorThread acception = null, 124cdf0e10cSrcweir dupAcception = null ; 125cdf0e10cSrcweir XAcceptor dupAcceptor = null ; 126cdf0e10cSrcweir XConnector xConnector = null ; 127cdf0e10cSrcweir 128d4fdf834SJohn Bampton // creating services required 129cdf0e10cSrcweir try { 130cdf0e10cSrcweir Object oConnector = ((XMultiServiceFactory)tParam.getMSF()). 131cdf0e10cSrcweir createInstance("com.sun.star.connection.Connector") ; 132cdf0e10cSrcweir 133cdf0e10cSrcweir xConnector = (XConnector) UnoRuntime.queryInterface 134cdf0e10cSrcweir (XConnector.class, oConnector) ; 135cdf0e10cSrcweir 136cdf0e10cSrcweir XInterface acceptor = (XInterface) ((XMultiServiceFactory) 137cdf0e10cSrcweir tParam.getMSF()).createInstance 138cdf0e10cSrcweir ("com.sun.star.connection.Acceptor") ; 139cdf0e10cSrcweir 140cdf0e10cSrcweir dupAcceptor = (XAcceptor) UnoRuntime.queryInterface 141cdf0e10cSrcweir (XAcceptor.class, acceptor) ; 142cdf0e10cSrcweir } catch (com.sun.star.uno.Exception e) { 143cdf0e10cSrcweir e.printStackTrace(log) ; 144cdf0e10cSrcweir throw new StatusException("Can't create service", e) ; 145cdf0e10cSrcweir } 146cdf0e10cSrcweir 147cdf0e10cSrcweir // Testing connection to the acceptor 148cdf0e10cSrcweir try { 149cdf0e10cSrcweir acception = new AcceptorThread(oObj) ; 150cdf0e10cSrcweir acception.start() ; 151cdf0e10cSrcweir 152cdf0e10cSrcweir try { 153cdf0e10cSrcweir Thread.sleep(500); 154cdf0e10cSrcweir } 155cdf0e10cSrcweir catch (java.lang.InterruptedException e) {} 156cdf0e10cSrcweir 157cdf0e10cSrcweir XConnection con = xConnector.connect(connectString) ; 158cdf0e10cSrcweir 159cdf0e10cSrcweir if (con == null) 160cdf0e10cSrcweir log.println("Connector returned : null") ; 161cdf0e10cSrcweir else 162cdf0e10cSrcweir log.println("Connector returned : " + con.getDescription()) ; 163cdf0e10cSrcweir 164cdf0e10cSrcweir try { 165cdf0e10cSrcweir acception.join(5 * 1000) ; 166cdf0e10cSrcweir } catch(InterruptedException e) {} 167cdf0e10cSrcweir 168cdf0e10cSrcweir if (acception.isAlive()) { 169cdf0e10cSrcweir 170cdf0e10cSrcweir result = false ; 171cdf0e10cSrcweir log.println("Method call haven't returned") ; 172cdf0e10cSrcweir 173cdf0e10cSrcweir if (acception.acceptedCall == null) 174cdf0e10cSrcweir log.println("Acceptor returned : null") ; 175cdf0e10cSrcweir else 176cdf0e10cSrcweir log.println("Acceptor returned : " + 177cdf0e10cSrcweir acception.acceptedCall.getDescription()) ; 178cdf0e10cSrcweir } else { 179cdf0e10cSrcweir if (acception.ex != null) { 180bb6af6bcSPedro Giffuni log.println("Exception occurred in accept() thread :") ; 181cdf0e10cSrcweir acception.ex.printStackTrace(log) ; 182cdf0e10cSrcweir } 183cdf0e10cSrcweir 184cdf0e10cSrcweir if (acception.acceptedCall == null) 185cdf0e10cSrcweir log.println("Method returned : null") ; 186cdf0e10cSrcweir else 187cdf0e10cSrcweir log.println("Method returned : " + 188cdf0e10cSrcweir acception.acceptedCall.getDescription()) ; 189cdf0e10cSrcweir 190cdf0e10cSrcweir result &= acception.acceptedCall != null ; 191cdf0e10cSrcweir } 192cdf0e10cSrcweir } catch (com.sun.star.connection.ConnectionSetupException e) { 193cdf0e10cSrcweir e.printStackTrace(log) ; 194cdf0e10cSrcweir result = false ; 195cdf0e10cSrcweir } catch (com.sun.star.connection.NoConnectException e) { 196cdf0e10cSrcweir e.printStackTrace(log) ; 197cdf0e10cSrcweir result = false ; 198cdf0e10cSrcweir } finally { 199cdf0e10cSrcweir oObj.stopAccepting(); 200cdf0e10cSrcweir if (acception.isAlive()) { 201cdf0e10cSrcweir acception.interrupt(); 202cdf0e10cSrcweir } 203cdf0e10cSrcweir } 204cdf0e10cSrcweir 205cdf0e10cSrcweir // duplicate acceptor test 206cdf0e10cSrcweir // creating the additional acceptor which listens 207cdf0e10cSrcweir // on the same port 208cdf0e10cSrcweir 209cdf0e10cSrcweir log.println("___ Testing for accepting on the same port ...") ; 210cdf0e10cSrcweir 211cdf0e10cSrcweir try { 212cdf0e10cSrcweir dupAcception = new AcceptorThread(dupAcceptor) ; 213cdf0e10cSrcweir dupAcception.start() ; 214cdf0e10cSrcweir 215cdf0e10cSrcweir try { 216cdf0e10cSrcweir dupAcception.join(1 * 1000) ; 217cdf0e10cSrcweir } catch(InterruptedException e) {} 218cdf0e10cSrcweir 219cdf0e10cSrcweir 220cdf0e10cSrcweir if (dupAcception.isAlive()) { 221cdf0e10cSrcweir log.println("Duplicate acceptor is listening ...") ; 222cdf0e10cSrcweir 223cdf0e10cSrcweir // now trying to accept on the same port as additional 224cdf0e10cSrcweir // acceptor 225cdf0e10cSrcweir acception = new AcceptorThread(oObj) ; 226cdf0e10cSrcweir acception.start() ; 227cdf0e10cSrcweir 228cdf0e10cSrcweir try { 229cdf0e10cSrcweir acception.join(3 * 1000) ; 230cdf0e10cSrcweir } catch(InterruptedException e) {} 231cdf0e10cSrcweir 232cdf0e10cSrcweir if (acception.isAlive()) { 233cdf0e10cSrcweir oObj.stopAccepting() ; 234cdf0e10cSrcweir acception.interrupt() ; 235cdf0e10cSrcweir 236cdf0e10cSrcweir log.println("Acceptor with the same port must cause"+ 237cdf0e10cSrcweir " an error but didn't") ; 238cdf0e10cSrcweir result = false ; 239cdf0e10cSrcweir } else { 240cdf0e10cSrcweir log.println("Accepted call = " + acception.acceptedCall) ; 241cdf0e10cSrcweir if (acception.ex == null) { 242cdf0e10cSrcweir //result = false ; 243cdf0e10cSrcweir log.println("No exception was thrown when trying"+ 244cdf0e10cSrcweir " to listen on the same port") ; 245cdf0e10cSrcweir } else { 246cdf0e10cSrcweir if (acception.ex instanceof 247cdf0e10cSrcweir com.sun.star.connection.AlreadyAcceptingException || 248cdf0e10cSrcweir acception.ex instanceof 249cdf0e10cSrcweir com.sun.star.connection.ConnectionSetupException) { 250cdf0e10cSrcweir 251*e3e50c87Smseidel log.println("Right exception was thrown when trying"+ 252cdf0e10cSrcweir " to listen on the same port") ; 253cdf0e10cSrcweir } else { 254cdf0e10cSrcweir result = false ; 255cdf0e10cSrcweir log.println("Wrong exception was thrown when trying"+ 256cdf0e10cSrcweir " to listen on the same port :") ; 257cdf0e10cSrcweir acception.ex.printStackTrace(log) ; 258cdf0e10cSrcweir } 259cdf0e10cSrcweir } 260cdf0e10cSrcweir } 261cdf0e10cSrcweir } 262cdf0e10cSrcweir } finally { 263cdf0e10cSrcweir dupAcceptor.stopAccepting() ; 264cdf0e10cSrcweir if (dupAcception.isAlive()) { 265cdf0e10cSrcweir dupAcception.interrupt() ; 266cdf0e10cSrcweir } 267cdf0e10cSrcweir } 268cdf0e10cSrcweir 269cdf0e10cSrcweir tRes.tested("accept()", result) ; 270cdf0e10cSrcweir } 271cdf0e10cSrcweir 272cdf0e10cSrcweir /** 273cdf0e10cSrcweir * Starts thread with Acceptor and then calls <code>stopListening</code> 274cdf0e10cSrcweir * method. <p> 275cdf0e10cSrcweir * Has OK status if <code>accept</code> method successfully returns and 276cdf0e10cSrcweir * rises no exceptions. 277cdf0e10cSrcweir */ _stopAccepting()278cdf0e10cSrcweir public void _stopAccepting() { 279cdf0e10cSrcweir boolean result = true ; 280cdf0e10cSrcweir 281cdf0e10cSrcweir 282cdf0e10cSrcweir AcceptorThread acception = new AcceptorThread(oObj) ; 283cdf0e10cSrcweir 284cdf0e10cSrcweir acception.start() ; 285cdf0e10cSrcweir 286cdf0e10cSrcweir oObj.stopAccepting() ; 287cdf0e10cSrcweir 288cdf0e10cSrcweir try { 289cdf0e10cSrcweir acception.join(3 * 1000) ; 290cdf0e10cSrcweir } catch (InterruptedException e) {} 291cdf0e10cSrcweir 292cdf0e10cSrcweir if (acception.isAlive()) { 293cdf0e10cSrcweir acception.interrupt() ; 294cdf0e10cSrcweir 295cdf0e10cSrcweir result = false ; 296cdf0e10cSrcweir log.println("Method call haven't returned") ; 297cdf0e10cSrcweir 298cdf0e10cSrcweir } else { 299cdf0e10cSrcweir if (acception.ex != null) { 300bb6af6bcSPedro Giffuni log.println("Exception occurred in accept() thread :") ; 301cdf0e10cSrcweir acception.ex.printStackTrace(log) ; 302cdf0e10cSrcweir result = false ; 303cdf0e10cSrcweir } else { 304cdf0e10cSrcweir result = true ; 305cdf0e10cSrcweir } 306cdf0e10cSrcweir 307cdf0e10cSrcweir if (acception.acceptedCall == null) 308cdf0e10cSrcweir log.println("accept() returned : null") ; 309cdf0e10cSrcweir else 310cdf0e10cSrcweir log.println("accept() returned : " + 311cdf0e10cSrcweir acception.acceptedCall.getDescription()) ; 312cdf0e10cSrcweir } 313cdf0e10cSrcweir 314cdf0e10cSrcweir tRes.tested("stopAccepting()", result) ; 315cdf0e10cSrcweir } 316cdf0e10cSrcweir } 317