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