merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / mod / _stm / ObjectOutputStream.java
blob23223b718c93038936398372d6419ac47446a474
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ObjectOutputStream.java,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package mod._stm;
33 import java.io.PrintWriter;
34 import java.util.Vector;
36 import lib.StatusException;
37 import lib.TestCase;
38 import lib.TestEnvironment;
39 import lib.TestParameters;
41 import com.sun.star.io.XActiveDataSink;
42 import com.sun.star.io.XActiveDataSource;
43 import com.sun.star.io.XInputStream;
44 import com.sun.star.io.XObjectInputStream;
45 import com.sun.star.io.XObjectOutputStream;
46 import com.sun.star.io.XOutputStream;
47 import com.sun.star.io.XPersistObject;
48 import com.sun.star.lang.XMultiServiceFactory;
49 import com.sun.star.registry.CannotRegisterImplementationException;
50 import com.sun.star.registry.XImplementationRegistration;
51 import com.sun.star.registry.XSimpleRegistry;
52 import com.sun.star.uno.UnoRuntime;
53 import com.sun.star.uno.XInterface;
55 /**
56 * Test for object which is represented by service
57 * <code>com.sun.star.io.ObjectOutputStream</code>. <p>
58 * Object implements the following interfaces :
59 * <ul>
60 * <li> <code>com::sun::star::io::XActiveDataSource</code></li>
61 * <li> <code>com::sun::star::io::XOutputStream</code></li>
62 * <li> <code>com::sun::star::io::XConnectable</code></li>
63 * <li> <code>com::sun::star::io::XDataOutputStream</code></li>
64 * <li> <code>com::sun::star::io::XObjectOutputStream</code></li>
65 * </ul>
66 * The following files used by this test :
67 * <ul>
68 * <li><b> MyPersistObjectImpl.jar </b> : the implementation of the persist
69 * object</li>
70 * </ul> <p>
71 * @see com.sun.star.io.ObjectOutputStream
72 * @see com.sun.star.io.XActiveDataSource
73 * @see com.sun.star.io.XOutputStream
74 * @see com.sun.star.io.XConnectable
75 * @see com.sun.star.io.XDataOutputStream
76 * @see com.sun.star.io.XObjectOutputStream
77 * @see ifc.io._XActiveDataSource
78 * @see ifc.io._XOutputStream
79 * @see ifc.io._XConnectable
80 * @see ifc.io._XDataOutputStream
81 * @see ifc.io._XObjectOutputStream
83 public class ObjectOutputStream extends TestCase {
85 /**
86 * Register the implementation of service
87 * <code>com.sun.star.cmp.PersistObject</code> if not yet registered.
88 * @see com.sun.star.cmp.PersistObject
90 public void initialize(TestParameters tParam, PrintWriter log) {
91 XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
92 Object oPersObj = null;
93 // test first if object is already registered
94 try {
95 oPersObj = xMSF.createInstance("com.sun.star.cmp.PersistObject");
97 catch( com.sun.star.uno.Exception e ) {
98 log.println("Could not create instance of PersistObject");
99 e.printStackTrace(log);
100 log.println("Going on with test...");
102 if ( oPersObj == null ) {
103 // object is not available: it has to be registered
104 String url = util.utils.getFullTestURL
105 ("qadevlibs/MyPersistObjectImpl.jar");
106 XImplementationRegistration xir;
107 try {
108 Object o = xMSF.createInstance(
109 "com.sun.star.registry.ImplementationRegistration");
110 xir = (XImplementationRegistration)
111 UnoRuntime.queryInterface(
112 XImplementationRegistration.class, o);
114 catch (com.sun.star.uno.Exception e) {
115 System.err.println(
116 "Couldn't create implementation registration");
117 e.printStackTrace();
118 throw new StatusException("Couldn't create ImplReg", e);
121 XSimpleRegistry xReg = null;
122 try {
123 System.out.println("Register library: " + url);
124 xir.registerImplementation(
125 "com.sun.star.loader.Java2", url, xReg);
126 System.out.println("...done");
127 } catch (CannotRegisterImplementationException e) {
128 System.err.println("Name: " + url + " msg: " +
129 e.getMessage());
130 e.printStackTrace();
131 throw new StatusException(
132 "Couldn't register MyPersistObject", e);
138 * Creating a Testenvironment for the interfaces to be tested.
139 * Creates an instances of services
140 * <code>com.sun.star.io.ObjectOutputStream</code>,
141 * <code>com.sun.star.io.Pipe</code> and
142 * <code>com.sun.star.io.MarkableOutputStream</code>. Plugs the created
143 * markable output stream as output stream for the created
144 * <code>ObjectOutputStream</code>. Plugs the created pipe as output stream
145 * for the created <code>MarkableOutputStream</code>. Creates an instance
146 * of the service <code>com.sun.star.cmp.PersistObject</code>.
147 * Object relations created :
148 * <ul>
149 * <li> <code>'StreamData'</code> for
150 * {@link ifc.io._XDataOutputStream}(the data that should
151 * be written into the stream) </li>
152 * <li> <code>'ByteData'</code> for
153 * {@link ifc.io._XOutputStream}(the data that should be written into
154 * the stream) </li>
155 * <li> <code>'Connectable'</code> for
156 * {@link ifc.io._XConnectable}
157 * (another object that can be connected) </li>
158 * <li> <code>'OutputStream'</code> for
159 * {@link ifc.io._XActiveDataSource}
160 * (an input stream to set and get) </li>
161 * <li> <code>'PersistObject'</code> for
162 * {@link ifc.io._XObjectOutputStream}(the created instance of the
163 * <li> <code>'InputStream'</code> for
164 * {@link ifc.io._XObjectInputStream}(the created instance of the
165 * persist object ) </li>
166 * <li> <code>'XOutputStream.StreamChecker'</code> for
167 * {@link ifc.io._XOutputStream}( implementation of the interface
168 * ifc.io._XOutputStream.StreamChecker ) </li>
169 * </ul>
170 * @see com.sun.star.io.ObjectInputStream
171 * @see com.sun.star.io.ObjectOutputStream
172 * @see com.sun.star.io.Pipe
173 * @see com.sun.star.io.MarkableInputStream
174 * @see com.sun.star.io.MarkableOutputStream
175 * @see com.sun.star.cmp.PersistObject
177 public TestEnvironment createTestEnvironment(
178 TestParameters Param, PrintWriter log) throws StatusException {
180 XInterface oObj = null;
182 XObjectOutputStream oStream = null;
184 XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF();
185 Object ostream = null, istream = null;
186 Object aPipe = null;
187 Object mostream = null;
188 XInterface aConnect = null;
189 Object minstream = null;
191 try {
192 ostream = xMSF.createInstance
193 ( "com.sun.star.io.ObjectOutputStream" );
194 istream = xMSF.createInstance
195 ("com.sun.star.io.ObjectInputStream");
196 aPipe = xMSF.createInstance("com.sun.star.io.Pipe");
197 mostream = xMSF.createInstance
198 ("com.sun.star.io.MarkableOutputStream");
199 aConnect = (XInterface)xMSF.createInstance
200 ("com.sun.star.io.DataInputStream");
201 minstream = xMSF.createInstance
202 ("com.sun.star.io.MarkableInputStream");
203 } catch( com.sun.star.uno.Exception e ) {
204 e.printStackTrace(log);
205 throw new StatusException("Couldn't create instance", e);
208 // creating the pipe where object has to be written to
209 XActiveDataSource xdSo = (XActiveDataSource)
210 UnoRuntime.queryInterface(XActiveDataSource.class, ostream);
212 XActiveDataSource xdSmo = (XActiveDataSource)
213 UnoRuntime.queryInterface(XActiveDataSource.class, mostream);
215 XOutputStream moStream = (XOutputStream)
216 UnoRuntime.queryInterface(XOutputStream.class, mostream);
218 XActiveDataSink markIn = (XActiveDataSink)
219 UnoRuntime.queryInterface(XActiveDataSink.class, minstream);
220 XActiveDataSink inStream = (XActiveDataSink)
221 UnoRuntime.queryInterface(XActiveDataSink.class, istream);
222 XInputStream markInStream = (XInputStream)
223 UnoRuntime.queryInterface(XInputStream.class, minstream);
225 final XOutputStream PipeOut = (XOutputStream)
226 UnoRuntime.queryInterface(XOutputStream.class,aPipe);
227 final XInputStream PipeIn = (XInputStream)
228 UnoRuntime.queryInterface(XInputStream.class,aPipe);
230 markIn.setInputStream(PipeIn);
231 inStream.setInputStream(markInStream);
232 XObjectInputStream objInputStream = (XObjectInputStream)
233 UnoRuntime.queryInterface(XObjectInputStream.class, istream);
234 xdSo.setOutputStream(moStream);
235 xdSmo.setOutputStream(PipeOut);
237 oStream = (XObjectOutputStream)
238 UnoRuntime.queryInterface(XObjectOutputStream.class, ostream);
240 // creating Persistent object which has to be written
241 XPersistObject xPersObj = null ;
242 try {
243 Object oPersObj = xMSF.createInstance
244 ("com.sun.star.cmp.PersistObject");
245 xPersObj = (XPersistObject)
246 UnoRuntime.queryInterface(XPersistObject.class, oPersObj);
247 } catch (com.sun.star.uno.Exception e) {
248 e.printStackTrace(log);
249 throw new StatusException("Can't write persist object.", e) ;
252 oObj = oStream;
254 // all data types for writing to an XDataInputStream
255 Vector data = new Vector() ;
256 data.add(new Boolean(true)) ;
257 data.add(new Byte((byte)123)) ;
258 data.add(new Character((char)1234)) ;
259 data.add(new Short((short)1234)) ;
260 data.add(new Integer(123456)) ;
261 data.add(new Float(1.234)) ;
262 data.add(new Double(1.23456)) ;
263 data.add("DataInputStream") ;
264 // information for writing to the pipe
265 byte[] byteData = new byte[] {
266 1, 2, 3, 4, 5, 6, 7, 8 } ;
268 log.println("creating a new environment for object");
269 TestEnvironment tEnv = new TestEnvironment( oObj );
271 tEnv.addObjRelation("PersistObject", xPersObj);
272 tEnv.addObjRelation("StreamData", data);
273 tEnv.addObjRelation("ByteData", byteData);
274 tEnv.addObjRelation("OutputStream", aPipe);
275 tEnv.addObjRelation("Connectable", aConnect);
277 tEnv.addObjRelation("InputStream", objInputStream);
279 //add relation for io.XOutputStream
280 final XMultiServiceFactory msf = xMSF;
281 tEnv.addObjRelation("XOutputStream.StreamChecker",
282 new ifc.io._XOutputStream.StreamChecker() {
283 XInputStream xInStream = null;
284 public void resetStreams() {
285 if (xInStream != null) {
286 try {
287 xInStream.closeInput();
288 xInStream = null;
289 } catch(com.sun.star.io.IOException e) {
291 } else {
292 try {
293 PipeOut.closeOutput();
294 } catch(com.sun.star.io.IOException e) {
299 public XInputStream getInStream() {
300 resetStreams();
301 try {
302 Object oInStream = msf.createInstance(
303 "com.sun.star.io.ObjectInputStream");
304 xInStream = (XInputStream) UnoRuntime.queryInterface
305 (XInputStream.class, oInStream);
306 } catch(com.sun.star.uno.Exception e) {
307 return null;
310 XActiveDataSink xDataSink = (XActiveDataSink)
311 UnoRuntime.queryInterface(
312 XActiveDataSink.class, xInStream);
313 xDataSink.setInputStream(PipeIn);
315 return xInStream;
319 return tEnv;
320 } // finish method getTestEnvironment