bump product version to 5.0.4.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _streams / uno / ObjectOutputStream.java
blob3041744057440be172d132c78abe445955c012a8
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._streams.uno;
21 import com.sun.star.io.XActiveDataSink;
22 import com.sun.star.io.XActiveDataSource;
23 import com.sun.star.io.XInputStream;
24 import com.sun.star.io.XObjectInputStream;
25 import com.sun.star.io.XObjectOutputStream;
26 import com.sun.star.io.XOutputStream;
27 import com.sun.star.io.XPersistObject;
28 import com.sun.star.lang.XMultiServiceFactory;
29 import com.sun.star.registry.CannotRegisterImplementationException;
30 import com.sun.star.registry.XImplementationRegistration;
31 import com.sun.star.registry.XSimpleRegistry;
32 import com.sun.star.uno.UnoRuntime;
33 import com.sun.star.uno.XInterface;
34 import java.io.PrintWriter;
35 import java.util.ArrayList;
36 import lib.StatusException;
37 import lib.TestCase;
38 import lib.TestEnvironment;
39 import lib.TestParameters;
41 /**
42 * Test for object which is represented by service
43 * <code>com.sun.star.io.ObjectOutputStream</code>. <p>
44 * Object implements the following interfaces :
45 * <ul>
46 * <li> <code>com::sun::star::io::XActiveDataSource</code></li>
47 * <li> <code>com::sun::star::io::XOutputStream</code></li>
48 * <li> <code>com::sun::star::io::XConnectable</code></li>
49 * <li> <code>com::sun::star::io::XDataOutputStream</code></li>
50 * <li> <code>com::sun::star::io::XObjectOutputStream</code></li>
51 * </ul>
52 * The following files used by this test :
53 * <ul>
54 * <li><b> MyPersistObjectImpl.jar </b> : the implementation of the persist
55 * object</li>
56 * </ul> <p>
57 * @see com.sun.star.io.ObjectOutputStream
58 * @see com.sun.star.io.XActiveDataSource
59 * @see com.sun.star.io.XOutputStream
60 * @see com.sun.star.io.XConnectable
61 * @see com.sun.star.io.XDataOutputStream
62 * @see com.sun.star.io.XObjectOutputStream
63 * @see ifc.io._XActiveDataSource
64 * @see ifc.io._XOutputStream
65 * @see ifc.io._XConnectable
66 * @see ifc.io._XDataOutputStream
67 * @see ifc.io._XObjectOutputStream
69 public class ObjectOutputStream extends TestCase {
71 /**
72 * Register the implementation of service
73 * <code>com.sun.star.cmp.PersistObject</code> if not yet registered.
74 * @see com.sun.star.cmp.PersistObject
76 @Override
77 public void initialize(TestParameters tParam, PrintWriter log) {
78 XMultiServiceFactory xMSF = tParam.getMSF();
79 Object oPersObj = null;
80 // test first if object is already registered
81 try {
82 oPersObj = xMSF.createInstance("com.sun.star.cmp.PersistObject");
84 catch( com.sun.star.uno.Exception e ) {
85 log.println("Could not create instance of PersistObject");
86 e.printStackTrace(log);
87 log.println("Going on with test...");
89 if ( oPersObj == null ) {
90 // object is not available: it has to be registered
91 String url = util.utils.getFullTestURL
92 ("qadevlibs/MyPersistObjectImpl.jar");
93 XImplementationRegistration xir;
94 try {
95 Object o = xMSF.createInstance(
96 "com.sun.star.registry.ImplementationRegistration");
97 xir = UnoRuntime.queryInterface(
98 XImplementationRegistration.class, o);
100 catch (com.sun.star.uno.Exception e) {
101 System.err.println(
102 "Couldn't create implementation registration");
103 e.printStackTrace();
104 throw new StatusException("Couldn't create ImplReg", e);
107 XSimpleRegistry xReg = null;
108 try {
109 System.out.println("Register library: " + url);
110 xir.registerImplementation(
111 "com.sun.star.loader.Java2", url, xReg);
112 System.out.println("...done");
113 } catch (CannotRegisterImplementationException e) {
114 System.err.println("Name: " + url + " msg: " +
115 e.getMessage());
116 e.printStackTrace();
117 throw new StatusException(
118 "Couldn't register MyPersistObject", e);
124 * Creating a Testenvironment for the interfaces to be tested.
125 * Creates an instances of services
126 * <code>com.sun.star.io.ObjectOutputStream</code>,
127 * <code>com.sun.star.io.Pipe</code> and
128 * <code>com.sun.star.io.MarkableOutputStream</code>. Plugs the created
129 * markable output stream as output stream for the created
130 * <code>ObjectOutputStream</code>. Plugs the created pipe as output stream
131 * for the created <code>MarkableOutputStream</code>. Creates an instance
132 * of the service <code>com.sun.star.cmp.PersistObject</code>.
133 * Object relations created :
134 * <ul>
135 * <li> <code>'StreamData'</code> for
136 * {@link ifc.io._XDataOutputStream}(the data that should
137 * be written into the stream) </li>
138 * <li> <code>'ByteData'</code> for
139 * {@link ifc.io._XOutputStream}(the data that should be written into
140 * the stream) </li>
141 * <li> <code>'Connectable'</code> for
142 * {@link ifc.io._XConnectable}
143 * (another object that can be connected) </li>
144 * <li> <code>'OutputStream'</code> for
145 * {@link ifc.io._XActiveDataSource}
146 * (an input stream to set and get) </li>
147 * <li> <code>'PersistObject'</code> for
148 * {@link ifc.io._XObjectOutputStream}(the created instance of the
149 * <li> <code>'InputStream'</code> for
150 * {@link ifc.io._XObjectInputStream}(the created instance of the
151 * persist object ) </li>
152 * <li> <code>'XOutputStream.StreamChecker'</code> for
153 * {@link ifc.io._XOutputStream}( implementation of the interface
154 * ifc.io._XOutputStream.StreamChecker ) </li>
155 * </ul>
156 * @see com.sun.star.io.ObjectInputStream
157 * @see com.sun.star.io.ObjectOutputStream
158 * @see com.sun.star.io.Pipe
159 * @see com.sun.star.io.MarkableInputStream
160 * @see com.sun.star.io.MarkableOutputStream
161 * @see com.sun.star.cmp.PersistObject
163 @Override
164 protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
166 XInterface oObj = null;
168 XObjectOutputStream oStream = null;
170 XMultiServiceFactory xMSF = Param.getMSF();
171 Object ostream = null, istream = null;
172 Object aPipe = null;
173 Object mostream = null;
174 XInterface aConnect = null;
175 Object minstream = null;
177 try {
178 ostream = xMSF.createInstance
179 ( "com.sun.star.io.ObjectOutputStream" );
180 istream = xMSF.createInstance
181 ("com.sun.star.io.ObjectInputStream");
182 aPipe = xMSF.createInstance("com.sun.star.io.Pipe");
183 mostream = xMSF.createInstance
184 ("com.sun.star.io.MarkableOutputStream");
185 aConnect = (XInterface)xMSF.createInstance
186 ("com.sun.star.io.DataInputStream");
187 minstream = xMSF.createInstance
188 ("com.sun.star.io.MarkableInputStream");
189 } catch( com.sun.star.uno.Exception e ) {
190 e.printStackTrace(log);
191 throw new StatusException("Couldn't create instance", e);
194 // creating the pipe where object has to be written to
195 XActiveDataSource xdSo = UnoRuntime.queryInterface(XActiveDataSource.class, ostream);
197 XActiveDataSource xdSmo = UnoRuntime.queryInterface(XActiveDataSource.class, mostream);
199 XOutputStream moStream = UnoRuntime.queryInterface(XOutputStream.class, mostream);
201 XActiveDataSink markIn = UnoRuntime.queryInterface(XActiveDataSink.class, minstream);
202 XActiveDataSink inStream = UnoRuntime.queryInterface(XActiveDataSink.class, istream);
203 XInputStream markInStream = UnoRuntime.queryInterface(XInputStream.class, minstream);
205 final XOutputStream PipeOut = UnoRuntime.queryInterface(XOutputStream.class,aPipe);
206 final XInputStream PipeIn = UnoRuntime.queryInterface(XInputStream.class,aPipe);
208 markIn.setInputStream(PipeIn);
209 inStream.setInputStream(markInStream);
210 XObjectInputStream objInputStream = UnoRuntime.queryInterface(XObjectInputStream.class, istream);
211 xdSo.setOutputStream(moStream);
212 xdSmo.setOutputStream(PipeOut);
214 oStream = UnoRuntime.queryInterface(XObjectOutputStream.class, ostream);
216 // creating Persistent object which has to be written
217 XPersistObject xPersObj = null ;
218 try {
219 Object oPersObj = xMSF.createInstance
220 ("com.sun.star.cmp.PersistObject");
221 xPersObj = UnoRuntime.queryInterface(XPersistObject.class, oPersObj);
222 } catch (com.sun.star.uno.Exception e) {
223 e.printStackTrace(log);
224 throw new StatusException("Can't write persist object.", e) ;
227 oObj = oStream;
229 // all data types for writing to an XDataInputStream
230 ArrayList<Object> data = new ArrayList<Object>() ;
231 data.add(Boolean.TRUE) ;
232 data.add(Byte.valueOf((byte)123)) ;
233 data.add(new Character((char)1234)) ;
234 data.add(Short.valueOf((short)1234)) ;
235 data.add(Integer.valueOf(123456)) ;
236 data.add(new Float(1.234)) ;
237 data.add(new Double(1.23456)) ;
238 data.add("DataInputStream") ;
239 // information for writing to the pipe
240 byte[] byteData = new byte[] {
241 1, 2, 3, 4, 5, 6, 7, 8 } ;
243 log.println("creating a new environment for object");
244 TestEnvironment tEnv = new TestEnvironment( oObj );
246 tEnv.addObjRelation("PersistObject", xPersObj);
247 tEnv.addObjRelation("StreamData", data);
248 tEnv.addObjRelation("ByteData", byteData);
249 tEnv.addObjRelation("OutputStream", aPipe);
250 tEnv.addObjRelation("Connectable", aConnect);
252 tEnv.addObjRelation("InputStream", objInputStream);
254 //add relation for io.XOutputStream
255 final XMultiServiceFactory msf = xMSF;
256 tEnv.addObjRelation("XOutputStream.StreamChecker",
257 new ifc.io._XOutputStream.StreamChecker() {
258 XInputStream xInStream = null;
259 public void resetStreams() {
260 if (xInStream != null) {
261 try {
262 xInStream.closeInput();
263 xInStream = null;
264 } catch(com.sun.star.io.IOException e) {
266 } else {
267 try {
268 PipeOut.closeOutput();
269 } catch(com.sun.star.io.IOException e) {
274 public XInputStream getInStream() {
275 resetStreams();
276 try {
277 Object oInStream = msf.createInstance(
278 "com.sun.star.io.ObjectInputStream");
279 xInStream = UnoRuntime.queryInterface
280 (XInputStream.class, oInStream);
281 } catch(com.sun.star.uno.Exception e) {
282 return null;
285 XActiveDataSink xDataSink = UnoRuntime.queryInterface(
286 XActiveDataSink.class, xInStream);
287 xDataSink.setInputStream(PipeIn);
289 return xInStream;
293 return tEnv;
294 } // finish method getTestEnvironment