Browse Source

Make CharSet backwards compatible

master
Gintas Grigelionis 6 years ago
parent
commit
ab13b876c6
3 changed files with 91 additions and 4 deletions
  1. +47
    -0
      src/main/org/apache/tools/ant/types/CharSet.java
  2. +1
    -1
      src/main/org/apache/tools/ant/types/EnumeratedAttribute.java
  3. +43
    -3
      src/tests/junit/org/apache/tools/ant/types/CharSetTest.java

+ 47
- 0
src/main/org/apache/tools/ant/types/CharSet.java View File

@@ -18,7 +18,9 @@
package org.apache.tools.ant.types; package org.apache.tools.ant.types;


import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;


@@ -59,6 +61,30 @@ public class CharSet extends EnumeratedAttribute {
return new CharSet(Charset.defaultCharset().name()); return new CharSet(Charset.defaultCharset().name());
} }


/**
* 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>. * Convert this enumerated type to a <code>Charset</code>.
* @return a <code>Charset</code> object. * @return a <code>Charset</code> object.
@@ -75,4 +101,25 @@ public class CharSet extends EnumeratedAttribute {
public String[] getValues() { public String[] getValues() {
return VALUES.toArray(new String[0]); 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);
}
} }

+ 1
- 1
src/main/org/apache/tools/ant/types/EnumeratedAttribute.java View File

@@ -88,7 +88,7 @@ public abstract class EnumeratedAttribute {
* @param value the <code>String</code> value of the attribute * @param value the <code>String</code> value of the attribute
* @throws BuildException if the value is not valid for 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); int idx = indexOfValue(value);
if (idx == -1) { if (idx == -1) {
throw new BuildException(value + " is not a legal value for this attribute"); throw new BuildException(value + " is not a legal value for this attribute");


+ 43
- 3
src/tests/junit/org/apache/tools/ant/types/CharSetTest.java View File

@@ -27,7 +27,10 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;


import static org.hamcrest.Matchers.equalTo; 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.assertThat;
import static org.junit.Assert.assertTrue;


@RunWith(Enclosed.class) @RunWith(Enclosed.class)
public class CharSetTest { public class CharSetTest {
@@ -37,7 +40,10 @@ public class CharSetTest {
// requires JUnit 4.12 // requires JUnit 4.12
@Parameterized.Parameters(name = "legal argument: |{0}|") @Parameterized.Parameters(name = "legal argument: |{0}|")
public static Collection<String> data() { 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 @Parameterized.Parameter
@@ -46,7 +52,7 @@ public class CharSetTest {
@Test @Test
public void testCorrectNames() { public void testCorrectNames() {
CharSet cs = new CharSet(argument); 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) @Test(expected = BuildException.class)
public void testNonExistentNames() { 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()));
} }
} }




Loading…
Cancel
Save