@@ -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\t b\t c" },
245+ {"a\\ =\\ b" , "a=b" },
246+ {"a\\ \\ b" , "a\\ b" },
247+ {"\\ t\\ f\\ z" , "\t \f z" },
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