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