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