Browse Source

Factory method for easier instantiation.

Bug 14831

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@463601 13f79535-47bb-0310-9956-ffa450edef68
master
Jan Materne 18 years ago
parent
commit
94f6a31817
3 changed files with 51 additions and 5 deletions
  1. +27
    -2
      src/main/org/apache/tools/ant/types/EnumeratedAttribute.java
  2. +17
    -3
      src/tests/junit/org/apache/tools/ant/types/EnumeratedAttributeTest.java
  3. +7
    -0
      src/tests/junit/org/apache/tools/ant/types/EnumeratedAttributeTest_Factory.java

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

@@ -54,6 +54,32 @@ public abstract class EnumeratedAttribute {
} }


/** /**
* Factory method for instantiating EAs via API in a more
* developer friendly way.
* @param clazz Class, extending EA, which to instantiate
* @param value The value to set on that EA
* @return Configured EA
* @throws BuildException If the class could not be found or the value
* is not valid for the given EA-class.
* @see Bug-14831
*/
public static EnumeratedAttribute getInstance(
Class/*<? extends EnumeratedAttribute>*/ clazz,
String value) throws BuildException {
if (!EnumeratedAttribute.class.isAssignableFrom(clazz)) {
throw new BuildException("You have to provide a subclass from EnumeratedAttribut as clazz-parameter.");
}
EnumeratedAttribute ea = null;
try {
ea = (EnumeratedAttribute)clazz.newInstance();
} catch (Exception e) {
throw new BuildException(e);
}
ea.setValue(value);
return ea;
}

/**
* Invoked by {@link org.apache.tools.ant.IntrospectionHelper IntrospectionHelper}. * Invoked by {@link org.apache.tools.ant.IntrospectionHelper IntrospectionHelper}.
* @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
@@ -111,7 +137,6 @@ public abstract class EnumeratedAttribute {
return index; return index;
} }



/** /**
* Convert the value to its string form. * Convert the value to its string form.
* *
@@ -121,4 +146,4 @@ public abstract class EnumeratedAttribute {
return getValue(); return getValue();
} }


}
}

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

@@ -24,9 +24,7 @@ import org.apache.tools.ant.BuildException;


/** /**
* JUnit 3 testcases for org.apache.tools.ant.EnumeratedAttribute. * JUnit 3 testcases for org.apache.tools.ant.EnumeratedAttribute.
*
*/ */

public class EnumeratedAttributeTest extends TestCase { public class EnumeratedAttributeTest extends TestCase {


private static String[] expected = {"a", "b", "c"}; private static String[] expected = {"a", "b", "c"};
@@ -49,7 +47,22 @@ public class EnumeratedAttributeTest extends TestCase {
!(new TestNull()).containsValue("d")); !(new TestNull()).containsValue("d"));
} }


public void testExceptions() {
public void testFactory() {
EnumeratedAttributeTest_Factory ea1 = (EnumeratedAttributeTest_Factory)EnumeratedAttribute.getInstance(
EnumeratedAttributeTest_Factory.class,
"one");
assertEquals("Factory didnt set the right value.", ea1.getValue(), "one");
try {
EnumeratedAttributeTest_Factory ea2 = (EnumeratedAttributeTest_Factory)EnumeratedAttribute.getInstance(
EnumeratedAttributeTest_Factory.class,
"illegal");
fail("Factory should fail when trying to set an illegal value.");
} catch (BuildException be) {
// was expected
}
}

public void testExceptions() {
EnumeratedAttribute t1 = new TestNormal(); EnumeratedAttribute t1 = new TestNormal();
for (int i=0; i<expected.length; i++) { for (int i=0; i<expected.length; i++) {
try { try {
@@ -83,4 +96,5 @@ public class EnumeratedAttributeTest extends TestCase {
return null; return null;
} }
} }
} }

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

@@ -0,0 +1,7 @@
package org.apache.tools.ant.types;
public class EnumeratedAttributeTest_Factory extends EnumeratedAttribute {
public String[] getValues() {
return new String[] { "one", "two", "three" };
}
}

Loading…
Cancel
Save