You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by gi...@apache.org on 2018/11/14 21:43:52 UTC

ant git commit: A new CharSet type to hold available Charset names

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


A new CharSet type to hold available Charset names

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

Branch: refs/heads/master
Commit: fbfad85aec148d68501c5b68235902f01d8c1836
Parents: e208ad9
Author: Gintas Grigelionis <gi...@apache.org>
Authored: Wed Nov 14 22:43:37 2018 +0100
Committer: Gintas Grigelionis <gi...@apache.org>
Committed: Wed Nov 14 22:43:37 2018 +0100

----------------------------------------------------------------------
 WHATSNEW                                        |  6 +-
 .../org/apache/tools/ant/types/CharSet.java     | 78 ++++++++++++++++++++
 .../org/apache/tools/ant/types/CharSetTest.java | 70 ++++++++++++++++++
 3 files changed, 153 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ant/blob/fbfad85a/WHATSNEW
----------------------------------------------------------------------
diff --git a/WHATSNEW b/WHATSNEW
index 7fd0cb4..de54711 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -57,10 +57,14 @@ Other changes:
    Bugzilla Report 62424
 
  * properties used or set by BuildFileTask/BuildFileRule are documented
-   in MagicNames. A new magic property, ant.test.basedir.ignore, is
+   in MagicTestNames. A new magic property, ant.test.basedir.ignore, is
    introduced for cases where Ant projects set up for test purposes
    must ignore basedir set externally by test harness.
 
+ * a new CharSet type is provided for encoding or charset attributes in
+   tasks that must deal with different character encodings in files,
+   file names and other string resources.
+
 Changes from Ant 1.10.4 TO Ant 1.10.5
 =====================================
 

http://git-wip-us.apache.org/repos/asf/ant/blob/fbfad85a/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
new file mode 100644
index 0000000..ed41230
--- /dev/null
+++ b/src/main/org/apache/tools/ant/types/CharSet.java
@@ -0,0 +1,78 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.tools.ant.types;
+
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * EnumeratedAttribute implementation for Charset to use with encoding/charset attributes.
+ * @since Ant 1.10.6
+ */
+public class CharSet extends EnumeratedAttribute {
+
+    private static final List<String> VALUES = new ArrayList<>();
+
+    static {
+        for (Map.Entry<String, Charset> entry :  Charset.availableCharsets().entrySet()) {
+            VALUES.add(entry.getKey());
+            VALUES.addAll(entry.getValue().aliases());
+        }
+    }
+
+    /**
+     * Default constructor.
+     */
+    public CharSet() {
+    }
+
+    /**
+     * Construct a new CharSet with the specified value.
+     * @param value the EnumeratedAttribute value.
+     */
+    public CharSet(String value) {
+        setValue(value);
+    }
+
+    /**
+     * Get the default value as provided by Charset.
+     * @return the default value.
+     */
+    public static CharSet getDefault() {
+        return new CharSet(Charset.defaultCharset().name());
+    }
+
+    /**
+     * Convert this enumerated type to a <code>Charset</code>.
+     * @return a <code>Charset</code> object.
+     */
+    public Charset getCharset() {
+        return Charset.forName(getValue());
+    }
+
+    /**
+     * Return the possible values.
+     * @return String[] of Charset names.
+     */
+    @Override
+    public String[] getValues() {
+        return VALUES.toArray(new String[0]);
+    }
+}

http://git-wip-us.apache.org/repos/asf/ant/blob/fbfad85a/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
new file mode 100644
index 0000000..47d8021
--- /dev/null
+++ b/src/tests/junit/org/apache/tools/ant/types/CharSetTest.java
@@ -0,0 +1,70 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.tools.ant.types;
+
+import org.apache.tools.ant.BuildException;
+import org.junit.Test;
+import org.junit.experimental.runners.Enclosed;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+@RunWith(Enclosed.class)
+public class CharSetTest {
+
+    @RunWith(Parameterized.class)
+    public static class LegalArgumentTest {
+        // 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");
+        }
+
+        @Parameterized.Parameter
+        public String argument;
+
+        @Test
+        public void testCorrectNames() {
+            CharSet cs = new CharSet(argument);
+            assertThat(argument, equalTo(cs.getValue()));
+        }
+    }
+
+    @RunWith(Parameterized.class)
+    public static class IllegalArgumentTest {
+        // requires JUnit 4.12
+        @Parameterized.Parameters(name = "illegal argument: |{0}|")
+        public static Collection<String> data() {
+            return Arrays.asList("mojibake", "dummy");
+        }
+
+        @Parameterized.Parameter
+        public String argument;
+
+        @Test(expected = BuildException.class)
+        public void testNonExistentNames() {
+            new CharSet().setValue(argument);
+        }
+    }
+
+}