Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _stm / ObjectInputStream.java
blob7d85642453768ec8973d8f3dfff3ff900875d159
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.ObjectInputStream</code>. <p>
46 * Object implements the following interfaces :
47 * <ul>
48 * <li> <code>com::sun::star::io::XInputStream</code></li>
49 * <li> <code>com::sun::star::io::XMarkableStream</code></li>
50 * <li> <code>com::sun::star::io::XDataInputStream</code></li>
51 * <li> <code>com::sun::star::io::XConnectable</code></li>
52 * <li> <code>com::sun::star::io::XActiveDataSink</code></li>
53 * <li> <code>com::sun::star::io::XObjectInputStream</code></li>
54 * </ul>
55 * The following files used by this test :
56 * <ul>
57 * <li><b> MyPersistObjectImpl.jar </b> : the implementation of the persist
58 * object</li>
59 * </ul> <p>
60 * @see com.sun.star.io.ObjectInputStream
61 * @see com.sun.star.io.XInputStream
62 * @see com.sun.star.io.XMarkableStream
63 * @see com.sun.star.io.XDataInputStream
64 * @see com.sun.star.io.XConnectable
65 * @see com.sun.star.io.XActiveDataSink
66 * @see com.sun.star.io.XObjectInputStream
67 * @see ifc.io._XInputStream
68 * @see ifc.io._XMarkableStream
69 * @see ifc.io._XDataInputStream
70 * @see ifc.io._XConnectable
71 * @see ifc.io._XActiveDataSink
72 * @see ifc.io._XObjectInputStream
74 public class ObjectInputStream extends TestCase {
76 /**
77 * Register the implementation of service
78 * <code>com.sun.star.cmp.PersistObject</code> if not yet registered.
79 * @see com.sun.star.cmp.PersistObject
81 @Override
82 public void initialize(TestParameters tParam, PrintWriter log) {
83 XMultiServiceFactory xMSF = tParam.getMSF();
84 Object oPersObj = null;
85 // test first if object is already registered
86 try {
87 oPersObj = xMSF.createInstance("com.sun.star.cmp.PersistObject");
89 catch( com.sun.star.uno.Exception e ) {
90 log.println("Could not create instance of PersistObject");
91 e.printStackTrace(log);
92 log.println("Going on with test...");
94 if ( oPersObj == null ) {
95 // object is not available: it has to be registered
96 String url = util.utils.getFullTestURL
97 ("qadevlibs/MyPersistObjectImpl.jar");
98 XImplementationRegistration xir;
99 try {
100 Object o = xMSF.createInstance(
101 "com.sun.star.registry.ImplementationRegistration");
102 xir = UnoRuntime.queryInterface(
103 XImplementationRegistration.class, o);
106 catch (com.sun.star.uno.Exception e) {
107 System.err.println(
108 "Couldn't create implementation registration");
109 e.printStackTrace();
110 throw new StatusException("Couldn't create ImplReg", e);
113 XSimpleRegistry xReg = null;
114 try {
115 System.out.println("Register library: " + url);
116 xir.registerImplementation(
117 "com.sun.star.loader.Java2", url, xReg);
118 System.out.println("...done");
119 } catch (CannotRegisterImplementationException e) {
120 System.err.println("Name: " + url + " msg: " +
121 e.getMessage());
122 e.printStackTrace();
123 throw new StatusException(
124 "Couldn't register MyPersistObject", e);
130 * Creating a Testenvironment for the interfaces to be tested.
131 * Creates an instances of services
132 * <code>com.sun.star.io.ObjectInputStream</code>,
133 * <code>com.sun.star.io.ObjectOutputStream</code>,
134 * <code>com.sun.star.io.Pipe</code>,
135 * <code>com.sun.star.io.MarkableInputStream</code> and
136 * <code>com.sun.star.io.MarkableOutputStream</code>. Plugs the created
137 * markable output stream as output stream for the created
138 * <code>ObjectOutputStream</code>. Plugs the created pipe as output stream
139 * for the created <code>MarkableOutputStream</code>. Plugs the created
140 * markable input stream as input stream for the created
141 * <code>ObjectInputStream</code>. Plugs the created pipe as input stream
142 * for the created <code>MarkableInputStream</code>. Creates an instance
143 * of the service <code>com.sun.star.cmp.PersistObject</code> and writes
144 * the created object to the object output stream.
145 * Object relations created :
146 * <ul>
147 * <li> <code>'PersistObject'</code> for
148 * {@link ifc.io._XObjectInputStream}(the created instance of the
149 * persist object ) </li>
150 * <li> <code>'StreamData'</code> for
151 * {@link ifc.io._XDataInputStream}(the data that should be written into
152 * the stream) </li>
153 * <li> <code>'ByteData'</code> for
154 * {@link ifc.io._XInputStream}(the data that should be written into
155 * the stream) </li>
156 * <li> <code>'StreamWriter'</code> for
157 * {@link ifc.io._XDataInputStream}
158 * {@link ifc.io._XObjectInputStream}
159 * {@link ifc.io._XInputStream}(a stream to write data to) </li>
160 * <li> <code>'Connectable'</code> for
161 * {@link ifc.io._XConnectable}
162 * (another object that can be connected) </li>
163 * <li> <code>'InputStream'</code> for
164 * {@link ifc.io._XActiveDataSink}(an input stream to set and get) </li>
165 * </ul>
166 * @see com.sun.star.io.ObjectInputStream
167 * @see com.sun.star.io.ObjectOutputStream
168 * @see com.sun.star.io.Pipe
169 * @see com.sun.star.io.MarkableInputStream
170 * @see com.sun.star.io.MarkableOutputStream
171 * @see com.sun.star.cmp.PersistObject
173 @Override
174 public TestEnvironment createTestEnvironment(
175 TestParameters Param, PrintWriter log) throws StatusException {
177 System.out.println("create TestEnvironment started.");
178 XMultiServiceFactory xMSF = Param.getMSF();
179 Object ostream = null;
180 Object aPipe = null;
181 Object mostream = null;
182 Object mistream = null;
183 Object istream = null;
184 Object xConnect = null;
185 try {
186 istream = xMSF.createInstance
187 ("com.sun.star.io.ObjectInputStream");
188 ostream = xMSF.createInstance
189 ("com.sun.star.io.ObjectOutputStream");
190 aPipe = xMSF.createInstance
191 ("com.sun.star.io.Pipe");
192 mistream = xMSF.createInstance
193 ("com.sun.star.io.MarkableInputStream");
194 mostream = xMSF.createInstance
195 ("com.sun.star.io.MarkableOutputStream");
196 xConnect = xMSF.createInstance
197 ("com.sun.star.io.DataInputStream") ;
199 } catch( com.sun.star.uno.Exception e ) {
200 e.printStackTrace(log);
201 throw new StatusException("Couldn't create instance", e);
203 // Creating construction :
204 // ObjectOutputStream -> MarkableOutputStream -> Pipe ->
205 // -> MarkableInputStream -> ObjectInputStream
206 XActiveDataSource xdSo = UnoRuntime.queryInterface(XActiveDataSource.class, ostream);
208 XActiveDataSource xdSmo = UnoRuntime.queryInterface(XActiveDataSource.class, mostream);
210 XOutputStream moStream = UnoRuntime.queryInterface(XOutputStream.class, mostream);
212 XOutputStream PipeOut = UnoRuntime.queryInterface(XOutputStream.class, aPipe);
213 XInputStream PipeIn = UnoRuntime.queryInterface(XInputStream.class, aPipe);
215 xdSo.setOutputStream(moStream);
216 xdSmo.setOutputStream(PipeOut);
218 XObjectInputStream iStream = UnoRuntime.queryInterface(XObjectInputStream.class, istream);
219 XObjectOutputStream oStream = null;
220 oStream = UnoRuntime.queryInterface(XObjectOutputStream.class, ostream);
222 XActiveDataSink xmSi = UnoRuntime.queryInterface(XActiveDataSink.class, mistream);
223 XInputStream xmIstream = UnoRuntime.queryInterface(XInputStream.class, mistream);
225 XActiveDataSink xdSi = UnoRuntime.queryInterface
226 (XActiveDataSink.class, istream);
227 xdSi.setInputStream(xmIstream);
228 xmSi.setInputStream(PipeIn);
230 // creating Persist object which has to be written
231 XPersistObject xPersObj = null;
232 try {
233 Object oPersObj = xMSF.createInstance
234 ("com.sun.star.cmp.PersistObject");
235 xPersObj = UnoRuntime.queryInterface(XPersistObject.class, oPersObj);
236 } catch (com.sun.star.uno.Exception e) {
237 e.printStackTrace(log);
238 throw new StatusException("Can't write persist object.", e);
243 // all data types for writing to an XDataInputStream
244 ArrayList<Object> data = new ArrayList<Object>() ;
245 data.add(Boolean.TRUE) ;
246 data.add(Byte.valueOf((byte)123)) ;
247 data.add(new Character((char)1234)) ;
248 data.add(Short.valueOf((short)1234)) ;
249 data.add(Integer.valueOf(123456)) ;
250 data.add(new Float(1.234)) ;
251 data.add(new Double(1.23456)) ;
252 data.add("DataInputStream") ;
253 // information for writing to the pipe
254 byte[] byteData = new byte[] {
255 1, 2, 3, 4, 5, 6, 7, 8 } ;
258 System.out.println("create environment");
259 XInterface oObj = iStream;
260 log.println( "creating a new environment for object" );
261 TestEnvironment tEnv = new TestEnvironment( oObj );
263 // adding persistent object
264 tEnv.addObjRelation("PersistObject", xPersObj);
265 // add a connectable
266 tEnv.addObjRelation("Connectable", xConnect);
267 tEnv.addObjRelation("StreamWriter", oStream);
268 // for XActiveDataSink
269 tEnv.addObjRelation("InputStream", aPipe);
270 // adding sequence of data that must be read
271 // by XDataInputStream interface methods
272 tEnv.addObjRelation("StreamData", data) ;
273 // and by XInputStream interface methods
274 tEnv.addObjRelation("ByteData", byteData) ;
276 System.out.println("create TestEnvironment finished.");
277 return tEnv;
278 } // finish method getTestEnvironment