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