Update ooo320-m1
[ooovba.git] / qadevOOo / tests / java / ifc / io / _XDataInputStream.java
blob5e6c3f130d3cb2968623fff80a810b49f9dc0a54
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XDataInputStream.java,v $
10 * $Revision: 1.5 $
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 ************************************************************************/
31 package ifc.io;
33 import java.util.Vector;
35 import lib.MultiMethodTest;
36 import lib.Status;
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;
44 /**
45 * Testing <code>com.sun.star.io.XDataInputStream</code>
46 * interface methods:
47 * <ul>
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>
57 * </ul> <p>
58 * This test needs the following object relations :
59 * <ul>
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>
64 * <ul> <p>
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;
86 /**
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.
91 public void before(){
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();
108 } else
109 if (dataElem instanceof Byte) {
110 writeByte = ((Byte)dataElem).byteValue();
111 } else
112 if (dataElem instanceof Character) {
113 writeChar = ((Character)dataElem).charValue();
114 } else
115 if (dataElem instanceof Short) {
116 writeShort = ((Short)dataElem).shortValue();
117 } else
118 if (dataElem instanceof Integer) {
119 writeLong = ((Integer)dataElem).intValue();
120 } else
121 if (dataElem instanceof Long) {
122 writeHyper = ((Long)dataElem).longValue();
123 } else
124 if (dataElem instanceof Float) {
125 writeFloat = ((Float)dataElem).floatValue();
126 } else
127 if (dataElem instanceof Double) {
128 writeDouble = ((Double)dataElem).doubleValue();
129 } else
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() {
142 boolean res = true ;
143 try {
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);
149 byte readElem;
150 try {
151 readElem = oObj.readBoolean();
152 res = ((readElem != 0) == writeBoolean);
154 if (!res)
155 log.println("Must be read " +
156 writeBoolean +
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);
161 res = false;
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() {
173 boolean res = true ;
174 try {
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);
180 byte readElem;
181 try {
182 readElem = oObj.readByte() ;
183 res = (readElem == writeByte);
185 if (!res)
186 log.println("Must be read " +
187 writeByte +
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);
192 res = false;
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() {
204 boolean res = true ;
205 try {
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);
211 char readElem;
212 try {
213 readElem = oObj.readChar() ;
214 res = (readElem == writeChar);
216 if (!res)
217 log.println("Must be read " +
218 writeChar +
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);
223 res = false;
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() {
234 boolean res = true ;
235 try {
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);
241 short readElem;
242 try {
243 readElem = oObj.readShort() ;
244 res = (readElem == writeShort);
246 if (!res)
247 log.println("Must be read " +
248 writeShort +
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);
253 res = false;
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() {
264 try {
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);
270 boolean res = true ;
271 int readElem;
272 try {
273 readElem = oObj.readLong() ;
274 res = (readElem == writeLong);
276 if (!res)
277 log.println("Must be read " +
278 writeLong +
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);
283 res = false;
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() {
294 boolean res = true ;
295 try {
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);
301 long readElem;
302 try {
303 readElem = oObj.readHyper() ;
304 res = (readElem == writeHyper);
306 if (!res)
307 log.println("Must be read " +
308 writeHyper +
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);
313 res = false;
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() {
324 boolean res = true ;
325 try {
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);
331 float readElem;
332 try {
333 readElem = oObj.readFloat() ;
334 res = (readElem == writeFloat);
336 if (!res)
337 log.println("Must be read " +
338 writeFloat +
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);
343 res = false;
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() {
354 boolean res = true ;
355 try {
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);
361 double readElem;
362 try {
363 readElem = oObj.readDouble() ;
364 res = (readElem == writeDouble);
366 if (!res)
367 log.println("Must be read " +
368 writeDouble +
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);
373 res = false;
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() {
384 boolean res = true ;
385 try {
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);
391 String readElem;
392 try {
393 readElem = oObj.readUTF() ;
394 res = writeUTF.equals(readElem) ;
396 if (!res)
397 log.println("Must be read '" +
398 writeUTF +
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);
403 res = false;
405 tRes.tested("readUTF()", res);
409 * Forces object environment recreation.
411 public void after() {
412 try {
413 oStream.flush();
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() ;