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