Author: jhm Date: Fri Oct 13 02:04:31 2006 New Revision: 463601 URL: http://svn.apache.org/viewvc?view=rev&rev=463601 Log: Factory method for easier instantiation. Bug 14831
Added: ant/core/trunk/src/tests/junit/org/apache/tools/ant/types/EnumeratedAttributeTest_Factory.java Modified: ant/core/trunk/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java ant/core/trunk/src/tests/junit/org/apache/tools/ant/types/EnumeratedAttributeTest.java Modified: ant/core/trunk/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java?view=diff&rev=463601&r1=463600&r2=463601 ============================================================================== --- ant/core/trunk/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java (original) +++ ant/core/trunk/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java Fri Oct 13 02:04:31 2006 @@ -54,6 +54,32 @@ } /** + * 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 [EMAIL PROTECTED] org.apache.tools.ant.IntrospectionHelper IntrospectionHelper}. * @param value the <code>String</code> value of the attribute * @throws BuildException if the value is not valid for the attribute @@ -111,7 +137,6 @@ return index; } - /** * Convert the value to its string form. * @@ -121,4 +146,4 @@ return getValue(); } -} +} \ No newline at end of file Modified: ant/core/trunk/src/tests/junit/org/apache/tools/ant/types/EnumeratedAttributeTest.java URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/junit/org/apache/tools/ant/types/EnumeratedAttributeTest.java?view=diff&rev=463601&r1=463600&r2=463601 ============================================================================== --- ant/core/trunk/src/tests/junit/org/apache/tools/ant/types/EnumeratedAttributeTest.java (original) +++ ant/core/trunk/src/tests/junit/org/apache/tools/ant/types/EnumeratedAttributeTest.java Fri Oct 13 02:04:31 2006 @@ -24,9 +24,7 @@ /** * JUnit 3 testcases for org.apache.tools.ant.EnumeratedAttribute. - * */ - public class EnumeratedAttributeTest extends TestCase { private static String[] expected = {"a", "b", "c"}; @@ -49,7 +47,22 @@ !(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(); for (int i=0; i<expected.length; i++) { try { @@ -83,4 +96,5 @@ return null; } } + } Added: ant/core/trunk/src/tests/junit/org/apache/tools/ant/types/EnumeratedAttributeTest_Factory.java URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/junit/org/apache/tools/ant/types/EnumeratedAttributeTest_Factory.java?view=auto&rev=463601 ============================================================================== --- ant/core/trunk/src/tests/junit/org/apache/tools/ant/types/EnumeratedAttributeTest_Factory.java (added) +++ ant/core/trunk/src/tests/junit/org/apache/tools/ant/types/EnumeratedAttributeTest_Factory.java Fri Oct 13 02:04:31 2006 @@ -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" }; + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]