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 18 package libcore.java.io; 19 20 import java.io.ByteArrayInputStream; 21 import java.io.FilterReader; 22 import java.io.IOException; 23 import java.io.InputStreamReader; 24 25 public class OldFilterReaderTest extends junit.framework.TestCase { 26 27 private boolean called; 28 private FilterReader fr; 29 30 static class MyFilterReader extends java.io.FilterReader { 31 public MyFilterReader(java.io.Reader reader) { 32 super(reader); 33 } 34 } 35 36 class MockReader extends java.io.Reader { 37 public MockReader() { 38 } 39 40 public void close() throws IOException { 41 called = true; 42 } 43 44 public void mark(int readLimit) throws IOException { 45 called = true; 46 } 47 48 public boolean markSupported() { 49 called = true; 50 return false; 51 } 52 53 public int read() throws IOException { 54 called = true; 55 return 0; 56 } 57 58 public int read(char[] buffer, int offset, int count) throws IOException { 59 called = true; 60 return 0; 61 } 62 63 public boolean ready() throws IOException { 64 called = true; 65 return true; 66 } 67 68 public void reset() throws IOException { 69 called = true; 70 } 71 72 public long skip(long count) throws IOException { 73 called = true; 74 return 0; 75 } 76 } 77 78 public void test_ConstructorLjava_io_Reader() { 79 80 FilterReader myReader = null; 81 82 called = true; 83 84 try { 85 myReader = new MyFilterReader(null); 86 fail("NullPointerException expected."); 87 } catch (NullPointerException e) { 88 // expected 89 } 90 } 91 92 public void test_close() throws IOException { 93 fr.close(); 94 assertTrue("close() has not been called.", called); 95 } 96 97 public void test_markI() throws IOException { 98 fr.mark(0); 99 assertTrue("mark(int) has not been called.", called); 100 } 101 102 public void test_markSupported() { 103 fr.markSupported(); 104 assertTrue("markSupported() has not been called.", called); 105 } 106 107 public void test_read() throws IOException { 108 fr.read(); 109 assertTrue("read() has not been called.", called); 110 } 111 112 public void test_read$CII() throws IOException { 113 char[] buffer = new char[5]; 114 fr.read(buffer, 0, 5); 115 assertTrue("read(char[], int, int) has not been called.", called); 116 } 117 118 public void test_read$CII_Exception() throws IOException { 119 byte[] bbuffer = new byte[20]; 120 char[] buffer = new char[10]; 121 122 fr = new MyFilterReader(new InputStreamReader( 123 new ByteArrayInputStream(bbuffer))); 124 125 try { 126 fr.read(buffer, 0, -1); 127 fail("Test 1: IndexOutOfBoundsException expected."); 128 } catch (IndexOutOfBoundsException e) { 129 // Expected. 130 } 131 132 try { 133 fr.read(buffer, -1, 1); 134 fail("Test 2: IndexOutOfBoundsException expected."); 135 } catch (IndexOutOfBoundsException e) { 136 // Expected. 137 } 138 139 try { 140 fr.read(buffer, 10, 1); 141 fail("Test 3: IndexOutOfBoundsException expected."); 142 } catch (IndexOutOfBoundsException e) { 143 // Expected. 144 } 145 } 146 147 public void test_ready() throws IOException { 148 fr.ready(); 149 assertTrue("ready() has not been called.", called); 150 } 151 152 public void test_reset() throws IOException { 153 fr.reset(); 154 assertTrue("reset() has not been called.", called); 155 } 156 157 public void test_skip() throws IOException { 158 fr.skip(10); 159 assertTrue("skip(long) has not been called.", called); 160 } 161 162 protected void setUp() { 163 164 fr = new MyFilterReader(new MockReader()); 165 called = false; 166 } 167 168 protected void tearDown() { 169 170 try { 171 fr.close(); 172 } catch (Exception e) { 173 System.out.println("Exception during FilterReaderTest tear down."); 174 } 175 } 176 } 177