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