1 /*
2  * This file is part of the LibreOffice project.
3  *
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/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
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 .
17  */
18 
19 package ifc.io;
20 
21 import java.util.List;
22 
23 import lib.MultiMethodTest;
24 import lib.Status;
25 import lib.StatusException;
26 
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;
31 
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
56 */
57 public class _XDataInputStream extends MultiMethodTest {
58 
59     public XDataInputStream oObj = null;
60     public XDataOutputStream oStream = null;
61 
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;
72 
73 
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.
78      */
79     @Override
before()80     public void before(){
81 
82         XInterface x = (XInterface)tEnv.getObjRelation("StreamWriter") ;
83         oStream = UnoRuntime.queryInterface(
84                                                     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."));
88         }
89 
90         // extract data from vector
91         Object dataElem = null ;
92         for (int i = 0; i < data.size(); i++) {
93             dataElem = data.get(i) ;
94 
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;
121             }
122         }
123     }
124 
125     /**
126      * First writes a value to outStream then reads it from input. <p>
127      *
128      * Has <b> OK </b> status if read and written values are equal. <p>
129      */
_readBoolean()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);
137         }
138         byte readElem;
139         try {
140             readElem = oObj.readBoolean();
141             res = ((readElem != 0) == writeBoolean);
142 
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;
151         }
152 
153         tRes.tested("readBoolean()", res) ;
154     }
155 
156     /**
157      * First writes a value to outStream then reads it from input. <p>
158      *
159      * Has <b> OK </b> status if read and written values are equal. <p>
160      */
_readByte()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);
168         }
169         byte readElem;
170         try {
171             readElem = oObj.readByte() ;
172             res = (readElem == writeByte);
173 
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;
182         }
183 
184         tRes.tested("readByte()", res) ;
185     }
186 
187     /**
188      * First writes a value to outStream then reads it from input. <p>
189      *
190      * Has <b> OK </b> status if read and written values are equal. <p>
191      */
_readChar()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);
199         }
200         char readElem;
201         try {
202             readElem = oObj.readChar() ;
203             res = (readElem == writeChar);
204 
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;
213         }
214         tRes.tested("readChar()", res);
215     }
216 
217     /**
218      * First writes a value to outStream then reads it from input. <p>
219      *
220      * Has <b> OK </b> status if read and written values are equal. <p>
221      */
_readShort()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);
229         }
230         short readElem;
231         try {
232             readElem = oObj.readShort() ;
233             res = (readElem == writeShort);
234 
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;
243         }
244         tRes.tested("readShort()", res);
245     }
246 
247     /**
248      * First writes a value to outStream then reads it from input. <p>
249      *
250      * Has <b> OK </b> status if read and written values are equal. <p>
251      */
_readLong()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);
258         }
259         boolean res = true ;
260         int readElem;
261         try {
262             readElem = oObj.readLong() ;
263             res = (readElem == writeLong);
264 
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;
273         }
274         tRes.tested("readLong()", res);
275     }
276 
277     /**
278      * First writes a value to outStream then reads it from input. <p>
279      *
280      * Has <b> OK </b> status if read and written values are equal. <p>
281      */
_readHyper()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);
289         }
290         long readElem;
291         try {
292             readElem = oObj.readHyper() ;
293             res = (readElem == writeHyper);
294 
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;
303         }
304         tRes.tested("readHyper()", res);
305     }
306 
307     /**
308      * First writes a value to outStream then reads it from input. <p>
309      *
310      * Has <b> OK </b> status if read and written values are equal. <p>
311      */
_readFloat()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);
319         }
320         float readElem;
321         try {
322             readElem = oObj.readFloat() ;
323             res = (readElem == writeFloat);
324 
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;
333         }
334         tRes.tested("readFloat()", res);
335     }
336 
337     /**
338      * First writes a value to outStream then reads it from input. <p>
339      *
340      * Has <b> OK </b> status if read and written values are equal. <p>
341      */
_readDouble()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);
349         }
350         double readElem;
351         try {
352             readElem = oObj.readDouble() ;
353             res = (readElem == writeDouble);
354 
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;
363         }
364         tRes.tested("readDouble()", res);
365     }
366 
367     /**
368      * First writes a value to outStream then reads it from input. <p>
369      *
370      * Has <b> OK </b> status if read and written values are equal. <p>
371      */
_readUTF()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);
379         }
380         String readElem;
381         try {
382             readElem = oObj.readUTF() ;
383             res = writeUTF.equals(readElem) ;
384 
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;
393         }
394         tRes.tested("readUTF()", res);
395     }
396 
397     /**
398      * Forces object environment recreation.
399      */
400     @Override
after()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);
410         }
411         this.disposeEnvironment() ;
412     }
413 }
414 
415