* 0-128 and supports multibyte characters (>128) which represent chinese
* symbols.
*/
public void testWriteChinese() throws Exception {
Writer writer = new StringWriter();
Encoding encoding = new BitSetEncoding("GB18030", 2025);
CharsetEncodingWriter csew =
new CharsetEncodingWriter(writer, encoding);
// All of these are valid chinese characters
char[] chars = new char[] {
'a','c','c','d','e','f','g','h','i','j','k',
'A','B','C','D','E','F','G','H','I','J','K',
'0','1','2','3','4','5','6','7','8','9',
0xd6b2, 0xbfc3, 0xd6ea, 0xcda9, 0xe9ab,
0xbcd2, 0xabc7, 0xbec0, 0xd6b7, 0xa9d0};
csew.write(chars);
String result = writer.toString();
char[] out = result.toCharArray();
assertEquals( chars.length, out.length );
for( int i = 0; i < chars.length; i++ ){
if( chars[i] != out[i] ) {
fail( "Invalid character " + out[i] + " at index " + i );
}
}
// These are not valid chinese characters
char[] invalidChars = new char[] {
195,'a', 185,'z', 210,'c', 188,'d', 208,'y', 208,'q'
};
writer = new StringWriter();
csew = new CharsetEncodingWriter(writer, encoding);
csew.write(invalidChars);
result = writer.toString();
assertEquals("Output not as expected.",
"Ãa¹zÒc¼dÐyÐq", result );
// Finally test some specific characters
CharacterRepresentable rep;
rep = encoding.checkCharacter(32654);
assertEquals( "Character should be representable",
true, rep.isRepresentable());
rep = encoding.checkCharacter(22269);
assertEquals( "Character should be representable",
true, rep.isRepresentable());
rep = encoding.checkCharacter(19968);
assertEquals( "Character should be representable",
true, rep.isRepresentable());
rep = encoding.checkCharacter(214);
assertEquals( "Character should not be representable",
true, rep.notRepresentable());
rep = encoding.checkCharacter(188);
assertEquals( "Character should not be representable",
true, rep.notRepresentable());
}