Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / io / _XDataInputStream.java
blobe451b8e7eb05c61377836730f39a7843eb481424
1 /*
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 .
19 package ifc.io;
21 import java.util.List;
23 import lib.MultiMethodTest;
24 import lib.Status;
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;
32 /**
33 * Testing <code>com.sun.star.io.XDataInputStream</code>
34 * interface methods:
35 * <ul>
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>
45 * </ul> <p>
46 * This test needs the following object relations :
47 * <ul>
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>
52 * <ul> <p>
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;
74 /**
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")
80 @Override
81 public void before(){
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();
97 } else
98 if (dataElem instanceof Byte) {
99 writeByte = ((Byte)dataElem).byteValue();
100 } else
101 if (dataElem instanceof Character) {
102 writeChar = ((Character)dataElem).charValue();
103 } else
104 if (dataElem instanceof Short) {
105 writeShort = ((Short)dataElem).shortValue();
106 } else
107 if (dataElem instanceof Integer) {
108 writeLong = ((Integer)dataElem).intValue();
109 } else
110 if (dataElem instanceof Long) {
111 writeHyper = ((Long)dataElem).longValue();
112 } else
113 if (dataElem instanceof Float) {
114 writeFloat = ((Float)dataElem).floatValue();
115 } else
116 if (dataElem instanceof Double) {
117 writeDouble = ((Double)dataElem).doubleValue();
118 } else
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() {
131 boolean res = true ;
132 try {
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);
138 byte readElem;
139 try {
140 readElem = oObj.readBoolean();
141 res = ((readElem != 0) == writeBoolean);
143 if (!res)
144 log.println("Must be read " +
145 writeBoolean +
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);
150 res = false;
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() {
162 boolean res = true ;
163 try {
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);
169 byte readElem;
170 try {
171 readElem = oObj.readByte() ;
172 res = (readElem == writeByte);
174 if (!res)
175 log.println("Must be read " +
176 writeByte +
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);
181 res = false;
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() {
193 boolean res = true ;
194 try {
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);
200 char readElem;
201 try {
202 readElem = oObj.readChar() ;
203 res = (readElem == writeChar);
205 if (!res)
206 log.println("Must be read " +
207 writeChar +
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);
212 res = false;
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() {
223 boolean res = true ;
224 try {
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);
230 short readElem;
231 try {
232 readElem = oObj.readShort() ;
233 res = (readElem == writeShort);
235 if (!res)
236 log.println("Must be read " +
237 writeShort +
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);
242 res = false;
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() {
253 try {
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);
259 boolean res = true ;
260 int readElem;
261 try {
262 readElem = oObj.readLong() ;
263 res = (readElem == writeLong);
265 if (!res)
266 log.println("Must be read " +
267 writeLong +
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);
272 res = false;
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() {
283 boolean res = true ;
284 try {
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);
290 long readElem;
291 try {
292 readElem = oObj.readHyper() ;
293 res = (readElem == writeHyper);
295 if (!res)
296 log.println("Must be read " +
297 writeHyper +
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);
302 res = false;
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() {
313 boolean res = true ;
314 try {
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);
320 float readElem;
321 try {
322 readElem = oObj.readFloat() ;
323 res = (readElem == writeFloat);
325 if (!res)
326 log.println("Must be read " +
327 writeFloat +
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);
332 res = false;
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() {
343 boolean res = true ;
344 try {
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);
350 double readElem;
351 try {
352 readElem = oObj.readDouble() ;
353 res = (readElem == writeDouble);
355 if (!res)
356 log.println("Must be read " +
357 writeDouble +
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);
362 res = false;
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() {
373 boolean res = true ;
374 try {
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);
380 String readElem;
381 try {
382 readElem = oObj.readUTF() ;
383 res = writeUTF.equals(readElem) ;
385 if (!res)
386 log.println("Must be read '" +
387 writeUTF +
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);
392 res = false;
394 tRes.tested("readUTF()", res);
398 * Forces object environment recreation.
400 @Override
401 public void after() {
402 try {
403 oStream.flush();
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() ;