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._streams.uno;
25 
26 import com.sun.star.io.NotConnectedException;
27 import com.sun.star.io.XActiveDataSink;
28 import com.sun.star.io.XActiveDataSource;
29 import com.sun.star.io.XInputStream;
30 import com.sun.star.io.XOutputStream;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.uno.UnoRuntime;
33 import com.sun.star.uno.XInterface;
34 import java.io.PrintWriter;
35 import lib.StatusException;
36 import lib.TestCase;
37 import lib.TestEnvironment;
38 import lib.TestParameters;
39 
40 /**
41 * Test for object which is represented by service
42 * <code>com.sun.star.io.Pump</code>. <p>
43 * Object implements the following interfaces :
44 * <ul>
45 *  <li> <code>com::sun::star::io::XActiveDataSource</code></li>
46 *  <li> <code>com::sun::star::io::XActiveDataControl</code></li>
47 *  <li> <code>com::sun::star::io::XActiveDataSink</code></li>
48 * </ul>
49 * @see com.sun.star.io.Pump
50 * @see com.sun.star.io.XActiveDataSource
51 * @see com.sun.star.io.XActiveDataControl
52 * @see com.sun.star.io.XActiveDataSink
53 * @see ifc.io._XActiveDataSource
54 * @see ifc.io._XActiveDataControl
55 * @see ifc.io._XActiveDataSink
56 */
57 public class Pump extends TestCase {
58 
59     /**
60     * Creating a Testenvironment for the interfaces to be tested.
61     * Creates an instance of the service <code>com.sun.star.io.Pump</code>.
62     * Settings up input and output streams for the created pump.
63     * Object relations created :
64     * <ul>
65     *  <li> <code>'InputStream'</code> for
66     *      {@link ifc.io._XActiveDataSource}(an input stream to set) </li>
67     *  <li> <code>'OutputStream'</code> for
68     *      {@link ifc.io._XActiveDataSource}(an output stream to set) </li>
69     * </ul>
70     * @see com.sun.star.io.Pump
71     */
createTestEnvironment(TestParameters Param, PrintWriter log)72     protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
73 
74         Object oInterface = null;
75         XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF();
76         XInterface oPipe;
77 
78         // creating an instance of stm.Pump
79         try {
80             oInterface = xMSF.createInstance( "com.sun.star.io.Pump" );
81             oPipe = (XInterface) xMSF.createInstance( "com.sun.star.io.Pipe" );
82         } catch (com.sun.star.uno.Exception e) {
83             e.printStackTrace(log);
84             throw new StatusException("Can't create the needed objects.", e) ;
85         }
86 
87 
88         XInterface oObj = (XInterface) oInterface;
89 
90         // setting up input and output streams for pump
91         XActiveDataSink xSink = (XActiveDataSink)
92             UnoRuntime.queryInterface(XActiveDataSink.class, oObj);
93         XActiveDataSource xSource = (XActiveDataSource)
94             UnoRuntime.queryInterface(XActiveDataSource.class, oObj);
95 
96         XInputStream xInput = new MyInput();
97         XOutputStream xOutput = new MyOutput();
98 
99         xSink.setInputStream(xInput);
100         xSource.setOutputStream(xOutput);
101 
102         log.println("creating a new environment for object");
103         TestEnvironment tEnv = new TestEnvironment( oObj );
104 
105         // add object relations for ActiveDataSource and XActiveDataSink
106         tEnv.addObjRelation("InputStream", oPipe);
107         tEnv.addObjRelation("OutputStream", oPipe);
108 
109         return tEnv;
110     } // finish method getTestEnvironment
111 
112     /**
113     * XInputStream implementation to use with the test. It returns bytes of
114     * which a simple string consists.
115     */
116     private static class MyInput implements XInputStream  {
117         String str = "Pump tesing string" ;
118 
readBytes(byte[][] bytes, int len)119         public int readBytes(byte[][] bytes, int len)
120             throws NotConnectedException{
121 
122             if (str == null)
123                 throw new NotConnectedException("Input stream was closed");
124 
125             int actual = 0 ;
126             if (len <= str.length()) {
127                 String resStr = str.substring(0, len-1) ;
128                 bytes[0] = resStr.getBytes() ;
129                 actual = len ;
130                 str = str.substring(len) ;
131             } else {
132                 bytes[0] = str.getBytes() ;
133                 actual = str.length() ;
134             }
135 
136             return actual;
137         }
138 
readSomeBytes(byte[][] bytes, int len)139         public int readSomeBytes(byte[][] bytes, int len)
140             throws NotConnectedException{
141             return readBytes(bytes, len);
142         }
143 
skipBytes(int len)144         public void skipBytes(int len) throws NotConnectedException {
145             if (str == null)
146                 throw new NotConnectedException("Stream was closed.") ;
147 
148             if (len >= str.length())
149                 str = "" ;
150             else
151                 str = str.substring(len) ;
152         }
153 
closeInput()154         public void closeInput() throws NotConnectedException {
155             if (str == null)
156                 throw new NotConnectedException("Stream was closed.") ;
157 
158             str = null ;
159         }
160 
available()161         public int available() throws NotConnectedException {
162             if (str == null)
163                 throw new NotConnectedException("Stream was closed.") ;
164 
165             return str.length();
166         }
167     }
168 
169     /**
170     * Dummy XOutputStream implementation to use with the test. Does nothing.
171     */
172     private static class MyOutput implements XOutputStream {
writeBytes(byte[] bytes)173         public void writeBytes(byte[] bytes) {
174         }
175 
flush()176         public void flush() {
177         }
178 
closeOutput()179         public void closeOutput() {
180         }
181     }
182 }
183 
184