You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CharactersetEncoderTestCase.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.afp.fonts;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.IOException;
  21. import java.nio.charset.CharacterCodingException;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import static org.junit.Assert.assertEquals;
  25. import static org.junit.Assert.assertFalse;
  26. import static org.junit.Assert.assertTrue;
  27. /**
  28. * Test {@link CharactersetEncoder}
  29. */
  30. public class CharactersetEncoderTestCase {
  31. private CharactersetEncoder singlebyteEncoder;
  32. private CharactersetEncoder doublebyteEncoder;
  33. @Before
  34. public void setUp() {
  35. singlebyteEncoder = CharactersetEncoder.newInstance("cp500", CharacterSetType.SINGLE_BYTE);
  36. doublebyteEncoder = CharactersetEncoder.newInstance("cp937",
  37. CharacterSetType.DOUBLE_BYTE_LINE_DATA);
  38. }
  39. // This is just an arbitrary CJK string
  40. private final String testCJKText = "\u8ACB\u65BC\u627F\u505A\u65E5\u4E03\u65E5\u5167\u672A\u9054"
  41. + "\u4E03\u65E5\u4E4B\u5B9A\u5B58\u8005\u4EE5\u5BE6\u969B\u5230\u671F\u65E5\u5167\u78BA"
  42. + "\u8A8D\u672C\u4EA4\u6613\u5167\u5BB9\u3002\u5982\u672A\u65BC\u4E0A\u8FF0\u671F\u9593"
  43. + "\u5167\u63D0\u51FA\u7570\u8B70\uFF0C\u8996\u540C\u610F\u627F\u8A8D\u672C\u4EA4\u6613"
  44. + "\u3002";
  45. private final byte[] test6CJKChars = {
  46. (byte) 0x61, (byte) 0x99,
  47. (byte) 0x50, (byte) 0xf4,
  48. (byte) 0x50, (byte) 0xd4,
  49. (byte) 0x56, (byte) 0x99,
  50. (byte) 0x4c, (byte) 0xc9,
  51. (byte) 0x4c, (byte) 0x44 };
  52. private final String testEngText = "Hello World!";
  53. private final byte[] testEngChars = {
  54. (byte) 0xc8, // H
  55. (byte) 0x85, // e
  56. (byte) 0x93, // l
  57. (byte) 0x93, // l
  58. (byte) 0x96, // o
  59. (byte) 0x40, // " "
  60. (byte) 0xe6, // W
  61. (byte) 0x96, // o
  62. (byte) 0x99, // r
  63. (byte) 0x93, // l
  64. (byte) 0x84, // d
  65. (byte) 0x4f // !
  66. };
  67. /**
  68. * Tests canEncode() - tests that canEncode() responds properly to various input characters.
  69. */
  70. @Test
  71. public void testCanEncode() {
  72. // Both SBCS and DBCS should support Latin characters
  73. for (char c = '!'; c < '~'; c++) {
  74. assertTrue(singlebyteEncoder.canEncode(c));
  75. assertTrue(doublebyteEncoder.canEncode(c));
  76. }
  77. // ONLY the double byte characters can handle CJK text
  78. for (char c : testCJKText.toCharArray()) {
  79. assertFalse(singlebyteEncoder.canEncode(c));
  80. assertTrue(doublebyteEncoder.canEncode(c));
  81. }
  82. // Ensure that double byte encoder doesn't just return true all the time...
  83. assertFalse(doublebyteEncoder.canEncode('\u00BB'));
  84. }
  85. @Test
  86. public void testEncode() throws CharacterCodingException, IOException {
  87. CharactersetEncoder.EncodedChars encChars; // = doublebyteEncoder.encode(testCJKText);
  88. ByteArrayOutputStream bOut = new ByteArrayOutputStream();
  89. // JAVA 1.5 has a bug in the JVM in which these err for some reason... JAVA 1.6 no issues
  90. /*encChars.writeTo(bOut, 0, encChars.getLength());
  91. byte[] bytes = bOut.toByteArray();
  92. for (int i = 0; i < 12; i++) {
  93. assertEquals(test6CJKChars[i], bytes[i]);
  94. }
  95. bOut.reset();*/
  96. encChars = singlebyteEncoder.encode(testEngText);
  97. encChars.writeTo(bOut, 0, encChars.getLength());
  98. byte[] engBytes = bOut.toByteArray();
  99. for (int i = 0; i < testEngChars.length; i++) {
  100. assertEquals(testEngChars[i], engBytes[i]);
  101. }
  102. assertEquals(testEngChars.length, engBytes.length);
  103. }
  104. }