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.DataOutputStream</code>. <p>
45 * Object implements the following interfaces :
46 * <ul>
47 *  <li> <code>com::sun::star::io::XActiveDataSource</code></li>
48 *  <li> <code>com::sun::star::io::XOutputStream</code></li>
49 *  <li> <code>com::sun::star::io::XDataOutputStream</code></li>
50 * </ul>
51 * @see com.sun.star.io.DataOutputStream
52 * @see com.sun.star.io.XActiveDataSource
53 * @see com.sun.star.io.XOutputStream
54 * @see com.sun.star.io.XDataOutputStream
55 * @see ifc.io._XActiveDataSource
56 * @see ifc.io._XOutputStream
57 * @see ifc.io._XDataOutputStream
58 */
59 public class DataOutputStream extends TestCase {
60 
61     /**
62     * Creating a Testenvironment for the interfaces to be tested.
63     * Creates an instance of the service
64     * <code>com.sun.star.io.DataOutputStream</code>
65     * and an instance of the service <code>com.sun.star.io.Pipe</code>.
66     * Plugs the created pipe as output stream for the created DataOutputStream.
67     * @see com.sun.star.io.DataOutputStream
68     * Object relations created :
69     * <ul>
70     *  <li> <code>'StreamData'</code> for
71     *      {@link ifc.io._XDataInputStream}(the data that should be written into
72     *      the stream) </li>
73     *  <li> <code>'ByteData'</code> for
74     *      {@link ifc.io._XInputStream}(the data that should be written into
75     *      the stream) </li>
76     *  <li> <code>'OutputStream'</code> for
77     *      {@link ifc.io._XActiveDataSource}
78     *      (an input stream to set and get) </li>
79     *  <li> <code>'XOutputStream.StreamChecker'</code> for
80     *      {@link ifc.io._XOutputStream}( implementation of the interface
81     *      ifc.io._XOutputStream.StreamChecker ) </li>
82     * </ul>
83     */
createTestEnvironment( TestParameters Param, PrintWriter log)84     public TestEnvironment createTestEnvironment(
85         TestParameters Param, PrintWriter log) throws StatusException {
86 
87         XInterface oObj = null;
88         Object oInterface = null;
89         XInterface oPipe = null;
90         XMultiServiceFactory xMSF = null ;
91 
92         try {
93             xMSF = (XMultiServiceFactory)Param.getMSF();
94             oInterface = xMSF.createInstance
95                 ("com.sun.star.io.DataOutputStream");
96             oPipe = (XInterface)xMSF.createInstance("com.sun.star.io.Pipe");
97         } catch(com.sun.star.uno.Exception e) {
98             e.printStackTrace(log);
99             throw new StatusException("Couldn't create instance", e);
100         }
101 
102         oObj = (XInterface) oInterface;
103 
104         final XOutputStream xPipeOutput = (XOutputStream)
105             UnoRuntime.queryInterface(XOutputStream.class, oPipe);
106 
107         XActiveDataSource xDataSource = (XActiveDataSource)
108             UnoRuntime.queryInterface(XActiveDataSource.class, oObj);
109 
110         xDataSource.setOutputStream(xPipeOutput);
111 
112         // all data types for writing to an XDataInputStream
113         Vector data = new Vector() ;
114         data.add(new Boolean(true)) ;
115         data.add(new Byte((byte)123)) ;
116         data.add(new Character((char)1234)) ;
117         data.add(new Short((short)1234)) ;
118         data.add(new Integer(123456)) ;
119         data.add(new Float(1.234)) ;
120         data.add(new Double(1.23456)) ;
121         data.add("DataInputStream") ;
122         // information for writing to the pipe
123         byte[] byteData = new byte[] {
124             1, 2, 3, 4, 5, 6, 7, 8 } ;
125 
126         log.println("creating a new environment for object");
127         TestEnvironment tEnv = new TestEnvironment( oObj );
128 
129         tEnv.addObjRelation("StreamData", data);
130         tEnv.addObjRelation("ByteData", byteData);
131         tEnv.addObjRelation("OutputStream", oPipe);
132 
133         //add relation for io.XOutputStream
134         final XMultiServiceFactory msf = xMSF;
135         final XInputStream xPipeInput = (XInputStream)
136             UnoRuntime.queryInterface(XInputStream.class, oPipe);
137         tEnv.addObjRelation("XOutputStream.StreamChecker",
138             new ifc.io._XOutputStream.StreamChecker() {
139                 XInputStream xInStream = null;
140                 public void resetStreams() {
141                     if (xInStream != null) {
142                         try {
143                             xInStream.closeInput();
144                             xInStream = null;
145                         } catch(com.sun.star.io.IOException e) {
146                         }
147                     } else {
148                         try {
149                             xPipeOutput.closeOutput();
150                         } catch(com.sun.star.io.IOException e) {
151                         }
152                     }
153                 }
154 
155                 public XInputStream getInStream() {
156                     resetStreams();
157                     try {
158                         Object oInStream = msf.createInstance(
159                             "com.sun.star.io.DataInputStream");
160                         xInStream = (XInputStream) UnoRuntime.queryInterface
161                             (XInputStream.class, oInStream);
162                     } catch(com.sun.star.uno.Exception e) {
163                         return null;
164                     }
165 
166                     XActiveDataSink xDataSink = (XActiveDataSink)
167                         UnoRuntime.queryInterface(
168                             XActiveDataSink.class, xInStream);
169                     xDataSink.setInputStream(xPipeInput);
170 
171                     return xInStream;
172                 }
173             });
174 
175         return tEnv;
176     } // finish method getTestEnvironment
177 }
178 
179