1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 package ifc.io; 29 30 import lib.MultiMethodTest; 31 import lib.Status; 32 import lib.StatusException; 33 34 import com.sun.star.io.XInputStream; 35 import com.sun.star.io.XOutputStream; 36 37 /** 38 * Testing <code>com.sun.star.io.XOutputStream</code> 39 * interface methods: 40 * <ul> 41 * <li><code>writeBytes()</code></li> 42 * <li><code>flush()</code></li> 43 * <li><code>closeOutput()</code></li> 44 * </ul> <p> 45 * This test needs the following object relations : 46 * <ul> 47 * <li> <code>'ByteData'</code> : Data that is written on the stream. 48 * </li> 49 * <li> <code>'XOutputStream.StreamChecker'</code> : <code> 50 * _XOutputStream.StreamChecker</code> interface implementation 51 * which can reset streams and return input stream for check if the 52 * data was successfully written.</li> 53 * <ul> <p> 54 * After test completion object environment has to be recreated. 55 * @see com.sun.star.io.XOutputStream 56 */ 57 public class _XOutputStream extends MultiMethodTest { 58 59 public XOutputStream oObj = null; 60 StreamChecker checker = null; 61 byte[] data = null; 62 63 public static interface StreamChecker { 64 public XInputStream getInStream(); 65 public void resetStreams(); 66 } 67 68 protected void before() { 69 checker = (StreamChecker) 70 tEnv.getObjRelation("XOutputStream.StreamChecker"); 71 if (checker == null) throw 72 new StatusException(Status.failed( 73 "Couldn't get relation 'XOutputStream.StreamChecker'")); 74 75 data = (byte[])tEnv.getObjRelation("ByteData"); 76 if (data == null) throw 77 new StatusException(Status.failed( 78 "Couldn't get relation 'ByteData'")); 79 } 80 /** 81 * Test writes data to stream. <p> 82 * Has <b> OK </b> status if the method successfully returns 83 * and no exceptions were thrown. <p> 84 */ 85 public void _writeBytes() { 86 boolean res = true; 87 try { 88 oObj.writeBytes(data); 89 } catch (com.sun.star.io.IOException e) { 90 e.printStackTrace(log) ; 91 res = false; 92 } 93 94 XInputStream xInStream = checker.getInStream(); 95 byte[][] readData = new byte[1][data.length]; 96 try { 97 xInStream.readBytes(readData, data.length); 98 } catch(com.sun.star.io.IOException e) { 99 log.println("Couldn't read data:" + e); 100 res = false; 101 } 102 103 for(int i = 0; i < readData[0].length; i++) { 104 log.println("Expected: "+data[i]+", actual is "+readData[0][i]); 105 res &= readData[0][i] == data[i]; 106 } 107 108 tRes.tested("writeBytes()", res); 109 } 110 111 /** 112 * Test flushes out data from stream. <p> 113 * Has <b> OK </b> status if the method successfully returns 114 * and no exceptions were thrown. <p> 115 * The following method tests are to be completed successfully before : 116 * <ul> 117 * <li> <code> writeBytes() </code></li> 118 * </ul> 119 */ 120 public void _flush() { 121 requiredMethod("writeBytes()"); 122 123 boolean res; 124 try { 125 oObj.flush(); 126 res = true; 127 } catch (com.sun.star.io.IOException e) { 128 e.printStackTrace(log) ; 129 res = false; 130 } 131 132 tRes.tested("flush()", res); 133 } 134 135 /** 136 * Test calls the method. <p> 137 * Has <b> OK </b> status if the method successfully returns 138 * and no exceptions were thrown. <p> 139 * The following method tests are to be completed successfully before : 140 * <ul> 141 * <li> <code> writeBytes() </code></li> 142 * </ul> 143 * The following method tests are to be executed before : 144 * <ul> 145 * <li><code> flush() </code></li> 146 * </ul> 147 */ 148 public void _closeOutput() { 149 requiredMethod("writeBytes()"); 150 executeMethod("flush()"); 151 152 boolean res; 153 try { 154 oObj.closeOutput(); 155 res = true; 156 } catch (com.sun.star.io.IOException e) { 157 e.printStackTrace(log); 158 res = false; 159 } 160 161 log.println("This method is called in main module"); 162 163 tRes.tested("closeOutput()", res); 164 } 165 166 /** 167 * Forces object environment recreation. 168 */ 169 public void after() { 170 this.disposeEnvironment() ; 171 } 172 } 173 174