View Javadoc
1   /*
2    * Copyright 2007 Kasper B. Graversen
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Tests the MethodCache class.
30   * 
31   * @author James Bassett
32   */
33  public class MethodCacheTest {
34  	
35  	private MethodCache cache;
36  	
37  	/**
38  	 * Sets up before the test.
39  	 */
40  	@Before
41  	public void setUp() {
42  		cache = new MethodCache();
43  	}
44  	
45  	/**
46  	 * Tidies up after the test.
47  	 */
48  	@After
49  	public void tearDown() {
50  		cache = null;
51  	}
52  	
53  	/**
54  	 * Tests getGetMethod().
55  	 */
56  	@Test
57  	public void testGetGetMethod() throws Exception {
58  		ReflectionBean bean = new ReflectionBean();
59  		
60  		// first time - not cached
61  		final long start = System.currentTimeMillis();
62  		final Method uncachedGetter = cache.getGetMethod(bean, "name");
63  		final long uncachedTime = System.currentTimeMillis() - start;
64  		
65  		// second time - cached
66  		final long start2 = System.currentTimeMillis();
67  		final Method cachedGetter = cache.getGetMethod(bean, "name");
68  		final long cachedTime = System.currentTimeMillis() - start2;
69  		
70  		// retrieval from cache should be at least as fast as reflection
71  		System.out.println(String.format("getGetMethod uncachedTime: %d, cachedTime: %d", uncachedTime, cachedTime));
72  		assertTrue(cachedTime <= uncachedTime);
73  		
74  		// the same getter method should be returned in both calls
75  		assertEquals(uncachedGetter, cachedGetter);
76  		
77  		// test the uncached getter works
78  		final String name1 = "uncached";
79  		bean.setName(name1);
80  		assertEquals(name1, uncachedGetter.invoke(bean));
81  		
82  		// test the cached getter works
83  		final String name2 = "cached";
84  		bean.setName(name2);
85  		assertEquals(name2, cachedGetter.invoke(bean));
86  	}
87  	
88  	/**
89  	 * Tests getSetMethod().
90  	 */
91  	@Test
92  	public void testGetSetMethod() throws Exception {
93  		ReflectionBean bean = new ReflectionBean();
94  		
95  		// first time - not cached
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 		// second time - cached
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 		// retrieval from cache should be at least as fast as reflection
106 		System.out.println(String.format("getSetMethod uncachedTime: %d, cachedTime: %d", uncachedTime, cachedTime));
107 		assertTrue(cachedTime <= uncachedTime);
108 		
109 		// the same setter method should be returned in both calls
110 		assertEquals(uncachedSetter, cachedSetter);
111 		
112 		// test the uncached setter works
113 		final String name1 = "uncached";
114 		uncachedSetter.invoke(bean, name1);
115 		assertEquals(name1, bean.getName());
116 		
117 		// test the cached setter works
118 		final String name2 = "cached";
119 		cachedSetter.invoke(bean, name2);
120 		assertEquals(name2, bean.getName());
121 	}
122 	
123 	/**
124 	 * Tests getGetMethod() with a null object (should throw an exception).
125 	 */
126 	@Test(expected = NullPointerException.class)
127 	public void testGetGetMethodWithNullObject() {
128 		cache.getGetMethod(null, "name");
129 	}
130 	
131 	/**
132 	 * Tests getGetMethod() with a null field name (should throw an exception).
133 	 */
134 	@Test(expected = NullPointerException.class)
135 	public void testGetGetMethodWithNullFieldName() {
136 		cache.getGetMethod(new ReflectionBean(), null);
137 	}
138 	
139 	/**
140 	 * Tests getSetMethod() with a null object (should throw an exception).
141 	 */
142 	@Test(expected = NullPointerException.class)
143 	public void testGetSetMethodWithNullObject() {
144 		cache.getSetMethod(null, "name", String.class);
145 	}
146 	
147 	/**
148 	 * Tests getSetMethod() with a null field name (should throw an exception).
149 	 */
150 	@Test(expected = NullPointerException.class)
151 	public void testGetSetMethodWithNullFieldName() {
152 		cache.getSetMethod(new ReflectionBean(), null, String.class);
153 	}
154 	
155 	/**
156 	 * Tests getSetMethod() with a null argument type (should throw an exception).
157 	 */
158 	@Test(expected = NullPointerException.class)
159 	public void testGetSetMethodWithNullArgumentType() {
160 		cache.getSetMethod(new ReflectionBean(), "name", null);
161 	}
162 	
163 }