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