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 FallbackCharsetDecoderInputStreamTest extends TestCase { 47 48 public void testISO_8859_1butwindows_1252() { 49 String test = "\u2014mple"; 50 try { 51 InputStream in = new FallbackCharsetDecoderInputStream( new ByteArrayInputStream ( test.getBytes("windows-1252")), Charset.forName("ISO-8859-1") ); 52 StringBuffer result = new StringBuffer (); 54 int next = in.read(); 55 while( next != -1) { 56 result.append( (char) next ); 57 next = in.read(); 58 } 59 60 assertEquals( test, result.toString()); 61 62 } catch (UnsupportedEncodingException e) { 63 e.printStackTrace(); 64 } catch (IOException e) { 65 e.printStackTrace(); 66 } 67 68 69 } 70 public void testUTF8() { 71 String test = "S\u0034mple Str\00edng"; 72 try { 73 InputStream in = new FallbackCharsetDecoderInputStream( 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 public void testUTF8_2() { 92 String test = "\u0413\u0424"; 93 try { 94 InputStream in = new FallbackCharsetDecoderInputStream( new ByteArrayInputStream ( test.getBytes("UTF-8")), Charset.forName("UTF-8") ); 95 StringBuffer result = new StringBuffer (); 96 int next = in.read(); 97 while( next != -1) { 98 result.append( (char) next ); 99 next = in.read(); 100 } 101 102 assertTrue( result.toString().equals(test)); 103 104 } catch (UnsupportedEncodingException e) { 105 e.printStackTrace(); 106 } catch (IOException e) { 107 e.printStackTrace(); 108 } 109 110 111 } 112 113 } 114 | Popular Tags |