WriterTest.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
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 */
16
17package org.apache.harmony.luni.tests.java.io;
18
19import java.io.IOException;
20import java.io.Writer;
21
22import junit.framework.TestCase;
23import dalvik.annotation.TestLevel;
24import dalvik.annotation.TestTargetClass;
25import dalvik.annotation.TestTargetNew;
26
27@TestTargetClass(Writer.class)
28public class WriterTest extends TestCase {
29
30    @TestTargetNew(
31            level = TestLevel.COMPLETE,
32            method = "Writer",
33            args = {}
34        )
35    public void test_Writer() {
36        MockWriter w = new MockWriter();
37        assertTrue("Test 1: Lock has not been set correctly.", w.lockSet(w));
38    }
39
40    @TestTargetNew(
41            level = TestLevel.COMPLETE,
42            method = "Writer",
43            args = {java.lang.Object.class}
44        )
45    public void test_WriterLjava_lang_Object() {
46        Object o = new Object();
47        MockWriter w;
48
49        try {
50            w = new MockWriter(null);
51            fail("Test 1: NullPointerException expected.");
52        } catch (NullPointerException e) {
53            // Expected.
54        }
55        w = new MockWriter(o);
56        assertTrue("Test 2: Lock has not been set correctly.", w.lockSet(o));
57    }
58
59    class MockWriter extends Writer {
60        final Object myLock;
61
62        MockWriter() {
63            super();
64            myLock = this;
65        }
66
67        MockWriter(Object lock) {
68            super(lock);
69            myLock = lock;
70        }
71
72        @Override
73        public synchronized void close() throws IOException {
74            // do nothing
75        }
76
77        @Override
78        public synchronized void flush() throws IOException {
79            // do nothing
80        }
81
82        @Override
83        public void write(char[] arg0, int arg1, int arg2) throws IOException {
84            assertTrue(Thread.holdsLock(myLock));
85        }
86
87        public boolean lockSet(Object o) {
88            return (lock == o);
89        }
90    }
91}
92