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