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