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 lib.MultiMethodTest;
22 import lib.Status;
23 import lib.StatusException;
24 
25 import com.sun.star.io.XInputStream;
26 import com.sun.star.io.XOutputStream;
27 
28 /**
29 * Testing <code>com.sun.star.io.XOutputStream</code>
30 * interface methods:
31 * <ul>
32 *   <li><code>writeBytes()</code></li>
33 *   <li><code>flush()</code></li>
34 *   <li><code>closeOutput()</code></li>
35 * </ul> <p>
36 * This test needs the following object relations :
37 * <ul>
38 *  <li> <code>'ByteData'</code> : Data that is written on the stream.
39 *  </li>
40 *  <li> <code>'XOutputStream.StreamChecker'</code> : <code>
41 *    _XOutputStream.StreamChecker</code> interface implementation
42 *    which can reset streams and return input stream for check if the
43 *    data was successfully written.</li>
44 * <ul> <p>
45 * After test completion object environment has to be recreated.
46 * @see com.sun.star.io.XOutputStream
47 */
48 public class _XOutputStream extends MultiMethodTest {
49 
50     public XOutputStream oObj = null;
51     StreamChecker checker = null;
52     byte[] data = null;
53 
54     public interface StreamChecker {
getInStream()55         XInputStream getInStream();
resetStreams()56         void resetStreams();
57     }
58 
59     @Override
before()60     protected void before() {
61         checker = (StreamChecker)
62             tEnv.getObjRelation("XOutputStream.StreamChecker");
63         if (checker == null) throw
64             new StatusException(Status.failed(
65                 "Couldn't get relation 'XOutputStream.StreamChecker'"));
66 
67         data = (byte[])tEnv.getObjRelation("ByteData");
68         if (data == null) throw
69             new StatusException(Status.failed(
70                 "Couldn't get relation 'ByteData'"));
71     }
72     /**
73     * Test writes data to stream. <p>
74     * Has <b> OK </b> status if the method successfully returns
75     * and no exceptions were thrown. <p>
76     */
_writeBytes()77     public void _writeBytes() {
78         boolean res = true;
79         try {
80             oObj.writeBytes(data);
81          } catch (com.sun.star.io.IOException e) {
82             e.printStackTrace(log) ;
83             res = false;
84         }
85 
86         byte[][] readData = new byte[1][data.length];
87         XInputStream xInStream = checker.getInStream();
88         if (xInStream != null) {
89             try {
90                 xInStream.readBytes(readData, data.length);
91             } catch(com.sun.star.io.IOException e) {
92                 log.println("Couldn't read data:" + e);
93                 res = false;
94             }
95         } else {
96             res = false;
97         }
98 
99         for(int i = 0; i < readData[0].length; i++) {
100             log.println("Expected: "+data[i]+", actual is "+readData[0][i]);
101             res &= readData[0][i] == data[i];
102         }
103 
104         tRes.tested("writeBytes()", res);
105     }
106 
107     /**
108     * Test flushes out data from stream. <p>
109     * Has <b> OK </b> status if the method successfully returns
110     * and no exceptions were thrown. <p>
111     * The following method tests are to be completed successfully before :
112     * <ul>
113     *  <li> <code> writeBytes() </code></li>
114     * </ul>
115     */
_flush()116     public void _flush() {
117         requiredMethod("writeBytes()");
118 
119         boolean res;
120         try {
121             oObj.flush();
122             res = true;
123         } catch (com.sun.star.io.IOException e) {
124             e.printStackTrace(log) ;
125             res = false;
126         }
127 
128         tRes.tested("flush()", res);
129     }
130 
131     /**
132     * Test calls the method. <p>
133     * Has <b> OK </b> status if the method successfully returns
134     * and no exceptions were thrown. <p>
135     * The following method tests are to be completed successfully before :
136     * <ul>
137     *  <li> <code> writeBytes() </code></li>
138     * </ul>
139     * The following method tests are to be executed before :
140     * <ul>
141     *  <li><code> flush() </code></li>
142     * </ul>
143     */
_closeOutput()144     public void _closeOutput() {
145         requiredMethod("writeBytes()");
146         executeMethod("flush()");
147 
148         boolean res;
149         try {
150             oObj.closeOutput();
151             res = true;
152         } catch (com.sun.star.io.IOException e) {
153             e.printStackTrace(log);
154             res = false;
155         }
156 
157         log.println("This method is called in main module");
158 
159         tRes.tested("closeOutput()", res);
160     }
161 
162     /**
163     * Forces object environment recreation.
164     */
165     @Override
after()166     public void after() {
167         this.disposeEnvironment() ;
168     }
169 }
170 
171