bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _stm / ObjectOutputStream.java
blob675de8cdd949e827b64016bf9acfd07f42ba1256
1 /*
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 .
19 package mod._stm;
21 import java.io.PrintWriter;
22 import java.util.ArrayList;
24 import lib.StatusException;
25 import lib.TestCase;
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;
43 /**
44 * Test for object which is represented by service
45 * <code>com.sun.star.io.ObjectOutputStream</code>. <p>
46 * Object implements the following interfaces :
47 * <ul>
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>
53 * </ul>
54 * The following files used by this test :
55 * <ul>
56 * <li><b> MyPersistObjectImpl.jar </b> : the implementation of the persist
57 * object</li>
58 * </ul> <p>
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 {
73 /**
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
78 public void initialize(TestParameters tParam, PrintWriter log) {
79 XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
80 Object oPersObj = null;
81 // test first if object is already registered
82 try {
83 oPersObj = xMSF.createInstance("com.sun.star.cmp.PersistObject");
85 catch( com.sun.star.uno.Exception e ) {
86 log.println("Could not create instance of PersistObject");
87 e.printStackTrace(log);
88 log.println("Going on with test...");
90 if ( oPersObj == null ) {
91 // object is not available: it has to be registered
92 String url = util.utils.getFullTestURL
93 ("qadevlibs/MyPersistObjectImpl.jar");
94 XImplementationRegistration xir;
95 try {
96 Object o = xMSF.createInstance(
97 "com.sun.star.registry.ImplementationRegistration");
98 xir = UnoRuntime.queryInterface(
99 XImplementationRegistration.class, o);
101 catch (com.sun.star.uno.Exception e) {
102 System.err.println(
103 "Couldn't create implementation registration");
104 e.printStackTrace();
105 throw new StatusException("Couldn't create ImplReg", e);
108 XSimpleRegistry xReg = null;
109 try {
110 System.out.println("Register library: " + url);
111 xir.registerImplementation(
112 "com.sun.star.loader.Java2", url, xReg);
113 System.out.println("...done");
114 } catch (CannotRegisterImplementationException e) {
115 System.err.println("Name: " + url + " msg: " +
116 e.getMessage());
117 e.printStackTrace();
118 throw new StatusException(
119 "Couldn't register MyPersistObject", e);
125 * Creating a Testenvironment for the interfaces to be tested.
126 * Creates an instances of services
127 * <code>com.sun.star.io.ObjectOutputStream</code>,
128 * <code>com.sun.star.io.Pipe</code> and
129 * <code>com.sun.star.io.MarkableOutputStream</code>. Plugs the created
130 * markable output stream as output stream for the created
131 * <code>ObjectOutputStream</code>. Plugs the created pipe as output stream
132 * for the created <code>MarkableOutputStream</code>. Creates an instance
133 * of the service <code>com.sun.star.cmp.PersistObject</code>.
134 * Object relations created :
135 * <ul>
136 * <li> <code>'StreamData'</code> for
137 * {@link ifc.io._XDataOutputStream}(the data that should
138 * be written into the stream) </li>
139 * <li> <code>'ByteData'</code> for
140 * {@link ifc.io._XOutputStream}(the data that should be written into
141 * the stream) </li>
142 * <li> <code>'Connectable'</code> for
143 * {@link ifc.io._XConnectable}
144 * (another object that can be connected) </li>
145 * <li> <code>'OutputStream'</code> for
146 * {@link ifc.io._XActiveDataSource}
147 * (an input stream to set and get) </li>
148 * <li> <code>'PersistObject'</code> for
149 * {@link ifc.io._XObjectOutputStream}(the created instance of the
150 * <li> <code>'InputStream'</code> for
151 * {@link ifc.io._XObjectInputStream}(the created instance of the
152 * persist object ) </li>
153 * <li> <code>'XOutputStream.StreamChecker'</code> for
154 * {@link ifc.io._XOutputStream}( implementation of the interface
155 * ifc.io._XOutputStream.StreamChecker ) </li>
156 * </ul>
157 * @see com.sun.star.io.ObjectInputStream
158 * @see com.sun.star.io.ObjectOutputStream
159 * @see com.sun.star.io.Pipe
160 * @see com.sun.star.io.MarkableInputStream
161 * @see com.sun.star.io.MarkableOutputStream
162 * @see com.sun.star.cmp.PersistObject
164 public TestEnvironment createTestEnvironment(
165 TestParameters Param, PrintWriter log) throws StatusException {
167 XInterface oObj = null;
169 XObjectOutputStream oStream = null;
171 XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF();
172 Object ostream = null, istream = null;
173 Object aPipe = null;
174 Object mostream = null;
175 XInterface aConnect = null;
176 Object minstream = null;
178 try {
179 ostream = xMSF.createInstance
180 ( "com.sun.star.io.ObjectOutputStream" );
181 istream = xMSF.createInstance
182 ("com.sun.star.io.ObjectInputStream");
183 aPipe = xMSF.createInstance("com.sun.star.io.Pipe");
184 mostream = xMSF.createInstance
185 ("com.sun.star.io.MarkableOutputStream");
186 aConnect = (XInterface)xMSF.createInstance
187 ("com.sun.star.io.DataInputStream");
188 minstream = xMSF.createInstance
189 ("com.sun.star.io.MarkableInputStream");
190 } catch( com.sun.star.uno.Exception e ) {
191 e.printStackTrace(log);
192 throw new StatusException("Couldn't create instance", e);
195 // creating the pipe where object has to be written to
196 XActiveDataSource xdSo = UnoRuntime.queryInterface(XActiveDataSource.class, ostream);
198 XActiveDataSource xdSmo = UnoRuntime.queryInterface(XActiveDataSource.class, mostream);
200 XOutputStream moStream = UnoRuntime.queryInterface(XOutputStream.class, mostream);
202 XActiveDataSink markIn = UnoRuntime.queryInterface(XActiveDataSink.class, minstream);
203 XActiveDataSink inStream = UnoRuntime.queryInterface(XActiveDataSink.class, istream);
204 XInputStream markInStream = UnoRuntime.queryInterface(XInputStream.class, minstream);
206 final XOutputStream PipeOut = UnoRuntime.queryInterface(XOutputStream.class,aPipe);
207 final XInputStream PipeIn = UnoRuntime.queryInterface(XInputStream.class,aPipe);
209 markIn.setInputStream(PipeIn);
210 inStream.setInputStream(markInStream);
211 XObjectInputStream objInputStream = UnoRuntime.queryInterface(XObjectInputStream.class, istream);
212 xdSo.setOutputStream(moStream);
213 xdSmo.setOutputStream(PipeOut);
215 oStream = UnoRuntime.queryInterface(XObjectOutputStream.class, ostream);
217 // creating Persistent object which has to be written
218 XPersistObject xPersObj = null ;
219 try {
220 Object oPersObj = xMSF.createInstance
221 ("com.sun.star.cmp.PersistObject");
222 xPersObj = UnoRuntime.queryInterface(XPersistObject.class, oPersObj);
223 } catch (com.sun.star.uno.Exception e) {
224 e.printStackTrace(log);
225 throw new StatusException("Can't write persist object.", e) ;
228 oObj = oStream;
230 // all data types for writing to an XDataInputStream
231 ArrayList<Object> data = new ArrayList<Object>() ;
232 data.add(new Boolean(true)) ;
233 data.add(new Byte((byte)123)) ;
234 data.add(new Character((char)1234)) ;
235 data.add(new Short((short)1234)) ;
236 data.add(new Integer(123456)) ;
237 data.add(new Float(1.234)) ;
238 data.add(new Double(1.23456)) ;
239 data.add("DataInputStream") ;
240 // information for writing to the pipe
241 byte[] byteData = new byte[] {
242 1, 2, 3, 4, 5, 6, 7, 8 } ;
244 log.println("creating a new environment for object");
245 TestEnvironment tEnv = new TestEnvironment( oObj );
247 tEnv.addObjRelation("PersistObject", xPersObj);
248 tEnv.addObjRelation("StreamData", data);
249 tEnv.addObjRelation("ByteData", byteData);
250 tEnv.addObjRelation("OutputStream", aPipe);
251 tEnv.addObjRelation("Connectable", aConnect);
253 tEnv.addObjRelation("InputStream", objInputStream);
255 //add relation for io.XOutputStream
256 final XMultiServiceFactory msf = xMSF;
257 tEnv.addObjRelation("XOutputStream.StreamChecker",
258 new ifc.io._XOutputStream.StreamChecker() {
259 XInputStream xInStream = null;
260 public void resetStreams() {
261 if (xInStream != null) {
262 try {
263 xInStream.closeInput();
264 xInStream = null;
265 } catch(com.sun.star.io.IOException e) {
267 } else {
268 try {
269 PipeOut.closeOutput();
270 } catch(com.sun.star.io.IOException e) {
275 public XInputStream getInStream() {
276 resetStreams();
277 try {
278 Object oInStream = msf.createInstance(
279 "com.sun.star.io.ObjectInputStream");
280 xInStream = UnoRuntime.queryInterface
281 (XInputStream.class, oInStream);
282 } catch(com.sun.star.uno.Exception e) {
283 return null;
286 XActiveDataSink xDataSink = UnoRuntime.queryInterface(
287 XActiveDataSink.class, xInStream);
288 xDataSink.setInputStream(PipeIn);
290 return xInStream;
294 return tEnv;
295 } // finish method getTestEnvironment