1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package mod._streams.uno;
29 
30 import com.sun.star.io.XActiveDataSink;
31 import com.sun.star.io.XActiveDataSource;
32 import com.sun.star.io.XInputStream;
33 import com.sun.star.io.XOutputStream;
34 import com.sun.star.lang.XMultiServiceFactory;
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.uno.XInterface;
37 import java.io.PrintWriter;
38 import java.util.Vector;
39 import lib.StatusException;
40 import lib.TestCase;
41 import lib.TestEnvironment;
42 import lib.TestParameters;
43 
44 /**
45 * Test for object which is represented by service
46 * <code>com.sun.star.io.MarkableOutputStream</code>. <p>
47 * Object implements the following interfaces :
48 * <ul>
49 *  <li> <code>com::sun::star::io::XMarkableStream</code></li>
50 *  <li> <code>com::sun::star::io::XActiveDataSource</code></li>
51 *  <li> <code>com::sun::star::io::XOutputStream</code></li>
52 *  <li> <code>com::sun::star::io::XConnectable</code></li>
53 * </ul>
54 * @see com.sun.star.io.MarkableOutputStream
55 * @see com.sun.star.io.XMarkableStream
56 * @see com.sun.star.io.XActiveDataSource
57 * @see com.sun.star.io.XOutputStream
58 * @see com.sun.star.io.XConnectable
59 * @see ifc.io._XMarkableStream
60 * @see ifc.io._XActiveDataSource
61 * @see ifc.io._XOutputStream
62 * @see ifc.io._XConnectable
63 */
64 public class MarkableOutputStream extends TestCase {
65 
66     /**
67     * Creating a Testenvironment for the interfaces to be tested.
68     * Creates an instances of services <code>com.sun.star.io.Pipe</code>,
69     * <code>com.sun.star.io.MarkableInputStream</code> and
70     * <code>com.sun.star.io.MarkableOutputStream</code>.
71     * Plugs the created pipe as output stream for the created
72     * <code>MarkableOutputStream</code>. Plugs the created pipe as input stream
73     * for the created <code>MarkableInputStream</code>.
74     * Object relations created :
75     * <ul>
76     *  <li> <code>'ByteData'</code> for
77     *      {@link ifc.io._XOutputStream}(the data that should be written into
78     *      the stream) </li>
79     *  <li> <code>'StreamData'</code> for
80     *      {@link ifc.io._XDataOutputStream}(the data that should be
81     *      written into the stream) </li>
82     *  <li> <code>'Connectable'</code> for
83     *      {@link ifc.io._XConnectable}
84     *      (another object that can be connected) </li>
85     *  <li> <code>'OutputStream'</code> for
86     *      {@link ifc.io._XActiveDataSource}
87     *      (an input stream to set and get) </li>
88     *  <li> <code>'XOutputStream.StreamChecker'</code> for
89     *      {@link ifc.io._XOutputStream}( implementation of the interface
90     *      ifc.io._XOutputStream.StreamChecker ) </li>
91     * </ul>
92     * @see com.sun.star.io.Pipe
93     * @see com.sun.star.io.MarkableInputStream
94     * @see com.sun.star.io.MarkableOutputStream
95     */
96     protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
97 
98         XInterface oObj = null;
99 
100         XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF();
101         Object aPipe = null;
102         Object mostream = null;
103         Object mistream = null;
104         XInterface aConnect;
105 
106         try {
107             aPipe = xMSF.createInstance("com.sun.star.io.Pipe");
108             mistream = xMSF.createInstance
109                 ("com.sun.star.io.MarkableInputStream");
110             mostream = xMSF.createInstance
111                 ("com.sun.star.io.MarkableOutputStream");
112             aConnect = (XInterface)xMSF.createInstance
113                 ("com.sun.star.io.DataOutputStream");
114         } catch(com.sun.star.uno.Exception e) {
115             e.printStackTrace(log);
116             throw new StatusException("Couldn't create instance", e) ;
117         }
118 
119         // Creating construction :
120         // MarkableOutputStream -> Pipe -> MarkableInputStream
121         XActiveDataSource xdSmo = (XActiveDataSource)
122             UnoRuntime.queryInterface(XActiveDataSource.class, mostream);
123 
124         final XOutputStream PipeOut = (XOutputStream)
125             UnoRuntime.queryInterface(XOutputStream.class,aPipe);
126         final XInputStream PipeIn = (XInputStream)
127             UnoRuntime.queryInterface(XInputStream.class,aPipe);
128 
129         xdSmo.setOutputStream(PipeOut);
130 
131         XActiveDataSink xmSi = (XActiveDataSink)
132             UnoRuntime.queryInterface(XActiveDataSink.class, mistream);
133 
134         xmSi.setInputStream(PipeIn) ;
135 
136         oObj = (XInterface) mostream;
137 
138         // all data types for writing to an XDataInputStream
139         Vector data = new Vector() ;
140         data.add(new Boolean(true)) ;
141         data.add(new Byte((byte)123)) ;
142         data.add(new Character((char)1234)) ;
143         data.add(new Short((short)1234)) ;
144         data.add(new Integer(123456)) ;
145         data.add(new Float(1.234)) ;
146         data.add(new Double(1.23456)) ;
147         data.add("DataInputStream") ;
148         // information for writing to the pipe
149         byte[] byteData = new byte[] {
150             1, 2, 3, 4, 5, 6, 7, 8 } ;
151 
152         log.println( "creating a new environment for object" );
153         TestEnvironment tEnv = new TestEnvironment( oObj );
154 
155         tEnv.addObjRelation("StreamData", data);
156         tEnv.addObjRelation("ByteData", byteData);
157         tEnv.addObjRelation("OutputStream", aPipe);
158         tEnv.addObjRelation("Connectable", aConnect);
159 
160         //add relation for io.XOutputStream
161         final XMultiServiceFactory msf = xMSF;
162         tEnv.addObjRelation("XOutputStream.StreamChecker",
163             new ifc.io._XOutputStream.StreamChecker() {
164                 XInputStream xInStream = null;
165                 public void resetStreams() {
166                     if (xInStream != null) {
167                         try {
168                             xInStream.closeInput();
169                             xInStream = null;
170                         } catch(com.sun.star.io.IOException e) {
171                         }
172                     } else {
173                         try {
174                             PipeOut.closeOutput();
175                         } catch(com.sun.star.io.IOException e) {
176                         }
177                     }
178                 }
179 
180                 public XInputStream getInStream() {
181                     resetStreams();
182                     try {
183                         Object oInStream = msf.createInstance(
184                             "com.sun.star.io.MarkableInputStream");
185                         xInStream = (XInputStream) UnoRuntime.queryInterface
186                             (XInputStream.class, oInStream);
187                     } catch(com.sun.star.uno.Exception e) {
188                         return null;
189                     }
190 
191                     XActiveDataSink xDataSink = (XActiveDataSink)
192                         UnoRuntime.queryInterface(
193                             XActiveDataSink.class, xInStream);
194                     xDataSink.setInputStream(PipeIn);
195 
196                     return xInStream;
197                 }
198             });
199 
200         return tEnv;
201     } // finish method getTestEnvironment
202 
203 }
204 
205