1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Pump.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import java
.io
.PrintWriter
;
35 import lib
.StatusException
;
37 import lib
.TestEnvironment
;
38 import lib
.TestParameters
;
40 import com
.sun
.star
.io
.NotConnectedException
;
41 import com
.sun
.star
.io
.XActiveDataSink
;
42 import com
.sun
.star
.io
.XActiveDataSource
;
43 import com
.sun
.star
.io
.XInputStream
;
44 import com
.sun
.star
.io
.XOutputStream
;
45 import com
.sun
.star
.lang
.XMultiServiceFactory
;
46 import com
.sun
.star
.uno
.UnoRuntime
;
47 import com
.sun
.star
.uno
.XInterface
;
50 * Test for object which is represented by service
51 * <code>com.sun.star.io.Pump</code>. <p>
52 * Object implements the following interfaces :
54 * <li> <code>com::sun::star::io::XActiveDataSource</code></li>
55 * <li> <code>com::sun::star::io::XActiveDataControl</code></li>
56 * <li> <code>com::sun::star::io::XActiveDataSink</code></li>
58 * @see com.sun.star.io.Pump
59 * @see com.sun.star.io.XActiveDataSource
60 * @see com.sun.star.io.XActiveDataControl
61 * @see com.sun.star.io.XActiveDataSink
62 * @see ifc.io._XActiveDataSource
63 * @see ifc.io._XActiveDataControl
64 * @see ifc.io._XActiveDataSink
66 public class Pump
extends TestCase
{
69 * Creating a Testenvironment for the interfaces to be tested.
70 * Creates an instance of the service <code>com.sun.star.io.Pump</code>.
71 * Settings up input and output streams for the created pump.
72 * Object relations created :
74 * <li> <code>'InputStream'</code> for
75 * {@link ifc.io._XActiveDataSource}(an input stream to set) </li>
76 * <li> <code>'OutputStream'</code> for
77 * {@link ifc.io._XActiveDataSource}(an output stream to set) </li>
79 * @see com.sun.star.io.Pump
81 public TestEnvironment
createTestEnvironment(
82 TestParameters Param
, PrintWriter log
) throws StatusException
{
84 Object oInterface
= null;
85 XMultiServiceFactory xMSF
= (XMultiServiceFactory
)Param
.getMSF();
88 // creating an instance of stm.Pump
90 oInterface
= xMSF
.createInstance( "com.sun.star.io.Pump" );
91 oPipe
= (XInterface
) xMSF
.createInstance( "com.sun.star.io.Pipe" );
92 } catch (com
.sun
.star
.uno
.Exception e
) {
93 e
.printStackTrace(log
);
94 throw new StatusException("Can't create the needed objects.", e
) ;
98 XInterface oObj
= (XInterface
) oInterface
;
100 // setting up input and output streams for pump
101 XActiveDataSink xSink
= (XActiveDataSink
)
102 UnoRuntime
.queryInterface(XActiveDataSink
.class, oObj
);
103 XActiveDataSource xSource
= (XActiveDataSource
)
104 UnoRuntime
.queryInterface(XActiveDataSource
.class, oObj
);
106 XInputStream xInput
= new MyInput();
107 XOutputStream xOutput
= new MyOutput();
109 xSink
.setInputStream(xInput
);
110 xSource
.setOutputStream(xOutput
);
112 log
.println("creating a new environment for object");
113 TestEnvironment tEnv
= new TestEnvironment( oObj
);
115 // add object relations for ActiveDataSource and XActiveDataSink
116 tEnv
.addObjRelation("InputStream", oPipe
);
117 tEnv
.addObjRelation("OutputStream", oPipe
);
120 } // finish method getTestEnvironment
123 * XInputStream implementation to use with the test. It returns bytes of
124 * which a simple string consists.
126 private static class MyInput
implements XInputStream
{
127 String str
= "Pump tesing string" ;
129 public int readBytes(byte[][] bytes
, int len
)
130 throws NotConnectedException
{
133 throw new NotConnectedException("Input stream was closed");
136 if (len
<= str
.length()) {
137 String resStr
= str
.substring(0, len
-1) ;
138 bytes
[0] = resStr
.getBytes() ;
140 str
= str
.substring(len
) ;
142 bytes
[0] = str
.getBytes() ;
143 actual
= str
.length() ;
149 public int readSomeBytes(byte[][] bytes
, int len
)
150 throws NotConnectedException
{
151 return readBytes(bytes
, len
);
154 public void skipBytes(int len
) throws NotConnectedException
{
156 throw new NotConnectedException("Stream was closed.") ;
158 if (len
>= str
.length())
161 str
= str
.substring(len
) ;
164 public void closeInput() throws NotConnectedException
{
166 throw new NotConnectedException("Stream was closed.") ;
171 public int available() throws NotConnectedException
{
173 throw new NotConnectedException("Stream was closed.") ;
180 * Dummy XOutputStream implementation to use with the test. Does nothing.
182 private static class MyOutput
implements XOutputStream
{
183 public void writeBytes(byte[] bytes
) {
186 public void flush() {
189 public void closeOutput() {