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