WriterTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.apache.harmony.luni.tests.java.io;
17
18import java.io.IOException;
19import java.io.Writer;
20
21import junit.framework.TestCase;
22
23public class WriterTest extends TestCase {
24
25    /**
26     * @tests java.io.Writer#append(char)
27     */
28    public void test_appendChar() throws IOException {
29        char testChar = ' ';
30        MockWriter writer = new MockWriter(20);
31        writer.append(testChar);
32        assertEquals(String.valueOf(testChar), String.valueOf(writer
33                .getContents()));
34        writer.close();
35    }
36
37    /**
38     * @tests java.io.Writer#append(CharSequence)
39     */
40    public void test_appendCharSequence() throws IOException {
41        String testString = "My Test String";
42        MockWriter writer = new MockWriter(20);
43        writer.append(testString);
44        assertEquals(testString, String.valueOf(writer.getContents()));
45        writer.close();
46
47    }
48
49    /**
50     * @tests java.io.Writer#append(CharSequence, int, int)
51     */
52    public void test_appendCharSequenceIntInt() throws IOException {
53        String testString = "My Test String";
54        MockWriter writer = new MockWriter(20);
55        writer.append(testString, 1, 3);
56        assertEquals(testString.substring(1, 3), String.valueOf(writer
57                .getContents()));
58        writer.close();
59
60    }
61
62
63
64    /**
65     * @tests java.io.Writer#write(String)
66     */
67    public void test_writeLjava_lang_String() throws IOException {
68        // Regression for HARMONY-51
69        Object lock = new Object();
70        Writer wr = new MockLockWriter(lock);
71        wr.write("Some string");
72        wr.close();
73    }
74
75    class MockLockWriter extends Writer {
76        final Object myLock;
77
78        MockLockWriter(Object lock) {
79            super(lock);
80            myLock = lock;
81        }
82
83        @Override
84        public synchronized void close() throws IOException {
85            // do nothing
86        }
87
88        @Override
89        public synchronized void flush() throws IOException {
90            // do nothing
91        }
92
93        @Override
94        public void write(char[] arg0, int arg1, int arg2) throws IOException {
95            assertTrue(Thread.holdsLock(myLock));
96        }
97    }
98
99
100    class MockWriter extends Writer {
101        private char[] contents;
102
103        private int length;
104
105        private int offset;
106
107        MockWriter(int capacity) {
108            contents = new char[capacity];
109            length = capacity;
110            offset = 0;
111        }
112
113        public synchronized void close() throws IOException {
114            flush();
115            contents = null;
116        }
117
118        public synchronized void flush() throws IOException {
119            // do nothing
120        }
121
122        public void write(char[] buffer, int offset, int count)
123                throws IOException {
124            if (null == contents) {
125                throw new IOException();
126            }
127            if (offset < 0 || count < 0 || offset >= buffer.length) {
128                throw new IndexOutOfBoundsException();
129            }
130            count = Math.min(count, buffer.length - offset);
131            count = Math.min(count, this.length - this.offset);
132            for (int i = 0; i < count; i++) {
133                contents[this.offset + i] = buffer[offset + i];
134            }
135            this.offset += count;
136
137        }
138
139        public char[] getContents() {
140            char[] result = new char[offset];
141            for (int i = 0; i < offset; i++) {
142                result[i] = contents[i];
143            }
144            return result;
145        }
146    }
147
148}
149