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 possiblitiy 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.
81 XInterface x
= (XInterface
)tEnv
.getObjRelation("StreamWriter") ;
82 oStream
= UnoRuntime
.queryInterface(
83 XDataOutputStream
.class, x
);
84 List
<Object
> data
= (List
<Object
>) tEnv
.getObjRelation("StreamData") ;
85 if (data
== null || oStream
== null) {
86 throw new StatusException(Status
.failed("Object relation not found."));
89 // extract data from vector
90 Object dataElem
= null ;
91 for (int i
= 0; i
< data
.size(); i
++) {
92 dataElem
= data
.get(i
) ;
94 if (dataElem
instanceof Boolean
) {
95 writeBoolean
= ((Boolean
)dataElem
).booleanValue();
97 if (dataElem
instanceof Byte
) {
98 writeByte
= ((Byte
)dataElem
).byteValue();
100 if (dataElem
instanceof Character
) {
101 writeChar
= ((Character
)dataElem
).charValue();
103 if (dataElem
instanceof Short
) {
104 writeShort
= ((Short
)dataElem
).shortValue();
106 if (dataElem
instanceof Integer
) {
107 writeLong
= ((Integer
)dataElem
).intValue();
109 if (dataElem
instanceof Long
) {
110 writeHyper
= ((Long
)dataElem
).longValue();
112 if (dataElem
instanceof Float
) {
113 writeFloat
= ((Float
)dataElem
).floatValue();
115 if (dataElem
instanceof Double
) {
116 writeDouble
= ((Double
)dataElem
).doubleValue();
118 if (dataElem
instanceof String
) {
119 writeUTF
= (String
)dataElem
;
125 * First writes a value to outStream then reads it from input. <p>
127 * Has <b> OK </b> status if read and written values are equal. <p>
129 public void _readBoolean() {
132 oStream
.writeBoolean(writeBoolean
);
133 } catch (com
.sun
.star
.io
.IOException e
) {
134 e
.printStackTrace(log
);
135 throw new StatusException("Can't write data to the stream", e
);
139 readElem
= oObj
.readBoolean();
140 res
= ((readElem
!= 0) == writeBoolean
);
143 log
.println("Must be read " +
145 " but was read " + (readElem
!= 0)) ;
146 } catch (com
.sun
.star
.io
.IOException e
) {
147 log
.println("Couldn't read Boolean from stream");
148 e
.printStackTrace(log
);
152 tRes
.tested("readBoolean()", res
) ;
156 * First writes a value to outStream then reads it from input. <p>
158 * Has <b> OK </b> status if read and written values are equal. <p>
160 public void _readByte() {
163 oStream
.writeByte(writeByte
);
164 } catch (com
.sun
.star
.io
.IOException e
) {
165 e
.printStackTrace(log
);
166 throw new StatusException("Can't write data to the stream", e
);
170 readElem
= oObj
.readByte() ;
171 res
= (readElem
== writeByte
);
174 log
.println("Must be read " +
176 " but was read " + readElem
);
177 } catch(com
.sun
.star
.io
.IOException e
) {
178 log
.println("Couldn't read Byte from stream");
179 e
.printStackTrace(log
);
183 tRes
.tested("readByte()", res
) ;
187 * First writes a value to outStream then reads it from input. <p>
189 * Has <b> OK </b> status if read and written values are equal. <p>
191 public void _readChar() {
194 oStream
.writeChar(writeChar
);
195 } catch (com
.sun
.star
.io
.IOException e
) {
196 e
.printStackTrace(log
);
197 throw new StatusException("Can't write data to the stream", e
);
201 readElem
= oObj
.readChar() ;
202 res
= (readElem
== writeChar
);
205 log
.println("Must be read " +
207 " but was read " + readElem
) ;
208 } catch( com
.sun
.star
.io
.IOException e
) {
209 log
.println("Couldn't read Char from stream");
210 e
.printStackTrace(log
);
213 tRes
.tested("readChar()", res
);
217 * First writes a value to outStream then reads it from input. <p>
219 * Has <b> OK </b> status if read and written values are equal. <p>
221 public void _readShort() {
224 oStream
.writeShort(writeShort
);
225 } catch (com
.sun
.star
.io
.IOException e
) {
226 e
.printStackTrace(log
);
227 throw new StatusException("Can't write data to the stream", e
);
231 readElem
= oObj
.readShort() ;
232 res
= (readElem
== writeShort
);
235 log
.println("Must be read " +
237 " but was read " + readElem
) ;
238 } catch( com
.sun
.star
.io
.IOException e
) {
239 log
.println("Couldn't read Short from stream");
240 e
.printStackTrace(log
);
243 tRes
.tested("readShort()", res
);
247 * First writes a value to outStream then reads it from input. <p>
249 * Has <b> OK </b> status if read and written values are equal. <p>
251 public void _readLong() {
253 oStream
.writeLong(writeLong
);
254 } catch (com
.sun
.star
.io
.IOException e
) {
255 e
.printStackTrace(log
);
256 throw new StatusException("Can't write data to the stream", e
);
261 readElem
= oObj
.readLong() ;
262 res
= (readElem
== writeLong
);
265 log
.println("Must be read " +
267 " but was read " + readElem
) ;
268 } catch( com
.sun
.star
.io
.IOException e
) {
269 log
.println("Couldn't read Long from stream");
270 e
.printStackTrace(log
);
273 tRes
.tested("readLong()", res
);
277 * First writes a value to outStream then reads it from input. <p>
279 * Has <b> OK </b> status if read and written values are equal. <p>
281 public void _readHyper() {
284 oStream
.writeHyper(writeHyper
);
285 } catch (com
.sun
.star
.io
.IOException e
) {
286 e
.printStackTrace(log
);
287 throw new StatusException("Can't write data to the stream", e
);
291 readElem
= oObj
.readHyper() ;
292 res
= (readElem
== writeHyper
);
295 log
.println("Must be read " +
297 " but was read " + readElem
) ;
298 } catch( com
.sun
.star
.io
.IOException e
) {
299 log
.println("Couldn't read Hyper from stream");
300 e
.printStackTrace(log
);
303 tRes
.tested("readHyper()", res
);
307 * First writes a value to outStream then reads it from input. <p>
309 * Has <b> OK </b> status if read and written values are equal. <p>
311 public void _readFloat() {
314 oStream
.writeFloat(writeFloat
);
315 } catch (com
.sun
.star
.io
.IOException e
) {
316 e
.printStackTrace(log
);
317 throw new StatusException("Can't write data to the stream", e
);
321 readElem
= oObj
.readFloat() ;
322 res
= (readElem
== writeFloat
);
325 log
.println("Must be read " +
327 " but was read " + readElem
) ;
328 } catch( com
.sun
.star
.io
.IOException e
) {
329 log
.println("Couldn't read Float from stream");
330 e
.printStackTrace(log
);
333 tRes
.tested("readFloat()", res
);
337 * First writes a value to outStream then reads it from input. <p>
339 * Has <b> OK </b> status if read and written values are equal. <p>
341 public void _readDouble() {
344 oStream
.writeDouble(writeDouble
);
345 } catch (com
.sun
.star
.io
.IOException e
) {
346 e
.printStackTrace(log
);
347 throw new StatusException("Can't write data to the stream", e
);
351 readElem
= oObj
.readDouble() ;
352 res
= (readElem
== writeDouble
);
355 log
.println("Must be read " +
357 " but was read " + readElem
) ;
358 } catch( com
.sun
.star
.io
.IOException e
) {
359 log
.println("Couldn't read Double from stream");
360 e
.printStackTrace(log
);
363 tRes
.tested("readDouble()", res
);
367 * First writes a value to outStream then reads it from input. <p>
369 * Has <b> OK </b> status if read and written values are equal. <p>
371 public void _readUTF() {
374 oStream
.writeUTF(writeUTF
);
375 } catch (com
.sun
.star
.io
.IOException e
) {
376 e
.printStackTrace(log
);
377 throw new StatusException("Can't write data to the stream", e
);
381 readElem
= oObj
.readUTF() ;
382 res
= writeUTF
.equals(readElem
) ;
385 log
.println("Must be read '" +
387 "' but was read '" + readElem
+ "'") ;
388 } catch( com
.sun
.star
.io
.IOException e
) {
389 log
.println("Couldn't read String from stream");
390 e
.printStackTrace(log
);
393 tRes
.tested("readUTF()", res
);
397 * Forces object environment recreation.
399 public void after() {
402 } catch (com
.sun
.star
.io
.NotConnectedException e
) {
403 e
.printStackTrace(log
);
404 } catch (com
.sun
.star
.io
.BufferSizeExceededException e
) {
405 e
.printStackTrace(log
);
406 } catch (com
.sun
.star
.io
.IOException e
) {
407 e
.printStackTrace(log
);
409 this.disposeEnvironment() ;