1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * 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 17 package com.android.internal.os; 18 19 import junit.framework.TestCase; 20 21 import java.util.Arrays; 22 import java.util.List; 23 import java.util.ArrayList; 24 import java.util.Collections; 25 import java.io.StringWriter; 26 import java.io.PrintWriter; 27 28 public class LoggingPrintStreamTest extends TestCase { 29 30 TestPrintStream out = new TestPrintStream(); 31 testPrintException()32 public void testPrintException() { 33 @SuppressWarnings("ThrowableInstanceNeverThrown") 34 Throwable t = new Throwable("Ignore me."); 35 36 StringWriter sout = new StringWriter(); 37 t.printStackTrace(new PrintWriter(sout)); 38 39 t.printStackTrace(out); 40 // t.printStackTrace(); 41 42 String[] lines = sout.toString().split("\\n"); 43 assertEquals(Arrays.asList(lines), out.lines); 44 } 45 testPrintObject()46 public void testPrintObject() { 47 Object o = new Object(); 48 out.print(4); 49 out.print(o); 50 out.print(2); 51 out.flush(); 52 assertEquals(Arrays.asList("4" + o + "2"), out.lines); 53 } 54 testPrintlnObject()55 public void testPrintlnObject() { 56 Object o = new Object(); 57 out.print(4); 58 out.println(o); 59 out.print(2); 60 out.flush(); 61 assertEquals(Arrays.asList("4" + o, "2"), out.lines); 62 } 63 testPrintf()64 public void testPrintf() { 65 out.printf("Name: %s\nEmployer: %s", "Bob", "Google"); 66 assertEquals(Arrays.asList("Name: Bob"), out.lines); 67 out.flush(); 68 assertEquals(Arrays.asList("Name: Bob", "Employer: Google"), out.lines); 69 } 70 testPrintInt()71 public void testPrintInt() { 72 out.print(4); 73 out.print(2); 74 assertTrue(out.lines.isEmpty()); 75 out.flush(); 76 assertEquals(Collections.singletonList("42"), out.lines); 77 } 78 testPrintlnInt()79 public void testPrintlnInt() { 80 out.println(4); 81 out.println(2); 82 assertEquals(Arrays.asList("4", "2"), out.lines); 83 } 84 testPrintCharArray()85 public void testPrintCharArray() { 86 out.print("Foo\nBar\nTee".toCharArray()); 87 assertEquals(Arrays.asList("Foo", "Bar"), out.lines); 88 out.flush(); 89 assertEquals(Arrays.asList("Foo", "Bar", "Tee"), out.lines); 90 } 91 testPrintString()92 public void testPrintString() { 93 out.print("Foo\nBar\nTee"); 94 assertEquals(Arrays.asList("Foo", "Bar"), out.lines); 95 out.flush(); 96 assertEquals(Arrays.asList("Foo", "Bar", "Tee"), out.lines); 97 } 98 testPrintlnCharArray()99 public void testPrintlnCharArray() { 100 out.println("Foo\nBar\nTee".toCharArray()); 101 assertEquals(Arrays.asList("Foo", "Bar", "Tee"), out.lines); 102 } 103 testPrintlnString()104 public void testPrintlnString() { 105 out.println("Foo\nBar\nTee"); 106 assertEquals(Arrays.asList("Foo", "Bar", "Tee"), out.lines); 107 } 108 testPrintlnStringWithBufferedData()109 public void testPrintlnStringWithBufferedData() { 110 out.print(5); 111 out.println("Foo\nBar\nTee"); 112 assertEquals(Arrays.asList("5Foo", "Bar", "Tee"), out.lines); 113 } 114 testAppend()115 public void testAppend() { 116 out.append("Foo\n") 117 .append('4') 118 .append('\n') 119 .append("Bar", 1, 2) 120 .append('\n'); 121 assertEquals(Arrays.asList("Foo", "4", "a"), out.lines); 122 } 123 124 static class TestPrintStream extends LoggingPrintStream { 125 126 final List<String> lines = new ArrayList<String>(); 127 log(String line)128 protected void log(String line) { 129 lines.add(line); 130 } 131 } 132 133 } 134