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