2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import java
.io
.PrintWriter
;
22 import java
.util
.ArrayList
;
24 import lib
.StatusException
;
26 import lib
.TestEnvironment
;
27 import lib
.TestParameters
;
29 import com
.sun
.star
.io
.XActiveDataSink
;
30 import com
.sun
.star
.io
.XActiveDataSource
;
31 import com
.sun
.star
.io
.XInputStream
;
32 import com
.sun
.star
.io
.XObjectInputStream
;
33 import com
.sun
.star
.io
.XObjectOutputStream
;
34 import com
.sun
.star
.io
.XOutputStream
;
35 import com
.sun
.star
.io
.XPersistObject
;
36 import com
.sun
.star
.lang
.XMultiServiceFactory
;
37 import com
.sun
.star
.registry
.CannotRegisterImplementationException
;
38 import com
.sun
.star
.registry
.XImplementationRegistration
;
39 import com
.sun
.star
.registry
.XSimpleRegistry
;
40 import com
.sun
.star
.uno
.UnoRuntime
;
41 import com
.sun
.star
.uno
.XInterface
;
44 * Test for object which is represented by service
45 * <code>com.sun.star.io.ObjectOutputStream</code>. <p>
46 * Object implements the following interfaces :
48 * <li> <code>com::sun::star::io::XActiveDataSource</code></li>
49 * <li> <code>com::sun::star::io::XOutputStream</code></li>
50 * <li> <code>com::sun::star::io::XConnectable</code></li>
51 * <li> <code>com::sun::star::io::XDataOutputStream</code></li>
52 * <li> <code>com::sun::star::io::XObjectOutputStream</code></li>
54 * The following files used by this test :
56 * <li><b> MyPersistObjectImpl.jar </b> : the implementation of the persist
59 * @see com.sun.star.io.ObjectOutputStream
60 * @see com.sun.star.io.XActiveDataSource
61 * @see com.sun.star.io.XOutputStream
62 * @see com.sun.star.io.XConnectable
63 * @see com.sun.star.io.XDataOutputStream
64 * @see com.sun.star.io.XObjectOutputStream
65 * @see ifc.io._XActiveDataSource
66 * @see ifc.io._XOutputStream
67 * @see ifc.io._XConnectable
68 * @see ifc.io._XDataOutputStream
69 * @see ifc.io._XObjectOutputStream
71 public class ObjectOutputStream
extends TestCase
{
74 * Register the implementation of service
75 * <code>com.sun.star.cmp.PersistObject</code> if not yet registered.
76 * @see com.sun.star.cmp.PersistObject
79 public void initialize(TestParameters tParam
, PrintWriter log
) {
80 XMultiServiceFactory xMSF
= tParam
.getMSF();
81 Object oPersObj
= null;
82 // test first if object is already registered
84 oPersObj
= xMSF
.createInstance("com.sun.star.cmp.PersistObject");
86 catch( com
.sun
.star
.uno
.Exception e
) {
87 log
.println("Could not create instance of PersistObject");
88 e
.printStackTrace(log
);
89 log
.println("Going on with test...");
91 if ( oPersObj
== null ) {
92 // object is not available: it has to be registered
93 String url
= util
.utils
.getFullTestURL
94 ("qadevlibs/MyPersistObjectImpl.jar");
95 XImplementationRegistration xir
;
97 Object o
= xMSF
.createInstance(
98 "com.sun.star.registry.ImplementationRegistration");
99 xir
= UnoRuntime
.queryInterface(
100 XImplementationRegistration
.class, o
);
102 catch (com
.sun
.star
.uno
.Exception e
) {
104 "Couldn't create implementation registration");
106 throw new StatusException("Couldn't create ImplReg", e
);
109 XSimpleRegistry xReg
= null;
111 System
.out
.println("Register library: " + url
);
112 xir
.registerImplementation(
113 "com.sun.star.loader.Java2", url
, xReg
);
114 System
.out
.println("...done");
115 } catch (CannotRegisterImplementationException e
) {
116 System
.err
.println("Name: " + url
+ " msg: " +
119 throw new StatusException(
120 "Couldn't register MyPersistObject", e
);
126 * Creating a Testenvironment for the interfaces to be tested.
127 * Creates an instances of services
128 * <code>com.sun.star.io.ObjectOutputStream</code>,
129 * <code>com.sun.star.io.Pipe</code> and
130 * <code>com.sun.star.io.MarkableOutputStream</code>. Plugs the created
131 * markable output stream as output stream for the created
132 * <code>ObjectOutputStream</code>. Plugs the created pipe as output stream
133 * for the created <code>MarkableOutputStream</code>. Creates an instance
134 * of the service <code>com.sun.star.cmp.PersistObject</code>.
135 * Object relations created :
137 * <li> <code>'StreamData'</code> for
138 * {@link ifc.io._XDataOutputStream}(the data that should
139 * be written into the stream) </li>
140 * <li> <code>'ByteData'</code> for
141 * {@link ifc.io._XOutputStream}(the data that should be written into
143 * <li> <code>'Connectable'</code> for
144 * {@link ifc.io._XConnectable}
145 * (another object that can be connected) </li>
146 * <li> <code>'OutputStream'</code> for
147 * {@link ifc.io._XActiveDataSource}
148 * (an input stream to set and get) </li>
149 * <li> <code>'PersistObject'</code> for
150 * {@link ifc.io._XObjectOutputStream}(the created instance of the
151 * <li> <code>'InputStream'</code> for
152 * {@link ifc.io._XObjectInputStream}(the created instance of the
153 * persist object ) </li>
154 * <li> <code>'XOutputStream.StreamChecker'</code> for
155 * {@link ifc.io._XOutputStream}( implementation of the interface
156 * ifc.io._XOutputStream.StreamChecker ) </li>
158 * @see com.sun.star.io.ObjectInputStream
159 * @see com.sun.star.io.ObjectOutputStream
160 * @see com.sun.star.io.Pipe
161 * @see com.sun.star.io.MarkableInputStream
162 * @see com.sun.star.io.MarkableOutputStream
163 * @see com.sun.star.cmp.PersistObject
166 public TestEnvironment
createTestEnvironment(
167 TestParameters Param
, PrintWriter log
) throws StatusException
{
169 XInterface oObj
= null;
171 XObjectOutputStream oStream
= null;
173 XMultiServiceFactory xMSF
= Param
.getMSF();
174 Object ostream
= null, istream
= null;
176 Object mostream
= null;
177 XInterface aConnect
= null;
178 Object minstream
= null;
181 ostream
= xMSF
.createInstance
182 ( "com.sun.star.io.ObjectOutputStream" );
183 istream
= xMSF
.createInstance
184 ("com.sun.star.io.ObjectInputStream");
185 aPipe
= xMSF
.createInstance("com.sun.star.io.Pipe");
186 mostream
= xMSF
.createInstance
187 ("com.sun.star.io.MarkableOutputStream");
188 aConnect
= (XInterface
)xMSF
.createInstance
189 ("com.sun.star.io.DataInputStream");
190 minstream
= xMSF
.createInstance
191 ("com.sun.star.io.MarkableInputStream");
192 } catch( com
.sun
.star
.uno
.Exception e
) {
193 e
.printStackTrace(log
);
194 throw new StatusException("Couldn't create instance", e
);
197 // creating the pipe where object has to be written to
198 XActiveDataSource xdSo
= UnoRuntime
.queryInterface(XActiveDataSource
.class, ostream
);
200 XActiveDataSource xdSmo
= UnoRuntime
.queryInterface(XActiveDataSource
.class, mostream
);
202 XOutputStream moStream
= UnoRuntime
.queryInterface(XOutputStream
.class, mostream
);
204 XActiveDataSink markIn
= UnoRuntime
.queryInterface(XActiveDataSink
.class, minstream
);
205 XActiveDataSink inStream
= UnoRuntime
.queryInterface(XActiveDataSink
.class, istream
);
206 XInputStream markInStream
= UnoRuntime
.queryInterface(XInputStream
.class, minstream
);
208 final XOutputStream PipeOut
= UnoRuntime
.queryInterface(XOutputStream
.class,aPipe
);
209 final XInputStream PipeIn
= UnoRuntime
.queryInterface(XInputStream
.class,aPipe
);
211 markIn
.setInputStream(PipeIn
);
212 inStream
.setInputStream(markInStream
);
213 XObjectInputStream objInputStream
= UnoRuntime
.queryInterface(XObjectInputStream
.class, istream
);
214 xdSo
.setOutputStream(moStream
);
215 xdSmo
.setOutputStream(PipeOut
);
217 oStream
= UnoRuntime
.queryInterface(XObjectOutputStream
.class, ostream
);
219 // creating Persistent object which has to be written
220 XPersistObject xPersObj
= null ;
222 Object oPersObj
= xMSF
.createInstance
223 ("com.sun.star.cmp.PersistObject");
224 xPersObj
= UnoRuntime
.queryInterface(XPersistObject
.class, oPersObj
);
225 } catch (com
.sun
.star
.uno
.Exception e
) {
226 e
.printStackTrace(log
);
227 throw new StatusException("Can't write persist object.", e
) ;
232 // all data types for writing to an XDataInputStream
233 ArrayList
<Object
> data
= new ArrayList
<Object
>() ;
234 data
.add(Boolean
.TRUE
) ;
235 data
.add(Byte
.valueOf((byte)123)) ;
236 data
.add(new Character((char)1234)) ;
237 data
.add(Short
.valueOf((short)1234)) ;
238 data
.add(Integer
.valueOf(123456)) ;
239 data
.add(new Float(1.234)) ;
240 data
.add(new Double(1.23456)) ;
241 data
.add("DataInputStream") ;
242 // information for writing to the pipe
243 byte[] byteData
= new byte[] {
244 1, 2, 3, 4, 5, 6, 7, 8 } ;
246 log
.println("creating a new environment for object");
247 TestEnvironment tEnv
= new TestEnvironment( oObj
);
249 tEnv
.addObjRelation("PersistObject", xPersObj
);
250 tEnv
.addObjRelation("StreamData", data
);
251 tEnv
.addObjRelation("ByteData", byteData
);
252 tEnv
.addObjRelation("OutputStream", aPipe
);
253 tEnv
.addObjRelation("Connectable", aConnect
);
255 tEnv
.addObjRelation("InputStream", objInputStream
);
257 //add relation for io.XOutputStream
258 final XMultiServiceFactory msf
= xMSF
;
259 tEnv
.addObjRelation("XOutputStream.StreamChecker",
260 new ifc
.io
._XOutputStream
.StreamChecker() {
261 XInputStream xInStream
= null;
262 public void resetStreams() {
263 if (xInStream
!= null) {
265 xInStream
.closeInput();
267 } catch(com
.sun
.star
.io
.IOException e
) {
271 PipeOut
.closeOutput();
272 } catch(com
.sun
.star
.io
.IOException e
) {
277 public XInputStream
getInStream() {
280 Object oInStream
= msf
.createInstance(
281 "com.sun.star.io.ObjectInputStream");
282 xInStream
= UnoRuntime
.queryInterface
283 (XInputStream
.class, oInStream
);
284 } catch(com
.sun
.star
.uno
.Exception e
) {
288 XActiveDataSink xDataSink
= UnoRuntime
.queryInterface(
289 XActiveDataSink
.class, xInStream
);
290 xDataSink
.setInputStream(PipeIn
);
297 } // finish method getTestEnvironment