2 * Copyright (C) 2008, Google Inc.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org
.spearce
.jgit
.util
;
40 import static org
.spearce
.jgit
.util
.QuotedString
.GIT_PATH
;
42 import java
.io
.UnsupportedEncodingException
;
44 import junit
.framework
.TestCase
;
46 import org
.spearce
.jgit
.lib
.Constants
;
48 public class QuotedStringGitPathStyleTest
extends TestCase
{
49 private static void assertQuote(final String exp
, final String in
) {
50 final String r
= GIT_PATH
.quote(in
);
52 assertFalse(in
.equals(r
));
53 assertEquals('"' + exp
+ '"', r
);
56 private static void assertDequote(final String exp
, final String in
) {
59 b
= ('"' + in
+ '"').getBytes("ISO-8859-1");
60 } catch (UnsupportedEncodingException e
) {
61 throw new RuntimeException(e
);
63 final String r
= GIT_PATH
.dequote(b
, 0, b
.length
);
67 public void testQuote_Empty() {
68 assertEquals("\"\"", GIT_PATH
.quote(""));
71 public void testDequote_Empty1() {
72 assertEquals("", GIT_PATH
.dequote(new byte[0], 0, 0));
75 public void testDequote_Empty2() {
76 assertEquals("", GIT_PATH
.dequote(new byte[] { '"', '"' }, 0, 2));
79 public void testDequote_SoleDq() {
80 assertEquals("\"", GIT_PATH
.dequote(new byte[] { '"' }, 0, 1));
83 public void testQuote_BareA() {
84 final String in
= "a";
85 assertSame(in
, GIT_PATH
.quote(in
));
88 public void testDequote_BareA() {
89 final String in
= "a";
90 final byte[] b
= Constants
.encode(in
);
91 assertEquals(in
, GIT_PATH
.dequote(b
, 0, b
.length
));
94 public void testDequote_BareABCZ_OnlyBC() {
95 final String in
= "abcz";
96 final byte[] b
= Constants
.encode(in
);
97 final int p
= in
.indexOf('b');
98 assertEquals("bc", GIT_PATH
.dequote(b
, p
, p
+ 2));
101 public void testDequote_LoneBackslash() {
102 assertDequote("\\", "\\");
105 public void testQuote_NamedEscapes() {
106 assertQuote("\\a", "\u0007");
107 assertQuote("\\b", "\b");
108 assertQuote("\\f", "\f");
109 assertQuote("\\n", "\n");
110 assertQuote("\\r", "\r");
111 assertQuote("\\t", "\t");
112 assertQuote("\\v", "\u000B");
113 assertQuote("\\\\", "\\");
114 assertQuote("\\\"", "\"");
117 public void testDequote_NamedEscapes() {
118 assertDequote("\u0007", "\\a");
119 assertDequote("\b", "\\b");
120 assertDequote("\f", "\\f");
121 assertDequote("\n", "\\n");
122 assertDequote("\r", "\\r");
123 assertDequote("\t", "\\t");
124 assertDequote("\u000B", "\\v");
125 assertDequote("\\", "\\\\");
126 assertDequote("\"", "\\\"");
129 public void testDequote_OctalAll() {
130 for (int i
= 0; i
< 127; i
++) {
131 assertDequote("" + (char) i
, octalEscape(i
));
133 for (int i
= 128; i
< 256; i
++) {
134 int f
= 0xC0 | (i
>> 6);
135 int s
= 0x80 | (i
& 0x3f);
136 assertDequote("" + (char) i
, octalEscape(f
)+octalEscape(s
));
140 private String
octalEscape(int i
) {
141 String s
= Integer
.toOctalString(i
);
142 while (s
.length() < 3) {
148 public void testQuote_OctalAll() {
149 assertQuote("\\001", "\1");
150 assertQuote("\\176", "~");
151 assertQuote("\\303\\277", "\u00ff"); // \u00ff in UTF-8
154 public void testDequote_UnknownEscapeQ() {
155 assertDequote("\\q", "\\q");
158 public void testDequote_FooTabBar() {
159 assertDequote("foo\tbar", "foo\\tbar");
162 public void testDequote_Latin1() {
163 assertDequote("\u00c5ngstr\u00f6m", "\\305ngstr\\366m"); // Latin1
166 public void testDequote_UTF8() {
167 assertDequote("\u00c5ngstr\u00f6m", "\\303\\205ngstr\\303\\266m");
170 public void testDequote_RawUTF8() {
171 assertDequote("\u00c5ngstr\u00f6m", "\303\205ngstr\303\266m");
174 public void testDequote_RawLatin1() {
175 assertDequote("\u00c5ngstr\u00f6m", "\305ngstr\366m");
178 public void testQuote_Ang() {
179 assertQuote("\\303\\205ngstr\\303\\266m", "\u00c5ngstr\u00f6m");