1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir package mod._streams.uno; 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir import com.sun.star.io.XActiveDataSink; 31*cdf0e10cSrcweir import com.sun.star.io.XActiveDataSource; 32*cdf0e10cSrcweir import com.sun.star.io.XInputStream; 33*cdf0e10cSrcweir import com.sun.star.io.XObjectInputStream; 34*cdf0e10cSrcweir import com.sun.star.io.XObjectOutputStream; 35*cdf0e10cSrcweir import com.sun.star.io.XOutputStream; 36*cdf0e10cSrcweir import com.sun.star.io.XPersistObject; 37*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 38*cdf0e10cSrcweir import com.sun.star.registry.CannotRegisterImplementationException; 39*cdf0e10cSrcweir import com.sun.star.registry.XImplementationRegistration; 40*cdf0e10cSrcweir import com.sun.star.registry.XSimpleRegistry; 41*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 42*cdf0e10cSrcweir import com.sun.star.uno.XInterface; 43*cdf0e10cSrcweir import java.io.PrintWriter; 44*cdf0e10cSrcweir import java.util.Vector; 45*cdf0e10cSrcweir import lib.StatusException; 46*cdf0e10cSrcweir import lib.TestCase; 47*cdf0e10cSrcweir import lib.TestEnvironment; 48*cdf0e10cSrcweir import lib.TestParameters; 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir /** 51*cdf0e10cSrcweir * Test for object which is represented by service 52*cdf0e10cSrcweir * <code>com.sun.star.io.ObjectInputStream</code>. <p> 53*cdf0e10cSrcweir * Object implements the following interfaces : 54*cdf0e10cSrcweir * <ul> 55*cdf0e10cSrcweir * <li> <code>com::sun::star::io::XInputStream</code></li> 56*cdf0e10cSrcweir * <li> <code>com::sun::star::io::XMarkableStream</code></li> 57*cdf0e10cSrcweir * <li> <code>com::sun::star::io::XDataInputStream</code></li> 58*cdf0e10cSrcweir * <li> <code>com::sun::star::io::XConnectable</code></li> 59*cdf0e10cSrcweir * <li> <code>com::sun::star::io::XActiveDataSink</code></li> 60*cdf0e10cSrcweir * <li> <code>com::sun::star::io::XObjectInputStream</code></li> 61*cdf0e10cSrcweir * </ul> 62*cdf0e10cSrcweir * The following files used by this test : 63*cdf0e10cSrcweir * <ul> 64*cdf0e10cSrcweir * <li><b> MyPersistObjectImpl.jar </b> : the implementation of the persist 65*cdf0e10cSrcweir * object</li> 66*cdf0e10cSrcweir * </ul> <p> 67*cdf0e10cSrcweir * @see com.sun.star.io.ObjectInputStream 68*cdf0e10cSrcweir * @see com.sun.star.io.XInputStream 69*cdf0e10cSrcweir * @see com.sun.star.io.XMarkableStream 70*cdf0e10cSrcweir * @see com.sun.star.io.XDataInputStream 71*cdf0e10cSrcweir * @see com.sun.star.io.XConnectable 72*cdf0e10cSrcweir * @see com.sun.star.io.XActiveDataSink 73*cdf0e10cSrcweir * @see com.sun.star.io.XObjectInputStream 74*cdf0e10cSrcweir * @see ifc.io._XInputStream 75*cdf0e10cSrcweir * @see ifc.io._XMarkableStream 76*cdf0e10cSrcweir * @see ifc.io._XDataInputStream 77*cdf0e10cSrcweir * @see ifc.io._XConnectable 78*cdf0e10cSrcweir * @see ifc.io._XActiveDataSink 79*cdf0e10cSrcweir * @see ifc.io._XObjectInputStream 80*cdf0e10cSrcweir */ 81*cdf0e10cSrcweir public class ObjectInputStream extends TestCase { 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir /** 84*cdf0e10cSrcweir * Register the implementation of service 85*cdf0e10cSrcweir * <code>com.sun.star.cmp.PersistObject</code> if not yet registered. 86*cdf0e10cSrcweir * @see com.sun.star.cmp.PersistObject 87*cdf0e10cSrcweir */ 88*cdf0e10cSrcweir public void initialize(TestParameters tParam, PrintWriter log) { 89*cdf0e10cSrcweir XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF(); 90*cdf0e10cSrcweir Object oPersObj = null; 91*cdf0e10cSrcweir // test first if object is already registered 92*cdf0e10cSrcweir try { 93*cdf0e10cSrcweir oPersObj = xMSF.createInstance("com.sun.star.cmp.PersistObject"); 94*cdf0e10cSrcweir } 95*cdf0e10cSrcweir catch( com.sun.star.uno.Exception e ) { 96*cdf0e10cSrcweir log.println("Could not create instance of PersistObject"); 97*cdf0e10cSrcweir e.printStackTrace(log); 98*cdf0e10cSrcweir log.println("Going on with test..."); 99*cdf0e10cSrcweir } 100*cdf0e10cSrcweir if ( oPersObj == null ) { 101*cdf0e10cSrcweir // object is not available: it has to be registered 102*cdf0e10cSrcweir String url = util.utils.getFullTestURL 103*cdf0e10cSrcweir ("qadevlibs/MyPersistObjectImpl.jar"); 104*cdf0e10cSrcweir XImplementationRegistration xir; 105*cdf0e10cSrcweir try { 106*cdf0e10cSrcweir Object o = xMSF.createInstance( 107*cdf0e10cSrcweir "com.sun.star.registry.ImplementationRegistration"); 108*cdf0e10cSrcweir xir = (XImplementationRegistration) 109*cdf0e10cSrcweir UnoRuntime.queryInterface( 110*cdf0e10cSrcweir XImplementationRegistration.class, o); 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir } 113*cdf0e10cSrcweir catch (com.sun.star.uno.Exception e) { 114*cdf0e10cSrcweir System.err.println( 115*cdf0e10cSrcweir "Couldn't create implementation registration"); 116*cdf0e10cSrcweir e.printStackTrace(); 117*cdf0e10cSrcweir throw new StatusException("Couldn't create ImplReg", e); 118*cdf0e10cSrcweir } 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir XSimpleRegistry xReg = null; 121*cdf0e10cSrcweir try { 122*cdf0e10cSrcweir System.out.println("Register library: " + url); 123*cdf0e10cSrcweir xir.registerImplementation( 124*cdf0e10cSrcweir "com.sun.star.loader.Java2", url, xReg); 125*cdf0e10cSrcweir System.out.println("...done"); 126*cdf0e10cSrcweir } catch (CannotRegisterImplementationException e) { 127*cdf0e10cSrcweir System.err.println("Name: " + url + " msg: " + 128*cdf0e10cSrcweir e.getMessage()); 129*cdf0e10cSrcweir e.printStackTrace(); 130*cdf0e10cSrcweir throw new StatusException( 131*cdf0e10cSrcweir "Couldn't register MyPersistObject", e); 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir } 134*cdf0e10cSrcweir } 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir /** 137*cdf0e10cSrcweir * Creating a Testenvironment for the interfaces to be tested. 138*cdf0e10cSrcweir * Creates an instances of services 139*cdf0e10cSrcweir * <code>com.sun.star.io.ObjectInputStream</code>, 140*cdf0e10cSrcweir * <code>com.sun.star.io.ObjectOutputStream</code>, 141*cdf0e10cSrcweir * <code>com.sun.star.io.Pipe</code>, 142*cdf0e10cSrcweir * <code>com.sun.star.io.MarkableInputStream</code> and 143*cdf0e10cSrcweir * <code>com.sun.star.io.MarkableOutputStream</code>. Plugs the created 144*cdf0e10cSrcweir * markable output stream as output stream for the created 145*cdf0e10cSrcweir * <code>ObjectOutputStream</code>. Plugs the created pipe as output stream 146*cdf0e10cSrcweir * for the created <code>MarkableOutputStream</code>. Plugs the created 147*cdf0e10cSrcweir * markable input stream as input stream for the created 148*cdf0e10cSrcweir * <code>ObjectInputStream</code>. Plugs the created pipe as input stream 149*cdf0e10cSrcweir * for the created <code>MarkableInputStream</code>. Creates an instance 150*cdf0e10cSrcweir * of the service <code>com.sun.star.cmp.PersistObject</code> and writes 151*cdf0e10cSrcweir * the created object to the object output stream. 152*cdf0e10cSrcweir * Object relations created : 153*cdf0e10cSrcweir * <ul> 154*cdf0e10cSrcweir * <li> <code>'PersistObject'</code> for 155*cdf0e10cSrcweir * {@link ifc.io._XObjectInputStream}(the created instance of the 156*cdf0e10cSrcweir * persist object ) </li> 157*cdf0e10cSrcweir * <li> <code>'StreamData'</code> for 158*cdf0e10cSrcweir * {@link ifc.io._XDataInputStream}(the data that should be written into 159*cdf0e10cSrcweir * the stream) </li> 160*cdf0e10cSrcweir * <li> <code>'ByteData'</code> for 161*cdf0e10cSrcweir * {@link ifc.io._XInputStream}(the data that should be written into 162*cdf0e10cSrcweir * the stream) </li> 163*cdf0e10cSrcweir * <li> <code>'StreamWriter'</code> for 164*cdf0e10cSrcweir * {@link ifc.io._XDataInputStream} 165*cdf0e10cSrcweir * {@link ifc.io._XObjectInputStream} 166*cdf0e10cSrcweir * {@link ifc.io._XInputStream}(a stream to write data to) </li> 167*cdf0e10cSrcweir * <li> <code>'Connectable'</code> for 168*cdf0e10cSrcweir * {@link ifc.io._XConnectable} 169*cdf0e10cSrcweir * (another object that can be connected) </li> 170*cdf0e10cSrcweir * <li> <code>'InputStream'</code> for 171*cdf0e10cSrcweir * {@link ifc.io._XActiveDataSink}(an input stream to set and get) </li> 172*cdf0e10cSrcweir * </ul> 173*cdf0e10cSrcweir * @see com.sun.star.io.ObjectInputStream 174*cdf0e10cSrcweir * @see com.sun.star.io.ObjectOutputStream 175*cdf0e10cSrcweir * @see com.sun.star.io.Pipe 176*cdf0e10cSrcweir * @see com.sun.star.io.MarkableInputStream 177*cdf0e10cSrcweir * @see com.sun.star.io.MarkableOutputStream 178*cdf0e10cSrcweir * @see com.sun.star.cmp.PersistObject 179*cdf0e10cSrcweir */ 180*cdf0e10cSrcweir protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) { 181*cdf0e10cSrcweir 182*cdf0e10cSrcweir System.out.println("create TestEnvironment started."); 183*cdf0e10cSrcweir XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF(); 184*cdf0e10cSrcweir Object ostream = null; 185*cdf0e10cSrcweir Object aPipe = null; 186*cdf0e10cSrcweir Object mostream = null; 187*cdf0e10cSrcweir Object mistream = null; 188*cdf0e10cSrcweir Object istream = null; 189*cdf0e10cSrcweir Object xConnect = null; 190*cdf0e10cSrcweir try { 191*cdf0e10cSrcweir istream = xMSF.createInstance 192*cdf0e10cSrcweir ("com.sun.star.io.ObjectInputStream"); 193*cdf0e10cSrcweir ostream = xMSF.createInstance 194*cdf0e10cSrcweir ("com.sun.star.io.ObjectOutputStream"); 195*cdf0e10cSrcweir aPipe = xMSF.createInstance 196*cdf0e10cSrcweir ("com.sun.star.io.Pipe"); 197*cdf0e10cSrcweir mistream = xMSF.createInstance 198*cdf0e10cSrcweir ("com.sun.star.io.MarkableInputStream"); 199*cdf0e10cSrcweir mostream = xMSF.createInstance 200*cdf0e10cSrcweir ("com.sun.star.io.MarkableOutputStream"); 201*cdf0e10cSrcweir xConnect = (XInterface)xMSF.createInstance 202*cdf0e10cSrcweir ("com.sun.star.io.DataInputStream") ; 203*cdf0e10cSrcweir 204*cdf0e10cSrcweir } catch( com.sun.star.uno.Exception e ) { 205*cdf0e10cSrcweir e.printStackTrace(log); 206*cdf0e10cSrcweir throw new StatusException("Couldn't create instance", e); 207*cdf0e10cSrcweir } 208*cdf0e10cSrcweir // Creating construction : 209*cdf0e10cSrcweir // ObjectOutputStream -> MarkableOutputStream -> Pipe -> 210*cdf0e10cSrcweir // -> MarkableInputStream -> ObjectInputStream 211*cdf0e10cSrcweir XActiveDataSource xdSo = (XActiveDataSource) 212*cdf0e10cSrcweir UnoRuntime.queryInterface(XActiveDataSource.class, ostream); 213*cdf0e10cSrcweir 214*cdf0e10cSrcweir XActiveDataSource xdSmo = (XActiveDataSource) 215*cdf0e10cSrcweir UnoRuntime.queryInterface(XActiveDataSource.class, mostream); 216*cdf0e10cSrcweir 217*cdf0e10cSrcweir XOutputStream moStream = (XOutputStream) 218*cdf0e10cSrcweir UnoRuntime.queryInterface(XOutputStream.class, mostream); 219*cdf0e10cSrcweir 220*cdf0e10cSrcweir XOutputStream PipeOut = (XOutputStream) 221*cdf0e10cSrcweir UnoRuntime.queryInterface(XOutputStream.class, aPipe); 222*cdf0e10cSrcweir XInputStream PipeIn = (XInputStream) 223*cdf0e10cSrcweir UnoRuntime.queryInterface(XInputStream.class, aPipe); 224*cdf0e10cSrcweir 225*cdf0e10cSrcweir xdSo.setOutputStream(moStream); 226*cdf0e10cSrcweir xdSmo.setOutputStream(PipeOut); 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir XObjectInputStream iStream = (XObjectInputStream) 229*cdf0e10cSrcweir UnoRuntime.queryInterface(XObjectInputStream.class, istream); 230*cdf0e10cSrcweir XObjectOutputStream oStream = null; 231*cdf0e10cSrcweir oStream = (XObjectOutputStream) 232*cdf0e10cSrcweir UnoRuntime.queryInterface(XObjectOutputStream.class, ostream); 233*cdf0e10cSrcweir 234*cdf0e10cSrcweir XActiveDataSink xmSi = (XActiveDataSink) 235*cdf0e10cSrcweir UnoRuntime.queryInterface(XActiveDataSink.class, mistream); 236*cdf0e10cSrcweir XInputStream xmIstream = (XInputStream) 237*cdf0e10cSrcweir UnoRuntime.queryInterface(XInputStream.class, mistream); 238*cdf0e10cSrcweir 239*cdf0e10cSrcweir XActiveDataSink xdSi = (XActiveDataSink) UnoRuntime.queryInterface 240*cdf0e10cSrcweir (XActiveDataSink.class, istream); 241*cdf0e10cSrcweir xdSi.setInputStream(xmIstream); 242*cdf0e10cSrcweir xmSi.setInputStream(PipeIn); 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir // creating Persist object which has to be written 245*cdf0e10cSrcweir XPersistObject xPersObj = null; 246*cdf0e10cSrcweir try { 247*cdf0e10cSrcweir Object oPersObj = xMSF.createInstance 248*cdf0e10cSrcweir ("com.sun.star.cmp.PersistObject"); 249*cdf0e10cSrcweir xPersObj = (XPersistObject) 250*cdf0e10cSrcweir UnoRuntime.queryInterface(XPersistObject.class, oPersObj); 251*cdf0e10cSrcweir } catch (com.sun.star.uno.Exception e) { 252*cdf0e10cSrcweir e.printStackTrace(log); 253*cdf0e10cSrcweir throw new StatusException("Can't write persist object.", e); 254*cdf0e10cSrcweir } 255*cdf0e10cSrcweir 256*cdf0e10cSrcweir 257*cdf0e10cSrcweir 258*cdf0e10cSrcweir // all data types for writing to an XDataInputStream 259*cdf0e10cSrcweir Vector data = new Vector() ; 260*cdf0e10cSrcweir data.add(new Boolean(true)) ; 261*cdf0e10cSrcweir data.add(new Byte((byte)123)) ; 262*cdf0e10cSrcweir data.add(new Character((char)1234)) ; 263*cdf0e10cSrcweir data.add(new Short((short)1234)) ; 264*cdf0e10cSrcweir data.add(new Integer(123456)) ; 265*cdf0e10cSrcweir data.add(new Float(1.234)) ; 266*cdf0e10cSrcweir data.add(new Double(1.23456)) ; 267*cdf0e10cSrcweir data.add("DataInputStream") ; 268*cdf0e10cSrcweir // information for writing to the pipe 269*cdf0e10cSrcweir byte[] byteData = new byte[] { 270*cdf0e10cSrcweir 1, 2, 3, 4, 5, 6, 7, 8 } ; 271*cdf0e10cSrcweir 272*cdf0e10cSrcweir 273*cdf0e10cSrcweir System.out.println("create environment"); 274*cdf0e10cSrcweir XInterface oObj = iStream; 275*cdf0e10cSrcweir log.println( "creating a new environment for object" ); 276*cdf0e10cSrcweir TestEnvironment tEnv = new TestEnvironment( oObj ); 277*cdf0e10cSrcweir 278*cdf0e10cSrcweir // adding persistent object 279*cdf0e10cSrcweir tEnv.addObjRelation("PersistObject", xPersObj); 280*cdf0e10cSrcweir // add a connectable 281*cdf0e10cSrcweir tEnv.addObjRelation("Connectable", xConnect); 282*cdf0e10cSrcweir tEnv.addObjRelation("StreamWriter", oStream); 283*cdf0e10cSrcweir // for XActiveDataSink 284*cdf0e10cSrcweir tEnv.addObjRelation("InputStream", aPipe); 285*cdf0e10cSrcweir // adding sequence of data that must be read 286*cdf0e10cSrcweir // by XDataInputStream interface methods 287*cdf0e10cSrcweir tEnv.addObjRelation("StreamData", data) ; 288*cdf0e10cSrcweir // and by XInputStream interface methods 289*cdf0e10cSrcweir tEnv.addObjRelation("ByteData", byteData) ; 290*cdf0e10cSrcweir 291*cdf0e10cSrcweir System.out.println("create TestEnvironment finished."); 292*cdf0e10cSrcweir return tEnv; 293*cdf0e10cSrcweir } // finish method getTestEnvironment 294*cdf0e10cSrcweir 295*cdf0e10cSrcweir } 296*cdf0e10cSrcweir 297