1 36 package org.columba.ristretto.coder; 37 38 import java.io.IOException ; 39 import java.io.InputStream ; 40 import java.io.UnsupportedEncodingException ; 41 import java.nio.charset.Charset ; 42 43 import junit.framework.TestCase; 44 45 import org.columba.ristretto.io.CharSequenceSource; 46 import org.columba.ristretto.io.SourceInputStream; 47 48 public class CharsetEncoderInputStreamTest extends TestCase { 49 50 public void testUS_ASCII() { 51 String input = "This is a test"; 52 Charset charset = Charset.forName("US-ASCII"); 53 54 try { 55 byte[] bytes = input.getBytes(charset.displayName()); 56 57 InputStream in = 58 new CharsetEncoderInputStream( 59 new SourceInputStream(new CharSequenceSource(input)), 60 charset); 61 int next = in.read(); 62 int pos = 0; 63 while (next != -1) { 64 assertTrue(bytes[pos++] == next); 65 next = in.read(); 66 } 67 } catch (UnsupportedEncodingException e) { 68 e.printStackTrace(); 69 } catch (IOException e) { 70 e.printStackTrace(); 71 } 72 } 73 74 public void testISO_8859_1() { 75 String input = "This is a \u00e4est"; 76 Charset charset = Charset.forName("ISO-8859-1"); 77 78 try { 79 byte[] bytes = input.getBytes(charset.name()); 80 81 InputStream in = 82 new CharsetEncoderInputStream( 83 new SourceInputStream(new CharSequenceSource(input)), 84 charset); 85 int next = in.read(); 86 int pos = 0; 87 while (next != -1) { 88 assertTrue(bytes[pos++] == next); 89 next = in.read(); 90 } 91 } catch (UnsupportedEncodingException e) { 92 e.printStackTrace(); 93 } catch (IOException e) { 94 e.printStackTrace(); 95 } 96 } 97 98 public void testISO_2022_JP() { 99 String input = "\u3061\u3061\u3062"; 100 Charset charset = Charset.forName("ISO-2022-JP"); 101 102 try { 103 byte[] bytes = input.getBytes(charset.name()); 104 105 InputStream in = 106 new CharsetEncoderInputStream( 107 new SourceInputStream(new CharSequenceSource(input)), 108 charset); 109 int next = in.read(); 110 int pos = 0; 111 while (next != -1) { 112 assertTrue(bytes[pos++] == next); 113 next = in.read(); 114 } 115 } catch (UnsupportedEncodingException e) { 116 e.printStackTrace(); 117 } catch (IOException e) { 118 e.printStackTrace(); 119 } 120 121 } 122 } 123 | Popular Tags |