1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package ifc.io;
25 
26 import lib.MultiMethodTest;
27 import lib.Status;
28 
29 import com.sun.star.io.XInputStream;
30 import com.sun.star.io.XOutputStream;
31 import com.sun.star.uno.UnoRuntime;
32 import com.sun.star.uno.XInterface;
33 
34 /**
35 * Testing <code>com.sun.star.io.XInputStream</code>
36 * interface methods:
37 * <ul>
38 *   <li><code>readBytes()</code></li>
39 *   <li><code>readSomeBytes()</code></li>
40 *   <li><code>skipBytes()</code></li>
41 *   <li><code>available()</code></li>
42 *   <li><code>closeInput()</code></li>
43 * </ul> <p>
44 * This test needs the following object relations :
45 * <ul>
46 *  <li> <code>'StreamWriter'</code>:
47 *   object that supports interface <code>XOutputStream</code>;
48 *   a stream to write data to</li>
49 *  <li> <code>'ByteData'</code> (of type <code>byte []</code>):
50 *   data to write to the stream</li>
51 * <ul> <p>
52 
53 * @see com.sun.star.io.XInputStream
54 */
55 public class _XInputStream extends MultiMethodTest {
56 
57     public XInputStream oObj = null;
58     public XOutputStream oStream = null;
59 
60     byte[] bytes = null;
61 
62     int bytesReady = 0 ;
63 
64     /**
65      * Before the test, the stream writer and the data are ecxtracted from
66      * the object relations and the data is written to the stream.
67      */
before()68     public void before() {
69         XInterface x = (XInterface)tEnv.getObjRelation("StreamWriter");
70         oStream = (XOutputStream)UnoRuntime.queryInterface(
71                                                     XOutputStream.class, x) ;
72         bytes = (byte[])tEnv.getObjRelation("ByteData");
73         try {
74             oStream.writeBytes(bytes);
75         }
76         catch(com.sun.star.io.NotConnectedException e) {}
77         catch(com.sun.star.io.BufferSizeExceededException e) {}
78         catch(com.sun.star.io.IOException e) {}
79     }
80 
81     /**
82      * After the test, the stream writer is closed and the
83      * environment is disposed.
84      */
after()85     public void after() {
86         try {
87             oStream.flush();
88             oStream.closeOutput();
89         }
90         catch(com.sun.star.io.NotConnectedException e) {}
91         catch(com.sun.star.io.BufferSizeExceededException e) {}
92         catch(com.sun.star.io.IOException e) {}
93         this.disposeEnvironment();
94     }
95     /**
96     * Test calls the method and stores number of available bytes. <p>
97     * Has <b> OK </b> status if the method successfully returns
98     * and no exceptions were thrown. <p>
99     */
_available()100     public void _available() {
101         boolean result = true ;
102         try {
103             bytesReady = oObj.available() ;
104             log.println("Bytes available :" + bytesReady) ;
105         } catch (com.sun.star.io.IOException e){
106             e.printStackTrace(log) ;
107             result = false ;
108         }
109 
110         tRes.tested("available()", result) ;
111     }
112 
113     /**
114     * Test reads one byte from stream. If no bytes available
115     * then test of method is skipped. <p>
116     * Has <b> OK </b> status if returned value equal to number of read bytes,
117     * no exceptions were thrown and read data is not null. <p>
118     * The following method tests are to be completed successfully before :
119     * <ul>
120     *  <li> <code> available() </code> : to have available number
121     *  of bytes in stream </li>
122     * </ul>
123     */
_readBytes()124     public void _readBytes() {
125         requiredMethod("available()") ;
126         boolean result ;
127 
128         if (bytesReady-- > 0) {
129             try {
130                 byte[][] data = new byte[1][1] ;
131                 int read = oObj.readBytes(data, 1) ;
132 
133                 result = read == 1 &&
134                          data != null &&
135                          data.length == 1 ;
136             } catch (com.sun.star.io.IOException e){
137                 e.printStackTrace(log) ;
138                 result = false ;
139             }
140 
141             tRes.tested("readBytes()", result) ;
142         } else {
143             log.println("No more bytes available in the stream");
144             tRes.tested("readBytes()", Status.skipped(false));
145         }
146     }
147 
148     /**
149     * Test reads one byte from stream. If no bytes available
150     * then test of method is skipped. <p>
151     * Has <b> OK </b> status if returned value equal to number of read bytes,
152     * no exceptions were thrown and read data is not null. <p>
153     * The following method tests are to be completed successfully before :
154     * <ul>
155     *  <li> <code> available() </code> : to have available number
156     *  of bytes in stream </li>
157     * </ul>
158     */
_readSomeBytes()159     public void _readSomeBytes() {
160         requiredMethod("available()") ;
161         boolean result ;
162 
163         if (bytesReady-- > 0) {
164             try {
165                 byte[][] data = new byte [1][1] ;
166                 int read = oObj.readSomeBytes(data, 1) ;
167 
168                 result = read == 1 &&
169                          data != null &&
170                          data.length == 1 ;
171             } catch (com.sun.star.io.IOException e){
172                 e.printStackTrace(log) ;
173                 result = false ;
174             }
175             tRes.tested("readSomeBytes()", result) ;
176         } else {
177             log.println("No more bytes available in the stream") ;
178             tRes.tested("readBytes()", Status.skipped(false));
179         }
180     }
181 
182     /**
183     * Test skips one byte from stream. If no bytes available
184     * then test of method is skipped. <p>
185     * Has <b> OK </b> status if the method successfully returns
186     * and no exceptions were thrown. <p>
187     * The following method tests are to be completed successfully before :
188     * <ul>
189     *  <li> <code> available() </code> : to have available number
190     *  of bytes in stream </li>
191     * </ul>
192     */
_skipBytes()193     public void _skipBytes() {
194         requiredMethod("available()") ;
195         boolean result ;
196 
197         if (bytesReady-- > 0) {
198             try {
199                 oObj.skipBytes(1) ;
200 
201                 result = true ;
202             } catch (com.sun.star.io.IOException e){
203                 e.printStackTrace(log) ;
204                 result = false ;
205             }
206             tRes.tested("skipBytes()", result) ;
207         } else {
208             log.println("No more bytes available in the stream") ;
209             tRes.tested("readBytes()", Status.skipped(false));
210         }
211     }
212 
213     /**
214     * Test calls the method and forces object environment recreation. <p>
215     * Has <b> OK </b> status if the method successfully returns
216     * and no exceptions were thrown. <p>
217     * The following method tests are to be executed before :
218     * <ul>
219     *  <li> <code> available() </code> </li>
220     *  <li> <code> readBytes() </code> </li>
221     *  <li> <code> readSomeBytes() </code> </li>
222     *  <li> <code> skipBytes() </code> </li>
223     * </ul>
224     */
_closeInput()225     public void _closeInput() {
226         executeMethod("available()") ;
227         executeMethod("readBytes()") ;
228         executeMethod("readSomeBytes()") ;
229         executeMethod("skipBytes()") ;
230 
231         boolean result = true ;
232         try {
233             oObj.closeInput() ;
234         } catch (com.sun.star.io.IOException e){
235             e.printStackTrace(log) ;
236             result = false ;
237         }
238 
239         tRes.tested("closeInput()", result) ;
240         this.disposeEnvironment() ;
241     }
242 }
243 
244