ConsoleTest.java revision 9ca70c8c2b21dcf1ac641e87317cf8719f04d0a6
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 org.apache.harmony.luni.tests.java.io;
19
20import java.io.ByteArrayInputStream;
21import java.io.ByteArrayOutputStream;
22import java.io.Console;
23import java.io.IOError;
24import java.io.IOException;
25import java.io.InputStream;
26import java.io.OutputStream;
27import java.io.PrintWriter;
28import java.io.Reader;
29import java.lang.reflect.Constructor;
30import java.lang.reflect.Method;
31import java.util.Calendar;
32import java.util.GregorianCalendar;
33
34import junit.framework.TestCase;
35
36/**
37 * This file is test for java.io.Console. Due to the redirect problem, it can
38 * only be run on Harmony.
39 *
40 * @since 1.6
41 */
42public class ConsoleTest extends TestCase {
43    private static final byte[] bytes = "hello world\n".getBytes();
44
45    private InputStream in = new ByteArrayInputStream(bytes);
46    private OutputStream out = new ByteArrayOutputStream();
47    private Console console = null;
48
49    @Override protected void setUp() throws Exception {
50        super.setUp();
51        Constructor<Console> constructor =
52                Console.class.getDeclaredConstructor(InputStream.class, OutputStream.class);
53        constructor.setAccessible(true);
54        console = constructor.newInstance(in, out);
55    }
56
57    @Override protected void tearDown() throws Exception {
58        console = null;
59        super.tearDown();
60    }
61
62    public void test_getConsole() throws Exception {
63        assertNotNull(System.console());
64    }
65
66    public void test_flush() {
67        console.flush();
68        assertFalse(console.writer().checkError());
69    }
70
71    public void test_format_LString_LObject() {
72        assertSame(console, console.format("%d %s", 1, "hello"));
73        String prompt = new String(((ByteArrayOutputStream)out).toByteArray());
74        assertEquals("1 hello", prompt);
75    }
76
77    public void test_printf_LString_LObject() {
78        Calendar c = new GregorianCalendar(1983, 2, 21);
79        assertSame(console, console.printf("%1$tm %1$te,%1$tY", c));
80        String prompt = new String(((ByteArrayOutputStream)out).toByteArray());
81        assertEquals("03 21,1983", prompt);
82    }
83
84    public void test_reader() throws IOException {
85        Reader reader1 = console.reader();
86        assertTrue(reader1.ready());
87        Reader reader2 = console.reader();
88        assertSame(reader1, reader2);
89    }
90
91    public void test_readLine() {
92        String line = console.readLine();
93        assertEquals("hello world", line);
94    }
95
96    public void test_readLine_LString_LObject() {
97        String line = console.readLine("%d %s", 2, "Please input a line of string to test:");
98        assertEquals("hello world", line);
99        String prompt = new String(((ByteArrayOutputStream)out).toByteArray());
100        assertEquals("2 Please input a line of string to test:", prompt);
101    }
102
103    public void test_readPassword() {
104        // Since we cannot set the echo off by using the mock input and output
105        // stream, so IOException will be thrown.
106        try {
107            console.readPassword();
108            fail();
109        } catch (IOError expected) {
110        }
111    }
112
113    public void test_readPassword_LString_LObject() {
114        try {
115            console.readPassword("%d", 3);
116            fail();
117        } catch (IOError expected){
118        }
119        String prompt = new String(((ByteArrayOutputStream)out).toByteArray());
120        assertEquals("3", prompt);
121    }
122
123    /**
124     * @tests {@link java.io.Console#writer()}
125     */
126    public void test_writer() {
127        PrintWriter writer1 = console.writer();
128        PrintWriter writer2 = console.writer();
129        assertSame(writer1, writer2);
130    }
131}
132