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: _XDataInputStream.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
.util
.Vector
;
35 import lib
.MultiMethodTest
;
37 import lib
.StatusException
;
39 import com
.sun
.star
.io
.XDataInputStream
;
40 import com
.sun
.star
.io
.XDataOutputStream
;
41 import com
.sun
.star
.uno
.UnoRuntime
;
42 import com
.sun
.star
.uno
.XInterface
;
45 * Testing <code>com.sun.star.io.XDataInputStream</code>
48 * <li><code>readBoolean()</code></li>
49 * <li><code>readByte()</code></li>
50 * <li><code>readChar()</code></li>
51 * <li><code>readShort()</code></li>
52 * <li><code>readLong()</code></li>
53 * <li><code>readHyper()</code></li>
54 * <li><code>readFloat()</code></li>
55 * <li><code>readDouble()</code></li>
56 * <li><code>readUTF()</code></li>
58 * This test needs the following object relations :
60 * <li> <code>'StreamData'</code> (of type <code>Vector</code>):
61 * vector of data for comparing with data that obtained from stream </li>
62 * <li> <code>'StreamWriter'</code> (of type <code>XDataOutputStream</code>):
63 * a possiblitiy to write values to the stream. </li>
65 * After test completion object environment has to be recreated.
66 * @see com.sun.star.io.XDataInputStream
67 * @see java.util.Vector
69 public class _XDataInputStream
extends MultiMethodTest
{
71 public XDataInputStream oObj
= null;
72 public XDataOutputStream oStream
= null;
74 // values that are written
75 private boolean writeBoolean
;
76 private byte writeByte
;
77 private char writeChar
;
78 private double writeDouble
;
79 private float writeFloat
;
80 private long writeHyper
;
81 private int writeLong
;
82 private short writeShort
;
83 private String writeUTF
;
87 * Retrieves relations. From relation 'StreamData' extracts
88 * data of different types and fills the appropriate variables.
89 * @throws StatusException If one of relations not found.
93 XInterface x
= (XInterface
)tEnv
.getObjRelation("StreamWriter") ;
94 oStream
= (XDataOutputStream
)UnoRuntime
.queryInterface(
95 XDataOutputStream
.class, x
);
96 Vector data
= (Vector
) tEnv
.getObjRelation("StreamData") ;
97 if (data
== null || oStream
== null) {
98 throw new StatusException(Status
.failed("Object relation not found."));
101 // extract data from vector
102 Object dataElem
= null ;
103 for (int i
= 0; i
< data
.size(); i
++) {
104 dataElem
= data
.get(i
) ;
106 if (dataElem
instanceof Boolean
) {
107 writeBoolean
= ((Boolean
)dataElem
).booleanValue();
109 if (dataElem
instanceof Byte
) {
110 writeByte
= ((Byte
)dataElem
).byteValue();
112 if (dataElem
instanceof Character
) {
113 writeChar
= ((Character
)dataElem
).charValue();
115 if (dataElem
instanceof Short
) {
116 writeShort
= ((Short
)dataElem
).shortValue();
118 if (dataElem
instanceof Integer
) {
119 writeLong
= ((Integer
)dataElem
).intValue();
121 if (dataElem
instanceof Long
) {
122 writeHyper
= ((Long
)dataElem
).longValue();
124 if (dataElem
instanceof Float
) {
125 writeFloat
= ((Float
)dataElem
).floatValue();
127 if (dataElem
instanceof Double
) {
128 writeDouble
= ((Double
)dataElem
).doubleValue();
130 if (dataElem
instanceof String
) {
131 writeUTF
= (String
)dataElem
;
137 * First writes a value to outStream then reads it from input. <p>
139 * Has <b> OK </b> status if read and written values are equal. <p>
141 public void _readBoolean() {
144 oStream
.writeBoolean(writeBoolean
);
145 } catch (com
.sun
.star
.io
.IOException e
) {
146 e
.printStackTrace(log
);
147 throw new StatusException("Can't write data to the stream", e
);
151 readElem
= oObj
.readBoolean();
152 res
= ((readElem
!= 0) == writeBoolean
);
155 log
.println("Must be read " +
157 " but was read " + (readElem
!= 0)) ;
158 } catch (com
.sun
.star
.io
.IOException e
) {
159 log
.println("Couldn't read Boolean from stream");
160 e
.printStackTrace(log
);
164 tRes
.tested("readBoolean()", res
) ;
168 * First writes a value to outStream then reads it from input. <p>
170 * Has <b> OK </b> status if read and written values are equal. <p>
172 public void _readByte() {
175 oStream
.writeByte(writeByte
);
176 } catch (com
.sun
.star
.io
.IOException e
) {
177 e
.printStackTrace(log
);
178 throw new StatusException("Can't write data to the stream", e
);
182 readElem
= oObj
.readByte() ;
183 res
= (readElem
== writeByte
);
186 log
.println("Must be read " +
188 " but was read " + readElem
);
189 } catch(com
.sun
.star
.io
.IOException e
) {
190 log
.println("Couldn't read Byte from stream");
191 e
.printStackTrace(log
);
195 tRes
.tested("readByte()", res
) ;
199 * First writes a value to outStream then reads it from input. <p>
201 * Has <b> OK </b> status if read and written values are equal. <p>
203 public void _readChar() {
206 oStream
.writeChar(writeChar
);
207 } catch (com
.sun
.star
.io
.IOException e
) {
208 e
.printStackTrace(log
);
209 throw new StatusException("Can't write data to the stream", e
);
213 readElem
= oObj
.readChar() ;
214 res
= (readElem
== writeChar
);
217 log
.println("Must be read " +
219 " but was read " + readElem
) ;
220 } catch( com
.sun
.star
.io
.IOException e
) {
221 log
.println("Couldn't read Char from stream");
222 e
.printStackTrace(log
);
225 tRes
.tested("readChar()", res
);
229 * First writes a value to outStream then reads it from input. <p>
231 * Has <b> OK </b> status if read and written values are equal. <p>
233 public void _readShort() {
236 oStream
.writeShort(writeShort
);
237 } catch (com
.sun
.star
.io
.IOException e
) {
238 e
.printStackTrace(log
);
239 throw new StatusException("Can't write data to the stream", e
);
243 readElem
= oObj
.readShort() ;
244 res
= (readElem
== writeShort
);
247 log
.println("Must be read " +
249 " but was read " + readElem
) ;
250 } catch( com
.sun
.star
.io
.IOException e
) {
251 log
.println("Couldn't read Short from stream");
252 e
.printStackTrace(log
);
255 tRes
.tested("readShort()", res
);
259 * First writes a value to outStream then reads it from input. <p>
261 * Has <b> OK </b> status if read and written values are equal. <p>
263 public void _readLong() {
265 oStream
.writeLong(writeLong
);
266 } catch (com
.sun
.star
.io
.IOException e
) {
267 e
.printStackTrace(log
);
268 throw new StatusException("Can't write data to the stream", e
);
273 readElem
= oObj
.readLong() ;
274 res
= (readElem
== writeLong
);
277 log
.println("Must be read " +
279 " but was read " + readElem
) ;
280 } catch( com
.sun
.star
.io
.IOException e
) {
281 log
.println("Couldn't read Long from stream");
282 e
.printStackTrace(log
);
285 tRes
.tested("readLong()", res
);
289 * First writes a value to outStream then reads it from input. <p>
291 * Has <b> OK </b> status if read and written values are equal. <p>
293 public void _readHyper() {
296 oStream
.writeHyper(writeHyper
);
297 } catch (com
.sun
.star
.io
.IOException e
) {
298 e
.printStackTrace(log
);
299 throw new StatusException("Can't write data to the stream", e
);
303 readElem
= oObj
.readHyper() ;
304 res
= (readElem
== writeHyper
);
307 log
.println("Must be read " +
309 " but was read " + readElem
) ;
310 } catch( com
.sun
.star
.io
.IOException e
) {
311 log
.println("Couldn't read Hyper from stream");
312 e
.printStackTrace(log
);
315 tRes
.tested("readHyper()", res
);
319 * First writes a value to outStream then reads it from input. <p>
321 * Has <b> OK </b> status if read and written values are equal. <p>
323 public void _readFloat() {
326 oStream
.writeFloat(writeFloat
);
327 } catch (com
.sun
.star
.io
.IOException e
) {
328 e
.printStackTrace(log
);
329 throw new StatusException("Can't write data to the stream", e
);
333 readElem
= oObj
.readFloat() ;
334 res
= (readElem
== writeFloat
);
337 log
.println("Must be read " +
339 " but was read " + readElem
) ;
340 } catch( com
.sun
.star
.io
.IOException e
) {
341 log
.println("Couldn't read Float from stream");
342 e
.printStackTrace(log
);
345 tRes
.tested("readFloat()", res
);
349 * First writes a value to outStream then reads it from input. <p>
351 * Has <b> OK </b> status if read and written values are equal. <p>
353 public void _readDouble() {
356 oStream
.writeDouble(writeDouble
);
357 } catch (com
.sun
.star
.io
.IOException e
) {
358 e
.printStackTrace(log
);
359 throw new StatusException("Can't write data to the stream", e
);
363 readElem
= oObj
.readDouble() ;
364 res
= (readElem
== writeDouble
);
367 log
.println("Must be read " +
369 " but was read " + readElem
) ;
370 } catch( com
.sun
.star
.io
.IOException e
) {
371 log
.println("Couldn't read Double from stream");
372 e
.printStackTrace(log
);
375 tRes
.tested("readDouble()", res
);
379 * First writes a value to outStream then reads it from input. <p>
381 * Has <b> OK </b> status if read and written values are equal. <p>
383 public void _readUTF() {
386 oStream
.writeUTF(writeUTF
);
387 } catch (com
.sun
.star
.io
.IOException e
) {
388 e
.printStackTrace(log
);
389 throw new StatusException("Can't write data to the stream", e
);
393 readElem
= oObj
.readUTF() ;
394 res
= writeUTF
.equals(readElem
) ;
397 log
.println("Must be read '" +
399 "' but was read '" + readElem
+ "'") ;
400 } catch( com
.sun
.star
.io
.IOException e
) {
401 log
.println("Couldn't read String from stream");
402 e
.printStackTrace(log
);
405 tRes
.tested("readUTF()", res
);
409 * Forces object environment recreation.
411 public void after() {
414 } catch (com
.sun
.star
.io
.NotConnectedException e
) {
415 e
.printStackTrace(log
);
416 } catch (com
.sun
.star
.io
.BufferSizeExceededException e
) {
417 e
.printStackTrace(log
);
418 } catch (com
.sun
.star
.io
.IOException e
) {
419 e
.printStackTrace(log
);
421 this.disposeEnvironment() ;