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 ifc.io;
29 
30 import lib.MultiMethodTest;
31 import lib.Status;
32 
33 import com.sun.star.io.XInputStream;
34 import com.sun.star.io.XOutputStream;
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.uno.XInterface;
37 
38 /**
39 * Testing <code>com.sun.star.io.XInputStream</code>
40 * interface methods:
41 * <ul>
42 *   <li><code>readBytes()</code></li>
43 *   <li><code>readSomeBytes()</code></li>
44 *   <li><code>skipBytes()</code></li>
45 *   <li><code>available()</code></li>
46 *   <li><code>closeInput()</code></li>
47 * </ul> <p>
48 * This test needs the following object relations :
49 * <ul>
50 *  <li> <code>'StreamWriter'</code>:
51 *   object that supports interface <code>XOutputStream</code>;
52 *   a stream to write data to</li>
53 *  <li> <code>'ByteData'</code> (of type <code>byte []</code>):
54 *   data to write to the stream</li>
55 * <ul> <p>
56 
57 * @see com.sun.star.io.XInputStream
58 */
59 public class _XInputStream extends MultiMethodTest {
60 
61     public XInputStream oObj = null;
62     public XOutputStream oStream = null;
63 
64     byte[] bytes = null;
65 
66     int bytesReady = 0 ;
67 
68     /**
69      * Before the test, the stream writer and the data are ecxtracted from
70      * the object relations and the data is written to the stream.
71      */
72     public void before() {
73         XInterface x = (XInterface)tEnv.getObjRelation("StreamWriter");
74         oStream = (XOutputStream)UnoRuntime.queryInterface(
75                                                     XOutputStream.class, x) ;
76         bytes = (byte[])tEnv.getObjRelation("ByteData");
77         try {
78             oStream.writeBytes(bytes);
79         }
80         catch(com.sun.star.io.NotConnectedException e) {}
81         catch(com.sun.star.io.BufferSizeExceededException e) {}
82         catch(com.sun.star.io.IOException e) {}
83     }
84 
85     /**
86      * After the test, the stream writer is closed and the
87      * environment is disposed.
88      */
89     public void after() {
90         try {
91             oStream.flush();
92             oStream.closeOutput();
93         }
94         catch(com.sun.star.io.NotConnectedException e) {}
95         catch(com.sun.star.io.BufferSizeExceededException e) {}
96         catch(com.sun.star.io.IOException e) {}
97         this.disposeEnvironment();
98     }
99     /**
100     * Test calls the method and stores number of available bytes. <p>
101     * Has <b> OK </b> status if the method successfully returns
102     * and no exceptions were thrown. <p>
103     */
104     public void _available() {
105         boolean result = true ;
106         try {
107             bytesReady = oObj.available() ;
108             log.println("Bytes available :" + bytesReady) ;
109         } catch (com.sun.star.io.IOException e){
110             e.printStackTrace(log) ;
111             result = false ;
112         }
113 
114         tRes.tested("available()", result) ;
115     }
116 
117     /**
118     * Test reads one byte from stream. If no bytes available
119     * then test of method is skipped. <p>
120     * Has <b> OK </b> status if returned value equal to number of read bytes,
121     * no exceptions were thrown and read data is not null. <p>
122     * The following method tests are to be completed successfully before :
123     * <ul>
124     *  <li> <code> available() </code> : to have available number
125     *  of bytes in stream </li>
126     * </ul>
127     */
128     public void _readBytes() {
129         requiredMethod("available()") ;
130         boolean result ;
131 
132         if (bytesReady-- > 0) {
133             try {
134                 byte[][] data = new byte[1][1] ;
135                 int read = oObj.readBytes(data, 1) ;
136 
137                 result = read == 1 &&
138                          data != null &&
139                          data.length == 1 ;
140             } catch (com.sun.star.io.IOException e){
141                 e.printStackTrace(log) ;
142                 result = false ;
143             }
144 
145             tRes.tested("readBytes()", result) ;
146         } else {
147             log.println("No more bytes available in the stream");
148             tRes.tested("readBytes()", Status.skipped(false));
149         }
150     }
151 
152     /**
153     * Test reads one byte from stream. If no bytes available
154     * then test of method is skipped. <p>
155     * Has <b> OK </b> status if returned value equal to number of read bytes,
156     * no exceptions were thrown and read data is not null. <p>
157     * The following method tests are to be completed successfully before :
158     * <ul>
159     *  <li> <code> available() </code> : to have available number
160     *  of bytes in stream </li>
161     * </ul>
162     */
163     public void _readSomeBytes() {
164         requiredMethod("available()") ;
165         boolean result ;
166 
167         if (bytesReady-- > 0) {
168             try {
169                 byte[][] data = new byte [1][1] ;
170                 int read = oObj.readSomeBytes(data, 1) ;
171 
172                 result = read == 1 &&
173                          data != null &&
174                          data.length == 1 ;
175             } catch (com.sun.star.io.IOException e){
176                 e.printStackTrace(log) ;
177                 result = false ;
178             }
179             tRes.tested("readSomeBytes()", result) ;
180         } else {
181             log.println("No more bytes available in the stream") ;
182             tRes.tested("readBytes()", Status.skipped(false));
183         }
184     }
185 
186     /**
187     * Test skips one byte from stream. If no bytes available
188     * then test of method is skipped. <p>
189     * Has <b> OK </b> status if the method successfully returns
190     * and no exceptions were thrown. <p>
191     * The following method tests are to be completed successfully before :
192     * <ul>
193     *  <li> <code> available() </code> : to have available number
194     *  of bytes in stream </li>
195     * </ul>
196     */
197     public void _skipBytes() {
198         requiredMethod("available()") ;
199         boolean result ;
200 
201         if (bytesReady-- > 0) {
202             try {
203                 oObj.skipBytes(1) ;
204 
205                 result = true ;
206             } catch (com.sun.star.io.IOException e){
207                 e.printStackTrace(log) ;
208                 result = false ;
209             }
210             tRes.tested("skipBytes()", result) ;
211         } else {
212             log.println("No more bytes available in the stream") ;
213             tRes.tested("readBytes()", Status.skipped(false));
214         }
215     }
216 
217     /**
218     * Test calls the method and forces object environment recreation. <p>
219     * Has <b> OK </b> status if the method successfully returns
220     * and no exceptions were thrown. <p>
221     * The following method tests are to be executed before :
222     * <ul>
223     *  <li> <code> available() </code> </li>
224     *  <li> <code> readBytes() </code> </li>
225     *  <li> <code> readSomeBytes() </code> </li>
226     *  <li> <code> skipBytes() </code> </li>
227     * </ul>
228     */
229     public void _closeInput() {
230         executeMethod("available()") ;
231         executeMethod("readBytes()") ;
232         executeMethod("readSomeBytes()") ;
233         executeMethod("skipBytes()") ;
234 
235         boolean result = true ;
236         try {
237             oObj.closeInput() ;
238         } catch (com.sun.star.io.IOException e){
239             e.printStackTrace(log) ;
240             result = false ;
241         }
242 
243         tRes.tested("closeInput()", result) ;
244         this.disposeEnvironment() ;
245     }
246 }
247 
248