Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _stm / ObjectOutputStream.java
blob4cc317b5bcbbe879a45ae568a2eac2eacadd1916
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 @Override
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
83 try {
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;
96 try {
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) {
103 System.err.println(
104 "Couldn't create implementation registration");
105 e.printStackTrace();
106 throw new StatusException("Couldn't create ImplReg", e);
109 XSimpleRegistry xReg = null;
110 try {
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: " +
117 e.getMessage());
118 e.printStackTrace();
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 :
136 * <ul>
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
142 * the stream) </li>
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>
157 * </ul>
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
165 @Override
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;
175 Object aPipe = null;
176 Object mostream = null;
177 XInterface aConnect = null;
178 Object minstream = null;
180 try {
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 ;
221 try {
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) ;
230 oObj = oStream;
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) {
264 try {
265 xInStream.closeInput();
266 xInStream = null;
267 } catch(com.sun.star.io.IOException e) {
269 } else {
270 try {
271 PipeOut.closeOutput();
272 } catch(com.sun.star.io.IOException e) {
277 public XInputStream getInStream() {
278 resetStreams();
279 try {
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) {
285 return null;
288 XActiveDataSink xDataSink = UnoRuntime.queryInterface(
289 XActiveDataSink.class, xInStream);
290 xDataSink.setInputStream(PipeIn);
292 return xInStream;
296 return tEnv;
297 } // finish method getTestEnvironment