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._stm;
25 
26 import java.io.PrintWriter;
27 import java.util.Vector;
28 
29 import lib.StatusException;
30 import lib.TestCase;
31 import lib.TestEnvironment;
32 import lib.TestParameters;
33 
34 import com.sun.star.io.XActiveDataSink;
35 import com.sun.star.io.XActiveDataSource;
36 import com.sun.star.io.XInputStream;
37 import com.sun.star.io.XOutputStream;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.uno.XInterface;
41 
42 /**
43 * Test for object which is represented by service
44 * <code>com.sun.star.io.MarkableOutputStream</code>. <p>
45 * Object implements the following interfaces :
46 * <ul>
47 *  <li> <code>com::sun::star::io::XMarkableStream</code></li>
48 *  <li> <code>com::sun::star::io::XActiveDataSource</code></li>
49 *  <li> <code>com::sun::star::io::XOutputStream</code></li>
50 *  <li> <code>com::sun::star::io::XConnectable</code></li>
51 * </ul>
52 * @see com.sun.star.io.MarkableOutputStream
53 * @see com.sun.star.io.XMarkableStream
54 * @see com.sun.star.io.XActiveDataSource
55 * @see com.sun.star.io.XOutputStream
56 * @see com.sun.star.io.XConnectable
57 * @see ifc.io._XMarkableStream
58 * @see ifc.io._XActiveDataSource
59 * @see ifc.io._XOutputStream
60 * @see ifc.io._XConnectable
61 */
62 public class MarkableOutputStream extends TestCase {
63 
64     /**
65     * Creating a Testenvironment for the interfaces to be tested.
66     * Creates an instances of services <code>com.sun.star.io.Pipe</code>,
67     * <code>com.sun.star.io.MarkableInputStream</code> and
68     * <code>com.sun.star.io.MarkableOutputStream</code>.
69     * Plugs the created pipe as output stream for the created
70     * <code>MarkableOutputStream</code>. Plugs the created pipe as input stream
71     * for the created <code>MarkableInputStream</code>.
72     * Object relations created :
73     * <ul>
74     *  <li> <code>'ByteData'</code> for
75     *      {@link ifc.io._XOutputStream}(the data that should be written into
76     *      the stream) </li>
77     *  <li> <code>'StreamData'</code> for
78     *      {@link ifc.io._XDataOutputStream}(the data that should be
79     *      written into the stream) </li>
80     *  <li> <code>'Connectable'</code> for
81     *      {@link ifc.io._XConnectable}
82     *      (another object that can be connected) </li>
83     *  <li> <code>'OutputStream'</code> for
84     *      {@link ifc.io._XActiveDataSource}
85     *      (an input stream to set and get) </li>
86     *  <li> <code>'XOutputStream.StreamChecker'</code> for
87     *      {@link ifc.io._XOutputStream}( implementation of the interface
88     *      ifc.io._XOutputStream.StreamChecker ) </li>
89     * </ul>
90     * @see com.sun.star.io.Pipe
91     * @see com.sun.star.io.MarkableInputStream
92     * @see com.sun.star.io.MarkableOutputStream
93     */
createTestEnvironment( TestParameters Param, PrintWriter log)94     public TestEnvironment createTestEnvironment(
95         TestParameters Param, PrintWriter log) throws StatusException {
96 
97         XInterface oObj = null;
98 
99         XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF();
100         Object aPipe = null;
101         Object mostream = null;
102         Object mistream = null;
103         XInterface aConnect;
104 
105         try {
106             aPipe = xMSF.createInstance("com.sun.star.io.Pipe");
107             mistream = xMSF.createInstance
108                 ("com.sun.star.io.MarkableInputStream");
109             mostream = xMSF.createInstance
110                 ("com.sun.star.io.MarkableOutputStream");
111             aConnect = (XInterface)xMSF.createInstance
112                 ("com.sun.star.io.DataOutputStream");
113         } catch(com.sun.star.uno.Exception e) {
114             e.printStackTrace(log);
115             throw new StatusException("Couldn't create instance", e) ;
116         }
117 
118         // Creating construction :
119         // MarkableOutputStream -> Pipe -> MarkableInputStream
120         XActiveDataSource xdSmo = (XActiveDataSource)
121             UnoRuntime.queryInterface(XActiveDataSource.class, mostream);
122 
123         final XOutputStream PipeOut = (XOutputStream)
124             UnoRuntime.queryInterface(XOutputStream.class,aPipe);
125         final XInputStream PipeIn = (XInputStream)
126             UnoRuntime.queryInterface(XInputStream.class,aPipe);
127 
128         xdSmo.setOutputStream(PipeOut);
129 
130         XActiveDataSink xmSi = (XActiveDataSink)
131             UnoRuntime.queryInterface(XActiveDataSink.class, mistream);
132 
133         xmSi.setInputStream(PipeIn) ;
134 
135         oObj = (XInterface) mostream;
136 
137         // all data types for writing to an XDataInputStream
138         Vector data = new Vector() ;
139         data.add(new Boolean(true)) ;
140         data.add(new Byte((byte)123)) ;
141         data.add(new Character((char)1234)) ;
142         data.add(new Short((short)1234)) ;
143         data.add(new Integer(123456)) ;
144         data.add(new Float(1.234)) ;
145         data.add(new Double(1.23456)) ;
146         data.add("DataInputStream") ;
147         // information for writing to the pipe
148         byte[] byteData = new byte[] {
149             1, 2, 3, 4, 5, 6, 7, 8 } ;
150 
151         log.println( "creating a new environment for object" );
152         TestEnvironment tEnv = new TestEnvironment( oObj );
153 
154         tEnv.addObjRelation("StreamData", data);
155         tEnv.addObjRelation("ByteData", byteData);
156         tEnv.addObjRelation("OutputStream", aPipe);
157         tEnv.addObjRelation("Connectable", aConnect);
158 
159         //add relation for io.XOutputStream
160         final XMultiServiceFactory msf = xMSF;
161         tEnv.addObjRelation("XOutputStream.StreamChecker",
162             new ifc.io._XOutputStream.StreamChecker() {
163                 XInputStream xInStream = null;
164                 public void resetStreams() {
165                     if (xInStream != null) {
166                         try {
167                             xInStream.closeInput();
168                             xInStream = null;
169                         } catch(com.sun.star.io.IOException e) {
170                         }
171                     } else {
172                         try {
173                             PipeOut.closeOutput();
174                         } catch(com.sun.star.io.IOException e) {
175                         }
176                     }
177                 }
178 
179                 public XInputStream getInStream() {
180                     resetStreams();
181                     try {
182                         Object oInStream = msf.createInstance(
183                             "com.sun.star.io.MarkableInputStream");
184                         xInStream = (XInputStream) UnoRuntime.queryInterface
185                             (XInputStream.class, oInStream);
186                     } catch(com.sun.star.uno.Exception e) {
187                         return null;
188                     }
189 
190                     XActiveDataSink xDataSink = (XActiveDataSink)
191                         UnoRuntime.queryInterface(
192                             XActiveDataSink.class, xInStream);
193                     xDataSink.setInputStream(PipeIn);
194 
195                     return xInStream;
196                 }
197             });
198 
199         return tEnv;
200     } // finish method getTestEnvironment
201 
202 }
203 
204