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