jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fschumac...@apache.org
Subject svn commit: r1647607 - in /jmeter/trunk: src/jorphan/org/apache/jorphan/reflect/ClassTools.java test/src/org/apache/jorphan/reflect/TestClassTools.java xdocs/changes.xml
Date Tue, 23 Dec 2014 16:23:16 GMT
Author: fschumacher
Date: Tue Dec 23 16:23:16 2014
New Revision: 1647607

URL: http://svn.apache.org/r1647607
Log:
Bug 57394: When constructing an instance using ClassTools#construct(String, int),
the int parameter was not used, but the default constructor was used instead.

Added a few test cases to test the methods of ClassTools and correct the unused parameter.

The only place in jmeter, where this method was called is in the JMeter class itself and
is used, when the property "mirror.server.port" is set to a value greater than zero. 
Without this patch that value would be ignored.

Bugzilla Id: 57394


Added:
    jmeter/trunk/test/src/org/apache/jorphan/reflect/TestClassTools.java
Modified:
    jmeter/trunk/src/jorphan/org/apache/jorphan/reflect/ClassTools.java
    jmeter/trunk/xdocs/changes.xml

Modified: jmeter/trunk/src/jorphan/org/apache/jorphan/reflect/ClassTools.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/jorphan/org/apache/jorphan/reflect/ClassTools.java?rev=1647607&r1=1647606&r2=1647607&view=diff
==============================================================================
--- jmeter/trunk/src/jorphan/org/apache/jorphan/reflect/ClassTools.java (original)
+++ jmeter/trunk/src/jorphan/org/apache/jorphan/reflect/ClassTools.java Tue Dec 23 16:23:16
2014
@@ -55,7 +55,7 @@ public class ClassTools {
     /**
      * 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 @@ public class ClassTools {
         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 @@ public class ClassTools {
             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;
     }

Added: jmeter/trunk/test/src/org/apache/jorphan/reflect/TestClassTools.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jorphan/reflect/TestClassTools.java?rev=1647607&view=auto
==============================================================================
--- jmeter/trunk/test/src/org/apache/jorphan/reflect/TestClassTools.java (added)
+++ jmeter/trunk/test/src/org/apache/jorphan/reflect/TestClassTools.java Tue Dec 23 16:23:16
2014
@@ -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 <code>true</code> 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;
+        }
+    }
+
+}

Modified: jmeter/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jmeter/trunk/xdocs/changes.xml?rev=1647607&r1=1647606&r2=1647607&view=diff
==============================================================================
--- jmeter/trunk/xdocs/changes.xml (original)
+++ jmeter/trunk/xdocs/changes.xml Tue Dec 23 16:23:16 2014
@@ -172,6 +172,7 @@ See  <bugzilla>56357</bugzilla> for deta
 <ul>
 <li><bug>57365</bug>Selected LAF is not correctly setup due to call of
UIManager.setLookAndFeel too late. Contributed by Ubik Load Pack (support at ubikloadpack.com)</li>
 <li><bug>57364</bug>Options &lt; Look And Feel does not update all
windows LAF. Contributed by Ubik Load Pack (support at ubikloadpack.com)</li>
+<li><bug>57394</bug>When constructing an instance with ClassTools#construct(String,
int) the integer was ignored and the default constructor was used instead.</li>
 </ul>
 
 <!-- =================== Improvements =================== -->



Mime
View raw message