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 android.core; 18 19import junit.framework.TestCase; 20 21import java.io.ByteArrayInputStream; 22import java.io.InputStreamReader; 23import android.test.suitebuilder.annotation.SmallTest; 24 25/** 26 * Checks basic InputStreamReader functionality. 27 */ 28public class InputStreamReaderTest extends TestCase { 29 30 /** 31 * Checks if ASCII encoding works with InputStreamReader 32 */ 33 @SmallTest 34 public void testAscii() throws Exception { 35 String str = "AbCdEfGhIjKlMnOpQrStUvWxYzX"; 36 ByteArrayInputStream aa = new ByteArrayInputStream(str.getBytes("ISO8859_1")); 37 InputStreamReader a = new InputStreamReader(aa, "ISO8859_1"); 38 39 try { 40 int x = a.read(); 41 assertEquals('A', x); 42 char[] c = new char[26]; 43 x = a.read(c, 0, 26); 44 assertTrue(a.getEncoding().equalsIgnoreCase("ISO8859_1")); 45 assertEquals(26, x); 46 assertEquals("bCdEfGhIjKlMnOpQrStUvWxYzX", String.valueOf(c)); 47 } finally { 48 a.close(); 49 } 50 } 51 52 /** 53 * Checks if Utf8 encoding works with InputStreamReader 54 */ 55 @SmallTest 56 public void testUtf8() throws Exception { 57 String str = "AbCdEfGhIjKlMnOpQrStUvWxYzX" + 58 "\u00a3\u00c5\u00c9"; // total of 30 characters 59 ByteArrayInputStream aa = 60 new ByteArrayInputStream(str.getBytes()); 61 62 InputStreamReader a = new InputStreamReader(aa); 63 64 try { 65 assertEquals("UTF8", a.getEncoding()); 66 67 int x = a.read(); 68 assertEquals('A', x); 69 70 char[] c = new char[29]; 71 x = a.read(c, 0, 3); 72 assertEquals(3, x); 73 assertEquals("bCd", new String(c, 0, 3)); 74 75 x = a.read(c, 3, 26); 76 assertEquals(26, x); 77 assertEquals("EfGhIjKlMnOpQrStUvWxYzX\u00a3\u00c5\u00c9", new String(c, 3, 26)); 78 } finally { 79 a.close(); 80 } 81 } 82 83 /** 84 * Checks if several encodings works with InputStreamReader 85 */ 86 @SmallTest 87 public void testStringy() throws Exception { 88 String src = "The quick brown fox\u00A0\u00FF" + 89 "\uFFFC\uD7C5\uDC03bloof"; 90 91 String[] enc = new String[]{ 92 "utf-8", "us-ascii", "iso-8859-1", "utf-16be", "utf-16le", 93 "utf-16", 94 }; 95 96 for (int i = 0; i < enc.length; i++) { 97 byte[] ba = src.getBytes(enc[i]); 98 99 String s1 = new String(ba, enc[i]); 100 101 ByteArrayInputStream bais = new ByteArrayInputStream(ba); 102 InputStreamReader r = new InputStreamReader(bais, enc[i]); 103 try { 104 char[] ca = new char[600]; 105 int n = r.read(ca, 0, 600); 106 107 String s2 = new String(ca, 0, n); 108 assertEquals(s1, s2); 109 } finally { 110 r.close(); 111 } 112 } 113 } 114} 115