Skip to content

Commit bccb704

Browse files
author
Yoshito Umaoka
committed
Added test cases for escape/unescape property key/value
Added test cases dedicated for escapePropKey, escapePropValue, unescapePropKey and unescapePropValue. Change leading tab encoding to use "\t", instead of "\u0009".
1 parent 2e6b6b6 commit bccb704

2 files changed

Lines changed: 97 additions & 2 deletions

File tree

gp-res-filter/src/main/java/com/ibm/g11n/pipeline/resfilter/JavaPropertiesResource.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,10 @@ private static String escape(String str, EscapeSpace escSpace) {
383383
char c = str.charAt(idx);
384384
if (c == ' ') {
385385
buf.append(BACKSLASH).append(' ');
386-
} else if (c == '\t' || c == '\f') {
387-
appendUnicodeEscape(buf, c);
386+
} else if (c == '\t') {
387+
buf.append(BACKSLASH).append('t');
388+
} else if (c == '\f') {
389+
buf.append(BACKSLASH).append('f');
388390
} else {
389391
break;
390392
}

gp-res-filter/src/test/java/com/ibm/g11n/pipeline/resfilter/JavaPropertiesResourceTest.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,97 @@ public void testPropDefParseLine() throws IOException {
177177

178178
assertEquals("PropDefs did not match.", EXPECTED_PROP_DEF_LIST, actualPropDefs);
179179
}
180+
181+
@Test
182+
public void testEscapePropsKey() {
183+
final String[][] testCases = {
184+
{"", ""},
185+
{"abc", "abc"},
186+
{"a b c", "a\\ b\\ c"},
187+
{" a b ", "\\ a\\ b\\ "},
188+
{" \t abc \t ", "\\ \\t\\ abc\\ \\t\\ "},
189+
{"\u0000\u0001", "\\u0000\\u0001"},
190+
{"a=b=c", "a\\=b\\=c"},
191+
{"a:b;c", "a\\:b;c"},
192+
{"!#$%()*+,-./", "\\!\\#$%()*+,-./"},
193+
{"' abc '", "'\\ abc\\ '"},
194+
{"a \"bc\"", "a\\ \"bc\""},
195+
{"\u3042\u3044", "\\u3042\\u3044"},
196+
};
197+
198+
for (String[] testCase : testCases) {
199+
String instr = testCase[0];
200+
String expected = testCase[1];
201+
202+
String escapedKey = JavaPropertiesResource.escapePropKey(instr);
203+
assertEquals("escapePropKey(" + instr + ")", expected, escapedKey);
204+
205+
String unescapedKey = JavaPropertiesResource.unescapePropKey(escapedKey);
206+
assertEquals("unescapePropKey(" + escapedKey + ")", instr, unescapedKey);
207+
}
208+
}
209+
210+
@Test
211+
public void testEscapePropsValue() {
212+
final String[][] testCases = {
213+
{"", ""},
214+
{"abc", "abc"},
215+
{"a b c", "a b c"},
216+
{" a b ", "\\ a b "},
217+
{" \t abc \t ", "\\ \\t\\ abc \\t "},
218+
{"\u0000\u0001", "\\u0000\\u0001"},
219+
{"a=b=c", "a\\=b\\=c"},
220+
{"a:b;c", "a\\:b;c"},
221+
{"!#$%()*+,-./", "\\!\\#$%()*+,-./"},
222+
{"' abc '", "' abc '"},
223+
{"a \"bc\"", "a \"bc\""},
224+
{"\u3042\u3044", "\\u3042\\u3044"},
225+
};
226+
227+
for (String[] testCase : testCases) {
228+
String instr = testCase[0];
229+
String expected = testCase[1];
230+
231+
String escapedVal = JavaPropertiesResource.escapePropValue(instr);
232+
assertEquals("escapePropValue(" + instr + ")", expected, escapedVal);
233+
234+
String unescapedVal = JavaPropertiesResource.unescapePropValue(escapedVal);
235+
assertEquals("unescapePropValue(" + escapedVal + ")", instr, unescapedVal);
236+
}
237+
}
238+
239+
private static final String[][] UNESC_TEST_CASES =
240+
{
241+
{"", ""},
242+
{"abc", "abc"},
243+
{"\\ abc\\u0020", " abc "},
244+
{"a\\tb\\u0009c", "a\tb\tc"},
245+
{"a\\=\\b", "a=b"},
246+
{"a\\\\b", "a\\b"},
247+
{"\\t\\f\\z", "\t\fz"},
248+
{"\\a\\b\\c", "abc"},
249+
{"\\u304A\\u304b", "\u304A\u304B"},
250+
};
251+
252+
@Test
253+
public void testUnescapePropsKey() {
254+
for (String[] testCase : UNESC_TEST_CASES) {
255+
String instr = testCase[0];
256+
String expected = testCase[1];
257+
258+
String unescapedKey = JavaPropertiesResource.unescapePropKey(instr);
259+
assertEquals("unescapePropKey(" + instr + ")", expected, unescapedKey);
260+
}
261+
}
262+
263+
@Test
264+
public void testUnescapePropsValue() {
265+
for (String[] testCase : UNESC_TEST_CASES) {
266+
String instr = testCase[0];
267+
String expected = testCase[1];
268+
269+
String unescapedVal = JavaPropertiesResource.unescapePropValue(instr);
270+
assertEquals("unescapePropValue(" + instr + ")", expected, unescapedVal);
271+
}
272+
}
180273
}

0 commit comments

Comments
 (0)