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 libcore.java.io;
19
20import java.io.ByteArrayOutputStream;
21import java.io.FilterWriter;
22import java.io.IOException;
23import java.io.OutputStreamWriter;
24
25public class OldFilterWriterTest extends junit.framework.TestCase {
26
27    private boolean called;
28    private FilterWriter fw;
29
30    static class MyFilterWriter extends java.io.FilterWriter {
31        public MyFilterWriter(java.io.Writer writer) {
32            super(writer);
33        }
34    }
35
36    class MockWriter extends java.io.Writer {
37        public MockWriter() {
38        }
39
40        public void close() throws IOException {
41            called = true;
42        }
43
44        public void flush() throws IOException {
45            called = true;
46        }
47
48        public void write(char[] buffer, int offset, int count) throws IOException {
49            called = true;
50        }
51
52        public void write(int oneChar) throws IOException {
53            called = true;
54        }
55
56        public void write(String str, int offset, int count) throws IOException {
57            called = true;
58        }
59
60        public long skip(long count) throws IOException {
61            called = true;
62            return 0;
63        }
64    }
65
66    public void test_ConstructorLjava_io_Writer() {
67
68        FilterWriter myWriter = null;
69
70        called = true;
71
72        try {
73            myWriter = new MyFilterWriter(null);
74            fail("NullPointerException expected.");
75        } catch (NullPointerException e) {
76            // expected
77        }
78    }
79
80    public void test_close() throws IOException {
81        fw.close();
82        assertTrue("close() has not been called.", called);
83    }
84
85    public void test_flush() throws IOException {
86        fw.flush();
87        assertTrue("flush() has not been called.", called);
88    }
89
90    public void test_writeI() throws IOException {
91        fw.write(0);
92        assertTrue("write(int) has not been called.", called);
93    }
94
95    public void test_write$CII() throws IOException {
96        char[] buffer = new char[5];
97        fw.write(buffer, 0, 5);
98        assertTrue("write(char[], int, int) has not been called.", called);
99    }
100
101    public void test_write$CII_Exception() throws IOException {
102        char[] buffer = new char[10];
103
104        fw = new MyFilterWriter(new OutputStreamWriter(
105            new ByteArrayOutputStream()));
106
107        try {
108            fw.write(buffer, 0, -1);
109            fail("Test 1: IndexOutOfBoundsException expected.");
110        } catch (IndexOutOfBoundsException e) {
111            // Expected.
112        }
113
114        try {
115            fw.write(buffer, -1, 1);
116            fail("Test 2: IndexOutOfBoundsException expected.");
117        } catch (IndexOutOfBoundsException e) {
118            // Expected.
119        }
120
121        try {
122            fw.write(buffer, 10, 1);
123            fail("Test 3: IndexOutOfBoundsException expected.");
124        } catch (IndexOutOfBoundsException e) {
125            // Expected.
126        }
127    }
128
129    public void test_writeLjava_lang_StringII() throws IOException {
130        fw.write("Hello world", 0, 5);
131        assertTrue("write(String, int, int) has not been called.", called);
132    }
133
134    protected void setUp() {
135
136        fw = new MyFilterWriter(new MockWriter());
137        called = false;
138    }
139
140    protected void tearDown() {
141
142        try {
143            fw.close();
144        } catch (Exception e) {
145            System.out.println("Exception during OldFilterWriterTest tear down.");
146        }
147    }
148}
149