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