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