1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package ifc.io;
25 
26 import java.util.Vector;
27 
28 import lib.MultiMethodTest;
29 import lib.Status;
30 import lib.StatusException;
31 
32 import com.sun.star.io.XDataInputStream;
33 import com.sun.star.io.XDataOutputStream;
34 import com.sun.star.uno.UnoRuntime;
35 import com.sun.star.uno.XInterface;
36 
37 /**
38 * Testing <code>com.sun.star.io.XDataInputStream</code>
39 * interface methods:
40 * <ul>
41 *   <li><code>readBoolean()</code></li>
42 *   <li><code>readByte()</code></li>
43 *   <li><code>readChar()</code></li>
44 *   <li><code>readShort()</code></li>
45 *   <li><code>readLong()</code></li>
46 *   <li><code>readHyper()</code></li>
47 *   <li><code>readFloat()</code></li>
48 *   <li><code>readDouble()</code></li>
49 *   <li><code>readUTF()</code></li>
50 * </ul> <p>
51 * This test needs the following object relations :
52 * <ul>
53 *  <li> <code>'StreamData'</code> (of type <code>Vector</code>):
54 *   vector of data for comparing with data that obtained from stream </li>
55 *  <li> <code>'StreamWriter'</code> (of type <code>XDataOutputStream</code>):
56 *   a possiblitiy to write values to the stream. </li>
57 * <ul> <p>
58 * After test completion object environment has to be recreated.
59 * @see com.sun.star.io.XDataInputStream
60 * @see java.util.Vector
61 */
62 public class _XDataInputStream extends MultiMethodTest {
63 
64     public XDataInputStream oObj = null;
65     public XDataOutputStream oStream = null;
66 
67     // values that are written
68     private boolean writeBoolean;
69     private byte writeByte;
70     private char writeChar;
71     private double writeDouble;
72     private float writeFloat;
73     private long writeHyper;
74     private int writeLong;
75     private short writeShort;
76     private String writeUTF;
77 
78 
79     /**
80      * Retrieves relations. From relation 'StreamData' extracts
81      * data of different types and fills the appropriate variables.
82      * @throws StatusException If one of relations not found.
83      */
before()84     public void before(){
85 
86         XInterface x = (XInterface)tEnv.getObjRelation("StreamWriter") ;
87         oStream = (XDataOutputStream)UnoRuntime.queryInterface(
88                                                     XDataOutputStream.class, x);
89         Vector data = (Vector) tEnv.getObjRelation("StreamData") ;
90         if (data == null || oStream == null) {
91             throw new StatusException(Status.failed("Object relation not found."));
92         }
93 
94         // extract data from vector
95         Object dataElem = null ;
96         for (int i = 0; i < data.size(); i++) {
97             dataElem = data.get(i) ;
98 
99             if (dataElem instanceof Boolean) {
100                 writeBoolean = ((Boolean)dataElem).booleanValue();
101             } else
102             if (dataElem instanceof Byte) {
103                 writeByte = ((Byte)dataElem).byteValue();
104             } else
105             if (dataElem instanceof Character) {
106                 writeChar = ((Character)dataElem).charValue();
107             } else
108             if (dataElem instanceof Short) {
109                 writeShort = ((Short)dataElem).shortValue();
110             } else
111             if (dataElem instanceof Integer) {
112                 writeLong = ((Integer)dataElem).intValue();
113             } else
114             if (dataElem instanceof Long) {
115                 writeHyper = ((Long)dataElem).longValue();
116             } else
117             if (dataElem instanceof Float) {
118                 writeFloat = ((Float)dataElem).floatValue();
119             } else
120             if (dataElem instanceof Double) {
121                 writeDouble = ((Double)dataElem).doubleValue();
122             } else
123             if (dataElem instanceof String) {
124                 writeUTF = (String)dataElem;
125             }
126         }
127     }
128 
129     /**
130      * First writes a value to outStream then reads it from input. <p>
131      *
132      * Has <b> OK </b> status if read and written values are equal. <p>
133      */
_readBoolean()134     public void _readBoolean() {
135         boolean res = true ;
136         try {
137             oStream.writeBoolean(writeBoolean);
138         } catch (com.sun.star.io.IOException e) {
139             e.printStackTrace(log);
140             throw new StatusException("Can't write data to the stream", e);
141         }
142         byte readElem;
143         try {
144             readElem = oObj.readBoolean();
145             res = ((readElem != 0) == writeBoolean);
146 
147             if (!res)
148                 log.println("Must be read " +
149                     writeBoolean +
150                     " but was read " + (readElem != 0)) ;
151         } catch (com.sun.star.io.IOException e) {
152             log.println("Couldn't  read Boolean from stream");
153             e.printStackTrace(log);
154             res = false;
155         }
156 
157         tRes.tested("readBoolean()", res) ;
158     }
159 
160     /**
161      * First writes a value to outStream then reads it from input. <p>
162      *
163      * Has <b> OK </b> status if read and written values are equal. <p>
164      */
_readByte()165     public void _readByte() {
166         boolean res = true ;
167         try {
168             oStream.writeByte(writeByte);
169         } catch (com.sun.star.io.IOException e) {
170             e.printStackTrace(log);
171             throw new StatusException("Can't write data to the stream", e);
172         }
173         byte readElem;
174         try {
175             readElem = oObj.readByte() ;
176             res = (readElem == writeByte);
177 
178             if (!res)
179                 log.println("Must be read " +
180                     writeByte +
181                     " but was read " + readElem);
182         } catch(com.sun.star.io.IOException e) {
183             log.println("Couldn't read Byte from stream");
184             e.printStackTrace(log);
185             res = false;
186         }
187 
188         tRes.tested("readByte()", res) ;
189     }
190 
191     /**
192      * First writes a value to outStream then reads it from input. <p>
193      *
194      * Has <b> OK </b> status if read and written values are equal. <p>
195      */
_readChar()196     public void _readChar() {
197         boolean res = true ;
198         try {
199             oStream.writeChar(writeChar);
200         } catch (com.sun.star.io.IOException e) {
201             e.printStackTrace(log);
202             throw new StatusException("Can't write data to the stream", e);
203         }
204         char readElem;
205         try {
206             readElem = oObj.readChar() ;
207             res = (readElem == writeChar);
208 
209             if (!res)
210                 log.println("Must be read " +
211                     writeChar +
212                     " but was read " + readElem) ;
213         } catch( com.sun.star.io.IOException e ) {
214             log.println("Couldn't read Char from stream");
215             e.printStackTrace(log);
216             res = false;
217         }
218         tRes.tested("readChar()", res);
219     }
220 
221     /**
222      * First writes a value to outStream then reads it from input. <p>
223      *
224      * Has <b> OK </b> status if read and written values are equal. <p>
225      */
_readShort()226     public void _readShort() {
227         boolean res = true ;
228         try {
229             oStream.writeShort(writeShort);
230         } catch (com.sun.star.io.IOException e) {
231             e.printStackTrace(log);
232             throw new StatusException("Can't write data to the stream", e);
233         }
234         short readElem;
235         try {
236             readElem = oObj.readShort() ;
237             res = (readElem == writeShort);
238 
239             if (!res)
240                 log.println("Must be read " +
241                     writeShort +
242                     " but was read " + readElem) ;
243         } catch( com.sun.star.io.IOException e ) {
244             log.println("Couldn't read Short from stream");
245             e.printStackTrace(log);
246             res = false;
247         }
248         tRes.tested("readShort()", res);
249     }
250 
251     /**
252      * First writes a value to outStream then reads it from input. <p>
253      *
254      * Has <b> OK </b> status if read and written values are equal. <p>
255      */
_readLong()256     public void _readLong() {
257         try {
258             oStream.writeLong(writeLong);
259         } catch (com.sun.star.io.IOException e) {
260             e.printStackTrace(log);
261             throw new StatusException("Can't write data to the stream", e);
262         }
263         boolean res = true ;
264         int readElem;
265         try {
266             readElem = oObj.readLong() ;
267             res = (readElem == writeLong);
268 
269             if (!res)
270                 log.println("Must be read " +
271                     writeLong +
272                     " but was read " + readElem) ;
273         } catch( com.sun.star.io.IOException e ) {
274             log.println("Couldn't read Long from stream");
275             e.printStackTrace(log);
276             res = false;
277         }
278         tRes.tested("readLong()", res);
279     }
280 
281     /**
282      * First writes a value to outStream then reads it from input. <p>
283      *
284      * Has <b> OK </b> status if read and written values are equal. <p>
285      */
_readHyper()286     public void _readHyper() {
287         boolean res = true ;
288         try {
289             oStream.writeHyper(writeHyper);
290         } catch (com.sun.star.io.IOException e) {
291             e.printStackTrace(log);
292             throw new StatusException("Can't write data to the stream", e);
293         }
294         long readElem;
295         try {
296             readElem = oObj.readHyper() ;
297             res = (readElem == writeHyper);
298 
299             if (!res)
300                 log.println("Must be read " +
301                     writeHyper +
302                     " but was read " + readElem) ;
303         } catch( com.sun.star.io.IOException e ) {
304             log.println("Couldn't read Hyper from stream");
305             e.printStackTrace(log);
306             res = false;
307         }
308         tRes.tested("readHyper()", res);
309     }
310 
311     /**
312      * First writes a value to outStream then reads it from input. <p>
313      *
314      * Has <b> OK </b> status if read and written values are equal. <p>
315      */
_readFloat()316     public void _readFloat() {
317         boolean res = true ;
318         try {
319             oStream.writeFloat(writeFloat);
320         } catch (com.sun.star.io.IOException e) {
321             e.printStackTrace(log);
322             throw new StatusException("Can't write data to the stream", e);
323         }
324         float readElem;
325         try {
326             readElem = oObj.readFloat() ;
327             res = (readElem == writeFloat);
328 
329             if (!res)
330                 log.println("Must be read " +
331                     writeFloat +
332                     " but was read " + readElem) ;
333         } catch( com.sun.star.io.IOException e ) {
334             log.println("Couldn't read Float from stream");
335             e.printStackTrace(log);
336             res = false;
337         }
338         tRes.tested("readFloat()", res);
339     }
340 
341     /**
342      * First writes a value to outStream then reads it from input. <p>
343      *
344      * Has <b> OK </b> status if read and written values are equal. <p>
345      */
_readDouble()346     public void _readDouble() {
347         boolean res = true ;
348         try {
349             oStream.writeDouble(writeDouble);
350         } catch (com.sun.star.io.IOException e) {
351             e.printStackTrace(log);
352             throw new StatusException("Can't write data to the stream", e);
353         }
354         double readElem;
355         try {
356             readElem = oObj.readDouble() ;
357             res = (readElem == writeDouble);
358 
359             if (!res)
360                 log.println("Must be read " +
361                     writeDouble +
362                     " but was read " + readElem) ;
363         } catch( com.sun.star.io.IOException e ) {
364             log.println("Couldn't read Double from stream");
365             e.printStackTrace(log);
366             res = false;
367         }
368         tRes.tested("readDouble()", res);
369     }
370 
371     /**
372      * First writes a value to outStream then reads it from input. <p>
373      *
374      * Has <b> OK </b> status if read and written values are equal. <p>
375      */
_readUTF()376     public void _readUTF() {
377         boolean res = true ;
378         try {
379             oStream.writeUTF(writeUTF);
380         } catch (com.sun.star.io.IOException e) {
381             e.printStackTrace(log);
382             throw new StatusException("Can't write data to the stream", e);
383         }
384         String readElem;
385         try {
386             readElem = oObj.readUTF() ;
387             res = writeUTF.equals(readElem) ;
388 
389             if (!res)
390                 log.println("Must be read '" +
391                     writeUTF +
392                     "' but was read '" + readElem + "'") ;
393         } catch( com.sun.star.io.IOException e ) {
394             log.println("Couldn't read String from stream");
395             e.printStackTrace(log);
396             res = false;
397         }
398         tRes.tested("readUTF()", res);
399     }
400 
401     /**
402      * Forces object environment recreation.
403      */
after()404     public void after() {
405         try {
406             oStream.flush();
407         } catch (com.sun.star.io.NotConnectedException e) {
408             e.printStackTrace(log);
409         } catch (com.sun.star.io.BufferSizeExceededException e) {
410             e.printStackTrace(log);
411         } catch (com.sun.star.io.IOException e) {
412             e.printStackTrace(log);
413         }
414         this.disposeEnvironment() ;
415     }
416 }
417 
418