1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package tests.api.java.io; 19 20import tests.api.java.io.FilterReaderTest.MyFilterReader; 21 22import java.io.ByteArrayInputStream; 23import java.io.ByteArrayOutputStream; 24import java.io.FilterWriter; 25import java.io.IOException; 26import java.io.InputStreamReader; 27import java.io.OutputStreamWriter; 28 29import dalvik.annotation.TestLevel; 30import dalvik.annotation.TestTargetClass; 31import dalvik.annotation.TestTargetNew; 32 33@TestTargetClass(FilterWriter.class) 34public class FilterWriterTest extends junit.framework.TestCase { 35 36 private boolean called; 37 private FilterWriter fw; 38 39 static class MyFilterWriter extends java.io.FilterWriter { 40 public MyFilterWriter(java.io.Writer writer) { 41 super(writer); 42 } 43 } 44 45 class MockWriter extends java.io.Writer { 46 public MockWriter() { 47 } 48 49 public void close() throws IOException { 50 called = true; 51 } 52 53 public void flush() throws IOException { 54 called = true; 55 } 56 57 public void write(char[] buffer, int offset, int count) throws IOException { 58 called = true; 59 } 60 61 public void write(int oneChar) throws IOException { 62 called = true; 63 } 64 65 public void write(String str, int offset, int count) throws IOException { 66 called = true; 67 } 68 69 public long skip(long count) throws IOException { 70 called = true; 71 return 0; 72 } 73 } 74 75 /** 76 * @tests java.io.FilterWriter#FilterReader(java.io.Reader) 77 */ 78 @TestTargetNew( 79 level = TestLevel.COMPLETE, 80 notes = "Verifies constructor FilterWriter(java.io.Writer).", 81 method = "FilterWriter", 82 args = {java.io.Writer.class} 83 ) 84 public void test_ConstructorLjava_io_Writer() { 85 86 FilterWriter myWriter = null; 87 88 called = true; 89 90 try { 91 myWriter = new MyFilterWriter(null); 92 fail("NullPointerException expected."); 93 } catch (NullPointerException e) { 94 // expected 95 } 96 } 97 98 /** 99 * @tests java.io.FilterWriter#close() 100 */ 101 @TestTargetNew( 102 level = TestLevel.COMPLETE, 103 notes = "Verifies close().", 104 method = "close", 105 args = {} 106 ) 107 public void test_close() throws IOException { 108 fw.close(); 109 assertTrue("close() has not been called.", called); 110 } 111 112 /** 113 * @tests java.io.FilterWriter#flush() 114 */ 115 @TestTargetNew( 116 level = TestLevel.COMPLETE, 117 notes = "Verifies flush().", 118 method = "flush", 119 args = {} 120 ) 121 public void test_flush() throws IOException { 122 fw.flush(); 123 assertTrue("flush() has not been called.", called); 124 } 125 126 /** 127 * @tests java.io.FilterWriter#write(int) 128 */ 129 @TestTargetNew( 130 level = TestLevel.COMPLETE, 131 notes = "Verifies write(int).", 132 method = "write", 133 args = {int.class} 134 ) 135 public void test_writeI() throws IOException { 136 fw.write(0); 137 assertTrue("write(int) has not been called.", called); 138 } 139 140 /** 141 * @tests java.io.FilterWriter#write(char[], int, int) 142 */ 143 @TestTargetNew( 144 level = TestLevel.COMPLETE, 145 notes = "Verifies write(char[], int, int).", 146 method = "write", 147 args = {char[].class, int.class, int.class} 148 ) 149 public void test_write$CII() throws IOException { 150 char[] buffer = new char[5]; 151 fw.write(buffer, 0, 5); 152 assertTrue("write(char[], int, int) has not been called.", called); 153 } 154 155 /** 156 * @tests java.io.FilterReader#read(char[], int, int) 157 */ 158 @TestTargetNew( 159 level = TestLevel.COMPLETE, 160 notes = "Verifies write(char[], int, int).", 161 method = "write", 162 args = {char[].class, int.class, int.class} 163 ) 164 public void test_write$CII_Exception() throws IOException { 165 char[] buffer = new char[10]; 166 167 fw = new MyFilterWriter(new OutputStreamWriter( 168 new ByteArrayOutputStream())); 169 170 try { 171 fw.write(buffer, 0, -1); 172 fail("Test 1: IndexOutOfBoundsException expected."); 173 } catch (IndexOutOfBoundsException e) { 174 // Expected. 175 } 176 177 try { 178 fw.write(buffer, -1, 1); 179 fail("Test 2: IndexOutOfBoundsException expected."); 180 } catch (IndexOutOfBoundsException e) { 181 // Expected. 182 } 183 184 try { 185 fw.write(buffer, 10, 1); 186 fail("Test 3: IndexOutOfBoundsException expected."); 187 } catch (IndexOutOfBoundsException e) { 188 // Expected. 189 } 190 } 191 192 /** 193 * @tests java.io.FilterWriter#write(char[], int, int) 194 */ 195 @TestTargetNew( 196 level = TestLevel.COMPLETE, 197 notes = "Verifies write(String, int, int).", 198 method = "write", 199 args = {java.lang.String.class, int.class, int.class} 200 ) 201 public void test_writeLjava_lang_StringII() throws IOException { 202 fw.write("Hello world", 0, 5); 203 assertTrue("write(String, int, int) has not been called.", called); 204 } 205 206 /** 207 * This method is called before a test is executed. It creates a 208 * FilterWriter instance. 209 */ 210 protected void setUp() { 211 212 fw = new MyFilterWriter(new MockWriter()); 213 called = false; 214 } 215 216 /** 217 * This method is called after a test is executed. It closes the 218 * FilterWriter instance. 219 */ 220 protected void tearDown() { 221 222 try { 223 fw.close(); 224 } catch (Exception e) { 225 System.out.println("Exception during FilterWriterTest tear down."); 226 } 227 } 228} 229