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