1*ef39d40dSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ef39d40dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ef39d40dSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ef39d40dSAndrew Rist * distributed with this work for additional information 6*ef39d40dSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ef39d40dSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ef39d40dSAndrew Rist * "License"); you may not use this file except in compliance 9*ef39d40dSAndrew Rist * with the License. You may obtain a copy of the License at 10*ef39d40dSAndrew Rist * 11*ef39d40dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*ef39d40dSAndrew Rist * 13*ef39d40dSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ef39d40dSAndrew Rist * software distributed under the License is distributed on an 15*ef39d40dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ef39d40dSAndrew Rist * KIND, either express or implied. See the License for the 17*ef39d40dSAndrew Rist * specific language governing permissions and limitations 18*ef39d40dSAndrew Rist * under the License. 19*ef39d40dSAndrew Rist * 20*ef39d40dSAndrew Rist *************************************************************/ 21*ef39d40dSAndrew Rist 22*ef39d40dSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package ifc.connection; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import java.io.PrintWriter; 27cdf0e10cSrcweir 28cdf0e10cSrcweir import lib.MultiMethodTest; 29cdf0e10cSrcweir import lib.StatusException; 30cdf0e10cSrcweir 31cdf0e10cSrcweir import com.sun.star.connection.XAcceptor; 32cdf0e10cSrcweir import com.sun.star.connection.XConnection; 33cdf0e10cSrcweir import com.sun.star.connection.XConnector; 34cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 35cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 36cdf0e10cSrcweir import com.sun.star.uno.XInterface; 37cdf0e10cSrcweir 38cdf0e10cSrcweir /** 39cdf0e10cSrcweir * Tests methods of <code>XAcceptor</code> interface. <p> 40cdf0e10cSrcweir * Required relations : 41cdf0e10cSrcweir * <ul> 42cdf0e10cSrcweir * <li> <code>'XAcceptor.connectStr'</code> : String variable. Has 43cdf0e10cSrcweir * the following format : 44cdf0e10cSrcweir * <code>'socket,host=<SOHost>,port=<UniquePort>' where <SOHost> is 45cdf0e10cSrcweir * the host where StarOffice is started. This string must be passed 46cdf0e10cSrcweir * as parameter to <code>accept()</code> method. </li> 47cdf0e10cSrcweir * <ul> <p> 48cdf0e10cSrcweir * This test <b>can not</b> be run in multiply threads. 49cdf0e10cSrcweir */ 50cdf0e10cSrcweir public class _XAcceptor extends MultiMethodTest { 51cdf0e10cSrcweir 52cdf0e10cSrcweir protected PrintWriter log_ ; 53cdf0e10cSrcweir 54cdf0e10cSrcweir /** 55cdf0e10cSrcweir * Calls <code>accept()</code> method in a separate thread. 56cdf0e10cSrcweir * Then stores exception thrown by call if it occured, or 57cdf0e10cSrcweir * return value. 58cdf0e10cSrcweir */ 59cdf0e10cSrcweir protected class AcceptorThread extends Thread { 60cdf0e10cSrcweir /** 61cdf0e10cSrcweir * If exception occured during method call it is 62cdf0e10cSrcweir * stored in this field. 63cdf0e10cSrcweir */ 64cdf0e10cSrcweir public Exception ex = null ; 65cdf0e10cSrcweir private XAcceptor acc = null ; 66cdf0e10cSrcweir /** 67cdf0e10cSrcweir * If method call returns some value it stores in this field. 68cdf0e10cSrcweir */ 69cdf0e10cSrcweir public XConnection acceptedCall = null ; 70cdf0e10cSrcweir 71cdf0e10cSrcweir /** 72cdf0e10cSrcweir * Creates object which can call <code>accept</code> method 73cdf0e10cSrcweir * of the Acceptor object specified. 74cdf0e10cSrcweir */ AcceptorThread(XAcceptor acc)75cdf0e10cSrcweir public AcceptorThread(XAcceptor acc) { 76cdf0e10cSrcweir this.acc = acc ; 77cdf0e10cSrcweir } 78cdf0e10cSrcweir 79cdf0e10cSrcweir /** 80cdf0e10cSrcweir * Call <code>accept()</code> method. 81cdf0e10cSrcweir */ run()82cdf0e10cSrcweir public void run() { 83cdf0e10cSrcweir try { 84cdf0e10cSrcweir acceptedCall = acc.accept(connectString) ; 85cdf0e10cSrcweir } catch (com.sun.star.lang.IllegalArgumentException e) { 86cdf0e10cSrcweir ex = e ; 87cdf0e10cSrcweir } catch (com.sun.star.connection.ConnectionSetupException e) { 88cdf0e10cSrcweir ex = e ; 89cdf0e10cSrcweir } catch (com.sun.star.connection.AlreadyAcceptingException e) { 90cdf0e10cSrcweir ex = e ; 91cdf0e10cSrcweir } 92cdf0e10cSrcweir } 93cdf0e10cSrcweir } 94cdf0e10cSrcweir 95cdf0e10cSrcweir public XAcceptor oObj = null; 96cdf0e10cSrcweir protected String connectString = null ; 97cdf0e10cSrcweir 98cdf0e10cSrcweir /** 99cdf0e10cSrcweir * Retrieves object relation. 100cdf0e10cSrcweir */ before()101cdf0e10cSrcweir public void before() throws StatusException { 102cdf0e10cSrcweir connectString = (String) 103cdf0e10cSrcweir tEnv.getObjRelation("XAcceptor.connectStr") ; 104cdf0e10cSrcweir 105cdf0e10cSrcweir log_ = log ; 106cdf0e10cSrcweir 107cdf0e10cSrcweir if (connectString == null) 108cdf0e10cSrcweir throw new StatusException("No object relation found", 109cdf0e10cSrcweir new NullPointerException()) ; 110cdf0e10cSrcweir } 111cdf0e10cSrcweir 112cdf0e10cSrcweir /** 113cdf0e10cSrcweir * First part : Thread with acceptor created, and it starts listening. 114cdf0e10cSrcweir * The main thread tries to connect to acceptor. Acception thread must 115cdf0e10cSrcweir * return and valid connection must be returned by Acceptor. <p> 116cdf0e10cSrcweir * 117cdf0e10cSrcweir * Second part : Trying to create second acceptor which listen on 118cdf0e10cSrcweir * the same port. Calling <code>accept()</code> method of the second 119cdf0e10cSrcweir * Acceptor must rise appropriate exception. <p> 120cdf0e10cSrcweir * 121cdf0e10cSrcweir * Has OK status if both test parts executed properly. 122cdf0e10cSrcweir */ _accept()123cdf0e10cSrcweir public void _accept() { 124cdf0e10cSrcweir boolean result = true ; 125cdf0e10cSrcweir AcceptorThread acception = null, 126cdf0e10cSrcweir dupAcception = null ; 127cdf0e10cSrcweir XAcceptor dupAcceptor = null ; 128cdf0e10cSrcweir XConnector xConnector = null ; 129cdf0e10cSrcweir 130cdf0e10cSrcweir // creating services requierd 131cdf0e10cSrcweir try { 132cdf0e10cSrcweir Object oConnector = ((XMultiServiceFactory)tParam.getMSF()). 133cdf0e10cSrcweir createInstance("com.sun.star.connection.Connector") ; 134cdf0e10cSrcweir 135cdf0e10cSrcweir xConnector = (XConnector) UnoRuntime.queryInterface 136cdf0e10cSrcweir (XConnector.class, oConnector) ; 137cdf0e10cSrcweir 138cdf0e10cSrcweir XInterface acceptor = (XInterface) ((XMultiServiceFactory) 139cdf0e10cSrcweir tParam.getMSF()).createInstance 140cdf0e10cSrcweir ("com.sun.star.connection.Acceptor") ; 141cdf0e10cSrcweir 142cdf0e10cSrcweir dupAcceptor = (XAcceptor) UnoRuntime.queryInterface 143cdf0e10cSrcweir (XAcceptor.class, acceptor) ; 144cdf0e10cSrcweir } catch (com.sun.star.uno.Exception e) { 145cdf0e10cSrcweir e.printStackTrace(log) ; 146cdf0e10cSrcweir throw new StatusException("Can't create service", e) ; 147cdf0e10cSrcweir } 148cdf0e10cSrcweir 149cdf0e10cSrcweir // Testing connection to the acceptor 150cdf0e10cSrcweir try { 151cdf0e10cSrcweir acception = new AcceptorThread(oObj) ; 152cdf0e10cSrcweir acception.start() ; 153cdf0e10cSrcweir 154cdf0e10cSrcweir try { 155cdf0e10cSrcweir Thread.sleep(500); 156cdf0e10cSrcweir } 157cdf0e10cSrcweir catch (java.lang.InterruptedException e) {} 158cdf0e10cSrcweir 159cdf0e10cSrcweir XConnection con = xConnector.connect(connectString) ; 160cdf0e10cSrcweir 161cdf0e10cSrcweir if (con == null) 162cdf0e10cSrcweir log.println("Connector returned : null") ; 163cdf0e10cSrcweir else 164cdf0e10cSrcweir log.println("Connector returned : " + con.getDescription()) ; 165cdf0e10cSrcweir 166cdf0e10cSrcweir try { 167cdf0e10cSrcweir acception.join(5 * 1000) ; 168cdf0e10cSrcweir } catch(InterruptedException e) {} 169cdf0e10cSrcweir 170cdf0e10cSrcweir if (acception.isAlive()) { 171cdf0e10cSrcweir 172cdf0e10cSrcweir result = false ; 173cdf0e10cSrcweir log.println("Method call haven't returned") ; 174cdf0e10cSrcweir 175cdf0e10cSrcweir if (acception.acceptedCall == null) 176cdf0e10cSrcweir log.println("Acceptor returned : null") ; 177cdf0e10cSrcweir else 178cdf0e10cSrcweir log.println("Acceptor returned : " + 179cdf0e10cSrcweir acception.acceptedCall.getDescription()) ; 180cdf0e10cSrcweir } else { 181cdf0e10cSrcweir if (acception.ex != null) { 182cdf0e10cSrcweir log.println("Exception occured in accept() thread :") ; 183cdf0e10cSrcweir acception.ex.printStackTrace(log) ; 184cdf0e10cSrcweir } 185cdf0e10cSrcweir 186cdf0e10cSrcweir if (acception.acceptedCall == null) 187cdf0e10cSrcweir log.println("Method returned : null") ; 188cdf0e10cSrcweir else 189cdf0e10cSrcweir log.println("Method returned : " + 190cdf0e10cSrcweir acception.acceptedCall.getDescription()) ; 191cdf0e10cSrcweir 192cdf0e10cSrcweir result &= acception.acceptedCall != null ; 193cdf0e10cSrcweir } 194cdf0e10cSrcweir } catch (com.sun.star.connection.ConnectionSetupException e) { 195cdf0e10cSrcweir e.printStackTrace(log) ; 196cdf0e10cSrcweir result = false ; 197cdf0e10cSrcweir } catch (com.sun.star.connection.NoConnectException e) { 198cdf0e10cSrcweir e.printStackTrace(log) ; 199cdf0e10cSrcweir result = false ; 200cdf0e10cSrcweir } finally { 201cdf0e10cSrcweir oObj.stopAccepting(); 202cdf0e10cSrcweir if (acception.isAlive()) { 203cdf0e10cSrcweir acception.interrupt(); 204cdf0e10cSrcweir } 205cdf0e10cSrcweir } 206cdf0e10cSrcweir 207cdf0e10cSrcweir // duplicate acceptor test 208cdf0e10cSrcweir // creating the additional acceptor which listens 209cdf0e10cSrcweir // on the same port 210cdf0e10cSrcweir 211cdf0e10cSrcweir log.println("___ Testing for accepting on the same port ...") ; 212cdf0e10cSrcweir 213cdf0e10cSrcweir try { 214cdf0e10cSrcweir dupAcception = new AcceptorThread(dupAcceptor) ; 215cdf0e10cSrcweir dupAcception.start() ; 216cdf0e10cSrcweir 217cdf0e10cSrcweir try { 218cdf0e10cSrcweir dupAcception.join(1 * 1000) ; 219cdf0e10cSrcweir } catch(InterruptedException e) {} 220cdf0e10cSrcweir 221cdf0e10cSrcweir 222cdf0e10cSrcweir if (dupAcception.isAlive()) { 223cdf0e10cSrcweir log.println("Duplicate acceptor is listening ...") ; 224cdf0e10cSrcweir 225cdf0e10cSrcweir // now trying to accept on the same port as additional 226cdf0e10cSrcweir // acceptor 227cdf0e10cSrcweir acception = new AcceptorThread(oObj) ; 228cdf0e10cSrcweir acception.start() ; 229cdf0e10cSrcweir 230cdf0e10cSrcweir try { 231cdf0e10cSrcweir acception.join(3 * 1000) ; 232cdf0e10cSrcweir } catch(InterruptedException e) {} 233cdf0e10cSrcweir 234cdf0e10cSrcweir if (acception.isAlive()) { 235cdf0e10cSrcweir oObj.stopAccepting() ; 236cdf0e10cSrcweir acception.interrupt() ; 237cdf0e10cSrcweir 238cdf0e10cSrcweir log.println("Acceptor with the same port must cause"+ 239cdf0e10cSrcweir " an error but didn't") ; 240cdf0e10cSrcweir result = false ; 241cdf0e10cSrcweir } else { 242cdf0e10cSrcweir log.println("Accepted call = " + acception.acceptedCall) ; 243cdf0e10cSrcweir if (acception.ex == null) { 244cdf0e10cSrcweir //result = false ; 245cdf0e10cSrcweir log.println("No exception was thrown when trying"+ 246cdf0e10cSrcweir " to listen on the same port") ; 247cdf0e10cSrcweir } else { 248cdf0e10cSrcweir if (acception.ex instanceof 249cdf0e10cSrcweir com.sun.star.connection.AlreadyAcceptingException || 250cdf0e10cSrcweir acception.ex instanceof 251cdf0e10cSrcweir com.sun.star.connection.ConnectionSetupException) { 252cdf0e10cSrcweir 253cdf0e10cSrcweir log.println("Rigth exception was thrown when trying"+ 254cdf0e10cSrcweir " to listen on the same port") ; 255cdf0e10cSrcweir } else { 256cdf0e10cSrcweir result = false ; 257cdf0e10cSrcweir log.println("Wrong exception was thrown when trying"+ 258cdf0e10cSrcweir " to listen on the same port :") ; 259cdf0e10cSrcweir acception.ex.printStackTrace(log) ; 260cdf0e10cSrcweir } 261cdf0e10cSrcweir } 262cdf0e10cSrcweir } 263cdf0e10cSrcweir } 264cdf0e10cSrcweir } finally { 265cdf0e10cSrcweir dupAcceptor.stopAccepting() ; 266cdf0e10cSrcweir if (dupAcception.isAlive()) { 267cdf0e10cSrcweir dupAcception.interrupt() ; 268cdf0e10cSrcweir } 269cdf0e10cSrcweir } 270cdf0e10cSrcweir 271cdf0e10cSrcweir tRes.tested("accept()", result) ; 272cdf0e10cSrcweir } 273cdf0e10cSrcweir 274cdf0e10cSrcweir /** 275cdf0e10cSrcweir * Starts thread with Acceptor and then calls <code>stopListening</code> 276cdf0e10cSrcweir * method. <p> 277cdf0e10cSrcweir * Has OK status if <code>accept</code> method successfully returns and 278cdf0e10cSrcweir * rises no exceptions. 279cdf0e10cSrcweir */ _stopAccepting()280cdf0e10cSrcweir public void _stopAccepting() { 281cdf0e10cSrcweir boolean result = true ; 282cdf0e10cSrcweir 283cdf0e10cSrcweir 284cdf0e10cSrcweir AcceptorThread acception = new AcceptorThread(oObj) ; 285cdf0e10cSrcweir 286cdf0e10cSrcweir acception.start() ; 287cdf0e10cSrcweir 288cdf0e10cSrcweir oObj.stopAccepting() ; 289cdf0e10cSrcweir 290cdf0e10cSrcweir try { 291cdf0e10cSrcweir acception.join(3 * 1000) ; 292cdf0e10cSrcweir } catch (InterruptedException e) {} 293cdf0e10cSrcweir 294cdf0e10cSrcweir if (acception.isAlive()) { 295cdf0e10cSrcweir acception.interrupt() ; 296cdf0e10cSrcweir 297cdf0e10cSrcweir result = false ; 298cdf0e10cSrcweir log.println("Method call haven't returned") ; 299cdf0e10cSrcweir 300cdf0e10cSrcweir } else { 301cdf0e10cSrcweir if (acception.ex != null) { 302cdf0e10cSrcweir log.println("Exception occured in accept() thread :") ; 303cdf0e10cSrcweir acception.ex.printStackTrace(log) ; 304cdf0e10cSrcweir result = false ; 305cdf0e10cSrcweir } else { 306cdf0e10cSrcweir result = true ; 307cdf0e10cSrcweir } 308cdf0e10cSrcweir 309cdf0e10cSrcweir if (acception.acceptedCall == null) 310cdf0e10cSrcweir log.println("accept() returned : null") ; 311cdf0e10cSrcweir else 312cdf0e10cSrcweir log.println("accept() returned : " + 313cdf0e10cSrcweir acception.acceptedCall.getDescription()) ; 314cdf0e10cSrcweir } 315cdf0e10cSrcweir 316cdf0e10cSrcweir tRes.tested("stopAccepting()", result) ; 317cdf0e10cSrcweir } 318cdf0e10cSrcweir } 319cdf0e10cSrcweir 320