View | Details | Raw Unified | Return to bug 57394
Collapse All | Expand All

(-)src/jorphan/org/apache/jorphan/reflect/ClassTools.java (-3 / +7 lines)
Lines 55-61 Link Here
55
    /**
55
    /**
56
     * Call a class constructor with an integer parameter
56
     * Call a class constructor with an integer parameter
57
     * @param className name of the class to be constructed
57
     * @param className name of the class to be constructed
58
     * @param parameter (integer)
58
     * @param parameter the value to be used in the constructor
59
     * @return an instance of the class
59
     * @return an instance of the class
60
     * @throws JMeterException if class cannot be created
60
     * @throws JMeterException if class cannot be created
61
     */
61
     */
Lines 64-71 Link Here
64
        Object instance = null;
64
        Object instance = null;
65
        try {
65
        try {
66
            Class<?> clazz = ClassUtils.getClass(className);
66
            Class<?> clazz = ClassUtils.getClass(className);
67
            clazz.getConstructor(new Class [] {Integer.TYPE});
67
            Constructor<?> constructor = clazz.getConstructor(Integer.TYPE);
68
            instance = ClassUtils.getClass(className).newInstance();
68
            instance = constructor.newInstance(parameter);
69
        } catch (ClassNotFoundException e) {
69
        } catch (ClassNotFoundException e) {
70
            throw new JMeterException(e);
70
            throw new JMeterException(e);
71
        } catch (InstantiationException e) {
71
        } catch (InstantiationException e) {
Lines 76-81 Link Here
76
            throw new JMeterException(e);
76
            throw new JMeterException(e);
77
        } catch (NoSuchMethodException e) {
77
        } catch (NoSuchMethodException e) {
78
            throw new JMeterException(e);
78
            throw new JMeterException(e);
79
        } catch (IllegalArgumentException e) {
80
            throw new JMeterException(e);
81
        } catch (InvocationTargetException e) {
82
            throw new JMeterException(e);
79
        }
83
        }
80
        return instance;
84
        return instance;
81
    }
85
    }
(-)test/src/org/apache/jorphan/reflect/TestClassTools.java (+96 lines)
Line 0 Link Here
1
package org.apache.jorphan.reflect;
2
3
import junit.framework.TestCase;
4
5
import org.apache.jorphan.util.JMeterException;
6
import org.junit.Test;
7
8
/**
9
 * Test various aspects of the {@link ClassTools} class
10
 */
11
public class TestClassTools extends TestCase {
12
13
    /**
14
     * Test that a class can be constructed using the default constructor
15
     * 
16
     * @throws JMeterException
17
     *             when something fails during object construction
18
     */
19
    @Test
20
    public void testConstructString() throws JMeterException {
21
        String dummy = (String) ClassTools.construct("java.lang.String");
22
        assertNotNull(dummy);
23
        assertEquals("", dummy);
24
    }
25
26
    /**
27
     * Test that a class can be constructed using an constructor with an integer
28
     * parameter
29
     * 
30
     * @throws JMeterException
31
     *             when something fails during object construction
32
     */
33
    @Test
34
    public void testConstructStringInt() throws JMeterException {
35
        Integer dummy = (Integer) ClassTools.construct("java.lang.Integer", 23);
36
        assertNotNull(dummy);
37
        assertEquals(Integer.valueOf(23), dummy);
38
    }
39
40
    /**
41
     * Test that a class can be constructed using an constructor with an string
42
     * parameter
43
     * 
44
     * @throws JMeterException
45
     *             when something fails during object construction
46
     */
47
    @Test
48
    public void testConstructStringString() throws JMeterException {
49
        String dummy = (String) ClassTools.construct("java.lang.String",
50
                "hello");
51
        assertNotNull(dummy);
52
        assertEquals("hello", dummy);
53
    }
54
55
    /**
56
     * Test that a simple method can be invoked on an object
57
     * 
58
     * @throws SecurityException
59
     *             when the method can not be used because of security concerns
60
     * @throws IllegalArgumentException
61
     *             when the method parameters does not match the given ones
62
     * @throws JMeterException
63
     *             when something fails while invoking the method
64
     */
65
    @Test
66
    public void testInvoke() throws SecurityException,
67
            IllegalArgumentException, JMeterException {
68
        Dummy dummy = new Dummy();
69
        ClassTools.invoke(dummy, "callMe");
70
        assertEquals(dummy.wasCalled(), true);
71
    }
72
73
    /**
74
     * Dummy class to be used for construction and invocation tests
75
     *
76
     */
77
    public static class Dummy {
78
        private boolean called = false;
79
80
        /**
81
         * @return <code>true</code> if {@link Dummy#callMe()} was called on
82
         *         this instance
83
         */
84
        public boolean wasCalled() {
85
            return this.called;
86
        }
87
88
        /**
89
         * Simple method to be called on void invocation
90
         */
91
        public void callMe() {
92
            this.called = true;
93
        }
94
    }
95
96
}
(-)xdocs/changes.xml (+1 lines)
Lines 172-177 Link Here
172
<ul>
172
<ul>
173
<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>
173
<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>
174
<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>
174
<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>
175
<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>
175
</ul>
176
</ul>
176
177
177
<!-- =================== Improvements =================== -->
178
<!-- =================== Improvements =================== -->

Return to bug 57394