2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import java
.util
.List
;
23 import lib
.MultiMethodTest
;
25 import lib
.StatusException
;
27 import com
.sun
.star
.io
.XDataInputStream
;
28 import com
.sun
.star
.io
.XDataOutputStream
;
29 import com
.sun
.star
.uno
.UnoRuntime
;
30 import com
.sun
.star
.uno
.XInterface
;
33 * Testing <code>com.sun.star.io.XDataInputStream</code>
36 * <li><code>readBoolean()</code></li>
37 * <li><code>readByte()</code></li>
38 * <li><code>readChar()</code></li>
39 * <li><code>readShort()</code></li>
40 * <li><code>readLong()</code></li>
41 * <li><code>readHyper()</code></li>
42 * <li><code>readFloat()</code></li>
43 * <li><code>readDouble()</code></li>
44 * <li><code>readUTF()</code></li>
46 * This test needs the following object relations :
48 * <li> <code>'StreamData'</code> (of type <code>Vector</code>):
49 * vector of data for comparing with data that obtained from stream </li>
50 * <li> <code>'StreamWriter'</code> (of type <code>XDataOutputStream</code>):
51 * a possibility to write values to the stream. </li>
53 * After test completion object environment has to be recreated.
54 * @see com.sun.star.io.XDataInputStream
55 * @see java.util.Vector
57 public class _XDataInputStream
extends MultiMethodTest
{
59 public XDataInputStream oObj
= null;
60 public XDataOutputStream oStream
= null;
62 // values that are written
63 private boolean writeBoolean
;
64 private byte writeByte
;
65 private char writeChar
;
66 private double writeDouble
;
67 private float writeFloat
;
68 private long writeHyper
;
69 private int writeLong
;
70 private short writeShort
;
71 private String writeUTF
;
75 * Retrieves relations. From relation 'StreamData' extracts
76 * data of different types and fills the appropriate variables.
77 * @throws StatusException If one of relations not found.
79 @SuppressWarnings("unchecked")
83 XInterface x
= (XInterface
)tEnv
.getObjRelation("StreamWriter") ;
84 oStream
= UnoRuntime
.queryInterface(XDataOutputStream
.class, x
);
85 List
<Object
> data
= (List
<Object
>) tEnv
.getObjRelation("StreamData") ;
86 if (data
== null || oStream
== null) {
87 throw new StatusException(Status
.failed("Object relation not found."));
90 // extract data from vector
91 Object dataElem
= null ;
92 for (int i
= 0; i
< data
.size(); i
++) {
93 dataElem
= data
.get(i
) ;
95 if (dataElem
instanceof Boolean
) {
96 writeBoolean
= ((Boolean
)dataElem
).booleanValue();
98 if (dataElem
instanceof Byte
) {
99 writeByte
= ((Byte
)dataElem
).byteValue();
101 if (dataElem
instanceof Character
) {
102 writeChar
= ((Character
)dataElem
).charValue();
104 if (dataElem
instanceof Short
) {
105 writeShort
= ((Short
)dataElem
).shortValue();
107 if (dataElem
instanceof Integer
) {
108 writeLong
= ((Integer
)dataElem
).intValue();
110 if (dataElem
instanceof Long
) {
111 writeHyper
= ((Long
)dataElem
).longValue();
113 if (dataElem
instanceof Float
) {
114 writeFloat
= ((Float
)dataElem
).floatValue();
116 if (dataElem
instanceof Double
) {
117 writeDouble
= ((Double
)dataElem
).doubleValue();
119 if (dataElem
instanceof String
) {
120 writeUTF
= (String
)dataElem
;
126 * First writes a value to outStream then reads it from input. <p>
128 * Has <b> OK </b> status if read and written values are equal. <p>
130 public void _readBoolean() {
133 oStream
.writeBoolean(writeBoolean
);
134 } catch (com
.sun
.star
.io
.IOException e
) {
135 e
.printStackTrace(log
);
136 throw new StatusException("Can't write data to the stream", e
);
140 readElem
= oObj
.readBoolean();
141 res
= ((readElem
!= 0) == writeBoolean
);
144 log
.println("Must be read " +
146 " but was read " + (readElem
!= 0)) ;
147 } catch (com
.sun
.star
.io
.IOException e
) {
148 log
.println("Couldn't read Boolean from stream");
149 e
.printStackTrace(log
);
153 tRes
.tested("readBoolean()", res
) ;
157 * First writes a value to outStream then reads it from input. <p>
159 * Has <b> OK </b> status if read and written values are equal. <p>
161 public void _readByte() {
164 oStream
.writeByte(writeByte
);
165 } catch (com
.sun
.star
.io
.IOException e
) {
166 e
.printStackTrace(log
);
167 throw new StatusException("Can't write data to the stream", e
);
171 readElem
= oObj
.readByte() ;
172 res
= (readElem
== writeByte
);
175 log
.println("Must be read " +
177 " but was read " + readElem
);
178 } catch(com
.sun
.star
.io
.IOException e
) {
179 log
.println("Couldn't read Byte from stream");
180 e
.printStackTrace(log
);
184 tRes
.tested("readByte()", res
) ;
188 * First writes a value to outStream then reads it from input. <p>
190 * Has <b> OK </b> status if read and written values are equal. <p>
192 public void _readChar() {
195 oStream
.writeChar(writeChar
);
196 } catch (com
.sun
.star
.io
.IOException e
) {
197 e
.printStackTrace(log
);
198 throw new StatusException("Can't write data to the stream", e
);
202 readElem
= oObj
.readChar() ;
203 res
= (readElem
== writeChar
);
206 log
.println("Must be read " +
208 " but was read " + readElem
) ;
209 } catch( com
.sun
.star
.io
.IOException e
) {
210 log
.println("Couldn't read Char from stream");
211 e
.printStackTrace(log
);
214 tRes
.tested("readChar()", res
);
218 * First writes a value to outStream then reads it from input. <p>
220 * Has <b> OK </b> status if read and written values are equal. <p>
222 public void _readShort() {
225 oStream
.writeShort(writeShort
);
226 } catch (com
.sun
.star
.io
.IOException e
) {
227 e
.printStackTrace(log
);
228 throw new StatusException("Can't write data to the stream", e
);
232 readElem
= oObj
.readShort() ;
233 res
= (readElem
== writeShort
);
236 log
.println("Must be read " +
238 " but was read " + readElem
) ;
239 } catch( com
.sun
.star
.io
.IOException e
) {
240 log
.println("Couldn't read Short from stream");
241 e
.printStackTrace(log
);
244 tRes
.tested("readShort()", res
);
248 * First writes a value to outStream then reads it from input. <p>
250 * Has <b> OK </b> status if read and written values are equal. <p>
252 public void _readLong() {
254 oStream
.writeLong(writeLong
);
255 } catch (com
.sun
.star
.io
.IOException e
) {
256 e
.printStackTrace(log
);
257 throw new StatusException("Can't write data to the stream", e
);
262 readElem
= oObj
.readLong() ;
263 res
= (readElem
== writeLong
);
266 log
.println("Must be read " +
268 " but was read " + readElem
) ;
269 } catch( com
.sun
.star
.io
.IOException e
) {
270 log
.println("Couldn't read Long from stream");
271 e
.printStackTrace(log
);
274 tRes
.tested("readLong()", res
);
278 * First writes a value to outStream then reads it from input. <p>
280 * Has <b> OK </b> status if read and written values are equal. <p>
282 public void _readHyper() {
285 oStream
.writeHyper(writeHyper
);
286 } catch (com
.sun
.star
.io
.IOException e
) {
287 e
.printStackTrace(log
);
288 throw new StatusException("Can't write data to the stream", e
);
292 readElem
= oObj
.readHyper() ;
293 res
= (readElem
== writeHyper
);
296 log
.println("Must be read " +
298 " but was read " + readElem
) ;
299 } catch( com
.sun
.star
.io
.IOException e
) {
300 log
.println("Couldn't read Hyper from stream");
301 e
.printStackTrace(log
);
304 tRes
.tested("readHyper()", res
);
308 * First writes a value to outStream then reads it from input. <p>
310 * Has <b> OK </b> status if read and written values are equal. <p>
312 public void _readFloat() {
315 oStream
.writeFloat(writeFloat
);
316 } catch (com
.sun
.star
.io
.IOException e
) {
317 e
.printStackTrace(log
);
318 throw new StatusException("Can't write data to the stream", e
);
322 readElem
= oObj
.readFloat() ;
323 res
= (readElem
== writeFloat
);
326 log
.println("Must be read " +
328 " but was read " + readElem
) ;
329 } catch( com
.sun
.star
.io
.IOException e
) {
330 log
.println("Couldn't read Float from stream");
331 e
.printStackTrace(log
);
334 tRes
.tested("readFloat()", res
);
338 * First writes a value to outStream then reads it from input. <p>
340 * Has <b> OK </b> status if read and written values are equal. <p>
342 public void _readDouble() {
345 oStream
.writeDouble(writeDouble
);
346 } catch (com
.sun
.star
.io
.IOException e
) {
347 e
.printStackTrace(log
);
348 throw new StatusException("Can't write data to the stream", e
);
352 readElem
= oObj
.readDouble() ;
353 res
= (readElem
== writeDouble
);
356 log
.println("Must be read " +
358 " but was read " + readElem
) ;
359 } catch( com
.sun
.star
.io
.IOException e
) {
360 log
.println("Couldn't read Double from stream");
361 e
.printStackTrace(log
);
364 tRes
.tested("readDouble()", res
);
368 * First writes a value to outStream then reads it from input. <p>
370 * Has <b> OK </b> status if read and written values are equal. <p>
372 public void _readUTF() {
375 oStream
.writeUTF(writeUTF
);
376 } catch (com
.sun
.star
.io
.IOException e
) {
377 e
.printStackTrace(log
);
378 throw new StatusException("Can't write data to the stream", e
);
382 readElem
= oObj
.readUTF() ;
383 res
= writeUTF
.equals(readElem
) ;
386 log
.println("Must be read '" +
388 "' but was read '" + readElem
+ "'") ;
389 } catch( com
.sun
.star
.io
.IOException e
) {
390 log
.println("Couldn't read String from stream");
391 e
.printStackTrace(log
);
394 tRes
.tested("readUTF()", res
);
398 * Forces object environment recreation.
401 public void after() {
404 } catch (com
.sun
.star
.io
.NotConnectedException e
) {
405 e
.printStackTrace(log
);
406 } catch (com
.sun
.star
.io
.BufferSizeExceededException e
) {
407 e
.printStackTrace(log
);
408 } catch (com
.sun
.star
.io
.IOException e
) {
409 e
.printStackTrace(log
);
411 this.disposeEnvironment() ;