xref: /AOO41X/main/qadevOOo/tests/java/ifc/io/_XInputStream.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package ifc.io;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import lib.MultiMethodTest;
31*cdf0e10cSrcweir import lib.Status;
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir import com.sun.star.io.XInputStream;
34*cdf0e10cSrcweir import com.sun.star.io.XOutputStream;
35*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
36*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir /**
39*cdf0e10cSrcweir * Testing <code>com.sun.star.io.XInputStream</code>
40*cdf0e10cSrcweir * interface methods:
41*cdf0e10cSrcweir * <ul>
42*cdf0e10cSrcweir *   <li><code>readBytes()</code></li>
43*cdf0e10cSrcweir *   <li><code>readSomeBytes()</code></li>
44*cdf0e10cSrcweir *   <li><code>skipBytes()</code></li>
45*cdf0e10cSrcweir *   <li><code>available()</code></li>
46*cdf0e10cSrcweir *   <li><code>closeInput()</code></li>
47*cdf0e10cSrcweir * </ul> <p>
48*cdf0e10cSrcweir * This test needs the following object relations :
49*cdf0e10cSrcweir * <ul>
50*cdf0e10cSrcweir *  <li> <code>'StreamWriter'</code>:
51*cdf0e10cSrcweir *   object that supports interface <code>XOutputStream</code>;
52*cdf0e10cSrcweir *   a stream to write data to</li>
53*cdf0e10cSrcweir *  <li> <code>'ByteData'</code> (of type <code>byte []</code>):
54*cdf0e10cSrcweir *   data to write to the stream</li>
55*cdf0e10cSrcweir * <ul> <p>
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir * @see com.sun.star.io.XInputStream
58*cdf0e10cSrcweir */
59*cdf0e10cSrcweir public class _XInputStream extends MultiMethodTest {
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir     public XInputStream oObj = null;
62*cdf0e10cSrcweir     public XOutputStream oStream = null;
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir     byte[] bytes = null;
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir     int bytesReady = 0 ;
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir     /**
69*cdf0e10cSrcweir      * Before the test, the stream writer and the data are ecxtracted from
70*cdf0e10cSrcweir      * the object relations and the data is written to the stream.
71*cdf0e10cSrcweir      */
72*cdf0e10cSrcweir     public void before() {
73*cdf0e10cSrcweir         XInterface x = (XInterface)tEnv.getObjRelation("StreamWriter");
74*cdf0e10cSrcweir         oStream = (XOutputStream)UnoRuntime.queryInterface(
75*cdf0e10cSrcweir                                                     XOutputStream.class, x) ;
76*cdf0e10cSrcweir         bytes = (byte[])tEnv.getObjRelation("ByteData");
77*cdf0e10cSrcweir         try {
78*cdf0e10cSrcweir             oStream.writeBytes(bytes);
79*cdf0e10cSrcweir         }
80*cdf0e10cSrcweir         catch(com.sun.star.io.NotConnectedException e) {}
81*cdf0e10cSrcweir         catch(com.sun.star.io.BufferSizeExceededException e) {}
82*cdf0e10cSrcweir         catch(com.sun.star.io.IOException e) {}
83*cdf0e10cSrcweir     }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir     /**
86*cdf0e10cSrcweir      * After the test, the stream writer is closed and the
87*cdf0e10cSrcweir      * environment is disposed.
88*cdf0e10cSrcweir      */
89*cdf0e10cSrcweir     public void after() {
90*cdf0e10cSrcweir         try {
91*cdf0e10cSrcweir             oStream.flush();
92*cdf0e10cSrcweir             oStream.closeOutput();
93*cdf0e10cSrcweir         }
94*cdf0e10cSrcweir         catch(com.sun.star.io.NotConnectedException e) {}
95*cdf0e10cSrcweir         catch(com.sun.star.io.BufferSizeExceededException e) {}
96*cdf0e10cSrcweir         catch(com.sun.star.io.IOException e) {}
97*cdf0e10cSrcweir         this.disposeEnvironment();
98*cdf0e10cSrcweir     }
99*cdf0e10cSrcweir     /**
100*cdf0e10cSrcweir     * Test calls the method and stores number of available bytes. <p>
101*cdf0e10cSrcweir     * Has <b> OK </b> status if the method successfully returns
102*cdf0e10cSrcweir     * and no exceptions were thrown. <p>
103*cdf0e10cSrcweir     */
104*cdf0e10cSrcweir     public void _available() {
105*cdf0e10cSrcweir         boolean result = true ;
106*cdf0e10cSrcweir         try {
107*cdf0e10cSrcweir             bytesReady = oObj.available() ;
108*cdf0e10cSrcweir             log.println("Bytes available :" + bytesReady) ;
109*cdf0e10cSrcweir         } catch (com.sun.star.io.IOException e){
110*cdf0e10cSrcweir             e.printStackTrace(log) ;
111*cdf0e10cSrcweir             result = false ;
112*cdf0e10cSrcweir         }
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir         tRes.tested("available()", result) ;
115*cdf0e10cSrcweir     }
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir     /**
118*cdf0e10cSrcweir     * Test reads one byte from stream. If no bytes available
119*cdf0e10cSrcweir     * then test of method is skipped. <p>
120*cdf0e10cSrcweir     * Has <b> OK </b> status if returned value equal to number of read bytes,
121*cdf0e10cSrcweir     * no exceptions were thrown and read data is not null. <p>
122*cdf0e10cSrcweir     * The following method tests are to be completed successfully before :
123*cdf0e10cSrcweir     * <ul>
124*cdf0e10cSrcweir     *  <li> <code> available() </code> : to have available number
125*cdf0e10cSrcweir     *  of bytes in stream </li>
126*cdf0e10cSrcweir     * </ul>
127*cdf0e10cSrcweir     */
128*cdf0e10cSrcweir     public void _readBytes() {
129*cdf0e10cSrcweir         requiredMethod("available()") ;
130*cdf0e10cSrcweir         boolean result ;
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir         if (bytesReady-- > 0) {
133*cdf0e10cSrcweir             try {
134*cdf0e10cSrcweir                 byte[][] data = new byte[1][1] ;
135*cdf0e10cSrcweir                 int read = oObj.readBytes(data, 1) ;
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir                 result = read == 1 &&
138*cdf0e10cSrcweir                          data != null &&
139*cdf0e10cSrcweir                          data.length == 1 ;
140*cdf0e10cSrcweir             } catch (com.sun.star.io.IOException e){
141*cdf0e10cSrcweir                 e.printStackTrace(log) ;
142*cdf0e10cSrcweir                 result = false ;
143*cdf0e10cSrcweir             }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir             tRes.tested("readBytes()", result) ;
146*cdf0e10cSrcweir         } else {
147*cdf0e10cSrcweir             log.println("No more bytes available in the stream");
148*cdf0e10cSrcweir             tRes.tested("readBytes()", Status.skipped(false));
149*cdf0e10cSrcweir         }
150*cdf0e10cSrcweir     }
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir     /**
153*cdf0e10cSrcweir     * Test reads one byte from stream. If no bytes available
154*cdf0e10cSrcweir     * then test of method is skipped. <p>
155*cdf0e10cSrcweir     * Has <b> OK </b> status if returned value equal to number of read bytes,
156*cdf0e10cSrcweir     * no exceptions were thrown and read data is not null. <p>
157*cdf0e10cSrcweir     * The following method tests are to be completed successfully before :
158*cdf0e10cSrcweir     * <ul>
159*cdf0e10cSrcweir     *  <li> <code> available() </code> : to have available number
160*cdf0e10cSrcweir     *  of bytes in stream </li>
161*cdf0e10cSrcweir     * </ul>
162*cdf0e10cSrcweir     */
163*cdf0e10cSrcweir     public void _readSomeBytes() {
164*cdf0e10cSrcweir         requiredMethod("available()") ;
165*cdf0e10cSrcweir         boolean result ;
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir         if (bytesReady-- > 0) {
168*cdf0e10cSrcweir             try {
169*cdf0e10cSrcweir                 byte[][] data = new byte [1][1] ;
170*cdf0e10cSrcweir                 int read = oObj.readSomeBytes(data, 1) ;
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir                 result = read == 1 &&
173*cdf0e10cSrcweir                          data != null &&
174*cdf0e10cSrcweir                          data.length == 1 ;
175*cdf0e10cSrcweir             } catch (com.sun.star.io.IOException e){
176*cdf0e10cSrcweir                 e.printStackTrace(log) ;
177*cdf0e10cSrcweir                 result = false ;
178*cdf0e10cSrcweir             }
179*cdf0e10cSrcweir             tRes.tested("readSomeBytes()", result) ;
180*cdf0e10cSrcweir         } else {
181*cdf0e10cSrcweir             log.println("No more bytes available in the stream") ;
182*cdf0e10cSrcweir             tRes.tested("readBytes()", Status.skipped(false));
183*cdf0e10cSrcweir         }
184*cdf0e10cSrcweir     }
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir     /**
187*cdf0e10cSrcweir     * Test skips one byte from stream. If no bytes available
188*cdf0e10cSrcweir     * then test of method is skipped. <p>
189*cdf0e10cSrcweir     * Has <b> OK </b> status if the method successfully returns
190*cdf0e10cSrcweir     * and no exceptions were thrown. <p>
191*cdf0e10cSrcweir     * The following method tests are to be completed successfully before :
192*cdf0e10cSrcweir     * <ul>
193*cdf0e10cSrcweir     *  <li> <code> available() </code> : to have available number
194*cdf0e10cSrcweir     *  of bytes in stream </li>
195*cdf0e10cSrcweir     * </ul>
196*cdf0e10cSrcweir     */
197*cdf0e10cSrcweir     public void _skipBytes() {
198*cdf0e10cSrcweir         requiredMethod("available()") ;
199*cdf0e10cSrcweir         boolean result ;
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir         if (bytesReady-- > 0) {
202*cdf0e10cSrcweir             try {
203*cdf0e10cSrcweir                 oObj.skipBytes(1) ;
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir                 result = true ;
206*cdf0e10cSrcweir             } catch (com.sun.star.io.IOException e){
207*cdf0e10cSrcweir                 e.printStackTrace(log) ;
208*cdf0e10cSrcweir                 result = false ;
209*cdf0e10cSrcweir             }
210*cdf0e10cSrcweir             tRes.tested("skipBytes()", result) ;
211*cdf0e10cSrcweir         } else {
212*cdf0e10cSrcweir             log.println("No more bytes available in the stream") ;
213*cdf0e10cSrcweir             tRes.tested("readBytes()", Status.skipped(false));
214*cdf0e10cSrcweir         }
215*cdf0e10cSrcweir     }
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir     /**
218*cdf0e10cSrcweir     * Test calls the method and forces object environment recreation. <p>
219*cdf0e10cSrcweir     * Has <b> OK </b> status if the method successfully returns
220*cdf0e10cSrcweir     * and no exceptions were thrown. <p>
221*cdf0e10cSrcweir     * The following method tests are to be executed before :
222*cdf0e10cSrcweir     * <ul>
223*cdf0e10cSrcweir     *  <li> <code> available() </code> </li>
224*cdf0e10cSrcweir     *  <li> <code> readBytes() </code> </li>
225*cdf0e10cSrcweir     *  <li> <code> readSomeBytes() </code> </li>
226*cdf0e10cSrcweir     *  <li> <code> skipBytes() </code> </li>
227*cdf0e10cSrcweir     * </ul>
228*cdf0e10cSrcweir     */
229*cdf0e10cSrcweir     public void _closeInput() {
230*cdf0e10cSrcweir         executeMethod("available()") ;
231*cdf0e10cSrcweir         executeMethod("readBytes()") ;
232*cdf0e10cSrcweir         executeMethod("readSomeBytes()") ;
233*cdf0e10cSrcweir         executeMethod("skipBytes()") ;
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir         boolean result = true ;
236*cdf0e10cSrcweir         try {
237*cdf0e10cSrcweir             oObj.closeInput() ;
238*cdf0e10cSrcweir         } catch (com.sun.star.io.IOException e){
239*cdf0e10cSrcweir             e.printStackTrace(log) ;
240*cdf0e10cSrcweir             result = false ;
241*cdf0e10cSrcweir         }
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir         tRes.tested("closeInput()", result) ;
244*cdf0e10cSrcweir         this.disposeEnvironment() ;
245*cdf0e10cSrcweir     }
246*cdf0e10cSrcweir }
247*cdf0e10cSrcweir 
248