• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 androidx.test.filters.Suppress;
20 
21 import junit.framework.TestCase;
22 
23 import java.io.PrintWriter;
24 import java.io.StringWriter;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.List;
29 
30 // this test causes a IllegalAccessError: superclass not accessible
31 @Suppress
32 public class LoggingPrintStreamTest extends TestCase {
33 
34     TestPrintStream out = new TestPrintStream();
35 
testPrintException()36     public void testPrintException() {
37         @SuppressWarnings("ThrowableInstanceNeverThrown")
38         Throwable t = new Throwable("Ignore me.");
39 
40         StringWriter sout = new StringWriter();
41         t.printStackTrace(new PrintWriter(sout));
42 
43         t.printStackTrace(out);
44         // t.printStackTrace();
45 
46         String[] lines = sout.toString().split("\\n");
47         assertEquals(Arrays.asList(lines), out.lines);
48     }
49 
testPrintObject()50     public void testPrintObject() {
51         Object o = new Object();
52         out.print(4);
53         out.print(o);
54         out.print(2);
55         out.flush();
56         assertEquals(Arrays.asList("4" + o + "2"), out.lines);
57     }
58 
testPrintlnObject()59     public void testPrintlnObject() {
60         Object o = new Object();
61         out.print(4);
62         out.println(o);
63         out.print(2);
64         out.flush();
65         assertEquals(Arrays.asList("4" + o, "2"), out.lines);
66     }
67 
testPrintf()68     public void testPrintf() {
69         out.printf("Name: %s\nEmployer: %s", "Bob", "Google");
70         assertEquals(Arrays.asList("Name: Bob"), out.lines);
71         out.flush();
72         assertEquals(Arrays.asList("Name: Bob", "Employer: Google"), out.lines);
73     }
74 
testPrintInt()75     public void testPrintInt() {
76         out.print(4);
77         out.print(2);
78         assertTrue(out.lines.isEmpty());
79         out.flush();
80         assertEquals(Collections.singletonList("42"), out.lines);
81     }
82 
testPrintlnInt()83     public void testPrintlnInt() {
84         out.println(4);
85         out.println(2);
86         assertEquals(Arrays.asList("4", "2"), out.lines);
87     }
88 
testPrintCharArray()89     public void testPrintCharArray() {
90         out.print("Foo\nBar\nTee".toCharArray());
91         assertEquals(Arrays.asList("Foo", "Bar"), out.lines);
92         out.flush();
93         assertEquals(Arrays.asList("Foo", "Bar", "Tee"), out.lines);
94     }
95 
testPrintString()96     public void testPrintString() {
97         out.print("Foo\nBar\nTee");
98         assertEquals(Arrays.asList("Foo", "Bar"), out.lines);
99         out.flush();
100         assertEquals(Arrays.asList("Foo", "Bar", "Tee"), out.lines);
101     }
102 
testPrintlnCharArray()103     public void testPrintlnCharArray() {
104         out.println("Foo\nBar\nTee".toCharArray());
105         assertEquals(Arrays.asList("Foo", "Bar", "Tee"), out.lines);
106     }
107 
testPrintlnString()108     public void testPrintlnString() {
109         out.println("Foo\nBar\nTee");
110         assertEquals(Arrays.asList("Foo", "Bar", "Tee"), out.lines);
111     }
112 
testPrintlnStringWithBufferedData()113     public void testPrintlnStringWithBufferedData() {
114         out.print(5);
115         out.println("Foo\nBar\nTee");
116         assertEquals(Arrays.asList("5Foo", "Bar", "Tee"), out.lines);
117     }
118 
testAppend()119     public void testAppend() {
120         out.append("Foo\n")
121             .append('4')
122             .append('\n')
123             .append("Bar", 1, 2)
124             .append('\n');
125         assertEquals(Arrays.asList("Foo", "4", "a"), out.lines);
126     }
127 
testMultiByteCharactersSpanningBuffers()128     public void testMultiByteCharactersSpanningBuffers() throws Exception {
129         // assume 3*1000 bytes won't fit in LoggingPrintStream's internal buffer
130         StringBuilder builder = new StringBuilder();
131         for (int i = 0; i < 1000; i++) {
132             builder.append("\u20AC"); // a Euro character; 3 bytes in UTF-8
133         }
134         String expected = builder.toString();
135 
136         out.write(expected.getBytes("UTF-8"));
137         out.flush();
138         assertEquals(Arrays.asList(expected), out.lines);
139     }
140 
testWriteOneByteAtATimeMultibyteCharacters()141     public void testWriteOneByteAtATimeMultibyteCharacters() throws Exception {
142         String expected = " \u20AC  \u20AC   \u20AC    \u20AC     ";
143         for (byte b : expected.getBytes()) {
144             out.write(b);
145         }
146         out.flush();
147         assertEquals(Arrays.asList(expected), out.lines);
148     }
149 
testWriteByteArrayAtATimeMultibyteCharacters()150     public void testWriteByteArrayAtATimeMultibyteCharacters() throws Exception {
151         String expected = " \u20AC  \u20AC   \u20AC    \u20AC     ";
152         out.write(expected.getBytes());
153         out.flush();
154         assertEquals(Arrays.asList(expected), out.lines);
155     }
156 
testWriteWithOffsetsMultibyteCharacters()157     public void testWriteWithOffsetsMultibyteCharacters() throws Exception {
158         String expected = " \u20AC  \u20AC   \u20AC    \u20AC     ";
159         byte[] bytes = expected.getBytes();
160         int i = 0;
161         while (i < bytes.length - 5) {
162             out.write(bytes, i, 5);
163             i += 5;
164         }
165         out.write(bytes, i, bytes.length - i);
166         out.flush();
167         assertEquals(Arrays.asList(expected), out.lines);
168     }
169 
testWriteFlushesOnNewlines()170     public void testWriteFlushesOnNewlines() throws Exception {
171         String a = " \u20AC  \u20AC ";
172         String b = "  \u20AC    \u20AC  ";
173         String c = "   ";
174         String toWrite = a + "\n" + b + "\n" + c;
175         out.write(toWrite.getBytes());
176         out.flush();
177         assertEquals(Arrays.asList(a, b, c), out.lines);
178     }
179 
180     static class TestPrintStream extends LoggingPrintStream {
181 
182         final List<String> lines = new ArrayList<String>();
183 
log(String line)184         protected void log(String line) {
185             lines.add(line);
186         }
187     }
188 }
189