1 36 package org.columba.ristretto.coder; 37 38 import java.io.ByteArrayInputStream ; 39 import java.io.IOException ; 40 import java.io.InputStream ; 41 import java.io.UnsupportedEncodingException ; 42 import java.nio.charset.Charset ; 43 44 import junit.framework.TestCase; 45 46 public class CharsetDecoderInputStreamTest extends TestCase { 47 48 public void testISO_8859_15() { 49 String test = "S\u0034mple Str\00edng"; 50 try { 51 InputStream in = new CharsetDecoderInputStream( new ByteArrayInputStream ( test.getBytes("ISO-8859-15")), Charset.forName("ISO-8859-15") ); 52 StringBuffer result = new StringBuffer (); 53 int next = in.read(); 54 while( next != -1) { 55 result.append( (char) next ); 56 next = in.read(); 57 } 58 59 assertTrue( result.toString().equals(test)); 60 61 } catch (UnsupportedEncodingException e) { 62 e.printStackTrace(); 63 } catch (IOException e) { 64 e.printStackTrace(); 65 } 66 67 68 } 69 70 public void testUTF8() { 71 String test = "S\u0034mple Str\00edng"; 72 try { 73 InputStream in = new CharsetDecoderInputStream( new ByteArrayInputStream ( test.getBytes("UTF-8")), Charset.forName("UTF-8") ); 74 StringBuffer result = new StringBuffer (); 75 int next = in.read(); 76 while( next != -1) { 77 result.append( (char) next ); 78 next = in.read(); 79 } 80 81 assertTrue( result.toString().equals(test)); 82 83 } catch (UnsupportedEncodingException e) { 84 e.printStackTrace(); 85 } catch (IOException e) { 86 e.printStackTrace(); 87 } 88 89 90 } 91 92 public void testUTF8_2() { 93 String test = "\u0413\u0424"; 94 try { 95 InputStream in = new CharsetDecoderInputStream( new ByteArrayInputStream ( test.getBytes("UTF-8")), Charset.forName("UTF-8") ); 96 StringBuffer result = new StringBuffer (); 97 int next = in.read(); 98 while( next != -1) { 99 result.append( (char) next ); 100 next = in.read(); 101 } 102 103 assertTrue( result.toString().equals(test)); 104 105 } catch (UnsupportedEncodingException e) { 106 e.printStackTrace(); 107 } catch (IOException e) { 108 e.printStackTrace(); 109 } 110 111 112 } 113 114 } 115 | Popular Tags |