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