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