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 lib.MultiMethodTest; 27 import lib.Status; 28 import lib.StatusException; 29 30 import com.sun.star.io.XInputStream; 31 import com.sun.star.io.XOutputStream; 32 33 /** 34 * Testing <code>com.sun.star.io.XOutputStream</code> 35 * interface methods: 36 * <ul> 37 * <li><code>writeBytes()</code></li> 38 * <li><code>flush()</code></li> 39 * <li><code>closeOutput()</code></li> 40 * </ul> <p> 41 * This test needs the following object relations : 42 * <ul> 43 * <li> <code>'ByteData'</code> : Data that is written on the stream. 44 * </li> 45 * <li> <code>'XOutputStream.StreamChecker'</code> : <code> 46 * _XOutputStream.StreamChecker</code> interface implementation 47 * which can reset streams and return input stream for check if the 48 * data was successfully written.</li> 49 * <ul> <p> 50 * After test completion object environment has to be recreated. 51 * @see com.sun.star.io.XOutputStream 52 */ 53 public class _XOutputStream extends MultiMethodTest { 54 55 public XOutputStream oObj = null; 56 StreamChecker checker = null; 57 byte[] data = null; 58 59 public static interface StreamChecker { getInStream()60 public XInputStream getInStream(); resetStreams()61 public void resetStreams(); 62 } 63 before()64 protected void before() { 65 checker = (StreamChecker) 66 tEnv.getObjRelation("XOutputStream.StreamChecker"); 67 if (checker == null) throw 68 new StatusException(Status.failed( 69 "Couldn't get relation 'XOutputStream.StreamChecker'")); 70 71 data = (byte[])tEnv.getObjRelation("ByteData"); 72 if (data == null) throw 73 new StatusException(Status.failed( 74 "Couldn't get relation 'ByteData'")); 75 } 76 /** 77 * Test writes data to stream. <p> 78 * Has <b> OK </b> status if the method successfully returns 79 * and no exceptions were thrown. <p> 80 */ _writeBytes()81 public void _writeBytes() { 82 boolean res = true; 83 try { 84 oObj.writeBytes(data); 85 } catch (com.sun.star.io.IOException e) { 86 e.printStackTrace(log) ; 87 res = false; 88 } 89 90 XInputStream xInStream = checker.getInStream(); 91 byte[][] readData = new byte[1][data.length]; 92 try { 93 xInStream.readBytes(readData, data.length); 94 } catch(com.sun.star.io.IOException e) { 95 log.println("Couldn't read data:" + e); 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 */ after()165 public void after() { 166 this.disposeEnvironment() ; 167 } 168 } 169 170