1*ef39d40dSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ef39d40dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ef39d40dSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ef39d40dSAndrew Rist * distributed with this work for additional information 6*ef39d40dSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ef39d40dSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ef39d40dSAndrew Rist * "License"); you may not use this file except in compliance 9*ef39d40dSAndrew Rist * with the License. You may obtain a copy of the License at 10*ef39d40dSAndrew Rist * 11*ef39d40dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*ef39d40dSAndrew Rist * 13*ef39d40dSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ef39d40dSAndrew Rist * software distributed under the License is distributed on an 15*ef39d40dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ef39d40dSAndrew Rist * KIND, either express or implied. See the License for the 17*ef39d40dSAndrew Rist * specific language governing permissions and limitations 18*ef39d40dSAndrew Rist * under the License. 19*ef39d40dSAndrew Rist * 20*ef39d40dSAndrew Rist *************************************************************/ 21*ef39d40dSAndrew Rist 22*ef39d40dSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package ifc.io; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import lib.MultiMethodTest; 27cdf0e10cSrcweir import lib.Status; 28cdf0e10cSrcweir import lib.StatusException; 29cdf0e10cSrcweir 30cdf0e10cSrcweir import com.sun.star.io.XInputStream; 31cdf0e10cSrcweir import com.sun.star.io.XOutputStream; 32cdf0e10cSrcweir 33cdf0e10cSrcweir /** 34cdf0e10cSrcweir * Testing <code>com.sun.star.io.XOutputStream</code> 35cdf0e10cSrcweir * interface methods: 36cdf0e10cSrcweir * <ul> 37cdf0e10cSrcweir * <li><code>writeBytes()</code></li> 38cdf0e10cSrcweir * <li><code>flush()</code></li> 39cdf0e10cSrcweir * <li><code>closeOutput()</code></li> 40cdf0e10cSrcweir * </ul> <p> 41cdf0e10cSrcweir * This test needs the following object relations : 42cdf0e10cSrcweir * <ul> 43cdf0e10cSrcweir * <li> <code>'ByteData'</code> : Data that is written on the stream. 44cdf0e10cSrcweir * </li> 45cdf0e10cSrcweir * <li> <code>'XOutputStream.StreamChecker'</code> : <code> 46cdf0e10cSrcweir * _XOutputStream.StreamChecker</code> interface implementation 47cdf0e10cSrcweir * which can reset streams and return input stream for check if the 48cdf0e10cSrcweir * data was successfully written.</li> 49cdf0e10cSrcweir * <ul> <p> 50cdf0e10cSrcweir * After test completion object environment has to be recreated. 51cdf0e10cSrcweir * @see com.sun.star.io.XOutputStream 52cdf0e10cSrcweir */ 53cdf0e10cSrcweir public class _XOutputStream extends MultiMethodTest { 54cdf0e10cSrcweir 55cdf0e10cSrcweir public XOutputStream oObj = null; 56cdf0e10cSrcweir StreamChecker checker = null; 57cdf0e10cSrcweir byte[] data = null; 58cdf0e10cSrcweir 59cdf0e10cSrcweir public static interface StreamChecker { getInStream()60cdf0e10cSrcweir public XInputStream getInStream(); resetStreams()61cdf0e10cSrcweir public void resetStreams(); 62cdf0e10cSrcweir } 63cdf0e10cSrcweir before()64cdf0e10cSrcweir protected void before() { 65cdf0e10cSrcweir checker = (StreamChecker) 66cdf0e10cSrcweir tEnv.getObjRelation("XOutputStream.StreamChecker"); 67cdf0e10cSrcweir if (checker == null) throw 68cdf0e10cSrcweir new StatusException(Status.failed( 69cdf0e10cSrcweir "Couldn't get relation 'XOutputStream.StreamChecker'")); 70cdf0e10cSrcweir 71cdf0e10cSrcweir data = (byte[])tEnv.getObjRelation("ByteData"); 72cdf0e10cSrcweir if (data == null) throw 73cdf0e10cSrcweir new StatusException(Status.failed( 74cdf0e10cSrcweir "Couldn't get relation 'ByteData'")); 75cdf0e10cSrcweir } 76cdf0e10cSrcweir /** 77cdf0e10cSrcweir * Test writes data to stream. <p> 78cdf0e10cSrcweir * Has <b> OK </b> status if the method successfully returns 79cdf0e10cSrcweir * and no exceptions were thrown. <p> 80cdf0e10cSrcweir */ _writeBytes()81cdf0e10cSrcweir public void _writeBytes() { 82cdf0e10cSrcweir boolean res = true; 83cdf0e10cSrcweir try { 84cdf0e10cSrcweir oObj.writeBytes(data); 85cdf0e10cSrcweir } catch (com.sun.star.io.IOException e) { 86cdf0e10cSrcweir e.printStackTrace(log) ; 87cdf0e10cSrcweir res = false; 88cdf0e10cSrcweir } 89cdf0e10cSrcweir 90cdf0e10cSrcweir XInputStream xInStream = checker.getInStream(); 91cdf0e10cSrcweir byte[][] readData = new byte[1][data.length]; 92cdf0e10cSrcweir try { 93cdf0e10cSrcweir xInStream.readBytes(readData, data.length); 94cdf0e10cSrcweir } catch(com.sun.star.io.IOException e) { 95cdf0e10cSrcweir log.println("Couldn't read data:" + e); 96cdf0e10cSrcweir res = false; 97cdf0e10cSrcweir } 98cdf0e10cSrcweir 99cdf0e10cSrcweir for(int i = 0; i < readData[0].length; i++) { 100cdf0e10cSrcweir log.println("Expected: "+data[i]+", actual is "+readData[0][i]); 101cdf0e10cSrcweir res &= readData[0][i] == data[i]; 102cdf0e10cSrcweir } 103cdf0e10cSrcweir 104cdf0e10cSrcweir tRes.tested("writeBytes()", res); 105cdf0e10cSrcweir } 106cdf0e10cSrcweir 107cdf0e10cSrcweir /** 108cdf0e10cSrcweir * Test flushes out data from stream. <p> 109cdf0e10cSrcweir * Has <b> OK </b> status if the method successfully returns 110cdf0e10cSrcweir * and no exceptions were thrown. <p> 111cdf0e10cSrcweir * The following method tests are to be completed successfully before : 112cdf0e10cSrcweir * <ul> 113cdf0e10cSrcweir * <li> <code> writeBytes() </code></li> 114cdf0e10cSrcweir * </ul> 115cdf0e10cSrcweir */ _flush()116cdf0e10cSrcweir public void _flush() { 117cdf0e10cSrcweir requiredMethod("writeBytes()"); 118cdf0e10cSrcweir 119cdf0e10cSrcweir boolean res; 120cdf0e10cSrcweir try { 121cdf0e10cSrcweir oObj.flush(); 122cdf0e10cSrcweir res = true; 123cdf0e10cSrcweir } catch (com.sun.star.io.IOException e) { 124cdf0e10cSrcweir e.printStackTrace(log) ; 125cdf0e10cSrcweir res = false; 126cdf0e10cSrcweir } 127cdf0e10cSrcweir 128cdf0e10cSrcweir tRes.tested("flush()", res); 129cdf0e10cSrcweir } 130cdf0e10cSrcweir 131cdf0e10cSrcweir /** 132cdf0e10cSrcweir * Test calls the method. <p> 133cdf0e10cSrcweir * Has <b> OK </b> status if the method successfully returns 134cdf0e10cSrcweir * and no exceptions were thrown. <p> 135cdf0e10cSrcweir * The following method tests are to be completed successfully before : 136cdf0e10cSrcweir * <ul> 137cdf0e10cSrcweir * <li> <code> writeBytes() </code></li> 138cdf0e10cSrcweir * </ul> 139cdf0e10cSrcweir * The following method tests are to be executed before : 140cdf0e10cSrcweir * <ul> 141cdf0e10cSrcweir * <li><code> flush() </code></li> 142cdf0e10cSrcweir * </ul> 143cdf0e10cSrcweir */ _closeOutput()144cdf0e10cSrcweir public void _closeOutput() { 145cdf0e10cSrcweir requiredMethod("writeBytes()"); 146cdf0e10cSrcweir executeMethod("flush()"); 147cdf0e10cSrcweir 148cdf0e10cSrcweir boolean res; 149cdf0e10cSrcweir try { 150cdf0e10cSrcweir oObj.closeOutput(); 151cdf0e10cSrcweir res = true; 152cdf0e10cSrcweir } catch (com.sun.star.io.IOException e) { 153cdf0e10cSrcweir e.printStackTrace(log); 154cdf0e10cSrcweir res = false; 155cdf0e10cSrcweir } 156cdf0e10cSrcweir 157cdf0e10cSrcweir log.println("This method is called in main module"); 158cdf0e10cSrcweir 159cdf0e10cSrcweir tRes.tested("closeOutput()", res); 160cdf0e10cSrcweir } 161cdf0e10cSrcweir 162cdf0e10cSrcweir /** 163cdf0e10cSrcweir * Forces object environment recreation. 164cdf0e10cSrcweir */ after()165cdf0e10cSrcweir public void after() { 166cdf0e10cSrcweir this.disposeEnvironment() ; 167cdf0e10cSrcweir } 168cdf0e10cSrcweir } 169cdf0e10cSrcweir 170