PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/java-1.7.0-openjdk/openjdk/jdk/test/sun/nio/cs/JISAutoDetectTest.java

#
Java | 158 lines | 103 code | 19 blank | 36 comment | 12 complexity | 5039207cc6feb4e5af45577d3a42c36d MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, LGPL-3.0, LGPL-2.0
  1. /*
  2. * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This code is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * version 2 for more details (a copy is included in the LICENSE file that
  13. * accompanied this code).
  14. *
  15. * You should have received a copy of the GNU General Public License version
  16. * 2 along with this work; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20. * or visit www.oracle.com if you need additional information or have any
  21. * questions.
  22. */
  23. /*
  24. * @test
  25. * @bug 4087261 4184592
  26. * @summary Make sure to determine Japanese text encoding as correctly
  27. * as possible.
  28. */
  29. import java.nio.charset.*;
  30. import java.nio.*;
  31. public class JISAutoDetectTest {
  32. class TestData {
  33. byte[] input;
  34. byte[] input2; // for second call
  35. String expectedCharset;
  36. }
  37. TestData[] data = new TestData[50];
  38. public static void main(String[] argv) throws Exception {
  39. JISAutoDetectTest test = new JISAutoDetectTest();
  40. test.execute();
  41. }
  42. void execute() throws Exception {
  43. CharBuffer output = CharBuffer.allocate(128);
  44. CharBuffer expectedOutput = CharBuffer.allocate(128);
  45. for (int i = 0; i < data.length; i++) {
  46. if (data[i] == null)
  47. break;
  48. CharsetDecoder autoDetect = Charset.forName("JISAutoDetect").newDecoder();
  49. CharsetDecoder dec = Charset.forName(data[i].expectedCharset).newDecoder();
  50. CoderResult ncr, mcr;
  51. output.clear();
  52. expectedOutput.clear();
  53. ncr = autoDetect.decode(ByteBuffer.wrap(data[i].input),
  54. output,
  55. true);
  56. mcr = dec.decode(ByteBuffer.wrap(data[i].input),
  57. expectedOutput,
  58. true);
  59. if (data[i].input2 != null) {
  60. ncr = autoDetect.decode(ByteBuffer.wrap(data[i].input2),
  61. output,
  62. true);
  63. mcr = dec.decode(ByteBuffer.wrap(data[i].input2),
  64. expectedOutput,
  65. true);
  66. }
  67. String testNumber = " (test#: " + i + ")";
  68. if (ncr != mcr)
  69. throw new Exception("JISAutoDetect returned a wrong result");
  70. output.flip();
  71. expectedOutput.flip();
  72. if (output.limit() != expectedOutput.limit())
  73. throw new Exception("JISAutoDetect returned a wrong length"+testNumber);
  74. for (int x = 0; x < output.limit(); x++) {
  75. if (expectedOutput.charAt(x) != output.charAt(x))
  76. throw new Exception("JISAutoDetect returned a wrong string"+testNumber);
  77. }
  78. }
  79. }
  80. public JISAutoDetectTest() {
  81. int i = 0;
  82. // 0
  83. data[i] = new TestData();
  84. data[i].input = new byte[] { (byte)'C', (byte)'o', (byte)'p', (byte)'y',
  85. (byte)'r', (byte)'i', (byte)'g', (byte)'h',
  86. (byte)'t', (byte)' ', (byte)0xa9, (byte)' ',
  87. (byte)'1', (byte)'9', (byte)'9', (byte)'8' };
  88. data[i].expectedCharset = "SJIS";
  89. // 1
  90. i++;
  91. data[i] = new TestData();
  92. data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
  93. (byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
  94. (byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde,
  95. (byte)0x82, (byte)0xc5, (byte)0x82, (byte)0xb7 };
  96. data[i].expectedCharset = "SJIS";
  97. // 2
  98. i++;
  99. data[i] = new TestData();
  100. data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
  101. (byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
  102. (byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde};
  103. data[i].expectedCharset = "SJIS";
  104. // 3
  105. i++;
  106. data[i] = new TestData();
  107. data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
  108. (byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
  109. (byte)0xc3, (byte)0xd1, (byte)0xbd };
  110. data[i].expectedCharset = "SJIS";
  111. // 4
  112. i++;
  113. data[i] = new TestData();
  114. data[i].input = new byte[] { (byte)0x8f, (byte)0xa1, (byte)0xaa };
  115. data[i].expectedCharset = "SJIS";
  116. // 5
  117. i++;
  118. data[i] = new TestData();
  119. data[i].input = new byte[] { (byte)0xa4, (byte)0xd2, (byte)0xa4, (byte)0xe9,
  120. (byte)0xa4, (byte)0xac, (byte)0xa4, (byte)0xca };
  121. data[i].expectedCharset = "EUC_JP";
  122. // 6
  123. i++;
  124. data[i] = new TestData();
  125. data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
  126. (byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
  127. (byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde,
  128. (byte)0xa4, (byte)0xc7, (byte)0xa4, (byte)0xb9 };
  129. data[i].expectedCharset = "EUC_JP";
  130. // 7 (for 4184592)
  131. i++;
  132. data[i] = new TestData();
  133. data[i].input = new byte[] { (byte)'a', (byte)'b', (byte)'c' };
  134. data[i].input2 = new byte[] { (byte)0x1b, (byte)'$', (byte)'B',
  135. (byte)'#', (byte)'4', (byte)'$', (byte)'5',
  136. (byte)0x1b, (byte)'(', (byte)'B' };
  137. data[i].expectedCharset = "ISO2022JP";
  138. }
  139. }