1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.supercsv.util;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertTrue;
20
21 import java.lang.reflect.Method;
22
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.supercsv.mock.ReflectionBean;
27
28
29
30
31
32
33 public class MethodCacheTest {
34
35 private MethodCache cache;
36
37
38
39
40 @Before
41 public void setUp() {
42 cache = new MethodCache();
43 }
44
45
46
47
48 @After
49 public void tearDown() {
50 cache = null;
51 }
52
53
54
55
56 @Test
57 public void testGetGetMethod() throws Exception {
58 ReflectionBean bean = new ReflectionBean();
59
60
61 final long start = System.currentTimeMillis();
62 final Method uncachedGetter = cache.getGetMethod(bean, "name");
63 final long uncachedTime = System.currentTimeMillis() - start;
64
65
66 final long start2 = System.currentTimeMillis();
67 final Method cachedGetter = cache.getGetMethod(bean, "name");
68 final long cachedTime = System.currentTimeMillis() - start2;
69
70
71 System.out.println(String.format("getGetMethod uncachedTime: %d, cachedTime: %d", uncachedTime, cachedTime));
72 assertTrue(cachedTime <= uncachedTime);
73
74
75 assertEquals(uncachedGetter, cachedGetter);
76
77
78 final String name1 = "uncached";
79 bean.setName(name1);
80 assertEquals(name1, uncachedGetter.invoke(bean));
81
82
83 final String name2 = "cached";
84 bean.setName(name2);
85 assertEquals(name2, cachedGetter.invoke(bean));
86 }
87
88
89
90
91 @Test
92 public void testGetSetMethod() throws Exception {
93 ReflectionBean bean = new ReflectionBean();
94
95
96 final long start = System.currentTimeMillis();
97 final Method uncachedSetter = cache.getSetMethod(bean, "name", String.class);
98 final long uncachedTime = System.currentTimeMillis() - start;
99
100
101 final long start2 = System.currentTimeMillis();
102 final Method cachedSetter = cache.getSetMethod(bean, "name", String.class);
103 final long cachedTime = System.currentTimeMillis() - start2;
104
105
106 System.out.println(String.format("getSetMethod uncachedTime: %d, cachedTime: %d", uncachedTime, cachedTime));
107 assertTrue(cachedTime <= uncachedTime);
108
109
110 assertEquals(uncachedSetter, cachedSetter);
111
112
113 final String name1 = "uncached";
114 uncachedSetter.invoke(bean, name1);
115 assertEquals(name1, bean.getName());
116
117
118 final String name2 = "cached";
119 cachedSetter.invoke(bean, name2);
120 assertEquals(name2, bean.getName());
121 }
122
123
124
125
126 @Test(expected = NullPointerException.class)
127 public void testGetGetMethodWithNullObject() {
128 cache.getGetMethod(null, "name");
129 }
130
131
132
133
134 @Test(expected = NullPointerException.class)
135 public void testGetGetMethodWithNullFieldName() {
136 cache.getGetMethod(new ReflectionBean(), null);
137 }
138
139
140
141
142 @Test(expected = NullPointerException.class)
143 public void testGetSetMethodWithNullObject() {
144 cache.getSetMethod(null, "name", String.class);
145 }
146
147
148
149
150 @Test(expected = NullPointerException.class)
151 public void testGetSetMethodWithNullFieldName() {
152 cache.getSetMethod(new ReflectionBean(), null, String.class);
153 }
154
155
156
157
158 @Test(expected = NullPointerException.class)
159 public void testGetSetMethodWithNullArgumentType() {
160 cache.getSetMethod(new ReflectionBean(), "name", null);
161 }
162
163 }