Repository: ant
Updated Branches:
  refs/heads/master fbfad85ae -> 1e30b48a0


Make CharSet backwards compatible

Project: http://git-wip-us.apache.org/repos/asf/ant/repo
Commit: http://git-wip-us.apache.org/repos/asf/ant/commit/ab13b876
Tree: http://git-wip-us.apache.org/repos/asf/ant/tree/ab13b876
Diff: http://git-wip-us.apache.org/repos/asf/ant/diff/ab13b876

Branch: refs/heads/master
Commit: ab13b876c6cca733449a5b2a4bf3386f3013ddd2
Parents: fbfad85
Author: Gintas Grigelionis <[email protected]>
Authored: Sat Nov 17 11:23:03 2018 +0100
Committer: Gintas Grigelionis <[email protected]>
Committed: Sat Nov 17 11:24:24 2018 +0100

----------------------------------------------------------------------
 .../org/apache/tools/ant/types/CharSet.java     | 47 ++++++++++++++++++++
 .../tools/ant/types/EnumeratedAttribute.java    |  2 +-
 .../org/apache/tools/ant/types/CharSetTest.java | 46 +++++++++++++++++--
 3 files changed, 91 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ant/blob/ab13b876/src/main/org/apache/tools/ant/types/CharSet.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/types/CharSet.java 
b/src/main/org/apache/tools/ant/types/CharSet.java
index ed41230..d55af40 100644
--- a/src/main/org/apache/tools/ant/types/CharSet.java
+++ b/src/main/org/apache/tools/ant/types/CharSet.java
@@ -18,7 +18,9 @@
 package org.apache.tools.ant.types;
 
 import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 
@@ -60,6 +62,30 @@ public class CharSet extends EnumeratedAttribute {
     }
 
     /**
+     * Convenience methood: get US-ASCII CharSet.
+     * @return the default value.
+     */
+    public static CharSet getAscii() {
+        return new CharSet(StandardCharsets.US_ASCII.name());
+    }
+
+    /**
+     * Convenience method: get UTF-8 CharSet.
+     * @return the default value.
+     */
+    public static CharSet getUtf8() {
+        return new CharSet(StandardCharsets.UTF_8.name());
+    }
+
+    /**
+     * Tell if CharSet values are aliases.
+     * @return true if CharSet values are aliases.
+     */
+    public boolean equivalent(CharSet cs) {
+        return getCharset().name().equals(cs.getCharset().name());
+    }
+
+    /**
      * Convert this enumerated type to a <code>Charset</code>.
      * @return a <code>Charset</code> object.
      */
@@ -75,4 +101,25 @@ public class CharSet extends EnumeratedAttribute {
     public String[] getValues() {
         return VALUES.toArray(new String[0]);
     }
+
+    /**
+     * Accept additional values for backwards compatibility
+     * (some java.io encoding names not available in java.nio)
+     * @param value the <code>String</code> value of the attribute
+     */
+    @Override
+    public final void setValue(final String value) {
+        String realValue = value;
+        if (value == null || value.isEmpty()) {
+           realValue = Charset.defaultCharset().name();
+       } else {
+           for (String v : Arrays.asList(value, value.toLowerCase(), 
value.toUpperCase())) {
+               if (VALUES.contains(v)) {
+                   realValue = v;
+                   break;
+               }
+           }
+        }
+        super.setValue(realValue);
+    }
 }

http://git-wip-us.apache.org/repos/asf/ant/blob/ab13b876/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java 
b/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java
index dac3966..80884c3 100644
--- a/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java
+++ b/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java
@@ -88,7 +88,7 @@ public abstract class EnumeratedAttribute {
      * @param value the <code>String</code> value of the attribute
      * @throws BuildException if the value is not valid for the attribute
      */
-    public final void setValue(String value) throws BuildException {
+    public void setValue(String value) throws BuildException {
         int idx = indexOfValue(value);
         if (idx == -1) {
             throw new BuildException(value + " is not a legal value for this 
attribute");

http://git-wip-us.apache.org/repos/asf/ant/blob/ab13b876/src/tests/junit/org/apache/tools/ant/types/CharSetTest.java
----------------------------------------------------------------------
diff --git a/src/tests/junit/org/apache/tools/ant/types/CharSetTest.java 
b/src/tests/junit/org/apache/tools/ant/types/CharSetTest.java
index 47d8021..16dda9f 100644
--- a/src/tests/junit/org/apache/tools/ant/types/CharSetTest.java
+++ b/src/tests/junit/org/apache/tools/ant/types/CharSetTest.java
@@ -27,7 +27,10 @@ import java.util.Arrays;
 import java.util.Collection;
 
 import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.equalToIgnoringCase;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
 
 @RunWith(Enclosed.class)
 public class CharSetTest {
@@ -37,7 +40,10 @@ public class CharSetTest {
         // requires JUnit 4.12
         @Parameterized.Parameters(name = "legal argument: |{0}|")
         public static Collection<String> data() {
-            return Arrays.asList("UTF-8", "ISO-8859-1", "037", "us", "IBM500");
+            return Arrays.asList("UTF-8", "ISO-8859-1", "037", "us", "IBM500",
+                    // some java.io encodings are not provided as aliases in 
java.nio.charset
+                    // so, for backwards compatibility, the case should not 
matter
+                    "ascii", "utf-8", "Cp1252");
         }
 
         @Parameterized.Parameter
@@ -46,7 +52,7 @@ public class CharSetTest {
         @Test
         public void testCorrectNames() {
             CharSet cs = new CharSet(argument);
-            assertThat(argument, equalTo(cs.getValue()));
+            assertThat(argument, equalToIgnoringCase(cs.getValue()));
         }
     }
 
@@ -63,7 +69,41 @@ public class CharSetTest {
 
         @Test(expected = BuildException.class)
         public void testNonExistentNames() {
-            new CharSet().setValue(argument);
+            new CharSet(argument);
+        }
+    }
+
+    @RunWith(Parameterized.class)
+    public static class LegalEquivalenceTest {
+        // requires JUnit 4.12
+        @Parameterized.Parameters(name = "equivalent argument: |{0}|")
+        public static Collection<String> data() {
+            return Arrays.asList("UTF8", "unicode-1-1-utf-8");
+        }
+
+        @Parameterized.Parameter
+        public String argument;
+
+        @Test
+        public void testCorrectNames() {
+            assertTrue(new CharSet(argument).equivalent(CharSet.getUtf8()));
+        }
+    }
+
+    @RunWith(Parameterized.class)
+    public static class IncorrectEquivalenceTest {
+        // requires JUnit 4.12
+        @Parameterized.Parameters(name = "non-equivalent argument: |{0}|")
+        public static Collection<String> data() {
+            return Arrays.asList("us", "ISO-8859-1", "default");
+        }
+
+        @Parameterized.Parameter
+        public String argument;
+
+        @Test
+        public void testIncorrectNames() {
+            assertFalse(new CharSet(argument).equivalent(CharSet.getUtf8()));
         }
     }
 

Reply via email to