summaryrefslogtreecommitdiffstats
path: root/tests/CoreTests/android/core/PrintWriterTest.java
blob: 09ee3898ff069ff5510271365a799d58553bb2c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.core;

import junit.framework.TestCase;

import java.io.PrintWriter;
import java.io.StringWriter;
import android.test.suitebuilder.annotation.SmallTest;

public class PrintWriterTest extends TestCase {

    @SmallTest
    public void testPrintWriter() throws Exception {
        String str = "AbCdEfGhIjKlMnOpQrStUvWxYz";
        StringWriter aa = new StringWriter();
        PrintWriter a = new PrintWriter(aa);

        try {
            a.write(str, 0, 26);
            a.write('X');

            assertEquals("AbCdEfGhIjKlMnOpQrStUvWxYzX", aa.toString());

            a.write("alphabravodelta", 5, 5);
            a.append('X');
            assertEquals("AbCdEfGhIjKlMnOpQrStUvWxYzXbravoX", aa.toString());
            a.append("omega");
            assertEquals("AbCdEfGhIjKlMnOpQrStUvWxYzXbravoXomega", aa.toString());
            a.print("ZZZ");
            assertEquals("AbCdEfGhIjKlMnOpQrStUvWxYzXbravoXomegaZZZ", aa.toString());
        } finally {
            a.close();
        }

        StringWriter ba = new StringWriter();
        PrintWriter b = new PrintWriter(ba);
        try {
            b.print(true);
            b.print((char) 'A');
            b.print("BCD".toCharArray());
            b.print((double) 1.2);
            b.print((float) 3);
            b.print((int) 4);
            b.print((long) 5);
            assertEquals("trueABCD1.23.045", ba.toString());
            b.println();
            b.println(true);
            b.println((char) 'A');
            b.println("BCD".toCharArray());
            b.println((double) 1.2);
            b.println((float) 3);
            b.println((int) 4);
            b.println((long) 5);
            b.print("THE END");
            assertEquals("trueABCD1.23.045\ntrue\nA\nBCD\n1.2\n3.0\n4\n5\nTHE END", ba.toString());
        } finally {
            b.close();
        }
    }
}