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.

ConstantsEncodingTest.java 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (C) 2008, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.lib;
  11. import static org.junit.Assert.assertArrayEquals;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.fail;
  14. import java.io.UnsupportedEncodingException;
  15. import org.junit.Test;
  16. public class ConstantsEncodingTest {
  17. @Test
  18. public void testEncodeASCII_SimpleASCII()
  19. throws UnsupportedEncodingException {
  20. final String src = "abc";
  21. final byte[] exp = { 'a', 'b', 'c' };
  22. final byte[] res = Constants.encodeASCII(src);
  23. assertArrayEquals(exp, res);
  24. assertEquals(src, new String(res, 0, res.length, "UTF-8"));
  25. }
  26. @Test
  27. public void testEncodeASCII_FailOnNonASCII() {
  28. final String src = "Ūnĭcōde̽";
  29. try {
  30. Constants.encodeASCII(src);
  31. fail("Incorrectly accepted a Unicode character");
  32. } catch (IllegalArgumentException err) {
  33. assertEquals("Not ASCII string: " + src, err.getMessage());
  34. }
  35. }
  36. @Test
  37. public void testEncodeASCII_Number13() {
  38. final long src = 13;
  39. final byte[] exp = { '1', '3' };
  40. final byte[] res = Constants.encodeASCII(src);
  41. assertArrayEquals(exp, res);
  42. }
  43. @Test
  44. public void testEncode_SimpleASCII() throws UnsupportedEncodingException {
  45. final String src = "abc";
  46. final byte[] exp = { 'a', 'b', 'c' };
  47. final byte[] res = Constants.encode(src);
  48. assertArrayEquals(exp, res);
  49. assertEquals(src, new String(res, 0, res.length, "UTF-8"));
  50. }
  51. @Test
  52. public void testEncode_Unicode() throws UnsupportedEncodingException {
  53. final String src = "Ūnĭcōde̽";
  54. final byte[] exp = { (byte) 0xC5, (byte) 0xAA, 0x6E, (byte) 0xC4,
  55. (byte) 0xAD, 0x63, (byte) 0xC5, (byte) 0x8D, 0x64, 0x65,
  56. (byte) 0xCC, (byte) 0xBD };
  57. final byte[] res = Constants.encode(src);
  58. assertArrayEquals(exp, res);
  59. assertEquals(src, new String(res, 0, res.length, "UTF-8"));
  60. }
  61. }