Browse Source

A new CharSet type to hold available Charset names

master
Gintas Grigelionis 6 years ago
parent
commit
fbfad85aec
3 changed files with 153 additions and 1 deletions
  1. +5
    -1
      WHATSNEW
  2. +78
    -0
      src/main/org/apache/tools/ant/types/CharSet.java
  3. +70
    -0
      src/tests/junit/org/apache/tools/ant/types/CharSetTest.java

+ 5
- 1
WHATSNEW View File

@@ -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
=====================================



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

@@ -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]);
}
}

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

@@ -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);
}
}

}

Loading…
Cancel
Save