1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package mod._stm;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import java.io.PrintWriter;
31*cdf0e10cSrcweir import java.util.Vector;
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir import lib.StatusException;
34*cdf0e10cSrcweir import lib.TestCase;
35*cdf0e10cSrcweir import lib.TestEnvironment;
36*cdf0e10cSrcweir import lib.TestParameters;
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir import com.sun.star.io.XActiveDataSink;
39*cdf0e10cSrcweir import com.sun.star.io.XActiveDataSource;
40*cdf0e10cSrcweir import com.sun.star.io.XInputStream;
41*cdf0e10cSrcweir import com.sun.star.io.XObjectInputStream;
42*cdf0e10cSrcweir import com.sun.star.io.XObjectOutputStream;
43*cdf0e10cSrcweir import com.sun.star.io.XOutputStream;
44*cdf0e10cSrcweir import com.sun.star.io.XPersistObject;
45*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
46*cdf0e10cSrcweir import com.sun.star.registry.CannotRegisterImplementationException;
47*cdf0e10cSrcweir import com.sun.star.registry.XImplementationRegistration;
48*cdf0e10cSrcweir import com.sun.star.registry.XSimpleRegistry;
49*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
50*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir /**
53*cdf0e10cSrcweir * Test for object which is represented by service
54*cdf0e10cSrcweir * <code>com.sun.star.io.ObjectInputStream</code>. <p>
55*cdf0e10cSrcweir * Object implements the following interfaces :
56*cdf0e10cSrcweir * <ul>
57*cdf0e10cSrcweir *  <li> <code>com::sun::star::io::XInputStream</code></li>
58*cdf0e10cSrcweir *  <li> <code>com::sun::star::io::XMarkableStream</code></li>
59*cdf0e10cSrcweir *  <li> <code>com::sun::star::io::XDataInputStream</code></li>
60*cdf0e10cSrcweir *  <li> <code>com::sun::star::io::XConnectable</code></li>
61*cdf0e10cSrcweir *  <li> <code>com::sun::star::io::XActiveDataSink</code></li>
62*cdf0e10cSrcweir *  <li> <code>com::sun::star::io::XObjectInputStream</code></li>
63*cdf0e10cSrcweir * </ul>
64*cdf0e10cSrcweir * The following files used by this test :
65*cdf0e10cSrcweir * <ul>
66*cdf0e10cSrcweir *  <li><b> MyPersistObjectImpl.jar </b> : the implementation of the persist
67*cdf0e10cSrcweir *       object</li>
68*cdf0e10cSrcweir * </ul> <p>
69*cdf0e10cSrcweir * @see com.sun.star.io.ObjectInputStream
70*cdf0e10cSrcweir * @see com.sun.star.io.XInputStream
71*cdf0e10cSrcweir * @see com.sun.star.io.XMarkableStream
72*cdf0e10cSrcweir * @see com.sun.star.io.XDataInputStream
73*cdf0e10cSrcweir * @see com.sun.star.io.XConnectable
74*cdf0e10cSrcweir * @see com.sun.star.io.XActiveDataSink
75*cdf0e10cSrcweir * @see com.sun.star.io.XObjectInputStream
76*cdf0e10cSrcweir * @see ifc.io._XInputStream
77*cdf0e10cSrcweir * @see ifc.io._XMarkableStream
78*cdf0e10cSrcweir * @see ifc.io._XDataInputStream
79*cdf0e10cSrcweir * @see ifc.io._XConnectable
80*cdf0e10cSrcweir * @see ifc.io._XActiveDataSink
81*cdf0e10cSrcweir * @see ifc.io._XObjectInputStream
82*cdf0e10cSrcweir */
83*cdf0e10cSrcweir public class ObjectInputStream extends TestCase {
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir     /**
86*cdf0e10cSrcweir     * Register the implementation of service
87*cdf0e10cSrcweir     * <code>com.sun.star.cmp.PersistObject</code> if not yet registered.
88*cdf0e10cSrcweir     * @see com.sun.star.cmp.PersistObject
89*cdf0e10cSrcweir     */
90*cdf0e10cSrcweir     public void initialize(TestParameters tParam, PrintWriter log) {
91*cdf0e10cSrcweir         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
92*cdf0e10cSrcweir         Object oPersObj = null;
93*cdf0e10cSrcweir         // test first if object is already registered
94*cdf0e10cSrcweir         try {
95*cdf0e10cSrcweir             oPersObj = xMSF.createInstance("com.sun.star.cmp.PersistObject");
96*cdf0e10cSrcweir         }
97*cdf0e10cSrcweir         catch( com.sun.star.uno.Exception e ) {
98*cdf0e10cSrcweir             log.println("Could not create instance of PersistObject");
99*cdf0e10cSrcweir             e.printStackTrace(log);
100*cdf0e10cSrcweir             log.println("Going on with test...");
101*cdf0e10cSrcweir         }
102*cdf0e10cSrcweir         if ( oPersObj == null ) {
103*cdf0e10cSrcweir             // object is  not available: it has to be registered
104*cdf0e10cSrcweir             String url = util.utils.getFullTestURL
105*cdf0e10cSrcweir                 ("qadevlibs/MyPersistObjectImpl.jar");
106*cdf0e10cSrcweir             XImplementationRegistration xir;
107*cdf0e10cSrcweir             try {
108*cdf0e10cSrcweir                 Object o = xMSF.createInstance(
109*cdf0e10cSrcweir                         "com.sun.star.registry.ImplementationRegistration");
110*cdf0e10cSrcweir                 xir = (XImplementationRegistration)
111*cdf0e10cSrcweir                                     UnoRuntime.queryInterface(
112*cdf0e10cSrcweir                                     XImplementationRegistration.class, o);
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir             }
115*cdf0e10cSrcweir             catch (com.sun.star.uno.Exception e) {
116*cdf0e10cSrcweir                 System.err.println(
117*cdf0e10cSrcweir                             "Couldn't create implementation registration");
118*cdf0e10cSrcweir                 e.printStackTrace();
119*cdf0e10cSrcweir                 throw new StatusException("Couldn't create ImplReg", e);
120*cdf0e10cSrcweir             }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir             XSimpleRegistry xReg = null;
123*cdf0e10cSrcweir             try {
124*cdf0e10cSrcweir                 System.out.println("Register library: " + url);
125*cdf0e10cSrcweir                 xir.registerImplementation(
126*cdf0e10cSrcweir                                     "com.sun.star.loader.Java2", url, xReg);
127*cdf0e10cSrcweir                 System.out.println("...done");
128*cdf0e10cSrcweir             } catch (CannotRegisterImplementationException e) {
129*cdf0e10cSrcweir                 System.err.println("Name: " + url + "  msg: " +
130*cdf0e10cSrcweir                                     e.getMessage());
131*cdf0e10cSrcweir                 e.printStackTrace();
132*cdf0e10cSrcweir                 throw new StatusException(
133*cdf0e10cSrcweir                                     "Couldn't register MyPersistObject", e);
134*cdf0e10cSrcweir             }
135*cdf0e10cSrcweir         }
136*cdf0e10cSrcweir     }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir     /**
139*cdf0e10cSrcweir     * Creating a Testenvironment for the interfaces to be tested.
140*cdf0e10cSrcweir     * Creates an instances of services
141*cdf0e10cSrcweir     * <code>com.sun.star.io.ObjectInputStream</code>,
142*cdf0e10cSrcweir     * <code>com.sun.star.io.ObjectOutputStream</code>,
143*cdf0e10cSrcweir     * <code>com.sun.star.io.Pipe</code>,
144*cdf0e10cSrcweir     * <code>com.sun.star.io.MarkableInputStream</code> and
145*cdf0e10cSrcweir     * <code>com.sun.star.io.MarkableOutputStream</code>. Plugs the created
146*cdf0e10cSrcweir     * markable output stream as output stream for the created
147*cdf0e10cSrcweir     * <code>ObjectOutputStream</code>. Plugs the created pipe as output stream
148*cdf0e10cSrcweir     * for the created <code>MarkableOutputStream</code>. Plugs the created
149*cdf0e10cSrcweir     * markable input stream as input stream for the created
150*cdf0e10cSrcweir     * <code>ObjectInputStream</code>. Plugs the created pipe as input stream
151*cdf0e10cSrcweir     * for the created <code>MarkableInputStream</code>. Creates an instance
152*cdf0e10cSrcweir     * of the service <code>com.sun.star.cmp.PersistObject</code> and writes
153*cdf0e10cSrcweir     * the created object to the object output stream.
154*cdf0e10cSrcweir     * Object relations created :
155*cdf0e10cSrcweir     * <ul>
156*cdf0e10cSrcweir     *  <li> <code>'PersistObject'</code> for
157*cdf0e10cSrcweir     *      {@link ifc.io._XObjectInputStream}(the created instance of the
158*cdf0e10cSrcweir     *      persist object ) </li>
159*cdf0e10cSrcweir     *  <li> <code>'StreamData'</code> for
160*cdf0e10cSrcweir     *      {@link ifc.io._XDataInputStream}(the data that should be written into
161*cdf0e10cSrcweir     *      the stream) </li>
162*cdf0e10cSrcweir     *  <li> <code>'ByteData'</code> for
163*cdf0e10cSrcweir     *      {@link ifc.io._XInputStream}(the data that should be written into
164*cdf0e10cSrcweir     *      the stream) </li>
165*cdf0e10cSrcweir     *  <li> <code>'StreamWriter'</code> for
166*cdf0e10cSrcweir     *      {@link ifc.io._XDataInputStream}
167*cdf0e10cSrcweir     *      {@link ifc.io._XObjectInputStream}
168*cdf0e10cSrcweir     *      {@link ifc.io._XInputStream}(a stream to write data to) </li>
169*cdf0e10cSrcweir     *  <li> <code>'Connectable'</code> for
170*cdf0e10cSrcweir     *      {@link ifc.io._XConnectable}
171*cdf0e10cSrcweir     *      (another object that can be connected) </li>
172*cdf0e10cSrcweir     *  <li> <code>'InputStream'</code> for
173*cdf0e10cSrcweir     *      {@link ifc.io._XActiveDataSink}(an input stream to set and get) </li>
174*cdf0e10cSrcweir     * </ul>
175*cdf0e10cSrcweir     * @see com.sun.star.io.ObjectInputStream
176*cdf0e10cSrcweir     * @see com.sun.star.io.ObjectOutputStream
177*cdf0e10cSrcweir     * @see com.sun.star.io.Pipe
178*cdf0e10cSrcweir     * @see com.sun.star.io.MarkableInputStream
179*cdf0e10cSrcweir     * @see com.sun.star.io.MarkableOutputStream
180*cdf0e10cSrcweir     * @see com.sun.star.cmp.PersistObject
181*cdf0e10cSrcweir     */
182*cdf0e10cSrcweir     public TestEnvironment createTestEnvironment(
183*cdf0e10cSrcweir         TestParameters Param, PrintWriter log) throws StatusException {
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir         System.out.println("create TestEnvironment started.");
186*cdf0e10cSrcweir         XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF();
187*cdf0e10cSrcweir         Object ostream = null;
188*cdf0e10cSrcweir         Object aPipe = null;
189*cdf0e10cSrcweir         Object mostream = null;
190*cdf0e10cSrcweir         Object mistream = null;
191*cdf0e10cSrcweir         Object istream = null;
192*cdf0e10cSrcweir         Object xConnect = null;
193*cdf0e10cSrcweir         try {
194*cdf0e10cSrcweir             istream = xMSF.createInstance
195*cdf0e10cSrcweir                 ("com.sun.star.io.ObjectInputStream");
196*cdf0e10cSrcweir             ostream = xMSF.createInstance
197*cdf0e10cSrcweir                 ("com.sun.star.io.ObjectOutputStream");
198*cdf0e10cSrcweir             aPipe = xMSF.createInstance
199*cdf0e10cSrcweir                 ("com.sun.star.io.Pipe");
200*cdf0e10cSrcweir             mistream = xMSF.createInstance
201*cdf0e10cSrcweir                 ("com.sun.star.io.MarkableInputStream");
202*cdf0e10cSrcweir             mostream = xMSF.createInstance
203*cdf0e10cSrcweir                 ("com.sun.star.io.MarkableOutputStream");
204*cdf0e10cSrcweir             xConnect = (XInterface)xMSF.createInstance
205*cdf0e10cSrcweir                 ("com.sun.star.io.DataInputStream") ;
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir         } catch( com.sun.star.uno.Exception e ) {
208*cdf0e10cSrcweir             e.printStackTrace(log);
209*cdf0e10cSrcweir             throw new StatusException("Couldn't create instance", e);
210*cdf0e10cSrcweir         }
211*cdf0e10cSrcweir         // Creating construction :
212*cdf0e10cSrcweir         // ObjectOutputStream -> MarkableOutputStream -> Pipe ->
213*cdf0e10cSrcweir         // -> MarkableInputStream -> ObjectInputStream
214*cdf0e10cSrcweir         XActiveDataSource xdSo = (XActiveDataSource)
215*cdf0e10cSrcweir             UnoRuntime.queryInterface(XActiveDataSource.class, ostream);
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir         XActiveDataSource xdSmo = (XActiveDataSource)
218*cdf0e10cSrcweir             UnoRuntime.queryInterface(XActiveDataSource.class, mostream);
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir         XOutputStream moStream = (XOutputStream)
221*cdf0e10cSrcweir             UnoRuntime.queryInterface(XOutputStream.class, mostream);
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir         XOutputStream PipeOut = (XOutputStream)
224*cdf0e10cSrcweir             UnoRuntime.queryInterface(XOutputStream.class, aPipe);
225*cdf0e10cSrcweir         XInputStream PipeIn = (XInputStream)
226*cdf0e10cSrcweir             UnoRuntime.queryInterface(XInputStream.class, aPipe);
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir         xdSo.setOutputStream(moStream);
229*cdf0e10cSrcweir         xdSmo.setOutputStream(PipeOut);
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir         XObjectInputStream iStream = (XObjectInputStream)
232*cdf0e10cSrcweir             UnoRuntime.queryInterface(XObjectInputStream.class, istream);
233*cdf0e10cSrcweir         XObjectOutputStream oStream = null;
234*cdf0e10cSrcweir         oStream = (XObjectOutputStream)
235*cdf0e10cSrcweir             UnoRuntime.queryInterface(XObjectOutputStream.class, ostream);
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir         XActiveDataSink xmSi = (XActiveDataSink)
238*cdf0e10cSrcweir             UnoRuntime.queryInterface(XActiveDataSink.class, mistream);
239*cdf0e10cSrcweir         XInputStream xmIstream = (XInputStream)
240*cdf0e10cSrcweir             UnoRuntime.queryInterface(XInputStream.class, mistream);
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir         XActiveDataSink xdSi = (XActiveDataSink) UnoRuntime.queryInterface
243*cdf0e10cSrcweir             (XActiveDataSink.class, istream);
244*cdf0e10cSrcweir         xdSi.setInputStream(xmIstream);
245*cdf0e10cSrcweir         xmSi.setInputStream(PipeIn);
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir         // creating Persist object which has to be written
248*cdf0e10cSrcweir         XPersistObject xPersObj = null;
249*cdf0e10cSrcweir         try {
250*cdf0e10cSrcweir             Object oPersObj = xMSF.createInstance
251*cdf0e10cSrcweir                 ("com.sun.star.cmp.PersistObject");
252*cdf0e10cSrcweir             xPersObj = (XPersistObject)
253*cdf0e10cSrcweir                 UnoRuntime.queryInterface(XPersistObject.class, oPersObj);
254*cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
255*cdf0e10cSrcweir             e.printStackTrace(log);
256*cdf0e10cSrcweir             throw new StatusException("Can't write persist object.", e);
257*cdf0e10cSrcweir         }
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir         // all data types for writing to an XDataInputStream
262*cdf0e10cSrcweir         Vector data = new Vector() ;
263*cdf0e10cSrcweir         data.add(new Boolean(true)) ;
264*cdf0e10cSrcweir         data.add(new Byte((byte)123)) ;
265*cdf0e10cSrcweir         data.add(new Character((char)1234)) ;
266*cdf0e10cSrcweir         data.add(new Short((short)1234)) ;
267*cdf0e10cSrcweir         data.add(new Integer(123456)) ;
268*cdf0e10cSrcweir         data.add(new Float(1.234)) ;
269*cdf0e10cSrcweir         data.add(new Double(1.23456)) ;
270*cdf0e10cSrcweir         data.add("DataInputStream") ;
271*cdf0e10cSrcweir         // information for writing to the pipe
272*cdf0e10cSrcweir         byte[] byteData = new byte[] {
273*cdf0e10cSrcweir             1, 2, 3, 4, 5, 6, 7, 8 } ;
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir         System.out.println("create environment");
277*cdf0e10cSrcweir         XInterface oObj = iStream;
278*cdf0e10cSrcweir         log.println( "creating a new environment for object" );
279*cdf0e10cSrcweir         TestEnvironment tEnv = new TestEnvironment( oObj );
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir         // adding persistent object
282*cdf0e10cSrcweir         tEnv.addObjRelation("PersistObject", xPersObj);
283*cdf0e10cSrcweir         // add a connectable
284*cdf0e10cSrcweir         tEnv.addObjRelation("Connectable", xConnect);
285*cdf0e10cSrcweir         tEnv.addObjRelation("StreamWriter", oStream);
286*cdf0e10cSrcweir         // for XActiveDataSink
287*cdf0e10cSrcweir         tEnv.addObjRelation("InputStream", aPipe);
288*cdf0e10cSrcweir         // adding sequence of data that must be read
289*cdf0e10cSrcweir         // by XDataInputStream interface methods
290*cdf0e10cSrcweir         tEnv.addObjRelation("StreamData", data) ;
291*cdf0e10cSrcweir         // and by XInputStream interface methods
292*cdf0e10cSrcweir         tEnv.addObjRelation("ByteData", byteData) ;
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir         System.out.println("create TestEnvironment finished.");
295*cdf0e10cSrcweir         return tEnv;
296*cdf0e10cSrcweir     } // finish method getTestEnvironment
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir }
299*cdf0e10cSrcweir 
300