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 mod._stm; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import java.io.PrintWriter; 27cdf0e10cSrcweir import java.util.Vector; 28cdf0e10cSrcweir 29cdf0e10cSrcweir import lib.StatusException; 30cdf0e10cSrcweir import lib.TestCase; 31cdf0e10cSrcweir import lib.TestEnvironment; 32cdf0e10cSrcweir import lib.TestParameters; 33cdf0e10cSrcweir 34cdf0e10cSrcweir import com.sun.star.io.XActiveDataSink; 35cdf0e10cSrcweir import com.sun.star.io.XActiveDataSource; 36cdf0e10cSrcweir import com.sun.star.io.XInputStream; 37cdf0e10cSrcweir import com.sun.star.io.XObjectInputStream; 38cdf0e10cSrcweir import com.sun.star.io.XObjectOutputStream; 39cdf0e10cSrcweir import com.sun.star.io.XOutputStream; 40cdf0e10cSrcweir import com.sun.star.io.XPersistObject; 41cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 42cdf0e10cSrcweir import com.sun.star.registry.CannotRegisterImplementationException; 43cdf0e10cSrcweir import com.sun.star.registry.XImplementationRegistration; 44cdf0e10cSrcweir import com.sun.star.registry.XSimpleRegistry; 45cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 46cdf0e10cSrcweir import com.sun.star.uno.XInterface; 47cdf0e10cSrcweir 48cdf0e10cSrcweir /** 49cdf0e10cSrcweir * Test for object which is represented by service 50cdf0e10cSrcweir * <code>com.sun.star.io.ObjectOutputStream</code>. <p> 51cdf0e10cSrcweir * Object implements the following interfaces : 52cdf0e10cSrcweir * <ul> 53cdf0e10cSrcweir * <li> <code>com::sun::star::io::XActiveDataSource</code></li> 54cdf0e10cSrcweir * <li> <code>com::sun::star::io::XOutputStream</code></li> 55cdf0e10cSrcweir * <li> <code>com::sun::star::io::XConnectable</code></li> 56cdf0e10cSrcweir * <li> <code>com::sun::star::io::XDataOutputStream</code></li> 57cdf0e10cSrcweir * <li> <code>com::sun::star::io::XObjectOutputStream</code></li> 58cdf0e10cSrcweir * </ul> 59cdf0e10cSrcweir * The following files used by this test : 60cdf0e10cSrcweir * <ul> 61cdf0e10cSrcweir * <li><b> MyPersistObjectImpl.jar </b> : the implementation of the persist 62cdf0e10cSrcweir * object</li> 63cdf0e10cSrcweir * </ul> <p> 64cdf0e10cSrcweir * @see com.sun.star.io.ObjectOutputStream 65cdf0e10cSrcweir * @see com.sun.star.io.XActiveDataSource 66cdf0e10cSrcweir * @see com.sun.star.io.XOutputStream 67cdf0e10cSrcweir * @see com.sun.star.io.XConnectable 68cdf0e10cSrcweir * @see com.sun.star.io.XDataOutputStream 69cdf0e10cSrcweir * @see com.sun.star.io.XObjectOutputStream 70cdf0e10cSrcweir * @see ifc.io._XActiveDataSource 71cdf0e10cSrcweir * @see ifc.io._XOutputStream 72cdf0e10cSrcweir * @see ifc.io._XConnectable 73cdf0e10cSrcweir * @see ifc.io._XDataOutputStream 74cdf0e10cSrcweir * @see ifc.io._XObjectOutputStream 75cdf0e10cSrcweir */ 76cdf0e10cSrcweir public class ObjectOutputStream extends TestCase { 77cdf0e10cSrcweir 78cdf0e10cSrcweir /** 79cdf0e10cSrcweir * Register the implementation of service 80cdf0e10cSrcweir * <code>com.sun.star.cmp.PersistObject</code> if not yet registered. 81cdf0e10cSrcweir * @see com.sun.star.cmp.PersistObject 82cdf0e10cSrcweir */ initialize(TestParameters tParam, PrintWriter log)83cdf0e10cSrcweir public void initialize(TestParameters tParam, PrintWriter log) { 84cdf0e10cSrcweir XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF(); 85cdf0e10cSrcweir Object oPersObj = null; 86cdf0e10cSrcweir // test first if object is already registered 87cdf0e10cSrcweir try { 88cdf0e10cSrcweir oPersObj = xMSF.createInstance("com.sun.star.cmp.PersistObject"); 89cdf0e10cSrcweir } 90cdf0e10cSrcweir catch( com.sun.star.uno.Exception e ) { 91cdf0e10cSrcweir log.println("Could not create instance of PersistObject"); 92cdf0e10cSrcweir e.printStackTrace(log); 93cdf0e10cSrcweir log.println("Going on with test..."); 94cdf0e10cSrcweir } 95cdf0e10cSrcweir if ( oPersObj == null ) { 96cdf0e10cSrcweir // object is not available: it has to be registered 97cdf0e10cSrcweir String url = util.utils.getFullTestURL 98cdf0e10cSrcweir ("qadevlibs/MyPersistObjectImpl.jar"); 99cdf0e10cSrcweir XImplementationRegistration xir; 100cdf0e10cSrcweir try { 101cdf0e10cSrcweir Object o = xMSF.createInstance( 102cdf0e10cSrcweir "com.sun.star.registry.ImplementationRegistration"); 103cdf0e10cSrcweir xir = (XImplementationRegistration) 104cdf0e10cSrcweir UnoRuntime.queryInterface( 105cdf0e10cSrcweir XImplementationRegistration.class, o); 106cdf0e10cSrcweir } 107cdf0e10cSrcweir catch (com.sun.star.uno.Exception e) { 108cdf0e10cSrcweir System.err.println( 109cdf0e10cSrcweir "Couldn't create implementation registration"); 110cdf0e10cSrcweir e.printStackTrace(); 111cdf0e10cSrcweir throw new StatusException("Couldn't create ImplReg", e); 112cdf0e10cSrcweir } 113cdf0e10cSrcweir 114cdf0e10cSrcweir XSimpleRegistry xReg = null; 115cdf0e10cSrcweir try { 116cdf0e10cSrcweir System.out.println("Register library: " + url); 117cdf0e10cSrcweir xir.registerImplementation( 118cdf0e10cSrcweir "com.sun.star.loader.Java2", url, xReg); 119cdf0e10cSrcweir System.out.println("...done"); 120cdf0e10cSrcweir } catch (CannotRegisterImplementationException e) { 121cdf0e10cSrcweir System.err.println("Name: " + url + " msg: " + 122cdf0e10cSrcweir e.getMessage()); 123cdf0e10cSrcweir e.printStackTrace(); 124cdf0e10cSrcweir throw new StatusException( 125cdf0e10cSrcweir "Couldn't register MyPersistObject", e); 126cdf0e10cSrcweir } 127cdf0e10cSrcweir } 128cdf0e10cSrcweir } 129cdf0e10cSrcweir 130cdf0e10cSrcweir /** 131cdf0e10cSrcweir * Creating a Testenvironment for the interfaces to be tested. 132cdf0e10cSrcweir * Creates an instances of services 133cdf0e10cSrcweir * <code>com.sun.star.io.ObjectOutputStream</code>, 134cdf0e10cSrcweir * <code>com.sun.star.io.Pipe</code> and 135cdf0e10cSrcweir * <code>com.sun.star.io.MarkableOutputStream</code>. Plugs the created 136cdf0e10cSrcweir * markable output stream as output stream for the created 137cdf0e10cSrcweir * <code>ObjectOutputStream</code>. Plugs the created pipe as output stream 138cdf0e10cSrcweir * for the created <code>MarkableOutputStream</code>. Creates an instance 139cdf0e10cSrcweir * of the service <code>com.sun.star.cmp.PersistObject</code>. 140cdf0e10cSrcweir * Object relations created : 141cdf0e10cSrcweir * <ul> 142cdf0e10cSrcweir * <li> <code>'StreamData'</code> for 143cdf0e10cSrcweir * {@link ifc.io._XDataOutputStream}(the data that should 144cdf0e10cSrcweir * be written into the stream) </li> 145cdf0e10cSrcweir * <li> <code>'ByteData'</code> for 146cdf0e10cSrcweir * {@link ifc.io._XOutputStream}(the data that should be written into 147cdf0e10cSrcweir * the stream) </li> 148cdf0e10cSrcweir * <li> <code>'Connectable'</code> for 149cdf0e10cSrcweir * {@link ifc.io._XConnectable} 150cdf0e10cSrcweir * (another object that can be connected) </li> 151cdf0e10cSrcweir * <li> <code>'OutputStream'</code> for 152cdf0e10cSrcweir * {@link ifc.io._XActiveDataSource} 153cdf0e10cSrcweir * (an input stream to set and get) </li> 154cdf0e10cSrcweir * <li> <code>'PersistObject'</code> for 155cdf0e10cSrcweir * {@link ifc.io._XObjectOutputStream}(the created instance of the 156cdf0e10cSrcweir * <li> <code>'InputStream'</code> for 157cdf0e10cSrcweir * {@link ifc.io._XObjectInputStream}(the created instance of the 158cdf0e10cSrcweir * persist object ) </li> 159cdf0e10cSrcweir * <li> <code>'XOutputStream.StreamChecker'</code> for 160cdf0e10cSrcweir * {@link ifc.io._XOutputStream}( implementation of the interface 161cdf0e10cSrcweir * ifc.io._XOutputStream.StreamChecker ) </li> 162cdf0e10cSrcweir * </ul> 163cdf0e10cSrcweir * @see com.sun.star.io.ObjectInputStream 164cdf0e10cSrcweir * @see com.sun.star.io.ObjectOutputStream 165cdf0e10cSrcweir * @see com.sun.star.io.Pipe 166cdf0e10cSrcweir * @see com.sun.star.io.MarkableInputStream 167cdf0e10cSrcweir * @see com.sun.star.io.MarkableOutputStream 168cdf0e10cSrcweir * @see com.sun.star.cmp.PersistObject 169cdf0e10cSrcweir */ createTestEnvironment( TestParameters Param, PrintWriter log)170cdf0e10cSrcweir public TestEnvironment createTestEnvironment( 171cdf0e10cSrcweir TestParameters Param, PrintWriter log) throws StatusException { 172cdf0e10cSrcweir 173cdf0e10cSrcweir XInterface oObj = null; 174cdf0e10cSrcweir 175cdf0e10cSrcweir XObjectOutputStream oStream = null; 176cdf0e10cSrcweir 177cdf0e10cSrcweir XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF(); 178cdf0e10cSrcweir Object ostream = null, istream = null; 179cdf0e10cSrcweir Object aPipe = null; 180cdf0e10cSrcweir Object mostream = null; 181cdf0e10cSrcweir XInterface aConnect = null; 182cdf0e10cSrcweir Object minstream = null; 183cdf0e10cSrcweir 184cdf0e10cSrcweir try { 185cdf0e10cSrcweir ostream = xMSF.createInstance 186cdf0e10cSrcweir ( "com.sun.star.io.ObjectOutputStream" ); 187cdf0e10cSrcweir istream = xMSF.createInstance 188cdf0e10cSrcweir ("com.sun.star.io.ObjectInputStream"); 189cdf0e10cSrcweir aPipe = xMSF.createInstance("com.sun.star.io.Pipe"); 190cdf0e10cSrcweir mostream = xMSF.createInstance 191cdf0e10cSrcweir ("com.sun.star.io.MarkableOutputStream"); 192cdf0e10cSrcweir aConnect = (XInterface)xMSF.createInstance 193cdf0e10cSrcweir ("com.sun.star.io.DataInputStream"); 194cdf0e10cSrcweir minstream = xMSF.createInstance 195cdf0e10cSrcweir ("com.sun.star.io.MarkableInputStream"); 196cdf0e10cSrcweir } catch( com.sun.star.uno.Exception e ) { 197cdf0e10cSrcweir e.printStackTrace(log); 198cdf0e10cSrcweir throw new StatusException("Couldn't create instance", e); 199cdf0e10cSrcweir } 200cdf0e10cSrcweir 201cdf0e10cSrcweir // creating the pipe where object has to be written to 202cdf0e10cSrcweir XActiveDataSource xdSo = (XActiveDataSource) 203cdf0e10cSrcweir UnoRuntime.queryInterface(XActiveDataSource.class, ostream); 204cdf0e10cSrcweir 205cdf0e10cSrcweir XActiveDataSource xdSmo = (XActiveDataSource) 206cdf0e10cSrcweir UnoRuntime.queryInterface(XActiveDataSource.class, mostream); 207cdf0e10cSrcweir 208cdf0e10cSrcweir XOutputStream moStream = (XOutputStream) 209cdf0e10cSrcweir UnoRuntime.queryInterface(XOutputStream.class, mostream); 210cdf0e10cSrcweir 211cdf0e10cSrcweir XActiveDataSink markIn = (XActiveDataSink) 212cdf0e10cSrcweir UnoRuntime.queryInterface(XActiveDataSink.class, minstream); 213cdf0e10cSrcweir XActiveDataSink inStream = (XActiveDataSink) 214cdf0e10cSrcweir UnoRuntime.queryInterface(XActiveDataSink.class, istream); 215cdf0e10cSrcweir XInputStream markInStream = (XInputStream) 216cdf0e10cSrcweir UnoRuntime.queryInterface(XInputStream.class, minstream); 217cdf0e10cSrcweir 218cdf0e10cSrcweir final XOutputStream PipeOut = (XOutputStream) 219cdf0e10cSrcweir UnoRuntime.queryInterface(XOutputStream.class,aPipe); 220cdf0e10cSrcweir final XInputStream PipeIn = (XInputStream) 221cdf0e10cSrcweir UnoRuntime.queryInterface(XInputStream.class,aPipe); 222cdf0e10cSrcweir 223cdf0e10cSrcweir markIn.setInputStream(PipeIn); 224cdf0e10cSrcweir inStream.setInputStream(markInStream); 225cdf0e10cSrcweir XObjectInputStream objInputStream = (XObjectInputStream) 226cdf0e10cSrcweir UnoRuntime.queryInterface(XObjectInputStream.class, istream); 227cdf0e10cSrcweir xdSo.setOutputStream(moStream); 228cdf0e10cSrcweir xdSmo.setOutputStream(PipeOut); 229cdf0e10cSrcweir 230cdf0e10cSrcweir oStream = (XObjectOutputStream) 231cdf0e10cSrcweir UnoRuntime.queryInterface(XObjectOutputStream.class, ostream); 232cdf0e10cSrcweir 233cdf0e10cSrcweir // creating Persistent object which has to be written 234cdf0e10cSrcweir XPersistObject xPersObj = null ; 235cdf0e10cSrcweir try { 236cdf0e10cSrcweir Object oPersObj = xMSF.createInstance 237cdf0e10cSrcweir ("com.sun.star.cmp.PersistObject"); 238cdf0e10cSrcweir xPersObj = (XPersistObject) 239cdf0e10cSrcweir UnoRuntime.queryInterface(XPersistObject.class, oPersObj); 240cdf0e10cSrcweir } catch (com.sun.star.uno.Exception e) { 241cdf0e10cSrcweir e.printStackTrace(log); 242cdf0e10cSrcweir throw new StatusException("Can't write persist object.", e) ; 243cdf0e10cSrcweir } 244cdf0e10cSrcweir 245cdf0e10cSrcweir oObj = oStream; 246cdf0e10cSrcweir 247cdf0e10cSrcweir // all data types for writing to an XDataInputStream 248cdf0e10cSrcweir Vector data = new Vector() ; 249cdf0e10cSrcweir data.add(new Boolean(true)) ; 250cdf0e10cSrcweir data.add(new Byte((byte)123)) ; 251cdf0e10cSrcweir data.add(new Character((char)1234)) ; 252cdf0e10cSrcweir data.add(new Short((short)1234)) ; 253cdf0e10cSrcweir data.add(new Integer(123456)) ; 254cdf0e10cSrcweir data.add(new Float(1.234)) ; 255cdf0e10cSrcweir data.add(new Double(1.23456)) ; 256cdf0e10cSrcweir data.add("DataInputStream") ; 257cdf0e10cSrcweir // information for writing to the pipe 258cdf0e10cSrcweir byte[] byteData = new byte[] { 259cdf0e10cSrcweir 1, 2, 3, 4, 5, 6, 7, 8 } ; 260cdf0e10cSrcweir 261cdf0e10cSrcweir log.println("creating a new environment for object"); 262cdf0e10cSrcweir TestEnvironment tEnv = new TestEnvironment( oObj ); 263cdf0e10cSrcweir 264cdf0e10cSrcweir tEnv.addObjRelation("PersistObject", xPersObj); 265cdf0e10cSrcweir tEnv.addObjRelation("StreamData", data); 266cdf0e10cSrcweir tEnv.addObjRelation("ByteData", byteData); 267cdf0e10cSrcweir tEnv.addObjRelation("OutputStream", aPipe); 268cdf0e10cSrcweir tEnv.addObjRelation("Connectable", aConnect); 269cdf0e10cSrcweir 270cdf0e10cSrcweir tEnv.addObjRelation("InputStream", objInputStream); 271cdf0e10cSrcweir 272cdf0e10cSrcweir //add relation for io.XOutputStream 273cdf0e10cSrcweir final XMultiServiceFactory msf = xMSF; 274cdf0e10cSrcweir tEnv.addObjRelation("XOutputStream.StreamChecker", 275cdf0e10cSrcweir new ifc.io._XOutputStream.StreamChecker() { 276cdf0e10cSrcweir XInputStream xInStream = null; 277cdf0e10cSrcweir public void resetStreams() { 278cdf0e10cSrcweir if (xInStream != null) { 279cdf0e10cSrcweir try { 280cdf0e10cSrcweir xInStream.closeInput(); 281cdf0e10cSrcweir xInStream = null; 282cdf0e10cSrcweir } catch(com.sun.star.io.IOException e) { 283cdf0e10cSrcweir } 284cdf0e10cSrcweir } else { 285cdf0e10cSrcweir try { 286cdf0e10cSrcweir PipeOut.closeOutput(); 287cdf0e10cSrcweir } catch(com.sun.star.io.IOException e) { 288cdf0e10cSrcweir } 289cdf0e10cSrcweir } 290cdf0e10cSrcweir } 291cdf0e10cSrcweir 292cdf0e10cSrcweir public XInputStream getInStream() { 293cdf0e10cSrcweir resetStreams(); 294cdf0e10cSrcweir try { 295cdf0e10cSrcweir Object oInStream = msf.createInstance( 296cdf0e10cSrcweir "com.sun.star.io.ObjectInputStream"); 297cdf0e10cSrcweir xInStream = (XInputStream) UnoRuntime.queryInterface 298cdf0e10cSrcweir (XInputStream.class, oInStream); 299cdf0e10cSrcweir } catch(com.sun.star.uno.Exception e) { 300cdf0e10cSrcweir return null; 301cdf0e10cSrcweir } 302cdf0e10cSrcweir 303cdf0e10cSrcweir XActiveDataSink xDataSink = (XActiveDataSink) 304cdf0e10cSrcweir UnoRuntime.queryInterface( 305cdf0e10cSrcweir XActiveDataSink.class, xInStream); 306cdf0e10cSrcweir xDataSink.setInputStream(PipeIn); 307cdf0e10cSrcweir 308cdf0e10cSrcweir return xInStream; 309cdf0e10cSrcweir } 310cdf0e10cSrcweir }); 311cdf0e10cSrcweir 312cdf0e10cSrcweir return tEnv; 313cdf0e10cSrcweir } // finish method getTestEnvironment 314cdf0e10cSrcweir 315cdf0e10cSrcweir } 316cdf0e10cSrcweir 317