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