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.assertNotNull;
20  import static org.junit.Assert.assertNull;
21  
22  import org.junit.Before;
23  import org.junit.Test;
24  
25  /**
26   * Tests the BeanInterfaceProxy class.
27   * 
28   * @author James Bassett
29   */
30  public class BeanInterfaceProxyTest {
31  	
32  	private TestInterface testInterface;
33  	
34  	/**
35  	 * Creates the proxy for the test.
36  	 */
37  	@Before
38  	public void setUp() {
39  		testInterface = BeanInterfaceProxy.createProxy(TestInterface.class);
40  	}
41  	
42  	/**
43  	 * Tests the proxy.
44  	 */
45  	@Test
46  	public void testProxy() {
47  		// no state saved yet
48  		assertNull(testInterface.getValue());
49  		
50  		// modify state
51  		String value = "value";
52  		testInterface.setValue(value);
53  		
54  		// check state was saved
55  		assertEquals(value, testInterface.getValue());
56  		
57  		// modify state (to null)
58  		testInterface.setValue(null);
59  		
60  		// check state was saved
61  		assertNull(testInterface.getValue());
62  	}
63  	
64  	/**
65  	 * Tests the proxy with chained setters (the proxy allows for setters which return the modified object).
66  	 */
67  	@Test
68  	public void testChainedSetters() {
69  		// no state saved yet
70  		assertNull(testInterface.getValue());
71  		
72  		// modify state
73  		String value = "value";
74  		String value2 = "value2";
75  		testInterface.setValue(value).setValue2(value2);
76  		
77  		// check state was saved
78  		assertEquals(value, testInterface.getValue());
79  		assertEquals(value2, testInterface.getValue2());
80  	}
81  	
82  	/**
83  	 * Tests createProxy() with null.
84  	 */
85  	@Test(expected = NullPointerException.class)
86  	public void testCreateProxyWithNull() {
87  		BeanInterfaceProxy.createProxy(null);
88  	}
89  	
90  	/**
91  	 * Tests invocation of a method that isn't a standard getter/setter.
92  	 */
93  	@Test(expected = IllegalArgumentException.class)
94  	public void testNotGetterOrSetter() {
95  		testInterface.isValueSet();
96  	}
97  	
98  	/**
99  	 * Tests invocation of a getter with a parameter (not a real getter).
100 	 */
101 	@Test(expected = IllegalArgumentException.class)
102 	public void testGetterWithParam() {
103 		testInterface.getWithParam("param");
104 	}
105 	
106 	/**
107 	 * Tests invocation of a method beginning with 'set' that takes many parameters (not a real setter).
108 	 */
109 	@Test(expected = IllegalArgumentException.class)
110 	public void testMultiParamSetter() {
111 		testInterface.setManyValues("value1", "value2");
112 	}
113 	
114 	/**
115 	 * Tests invocation of a setter without any parameters (not a real setter).
116 	 */
117 	@Test(expected = IllegalArgumentException.class)
118 	public void testSetterWithNoParams() {
119 		testInterface.setNothing();
120 	}
121 	
122 	/**
123 	 * Tests invocation of the <tt>getClass</tt> method, inherited from <tt>Object</tt> (doesn't call the
124 	 * <tt>InvocationHandler</tt>, but returns the proxy class!).
125 	 */
126 	@Test
127 	public void testGetClass() {
128 		assertNotNull(testInterface.getClass());
129 		
130 	}
131 	
132 	/**
133 	 * An interface to use for testing.
134 	 */
135 	private interface TestInterface {
136 		
137 		public TestInterface setValue(String value); // proxy allows setters that returned the modified object
138 		
139 		public void setValue2(String value);
140 		
141 		public String getValue();
142 		
143 		public String getValue2();
144 		
145 		public String getWithParam(String value1); // getters with params not allowed
146 		
147 		public void setManyValues(String value1, String value2); // multi param setters not allowed
148 		
149 		public void setNothing(); // setters should have 1 param
150 		
151 		public boolean isValueSet(); // methods not starting with get/set are not allowed (yes, even boolean getters!)
152 		
153 	}
154 	
155 }