FileDocCategorySizeDatePackage
LoggingPrintStreamTest.javaAPI DocAndroid 5.1 API5950Thu Mar 12 22:22:12 GMT 2015com.android.internal.os

LoggingPrintStreamTest

public class LoggingPrintStreamTest extends TestCase

Fields Summary
TestPrintStream
out
Constructors Summary
Methods Summary
public voidtestAppend()

        out.append("Foo\n")
            .append('4")
            .append('\n")
            .append("Bar", 1, 2)
            .append('\n");
        assertEquals(Arrays.asList("Foo", "4", "a"), out.lines);
    
public voidtestMultiByteCharactersSpanningBuffers()

        // assume 3*1000 bytes won't fit in LoggingPrintStream's internal buffer
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < 1000; i++) {
            builder.append("\u20AC"); // a Euro character; 3 bytes in UTF-8
        }
        String expected = builder.toString();

        out.write(expected.getBytes("UTF-8"));
        out.flush();
        assertEquals(Arrays.asList(expected), out.lines);
    
public voidtestPrintCharArray()

        out.print("Foo\nBar\nTee".toCharArray());
        assertEquals(Arrays.asList("Foo", "Bar"), out.lines);
        out.flush();
        assertEquals(Arrays.asList("Foo", "Bar", "Tee"), out.lines);
    
public voidtestPrintException()


       
        @SuppressWarnings("ThrowableInstanceNeverThrown")
        Throwable t = new Throwable("Ignore me.");

        StringWriter sout = new StringWriter();
        t.printStackTrace(new PrintWriter(sout));

        t.printStackTrace(out);
        // t.printStackTrace();

        String[] lines = sout.toString().split("\\n");
        assertEquals(Arrays.asList(lines), out.lines);
    
public voidtestPrintInt()

        out.print(4);
        out.print(2);
        assertTrue(out.lines.isEmpty());
        out.flush();
        assertEquals(Collections.singletonList("42"), out.lines);
    
public voidtestPrintObject()

        Object o = new Object();
        out.print(4);
        out.print(o);
        out.print(2);
        out.flush();
        assertEquals(Arrays.asList("4" + o + "2"), out.lines);
    
public voidtestPrintString()

        out.print("Foo\nBar\nTee");
        assertEquals(Arrays.asList("Foo", "Bar"), out.lines);
        out.flush();
        assertEquals(Arrays.asList("Foo", "Bar", "Tee"), out.lines);
    
public voidtestPrintf()

        out.printf("Name: %s\nEmployer: %s", "Bob", "Google");
        assertEquals(Arrays.asList("Name: Bob"), out.lines);
        out.flush();
        assertEquals(Arrays.asList("Name: Bob", "Employer: Google"), out.lines);
    
public voidtestPrintlnCharArray()

        out.println("Foo\nBar\nTee".toCharArray());
        assertEquals(Arrays.asList("Foo", "Bar", "Tee"), out.lines);
    
public voidtestPrintlnInt()

        out.println(4);
        out.println(2);
        assertEquals(Arrays.asList("4", "2"), out.lines);
    
public voidtestPrintlnObject()

        Object o = new Object();
        out.print(4);
        out.println(o);
        out.print(2);
        out.flush();
        assertEquals(Arrays.asList("4" + o, "2"), out.lines);
    
public voidtestPrintlnString()

        out.println("Foo\nBar\nTee");
        assertEquals(Arrays.asList("Foo", "Bar", "Tee"), out.lines);
    
public voidtestPrintlnStringWithBufferedData()

        out.print(5);
        out.println("Foo\nBar\nTee");
        assertEquals(Arrays.asList("5Foo", "Bar", "Tee"), out.lines);
    
public voidtestWriteByteArrayAtATimeMultibyteCharacters()

        String expected = " \u20AC  \u20AC   \u20AC    \u20AC     ";
        out.write(expected.getBytes());
        out.flush();
        assertEquals(Arrays.asList(expected), out.lines);
    
public voidtestWriteFlushesOnNewlines()

        String a = " \u20AC  \u20AC ";
        String b = "  \u20AC    \u20AC  ";
        String c = "   ";
        String toWrite = a + "\n" + b + "\n" + c;
        out.write(toWrite.getBytes());
        out.flush();
        assertEquals(Arrays.asList(a, b, c), out.lines);
    
public voidtestWriteOneByteAtATimeMultibyteCharacters()

        String expected = " \u20AC  \u20AC   \u20AC    \u20AC     ";
        for (byte b : expected.getBytes()) {
            out.write(b);
        }
        out.flush();
        assertEquals(Arrays.asList(expected), out.lines);
    
public voidtestWriteWithOffsetsMultibyteCharacters()

        String expected = " \u20AC  \u20AC   \u20AC    \u20AC     ";
        byte[] bytes = expected.getBytes();
        int i = 0;
        while (i < bytes.length - 5) {
            out.write(bytes, i, 5);
            i += 5;
        }
        out.write(bytes, i, bytes.length - i);
        out.flush();
        assertEquals(Arrays.asList(expected), out.lines);