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 |
} |