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.

QuotedStringBourneUserPathStyleTest.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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_USER_PATH;
  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 QuotedStringBourneUserPathStyleTest {
  18. private static void assertQuote(String in, String exp) {
  19. final String r = BOURNE_USER_PATH.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_USER_PATH.dequote(b, 0, b.length);
  27. assertEquals(exp, r);
  28. }
  29. @Test
  30. public void testQuote_Empty() {
  31. assertEquals("''", BOURNE_USER_PATH.quote(""));
  32. }
  33. @Test
  34. public void testDequote_Empty1() {
  35. assertEquals("", BOURNE_USER_PATH.dequote(new byte[0], 0, 0));
  36. }
  37. @Test
  38. public void testDequote_Empty2() {
  39. assertEquals("", BOURNE_USER_PATH.dequote(new byte[] { '\'', '\'' }, 0,
  40. 2));
  41. }
  42. @Test
  43. public void testDequote_SoleSq() {
  44. assertEquals("", BOURNE_USER_PATH.dequote(new byte[] { '\'' }, 0, 1));
  45. }
  46. @Test
  47. public void testQuote_BareA() {
  48. assertQuote("a", "a");
  49. }
  50. @Test
  51. public void testDequote_BareA() {
  52. final String in = "a";
  53. final byte[] b = Constants.encode(in);
  54. assertEquals(in, BOURNE_USER_PATH.dequote(b, 0, b.length));
  55. }
  56. @Test
  57. public void testDequote_BareABCZ_OnlyBC() {
  58. final String in = "abcz";
  59. final byte[] b = Constants.encode(in);
  60. final int p = in.indexOf('b');
  61. assertEquals("bc", BOURNE_USER_PATH.dequote(b, p, p + 2));
  62. }
  63. @Test
  64. public void testDequote_LoneBackslash() {
  65. assertDequote("\\", "\\");
  66. }
  67. @Test
  68. public void testQuote_NamedEscapes() {
  69. assertQuote("'", "'\\''");
  70. assertQuote("!", "'\\!'");
  71. assertQuote("a'b", "a'\\''b");
  72. assertQuote("a!b", "a'\\!'b");
  73. }
  74. @Test
  75. public void testDequote_NamedEscapes() {
  76. assertDequote("'", "'\\''");
  77. assertDequote("!", "'\\!'");
  78. assertDequote("a'b", "a'\\''b");
  79. assertDequote("a!b", "a'\\!'b");
  80. }
  81. @Test
  82. public void testQuote_User() {
  83. assertEquals("~foo/", BOURNE_USER_PATH.quote("~foo"));
  84. assertEquals("~foo/", BOURNE_USER_PATH.quote("~foo/"));
  85. assertEquals("~/", BOURNE_USER_PATH.quote("~/"));
  86. assertEquals("~foo/'a'", BOURNE_USER_PATH.quote("~foo/a"));
  87. assertEquals("~/'a'", BOURNE_USER_PATH.quote("~/a"));
  88. }
  89. @Test
  90. public void testDequote_User() {
  91. assertEquals("~foo", BOURNE_USER_PATH.dequote("~foo"));
  92. assertEquals("~foo/", BOURNE_USER_PATH.dequote("~foo/"));
  93. assertEquals("~/", BOURNE_USER_PATH.dequote("~/"));
  94. assertEquals("~foo/a", BOURNE_USER_PATH.dequote("~foo/'a'"));
  95. assertEquals("~/a", BOURNE_USER_PATH.dequote("~/'a'"));
  96. }
  97. }