1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package mod._stm;
25 
26 import java.io.PrintWriter;
27 import java.util.Vector;
28 
29 import lib.StatusException;
30 import lib.TestCase;
31 import lib.TestEnvironment;
32 import lib.TestParameters;
33 
34 import com.sun.star.io.XActiveDataSink;
35 import com.sun.star.io.XActiveDataSource;
36 import com.sun.star.io.XInputStream;
37 import com.sun.star.io.XObjectInputStream;
38 import com.sun.star.io.XObjectOutputStream;
39 import com.sun.star.io.XOutputStream;
40 import com.sun.star.io.XPersistObject;
41 import com.sun.star.lang.XMultiServiceFactory;
42 import com.sun.star.registry.CannotRegisterImplementationException;
43 import com.sun.star.registry.XImplementationRegistration;
44 import com.sun.star.registry.XSimpleRegistry;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.uno.XInterface;
47 
48 /**
49 * Test for object which is represented by service
50 * <code>com.sun.star.io.ObjectInputStream</code>. <p>
51 * Object implements the following interfaces :
52 * <ul>
53 *  <li> <code>com::sun::star::io::XInputStream</code></li>
54 *  <li> <code>com::sun::star::io::XMarkableStream</code></li>
55 *  <li> <code>com::sun::star::io::XDataInputStream</code></li>
56 *  <li> <code>com::sun::star::io::XConnectable</code></li>
57 *  <li> <code>com::sun::star::io::XActiveDataSink</code></li>
58 *  <li> <code>com::sun::star::io::XObjectInputStream</code></li>
59 * </ul>
60 * The following files used by this test :
61 * <ul>
62 *  <li><b> MyPersistObjectImpl.jar </b> : the implementation of the persist
63 *       object</li>
64 * </ul> <p>
65 * @see com.sun.star.io.ObjectInputStream
66 * @see com.sun.star.io.XInputStream
67 * @see com.sun.star.io.XMarkableStream
68 * @see com.sun.star.io.XDataInputStream
69 * @see com.sun.star.io.XConnectable
70 * @see com.sun.star.io.XActiveDataSink
71 * @see com.sun.star.io.XObjectInputStream
72 * @see ifc.io._XInputStream
73 * @see ifc.io._XMarkableStream
74 * @see ifc.io._XDataInputStream
75 * @see ifc.io._XConnectable
76 * @see ifc.io._XActiveDataSink
77 * @see ifc.io._XObjectInputStream
78 */
79 public class ObjectInputStream extends TestCase {
80 
81     /**
82     * Register the implementation of service
83     * <code>com.sun.star.cmp.PersistObject</code> if not yet registered.
84     * @see com.sun.star.cmp.PersistObject
85     */
initialize(TestParameters tParam, PrintWriter log)86     public void initialize(TestParameters tParam, PrintWriter log) {
87         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
88         Object oPersObj = null;
89         // test first if object is already registered
90         try {
91             oPersObj = xMSF.createInstance("com.sun.star.cmp.PersistObject");
92         }
93         catch( com.sun.star.uno.Exception e ) {
94             log.println("Could not create instance of PersistObject");
95             e.printStackTrace(log);
96             log.println("Going on with test...");
97         }
98         if ( oPersObj == null ) {
99             // object is  not available: it has to be registered
100             String url = util.utils.getFullTestURL
101                 ("qadevlibs/MyPersistObjectImpl.jar");
102             XImplementationRegistration xir;
103             try {
104                 Object o = xMSF.createInstance(
105                         "com.sun.star.registry.ImplementationRegistration");
106                 xir = (XImplementationRegistration)
107                                     UnoRuntime.queryInterface(
108                                     XImplementationRegistration.class, o);
109 
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.ObjectInputStream</code>,
138     * <code>com.sun.star.io.ObjectOutputStream</code>,
139     * <code>com.sun.star.io.Pipe</code>,
140     * <code>com.sun.star.io.MarkableInputStream</code> and
141     * <code>com.sun.star.io.MarkableOutputStream</code>. Plugs the created
142     * markable output stream as output stream for the created
143     * <code>ObjectOutputStream</code>. Plugs the created pipe as output stream
144     * for the created <code>MarkableOutputStream</code>. Plugs the created
145     * markable input stream as input stream for the created
146     * <code>ObjectInputStream</code>. Plugs the created pipe as input stream
147     * for the created <code>MarkableInputStream</code>. Creates an instance
148     * of the service <code>com.sun.star.cmp.PersistObject</code> and writes
149     * the created object to the object output stream.
150     * Object relations created :
151     * <ul>
152     *  <li> <code>'PersistObject'</code> for
153     *      {@link ifc.io._XObjectInputStream}(the created instance of the
154     *      persist object ) </li>
155     *  <li> <code>'StreamData'</code> for
156     *      {@link ifc.io._XDataInputStream}(the data that should be written into
157     *      the stream) </li>
158     *  <li> <code>'ByteData'</code> for
159     *      {@link ifc.io._XInputStream}(the data that should be written into
160     *      the stream) </li>
161     *  <li> <code>'StreamWriter'</code> for
162     *      {@link ifc.io._XDataInputStream}
163     *      {@link ifc.io._XObjectInputStream}
164     *      {@link ifc.io._XInputStream}(a stream to write data to) </li>
165     *  <li> <code>'Connectable'</code> for
166     *      {@link ifc.io._XConnectable}
167     *      (another object that can be connected) </li>
168     *  <li> <code>'InputStream'</code> for
169     *      {@link ifc.io._XActiveDataSink}(an input stream to set and get) </li>
170     * </ul>
171     * @see com.sun.star.io.ObjectInputStream
172     * @see com.sun.star.io.ObjectOutputStream
173     * @see com.sun.star.io.Pipe
174     * @see com.sun.star.io.MarkableInputStream
175     * @see com.sun.star.io.MarkableOutputStream
176     * @see com.sun.star.cmp.PersistObject
177     */
createTestEnvironment( TestParameters Param, PrintWriter log)178     public TestEnvironment createTestEnvironment(
179         TestParameters Param, PrintWriter log) throws StatusException {
180 
181         System.out.println("create TestEnvironment started.");
182         XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF();
183         Object ostream = null;
184         Object aPipe = null;
185         Object mostream = null;
186         Object mistream = null;
187         Object istream = null;
188         Object xConnect = null;
189         try {
190             istream = xMSF.createInstance
191                 ("com.sun.star.io.ObjectInputStream");
192             ostream = xMSF.createInstance
193                 ("com.sun.star.io.ObjectOutputStream");
194             aPipe = xMSF.createInstance
195                 ("com.sun.star.io.Pipe");
196             mistream = xMSF.createInstance
197                 ("com.sun.star.io.MarkableInputStream");
198             mostream = xMSF.createInstance
199                 ("com.sun.star.io.MarkableOutputStream");
200             xConnect = (XInterface)xMSF.createInstance
201                 ("com.sun.star.io.DataInputStream") ;
202 
203         } catch( com.sun.star.uno.Exception e ) {
204             e.printStackTrace(log);
205             throw new StatusException("Couldn't create instance", e);
206         }
207         // Creating construction :
208         // ObjectOutputStream -> MarkableOutputStream -> Pipe ->
209         // -> MarkableInputStream -> ObjectInputStream
210         XActiveDataSource xdSo = (XActiveDataSource)
211             UnoRuntime.queryInterface(XActiveDataSource.class, ostream);
212 
213         XActiveDataSource xdSmo = (XActiveDataSource)
214             UnoRuntime.queryInterface(XActiveDataSource.class, mostream);
215 
216         XOutputStream moStream = (XOutputStream)
217             UnoRuntime.queryInterface(XOutputStream.class, mostream);
218 
219         XOutputStream PipeOut = (XOutputStream)
220             UnoRuntime.queryInterface(XOutputStream.class, aPipe);
221         XInputStream PipeIn = (XInputStream)
222             UnoRuntime.queryInterface(XInputStream.class, aPipe);
223 
224         xdSo.setOutputStream(moStream);
225         xdSmo.setOutputStream(PipeOut);
226 
227         XObjectInputStream iStream = (XObjectInputStream)
228             UnoRuntime.queryInterface(XObjectInputStream.class, istream);
229         XObjectOutputStream oStream = null;
230         oStream = (XObjectOutputStream)
231             UnoRuntime.queryInterface(XObjectOutputStream.class, ostream);
232 
233         XActiveDataSink xmSi = (XActiveDataSink)
234             UnoRuntime.queryInterface(XActiveDataSink.class, mistream);
235         XInputStream xmIstream = (XInputStream)
236             UnoRuntime.queryInterface(XInputStream.class, mistream);
237 
238         XActiveDataSink xdSi = (XActiveDataSink) UnoRuntime.queryInterface
239             (XActiveDataSink.class, istream);
240         xdSi.setInputStream(xmIstream);
241         xmSi.setInputStream(PipeIn);
242 
243         // creating Persist object which has to be written
244         XPersistObject xPersObj = null;
245         try {
246             Object oPersObj = xMSF.createInstance
247                 ("com.sun.star.cmp.PersistObject");
248             xPersObj = (XPersistObject)
249                 UnoRuntime.queryInterface(XPersistObject.class, oPersObj);
250         } catch (com.sun.star.uno.Exception e) {
251             e.printStackTrace(log);
252             throw new StatusException("Can't write persist object.", e);
253         }
254 
255 
256 
257         // all data types for writing to an XDataInputStream
258         Vector data = new Vector() ;
259         data.add(new Boolean(true)) ;
260         data.add(new Byte((byte)123)) ;
261         data.add(new Character((char)1234)) ;
262         data.add(new Short((short)1234)) ;
263         data.add(new Integer(123456)) ;
264         data.add(new Float(1.234)) ;
265         data.add(new Double(1.23456)) ;
266         data.add("DataInputStream") ;
267         // information for writing to the pipe
268         byte[] byteData = new byte[] {
269             1, 2, 3, 4, 5, 6, 7, 8 } ;
270 
271 
272         System.out.println("create environment");
273         XInterface oObj = iStream;
274         log.println( "creating a new environment for object" );
275         TestEnvironment tEnv = new TestEnvironment( oObj );
276 
277         // adding persistent object
278         tEnv.addObjRelation("PersistObject", xPersObj);
279         // add a connectable
280         tEnv.addObjRelation("Connectable", xConnect);
281         tEnv.addObjRelation("StreamWriter", oStream);
282         // for XActiveDataSink
283         tEnv.addObjRelation("InputStream", aPipe);
284         // adding sequence of data that must be read
285         // by XDataInputStream interface methods
286         tEnv.addObjRelation("StreamData", data) ;
287         // and by XInputStream interface methods
288         tEnv.addObjRelation("ByteData", byteData) ;
289 
290         System.out.println("create TestEnvironment finished.");
291         return tEnv;
292     } // finish method getTestEnvironment
293 
294 }
295 
296