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.XDataOutputStream;
37 import com.sun.star.io.XInputStream;
38 import com.sun.star.io.XOutputStream;
39 import com.sun.star.lang.XMultiServiceFactory;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.uno.XInterface;
42 
43 /**
44 * Test for object which is represented by service
45 * <code>com.sun.star.io.DataInputStream</code>.
46 * <ul>
47 *  <li> <code>com::sun::star::io::XInputStream</code></li>
48 *  <li> <code>com::sun::star::io::XDataInputStream</code></li>
49 *  <li> <code>com::sun::star::io::XConnectable</code></li>
50 *  <li> <code>com::sun::star::io::XActiveDataSink</code></li>
51 * </ul>
52 * @see com.sun.star.io.DataInputStream
53 * @see com.sun.star.io.XInputStream
54 * @see com.sun.star.io.XDataInputStream
55 * @see com.sun.star.io.XConnectable
56 * @see com.sun.star.io.XActiveDataSink
57 * @see ifc.io._XInputStream
58 * @see ifc.io._XDataInputStream
59 * @see ifc.io._XConnectable
60 * @see ifc.io._XActiveDataSink
61 */
62 public class DataInputStream extends TestCase {
63 
64     /**
65     * Creates a Testenvironment for the interfaces to be tested.
66     * Creates <code>com.sun.star.io.DataInputStream</code> object,
67     * connects it to <code>com.sun.star.io.DataOutputStream</code>
68     * through <code>com.sun.star.io.Pipe</code>. All of possible data
69     * types are written into <code>DataOutputStream</code>.
70     * Object relations created :
71     * <ul>
72     *  <li> <code>'StreamData'</code> for
73     *      {@link ifc.io._XDataInputStream}(the data that should be written into
74     *      the stream) </li>
75     *  <li> <code>'ByteData'</code> for
76     *      {@link ifc.io._XInputStream}(the data that should be written into
77     *      the stream) </li>
78     *  <li> <code>'StreamWriter'</code> for
79     *      {@link ifc.io._XDataInputStream}
80     *      {@link ifc.io._XInputStream}(a stream to write data to) </li>
81     *  <li> <code>'Connectable'</code> for
82     *      {@link ifc.io._XConnectable}(another object that can be connected) </li>
83     *  <li> <code>'InputStream'</code> for
84     *      {@link ifc.io._XActiveDataSink}(an input stream to set and get) </li>
85     * </ul>
86     */
87     public TestEnvironment createTestEnvironment(
88         TestParameters Param, PrintWriter log) throws StatusException {
89 
90         Object oInterface = null;
91 
92         XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF();;
93         try {
94             oInterface = xMSF.createInstance("com.sun.star.io.DataInputStream");
95         } catch(com.sun.star.uno.Exception e) {
96             e.printStackTrace(log);
97             throw new StatusException("Couldn't create instance", e);
98         }
99 
100         XInterface oObj = (XInterface) oInterface;
101 
102         // creating and connecting DataOutputStream to the
103         // DataInputStream created through the Pipe
104         XActiveDataSink xDataSink = (XActiveDataSink)
105             UnoRuntime.queryInterface(XActiveDataSink.class, oObj);
106 
107         XInterface oPipe = null;
108         try {
109             oPipe = (XInterface)
110                 xMSF.createInstance("com.sun.star.io.Pipe");
111         } catch(com.sun.star.uno.Exception e) {
112             e.printStackTrace(log);
113             throw new StatusException("Couldn't create instance", e);
114         }
115 
116         XInputStream xPipeInput = (XInputStream)
117             UnoRuntime.queryInterface(XInputStream.class, oPipe);
118         XOutputStream xPipeOutput = (XOutputStream)
119             UnoRuntime.queryInterface(XOutputStream.class, oPipe);
120 
121         XInterface oDataOutput = null;
122         try {
123             oDataOutput = (XInterface)
124                 xMSF.createInstance("com.sun.star.io.DataOutputStream");
125         } catch(com.sun.star.uno.Exception e) {
126             e.printStackTrace(log);
127             throw new StatusException("Couldn't create instance", e);
128         }
129 
130         XDataOutputStream xDataOutput = (XDataOutputStream)
131             UnoRuntime.queryInterface(XDataOutputStream.class, oDataOutput) ;
132         XActiveDataSource xDataSource = (XActiveDataSource)
133             UnoRuntime.queryInterface(XActiveDataSource.class, oDataOutput) ;
134 
135         xDataSource.setOutputStream(xPipeOutput) ;
136         xDataSink.setInputStream(xPipeInput) ;
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         // createing a connectable object for XConnectable interface
153         XInterface xConnect = null;
154         try {
155             xConnect = (XInterface)xMSF.createInstance(
156                                     "com.sun.star.io.DataInputStream") ;
157         } catch (Exception e) {
158             log.println("Can't create DataInputStream");
159             e.printStackTrace(log);
160             throw new StatusException("Can't create DataInputStream", e);
161         }
162 
163         // creating an input stream to set in XActiveDataSink
164         XInterface oDataInput = null;
165         try {
166             oDataInput = (XInterface) xMSF.createInstance(
167                                         "com.sun.star.io.Pipe" );
168         } catch (com.sun.star.uno.Exception e) {
169             log.println("Can't create new in stream") ;
170             e.printStackTrace(log) ;
171             throw new StatusException("Can't create input stream", e) ;
172         }
173 
174 
175         log.println("creating a new environment for object");
176         TestEnvironment tEnv = new TestEnvironment( oObj );
177 
178         // adding sequence of data that must be read
179         // by XDataInputStream interface methods
180         tEnv.addObjRelation("StreamData", data) ;
181         // add a writer
182         tEnv.addObjRelation("StreamWriter", xDataOutput);
183         // add a connectable
184         tEnv.addObjRelation("Connectable", xConnect);
185         // add an inputStream
186         tEnv.addObjRelation("InputStream", oDataInput);
187         tEnv.addObjRelation("ByteData", byteData);
188 
189         return tEnv;
190     } // finish method getTestEnvironment
191 
192 }
193 
194