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.tests.java.io; 19 20import junit.framework.TestCase; 21import java.io.ByteArrayInputStream; 22import java.io.ByteArrayOutputStream; 23import java.io.Console; 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.util.Calendar; 31import java.util.GregorianCalendar; 32 33/** 34 * This file is test for java.io.Console. Due to the redirect problem, it can 35 * only be run on Harmony. 36 * 37 * @since 1.6 38 */ 39public class ConsoleTest extends TestCase { 40 private static final byte[] bytes = "hello world\n".getBytes(); 41 42 private InputStream in = new ByteArrayInputStream(bytes); 43 private OutputStream out = new ByteArrayOutputStream(); 44 private Console console = null; 45 46 @Override 47 protected void setUp() throws Exception { 48 super.setUp(); 49 Constructor<Console> constructor = 50 Console.class.getDeclaredConstructor(InputStream.class, OutputStream.class); 51 constructor.setAccessible(true); 52 console = constructor.newInstance(in, out); 53 } 54 55 @Override 56 protected void tearDown() throws Exception { 57 console = null; 58 super.tearDown(); 59 } 60 61 public void test_flush() { 62 console.flush(); 63 assertFalse(console.writer().checkError()); 64 } 65 66 public void test_format_LString_LObject() { 67 assertSame(console, console.format("%d %s", 1, "hello")); 68 String prompt = new String(((ByteArrayOutputStream) out).toByteArray()); 69 assertEquals("1 hello", prompt); 70 } 71 72 public void test_printf_LString_LObject() { 73 Calendar c = new GregorianCalendar(1983, 2, 21); 74 assertSame(console, console.printf("%1$tm %1$te,%1$tY", c)); 75 String prompt = new String(((ByteArrayOutputStream) out).toByteArray()); 76 assertEquals("03 21,1983", prompt); 77 } 78 79 public void test_reader() throws IOException { 80 Reader reader1 = console.reader(); 81 assertTrue(reader1.ready()); 82 Reader reader2 = console.reader(); 83 assertSame(reader1, reader2); 84 } 85 86 public void test_readLine() { 87 String line = console.readLine(); 88 assertEquals("hello world", line); 89 } 90 91 public void test_readLine_LString_LObject() { 92 String line = console.readLine("%d %s", 2, "Please input a line of string to test:"); 93 assertEquals("hello world", line); 94 String prompt = new String(((ByteArrayOutputStream) out).toByteArray()); 95 assertEquals("2 Please input a line of string to test:", prompt); 96 } 97 98 /** 99 * {@link java.io.Console#writer()} 100 */ 101 public void test_writer() { 102 PrintWriter writer1 = console.writer(); 103 PrintWriter writer2 = console.writer(); 104 assertSame(writer1, writer2); 105 } 106} 107