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.

QuotedStringBourneStyleTest.java 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.util;
  11. import static org.eclipse.jgit.util.QuotedString.BOURNE;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertFalse;
  14. import static org.junit.Assert.assertNotSame;
  15. import org.eclipse.jgit.lib.Constants;
  16. import org.junit.Test;
  17. public class QuotedStringBourneStyleTest {
  18. private static void assertQuote(String in, String exp) {
  19. final String r = BOURNE.quote(in);
  20. assertNotSame(in, r);
  21. assertFalse(in.equals(r));
  22. assertEquals('\'' + exp + '\'', r);
  23. }
  24. private static void assertDequote(String exp, String in) {
  25. final byte[] b = Constants.encode('\'' + in + '\'');
  26. final String r = BOURNE.dequote(b, 0, b.length);
  27. assertEquals(exp, r);
  28. }
  29. @Test
  30. public void testQuote_Empty() {
  31. assertEquals("''", BOURNE.quote(""));
  32. }
  33. @Test
  34. public void testDequote_Empty1() {
  35. assertEquals("", BOURNE.dequote(new byte[0], 0, 0));
  36. }
  37. @Test
  38. public void testDequote_Empty2() {
  39. assertEquals("", BOURNE.dequote(new byte[] { '\'', '\'' }, 0, 2));
  40. }
  41. @Test
  42. public void testDequote_SoleSq() {
  43. assertEquals("", BOURNE.dequote(new byte[] { '\'' }, 0, 1));
  44. }
  45. @Test
  46. public void testQuote_BareA() {
  47. assertQuote("a", "a");
  48. }
  49. @Test
  50. public void testDequote_BareA() {
  51. final String in = "a";
  52. final byte[] b = Constants.encode(in);
  53. assertEquals(in, BOURNE.dequote(b, 0, b.length));
  54. }
  55. @Test
  56. public void testDequote_BareABCZ_OnlyBC() {
  57. final String in = "abcz";
  58. final byte[] b = Constants.encode(in);
  59. final int p = in.indexOf('b');
  60. assertEquals("bc", BOURNE.dequote(b, p, p + 2));
  61. }
  62. @Test
  63. public void testDequote_LoneBackslash() {
  64. assertDequote("\\", "\\");
  65. }
  66. @Test
  67. public void testQuote_NamedEscapes() {
  68. assertQuote("'", "'\\''");
  69. assertQuote("!", "'\\!'");
  70. assertQuote("a'b", "a'\\''b");
  71. assertQuote("a!b", "a'\\!'b");
  72. }
  73. @Test
  74. public void testDequote_NamedEscapes() {
  75. assertDequote("'", "'\\''");
  76. assertDequote("!", "'\\!'");
  77. assertDequote("a'b", "a'\\''b");
  78. assertDequote("a!b", "a'\\!'b");
  79. }
  80. }