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 java.util.Vector;
31 
32 import lib.MultiMethodTest;
33 import lib.Status;
34 import lib.StatusException;
35 
36 import com.sun.star.io.XDataInputStream;
37 import com.sun.star.io.XDataOutputStream;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.uno.XInterface;
40 
41 /**
42 * Testing <code>com.sun.star.io.XDataInputStream</code>
43 * interface methods:
44 * <ul>
45 *   <li><code>readBoolean()</code></li>
46 *   <li><code>readByte()</code></li>
47 *   <li><code>readChar()</code></li>
48 *   <li><code>readShort()</code></li>
49 *   <li><code>readLong()</code></li>
50 *   <li><code>readHyper()</code></li>
51 *   <li><code>readFloat()</code></li>
52 *   <li><code>readDouble()</code></li>
53 *   <li><code>readUTF()</code></li>
54 * </ul> <p>
55 * This test needs the following object relations :
56 * <ul>
57 *  <li> <code>'StreamData'</code> (of type <code>Vector</code>):
58 *   vector of data for comparing with data that obtained from stream </li>
59 *  <li> <code>'StreamWriter'</code> (of type <code>XDataOutputStream</code>):
60 *   a possiblitiy to write values to the stream. </li>
61 * <ul> <p>
62 * After test completion object environment has to be recreated.
63 * @see com.sun.star.io.XDataInputStream
64 * @see java.util.Vector
65 */
66 public class _XDataInputStream extends MultiMethodTest {
67 
68     public XDataInputStream oObj = null;
69     public XDataOutputStream oStream = null;
70 
71     // values that are written
72     private boolean writeBoolean;
73     private byte writeByte;
74     private char writeChar;
75     private double writeDouble;
76     private float writeFloat;
77     private long writeHyper;
78     private int writeLong;
79     private short writeShort;
80     private String writeUTF;
81 
82 
83     /**
84      * Retrieves relations. From relation 'StreamData' extracts
85      * data of different types and fills the appropriate variables.
86      * @throws StatusException If one of relations not found.
87      */
88     public void before(){
89 
90         XInterface x = (XInterface)tEnv.getObjRelation("StreamWriter") ;
91         oStream = (XDataOutputStream)UnoRuntime.queryInterface(
92                                                     XDataOutputStream.class, x);
93         Vector data = (Vector) tEnv.getObjRelation("StreamData") ;
94         if (data == null || oStream == null) {
95             throw new StatusException(Status.failed("Object relation not found."));
96         }
97 
98         // extract data from vector
99         Object dataElem = null ;
100         for (int i = 0; i < data.size(); i++) {
101             dataElem = data.get(i) ;
102 
103             if (dataElem instanceof Boolean) {
104                 writeBoolean = ((Boolean)dataElem).booleanValue();
105             } else
106             if (dataElem instanceof Byte) {
107                 writeByte = ((Byte)dataElem).byteValue();
108             } else
109             if (dataElem instanceof Character) {
110                 writeChar = ((Character)dataElem).charValue();
111             } else
112             if (dataElem instanceof Short) {
113                 writeShort = ((Short)dataElem).shortValue();
114             } else
115             if (dataElem instanceof Integer) {
116                 writeLong = ((Integer)dataElem).intValue();
117             } else
118             if (dataElem instanceof Long) {
119                 writeHyper = ((Long)dataElem).longValue();
120             } else
121             if (dataElem instanceof Float) {
122                 writeFloat = ((Float)dataElem).floatValue();
123             } else
124             if (dataElem instanceof Double) {
125                 writeDouble = ((Double)dataElem).doubleValue();
126             } else
127             if (dataElem instanceof String) {
128                 writeUTF = (String)dataElem;
129             }
130         }
131     }
132 
133     /**
134      * First writes a value to outStream then reads it from input. <p>
135      *
136      * Has <b> OK </b> status if read and written values are equal. <p>
137      */
138     public void _readBoolean() {
139         boolean res = true ;
140         try {
141             oStream.writeBoolean(writeBoolean);
142         } catch (com.sun.star.io.IOException e) {
143             e.printStackTrace(log);
144             throw new StatusException("Can't write data to the stream", e);
145         }
146         byte readElem;
147         try {
148             readElem = oObj.readBoolean();
149             res = ((readElem != 0) == writeBoolean);
150 
151             if (!res)
152                 log.println("Must be read " +
153                     writeBoolean +
154                     " but was read " + (readElem != 0)) ;
155         } catch (com.sun.star.io.IOException e) {
156             log.println("Couldn't  read Boolean from stream");
157             e.printStackTrace(log);
158             res = false;
159         }
160 
161         tRes.tested("readBoolean()", res) ;
162     }
163 
164     /**
165      * First writes a value to outStream then reads it from input. <p>
166      *
167      * Has <b> OK </b> status if read and written values are equal. <p>
168      */
169     public void _readByte() {
170         boolean res = true ;
171         try {
172             oStream.writeByte(writeByte);
173         } catch (com.sun.star.io.IOException e) {
174             e.printStackTrace(log);
175             throw new StatusException("Can't write data to the stream", e);
176         }
177         byte readElem;
178         try {
179             readElem = oObj.readByte() ;
180             res = (readElem == writeByte);
181 
182             if (!res)
183                 log.println("Must be read " +
184                     writeByte +
185                     " but was read " + readElem);
186         } catch(com.sun.star.io.IOException e) {
187             log.println("Couldn't read Byte from stream");
188             e.printStackTrace(log);
189             res = false;
190         }
191 
192         tRes.tested("readByte()", res) ;
193     }
194 
195     /**
196      * First writes a value to outStream then reads it from input. <p>
197      *
198      * Has <b> OK </b> status if read and written values are equal. <p>
199      */
200     public void _readChar() {
201         boolean res = true ;
202         try {
203             oStream.writeChar(writeChar);
204         } catch (com.sun.star.io.IOException e) {
205             e.printStackTrace(log);
206             throw new StatusException("Can't write data to the stream", e);
207         }
208         char readElem;
209         try {
210             readElem = oObj.readChar() ;
211             res = (readElem == writeChar);
212 
213             if (!res)
214                 log.println("Must be read " +
215                     writeChar +
216                     " but was read " + readElem) ;
217         } catch( com.sun.star.io.IOException e ) {
218             log.println("Couldn't read Char from stream");
219             e.printStackTrace(log);
220             res = false;
221         }
222         tRes.tested("readChar()", res);
223     }
224 
225     /**
226      * First writes a value to outStream then reads it from input. <p>
227      *
228      * Has <b> OK </b> status if read and written values are equal. <p>
229      */
230     public void _readShort() {
231         boolean res = true ;
232         try {
233             oStream.writeShort(writeShort);
234         } catch (com.sun.star.io.IOException e) {
235             e.printStackTrace(log);
236             throw new StatusException("Can't write data to the stream", e);
237         }
238         short readElem;
239         try {
240             readElem = oObj.readShort() ;
241             res = (readElem == writeShort);
242 
243             if (!res)
244                 log.println("Must be read " +
245                     writeShort +
246                     " but was read " + readElem) ;
247         } catch( com.sun.star.io.IOException e ) {
248             log.println("Couldn't read Short from stream");
249             e.printStackTrace(log);
250             res = false;
251         }
252         tRes.tested("readShort()", res);
253     }
254 
255     /**
256      * First writes a value to outStream then reads it from input. <p>
257      *
258      * Has <b> OK </b> status if read and written values are equal. <p>
259      */
260     public void _readLong() {
261         try {
262             oStream.writeLong(writeLong);
263         } catch (com.sun.star.io.IOException e) {
264             e.printStackTrace(log);
265             throw new StatusException("Can't write data to the stream", e);
266         }
267         boolean res = true ;
268         int readElem;
269         try {
270             readElem = oObj.readLong() ;
271             res = (readElem == writeLong);
272 
273             if (!res)
274                 log.println("Must be read " +
275                     writeLong +
276                     " but was read " + readElem) ;
277         } catch( com.sun.star.io.IOException e ) {
278             log.println("Couldn't read Long from stream");
279             e.printStackTrace(log);
280             res = false;
281         }
282         tRes.tested("readLong()", res);
283     }
284 
285     /**
286      * First writes a value to outStream then reads it from input. <p>
287      *
288      * Has <b> OK </b> status if read and written values are equal. <p>
289      */
290     public void _readHyper() {
291         boolean res = true ;
292         try {
293             oStream.writeHyper(writeHyper);
294         } catch (com.sun.star.io.IOException e) {
295             e.printStackTrace(log);
296             throw new StatusException("Can't write data to the stream", e);
297         }
298         long readElem;
299         try {
300             readElem = oObj.readHyper() ;
301             res = (readElem == writeHyper);
302 
303             if (!res)
304                 log.println("Must be read " +
305                     writeHyper +
306                     " but was read " + readElem) ;
307         } catch( com.sun.star.io.IOException e ) {
308             log.println("Couldn't read Hyper from stream");
309             e.printStackTrace(log);
310             res = false;
311         }
312         tRes.tested("readHyper()", res);
313     }
314 
315     /**
316      * First writes a value to outStream then reads it from input. <p>
317      *
318      * Has <b> OK </b> status if read and written values are equal. <p>
319      */
320     public void _readFloat() {
321         boolean res = true ;
322         try {
323             oStream.writeFloat(writeFloat);
324         } catch (com.sun.star.io.IOException e) {
325             e.printStackTrace(log);
326             throw new StatusException("Can't write data to the stream", e);
327         }
328         float readElem;
329         try {
330             readElem = oObj.readFloat() ;
331             res = (readElem == writeFloat);
332 
333             if (!res)
334                 log.println("Must be read " +
335                     writeFloat +
336                     " but was read " + readElem) ;
337         } catch( com.sun.star.io.IOException e ) {
338             log.println("Couldn't read Float from stream");
339             e.printStackTrace(log);
340             res = false;
341         }
342         tRes.tested("readFloat()", res);
343     }
344 
345     /**
346      * First writes a value to outStream then reads it from input. <p>
347      *
348      * Has <b> OK </b> status if read and written values are equal. <p>
349      */
350     public void _readDouble() {
351         boolean res = true ;
352         try {
353             oStream.writeDouble(writeDouble);
354         } catch (com.sun.star.io.IOException e) {
355             e.printStackTrace(log);
356             throw new StatusException("Can't write data to the stream", e);
357         }
358         double readElem;
359         try {
360             readElem = oObj.readDouble() ;
361             res = (readElem == writeDouble);
362 
363             if (!res)
364                 log.println("Must be read " +
365                     writeDouble +
366                     " but was read " + readElem) ;
367         } catch( com.sun.star.io.IOException e ) {
368             log.println("Couldn't read Double from stream");
369             e.printStackTrace(log);
370             res = false;
371         }
372         tRes.tested("readDouble()", res);
373     }
374 
375     /**
376      * First writes a value to outStream then reads it from input. <p>
377      *
378      * Has <b> OK </b> status if read and written values are equal. <p>
379      */
380     public void _readUTF() {
381         boolean res = true ;
382         try {
383             oStream.writeUTF(writeUTF);
384         } catch (com.sun.star.io.IOException e) {
385             e.printStackTrace(log);
386             throw new StatusException("Can't write data to the stream", e);
387         }
388         String readElem;
389         try {
390             readElem = oObj.readUTF() ;
391             res = writeUTF.equals(readElem) ;
392 
393             if (!res)
394                 log.println("Must be read '" +
395                     writeUTF +
396                     "' but was read '" + readElem + "'") ;
397         } catch( com.sun.star.io.IOException e ) {
398             log.println("Couldn't read String from stream");
399             e.printStackTrace(log);
400             res = false;
401         }
402         tRes.tested("readUTF()", res);
403     }
404 
405     /**
406      * Forces object environment recreation.
407      */
408     public void after() {
409         try {
410             oStream.flush();
411         } catch (com.sun.star.io.NotConnectedException e) {
412             e.printStackTrace(log);
413         } catch (com.sun.star.io.BufferSizeExceededException e) {
414             e.printStackTrace(log);
415         } catch (com.sun.star.io.IOException e) {
416             e.printStackTrace(log);
417         }
418         this.disposeEnvironment() ;
419     }
420 }
421 
422