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