diff --git a/WHATSNEW b/WHATSNEW index 7fd0cb44b..de54711f9 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 ===================================== 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 000000000..ed4123019 --- /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 VALUES = new ArrayList<>(); + + static { + for (Map.Entry 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 Charset. + * @return a Charset 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]); + } +} 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 000000000..47d8021f2 --- /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 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 data() { + return Arrays.asList("mojibake", "dummy"); + } + + @Parameterized.Parameter + public String argument; + + @Test(expected = BuildException.class) + public void testNonExistentNames() { + new CharSet().setValue(argument); + } + } + +}