LoggingPrintStreamTest.java revision be81f4f15dad6d690efcab1973d1e174ce3b001b
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 17package com.android.internal.os; 18 19import junit.framework.TestCase; 20 21import java.io.PrintWriter; 22import java.io.StringWriter; 23import java.util.ArrayList; 24import java.util.Arrays; 25import java.util.Collections; 26import java.util.List; 27 28public class LoggingPrintStreamTest extends TestCase { 29 30 TestPrintStream out = new TestPrintStream(); 31 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 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 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 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 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 79 public void testPrintlnInt() { 80 out.println(4); 81 out.println(2); 82 assertEquals(Arrays.asList("4", "2"), out.lines); 83 } 84 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 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 99 public void testPrintlnCharArray() { 100 out.println("Foo\nBar\nTee".toCharArray()); 101 assertEquals(Arrays.asList("Foo", "Bar", "Tee"), out.lines); 102 } 103 104 public void testPrintlnString() { 105 out.println("Foo\nBar\nTee"); 106 assertEquals(Arrays.asList("Foo", "Bar", "Tee"), out.lines); 107 } 108 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 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 public void testMultiByteCharactersSpanningBuffers() throws Exception { 125 // assume 3*1000 bytes won't fit in LoggingPrintStream's internal buffer 126 StringBuilder builder = new StringBuilder(); 127 for (int i = 0; i < 1000; i++) { 128 builder.append("\u20AC"); // a Euro character; 3 bytes in UTF-8 129 } 130 String expected = builder.toString(); 131 132 out.write(expected.getBytes("UTF-8")); 133 out.flush(); 134 assertEquals(Arrays.asList(expected), out.lines); 135 } 136 137 public void testWriteOneByteAtATimeMultibyteCharacters() throws Exception { 138 String expected = " \u20AC \u20AC \u20AC \u20AC "; 139 for (byte b : expected.getBytes()) { 140 out.write(b); 141 } 142 out.flush(); 143 assertEquals(Arrays.asList(expected), out.lines); 144 } 145 146 public void testWriteByteArrayAtATimeMultibyteCharacters() throws Exception { 147 String expected = " \u20AC \u20AC \u20AC \u20AC "; 148 out.write(expected.getBytes()); 149 out.flush(); 150 assertEquals(Arrays.asList(expected), out.lines); 151 } 152 153 public void testWriteWithOffsetsMultibyteCharacters() throws Exception { 154 String expected = " \u20AC \u20AC \u20AC \u20AC "; 155 byte[] bytes = expected.getBytes(); 156 int i = 0; 157 while (i < bytes.length - 5) { 158 out.write(bytes, i, 5); 159 i += 5; 160 } 161 out.write(bytes, i, bytes.length - i); 162 out.flush(); 163 assertEquals(Arrays.asList(expected), out.lines); 164 } 165 166 public void testWriteFlushesOnNewlines() throws Exception { 167 String a = " \u20AC \u20AC "; 168 String b = " \u20AC \u20AC "; 169 String c = " "; 170 String toWrite = a + "\n" + b + "\n" + c; 171 out.write(toWrite.getBytes()); 172 out.flush(); 173 assertEquals(Arrays.asList(a, b, c), out.lines); 174 } 175 176 static class TestPrintStream extends LoggingPrintStream { 177 178 final List<String> lines = new ArrayList<String>(); 179 180 protected void log(String line) { 181 lines.add(line); 182 } 183 } 184} 185