Fixed escaping bug with keys containing metachars like colon.
authorJesse Glick <jglick@netbeans.org>
Fri, 07 Aug 2009 16:56:20 -0400
changeset 815fae825a66188
parent 814 0e8ba7f688ab
child 816 a608b762794e
child 933 90b133c9fb62
Fixed escaping bug with keys containing metachars like colon.
Affected code like SimpleFileOwnerQueryImplementation which stored URLs as keys in NbPreferences.
openide.util/src/org/openide/util/EditableProperties.java
openide.util/test/unit/src/org/openide/util/EditablePropertiesTest.java
     1.1 --- a/openide.util/src/org/openide/util/EditableProperties.java	Fri Aug 07 16:23:26 2009 -0400
     1.2 +++ b/openide.util/src/org/openide/util/EditableProperties.java	Fri Aug 07 16:56:20 2009 -0400
     1.3 @@ -658,7 +658,7 @@
     1.4              return output.toString();
     1.5          }
     1.6  
     1.7 -        private static String encode(String input, boolean escapeSpace) {
     1.8 +        private static String encode(String input, boolean forKey) {
     1.9              int len = input.length();
    1.10              StringBuilder output = new StringBuilder(len*2);
    1.11  
    1.12 @@ -666,10 +666,11 @@
    1.13                  char ch = input.charAt(x);
    1.14                  switch(ch) {
    1.15                      case ' ':
    1.16 -                        if (x == 0 || escapeSpace)  {
    1.17 +                    case '#':
    1.18 +                        if (x == 0 || forKey)  {
    1.19                              output.append('\\');
    1.20                          }
    1.21 -                        output.append(' ');
    1.22 +                        output.append(ch);
    1.23                          break;
    1.24                      case '\\':
    1.25                          output.append("\\\\");
    1.26 @@ -686,6 +687,13 @@
    1.27                      case '\f':
    1.28                          output.append("\\f");
    1.29                          break;
    1.30 +                    case '=':
    1.31 +                    case ':':
    1.32 +                        if (forKey) {
    1.33 +                            output.append('\\');
    1.34 +                        }
    1.35 +                        output.append(ch);
    1.36 +                        break;
    1.37                      default:
    1.38                          if ((ch < 0x0020) || (ch > 0x007e)) {
    1.39                              output.append("\\u");
     2.1 --- a/openide.util/test/unit/src/org/openide/util/EditablePropertiesTest.java	Fri Aug 07 16:23:26 2009 -0400
     2.2 +++ b/openide.util/test/unit/src/org/openide/util/EditablePropertiesTest.java	Fri Aug 07 16:56:20 2009 -0400
     2.3 @@ -227,12 +227,12 @@
     2.4          String output = getAsString(ep);
     2.5          String expected = "a\\ a=a space a"+System.getProperty("line.separator")+
     2.6                  "b\\u4567=val\\u1234"+System.getProperty("line.separator")+
     2.7 -                "@!#$%^\\\\=!@#$%^&*(){}\\\\"+System.getProperty("line.separator")+
     2.8 +                "@!\\#$%^\\\\=!@#$%^&*(){}\\\\"+System.getProperty("line.separator")+
     2.9                  "d\\nd=d\\nnewline\\nd"+System.getProperty("line.separator")+
    2.10                  "umlaut=\\u00fc"+System.getProperty("line.separator")+
    2.11                  "_a\\ a=\\"+System.getProperty("line.separator")+"    a space a"+System.getProperty("line.separator")+
    2.12                  "_b\\u4567=\\"+System.getProperty("line.separator")+"    val\\u1234"+System.getProperty("line.separator")+
    2.13 -                "_@!#$%^\\\\=\\"+System.getProperty("line.separator")+"    !@#$%^&*\\\\\\"+System.getProperty("line.separator")+
    2.14 +                "_@!\\#$%^\\\\=\\"+System.getProperty("line.separator")+"    !@#$%^&*\\\\\\"+System.getProperty("line.separator")+
    2.15                      "    (){}\\\\"+System.getProperty("line.separator")+
    2.16                  "_d\\nd=\\"+System.getProperty("line.separator")+"    d\\nnew\\"+System.getProperty("line.separator")+
    2.17                      "    line\\nd\\"+System.getProperty("line.separator")+
    2.18 @@ -252,6 +252,22 @@
    2.19          assertEquals("d\nnewline\nd\nend", ep.getProperty("_d\nd"));
    2.20          assertEquals(umlaut+umlaut, ep.getProperty("_umlaut"));
    2.21      }
    2.22 +
    2.23 +    public void testMetaCharacters() throws Exception {
    2.24 +        testRoundTrip("foo=bar", "v1");
    2.25 +        testRoundTrip("foo:bar", "v2");
    2.26 +        testRoundTrip("#foobar", "v3");
    2.27 +        testRoundTrip(" foo bar ", "v4");
    2.28 +    }
    2.29 +    private void testRoundTrip(String key, String value) throws Exception {
    2.30 +        EditableProperties ep = new EditableProperties(false);
    2.31 +        ep.setProperty(key, value);
    2.32 +        ByteArrayOutputStream baos = new ByteArrayOutputStream();
    2.33 +        ep.store(baos);
    2.34 +        ep = new EditableProperties(false);
    2.35 +        ep.load(new ByteArrayInputStream(baos.toByteArray()));
    2.36 +        assertEquals(baos.toString(), Collections.singletonMap(key, value), ep);
    2.37 +    }
    2.38      
    2.39      // test that iterator implementation is OK
    2.40      public void testIterator() throws Exception {