Index: src/jorphan/org/apache/jorphan/reflect/ClassTools.java =================================================================== --- src/jorphan/org/apache/jorphan/reflect/ClassTools.java (Revision 1647604) +++ src/jorphan/org/apache/jorphan/reflect/ClassTools.java (Arbeitskopie) @@ -55,7 +55,7 @@ /** * Call a class constructor with an integer parameter * @param className name of the class to be constructed - * @param parameter (integer) + * @param parameter the value to be used in the constructor * @return an instance of the class * @throws JMeterException if class cannot be created */ @@ -64,8 +64,8 @@ Object instance = null; try { Class clazz = ClassUtils.getClass(className); - clazz.getConstructor(new Class [] {Integer.TYPE}); - instance = ClassUtils.getClass(className).newInstance(); + Constructor constructor = clazz.getConstructor(Integer.TYPE); + instance = constructor.newInstance(parameter); } catch (ClassNotFoundException e) { throw new JMeterException(e); } catch (InstantiationException e) { @@ -76,6 +76,10 @@ throw new JMeterException(e); } catch (NoSuchMethodException e) { throw new JMeterException(e); + } catch (IllegalArgumentException e) { + throw new JMeterException(e); + } catch (InvocationTargetException e) { + throw new JMeterException(e); } return instance; } Index: test/src/org/apache/jorphan/reflect/TestClassTools.java =================================================================== --- test/src/org/apache/jorphan/reflect/TestClassTools.java (Revision 0) +++ test/src/org/apache/jorphan/reflect/TestClassTools.java (Arbeitskopie) @@ -0,0 +1,96 @@ +package org.apache.jorphan.reflect; + +import junit.framework.TestCase; + +import org.apache.jorphan.util.JMeterException; +import org.junit.Test; + +/** + * Test various aspects of the {@link ClassTools} class + */ +public class TestClassTools extends TestCase { + + /** + * Test that a class can be constructed using the default constructor + * + * @throws JMeterException + * when something fails during object construction + */ + @Test + public void testConstructString() throws JMeterException { + String dummy = (String) ClassTools.construct("java.lang.String"); + assertNotNull(dummy); + assertEquals("", dummy); + } + + /** + * Test that a class can be constructed using an constructor with an integer + * parameter + * + * @throws JMeterException + * when something fails during object construction + */ + @Test + public void testConstructStringInt() throws JMeterException { + Integer dummy = (Integer) ClassTools.construct("java.lang.Integer", 23); + assertNotNull(dummy); + assertEquals(Integer.valueOf(23), dummy); + } + + /** + * Test that a class can be constructed using an constructor with an string + * parameter + * + * @throws JMeterException + * when something fails during object construction + */ + @Test + public void testConstructStringString() throws JMeterException { + String dummy = (String) ClassTools.construct("java.lang.String", + "hello"); + assertNotNull(dummy); + assertEquals("hello", dummy); + } + + /** + * Test that a simple method can be invoked on an object + * + * @throws SecurityException + * when the method can not be used because of security concerns + * @throws IllegalArgumentException + * when the method parameters does not match the given ones + * @throws JMeterException + * when something fails while invoking the method + */ + @Test + public void testInvoke() throws SecurityException, + IllegalArgumentException, JMeterException { + Dummy dummy = new Dummy(); + ClassTools.invoke(dummy, "callMe"); + assertEquals(dummy.wasCalled(), true); + } + + /** + * Dummy class to be used for construction and invocation tests + * + */ + public static class Dummy { + private boolean called = false; + + /** + * @return true if {@link Dummy#callMe()} was called on + * this instance + */ + public boolean wasCalled() { + return this.called; + } + + /** + * Simple method to be called on void invocation + */ + public void callMe() { + this.called = true; + } + } + +} Index: xdocs/changes.xml =================================================================== --- xdocs/changes.xml (Revision 1647604) +++ xdocs/changes.xml (Arbeitskopie) @@ -172,6 +172,7 @@