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 android.core; 18 19 import junit.framework.TestCase; 20 21 import java.io.PrintWriter; 22 import java.io.StringWriter; 23 import android.test.suitebuilder.annotation.SmallTest; 24 25 public class PrintWriterTest extends TestCase { 26 27 @SmallTest testPrintWriter()28 public void testPrintWriter() throws Exception { 29 String str = "AbCdEfGhIjKlMnOpQrStUvWxYz"; 30 StringWriter aa = new StringWriter(); 31 PrintWriter a = new PrintWriter(aa); 32 33 try { 34 a.write(str, 0, 26); 35 a.write('X'); 36 37 assertEquals("AbCdEfGhIjKlMnOpQrStUvWxYzX", aa.toString()); 38 39 a.write("alphabravodelta", 5, 5); 40 a.append('X'); 41 assertEquals("AbCdEfGhIjKlMnOpQrStUvWxYzXbravoX", aa.toString()); 42 a.append("omega"); 43 assertEquals("AbCdEfGhIjKlMnOpQrStUvWxYzXbravoXomega", aa.toString()); 44 a.print("ZZZ"); 45 assertEquals("AbCdEfGhIjKlMnOpQrStUvWxYzXbravoXomegaZZZ", aa.toString()); 46 } finally { 47 a.close(); 48 } 49 50 StringWriter ba = new StringWriter(); 51 PrintWriter b = new PrintWriter(ba); 52 try { 53 b.print(true); 54 b.print((char) 'A'); 55 b.print("BCD".toCharArray()); 56 b.print((double) 1.2); 57 b.print((float) 3); 58 b.print((int) 4); 59 b.print((long) 5); 60 assertEquals("trueABCD1.23.045", ba.toString()); 61 b.println(); 62 b.println(true); 63 b.println((char) 'A'); 64 b.println("BCD".toCharArray()); 65 b.println((double) 1.2); 66 b.println((float) 3); 67 b.println((int) 4); 68 b.println((long) 5); 69 b.print("THE END"); 70 assertEquals("trueABCD1.23.045\ntrue\nA\nBCD\n1.2\n3.0\n4\n5\nTHE END", ba.toString()); 71 } finally { 72 b.close(); 73 } 74 } 75 } 76