aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/QuotedStringBourneUserPathStyleTest.java
blob: 1b7c92fada5f01981c917e4be0ed2b16e25c0aba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * Copyright (C) 2008, Google Inc. and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.eclipse.jgit.util;

import static org.eclipse.jgit.util.QuotedString.BOURNE_USER_PATH;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;

import org.eclipse.jgit.lib.Constants;
import org.junit.Test;

public class QuotedStringBourneUserPathStyleTest {
	private static void assertQuote(String in, String exp) {
		final String r = BOURNE_USER_PATH.quote(in);
		assertNotSame(in, r);
		assertFalse(in.equals(r));
		assertEquals('\'' + exp + '\'', r);
	}

	private static void assertDequote(String exp, String in) {
		final byte[] b = Constants.encode('\'' + in + '\'');
		final String r = BOURNE_USER_PATH.dequote(b, 0, b.length);
		assertEquals(exp, r);
	}

	@Test
	public void testQuote_Empty() {
		assertEquals("''", BOURNE_USER_PATH.quote(""));
	}

	@Test
	public void testDequote_Empty1() {
		assertEquals("", BOURNE_USER_PATH.dequote(new byte[0], 0, 0));
	}

	@Test
	public void testDequote_Empty2() {
		assertEquals("", BOURNE_USER_PATH.dequote(new byte[] { '\'', '\'' }, 0,
				2));
	}

	@Test
	public void testDequote_SoleSq() {
		assertEquals("", BOURNE_USER_PATH.dequote(new byte[] { '\'' }, 0, 1));
	}

	@Test
	public void testQuote_BareA() {
		assertQuote("a", "a");
	}

	@Test
	public void testDequote_BareA() {
		final String in = "a";
		final byte[] b = Constants.encode(in);
		assertEquals(in, BOURNE_USER_PATH.dequote(b, 0, b.length));
	}

	@Test
	public void testDequote_BareABCZ_OnlyBC() {
		final String in = "abcz";
		final byte[] b = Constants.encode(in);
		final int p = in.indexOf('b');
		assertEquals("bc", BOURNE_USER_PATH.dequote(b, p, p + 2));
	}

	@Test
	public void testDequote_LoneBackslash() {
		assertDequote("\\", "\\");
	}

	@Test
	public void testQuote_NamedEscapes() {
		assertQuote("'", "'\\''");
		assertQuote("!", "'\\!'");

		assertQuote("a'b", "a'\\''b");
		assertQuote("a!b", "a'\\!'b");
	}

	@Test
	public void testDequote_NamedEscapes() {
		assertDequote("'", "'\\''");
		assertDequote("!", "'\\!'");

		assertDequote("a'b", "a'\\''b");
		assertDequote("a!b", "a'\\!'b");
	}

	@Test
	public void testQuote_User() {
		assertEquals("~foo/", BOURNE_USER_PATH.quote("~foo"));
		assertEquals("~foo/", BOURNE_USER_PATH.quote("~foo/"));
		assertEquals("~/", BOURNE_USER_PATH.quote("~/"));

		assertEquals("~foo/'a'", BOURNE_USER_PATH.quote("~foo/a"));
		assertEquals("~/'a'", BOURNE_USER_PATH.quote("~/a"));
	}

	@Test
	public void testDequote_User() {
		assertEquals("~foo", BOURNE_USER_PATH.dequote("~foo"));
		assertEquals("~foo/", BOURNE_USER_PATH.dequote("~foo/"));
		assertEquals("~/", BOURNE_USER_PATH.dequote("~/"));

		assertEquals("~foo/a", BOURNE_USER_PATH.dequote("~foo/'a'"));
		assertEquals("~/a", BOURNE_USER_PATH.dequote("~/'a'"));
	}
}