View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  
18  package ch.elca.el4j.tests.util.interfaceenrichment;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNotNull;
22  
23  import java.lang.reflect.Method;
24  import java.rmi.Remote;
25  import java.rmi.RemoteException;
26  
27  import org.junit.Test;
28  import org.springframework.util.ClassUtils;
29  
30  import ch.elca.el4j.util.interfaceenrichment.EnrichmentDecorator;
31  import ch.elca.el4j.util.interfaceenrichment.InterfaceEnricher;
32  import ch.elca.el4j.util.interfaceenrichment.MethodDescriptor;
33  
34  /**
35   * This is the test case for the interface enrichment.
36   *
37   * @svnLink $Revision: 3878 $;$Date: 2009-08-04 15:06:35 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/test/java/ch/elca/el4j/tests/util/interfaceenrichment/InterfaceEnrichmentTest.java $
38   *
39   * @author Martin Zeltner (MZE)
40   */
41  public class InterfaceEnrichmentTest {
42  	/**
43  	 * Test interface with methods which have an array as parameter.
44  	 *
45  	 * @author Martin Zeltner (MZE)
46  	 */
47  	public interface MyArrayInterface {
48  		/**
49  		 * @return Returns the complex array.
50  		 */
51  		public MyValueObject[] getMyArray();
52  		
53  		/**
54  		 * @param myArray
55  		 *            Is the complex array to set.
56  		 */
57  		public void setMyArray(MyValueObject[] myArray);
58  	}
59  	
60  	public interface SubInterface extends MyArrayInterface {
61  		public int getInt();
62  	}
63  	
64  	/**
65  	 * Test value object, which is used in test interface above.
66  	 *
67  	 * @author Martin Zeltner (MZE)
68  	 */
69  	public static class MyValueObject {
70  		/**
71  		 * Property a.
72  		 */
73  		private String m_a;
74  
75  		/**
76  		 * Property b.
77  		 */
78  		private String m_b;
79  		
80  		/**
81  		 * Property c.
82  		 */
83  		private String m_c;
84  		
85  		/**
86  		 * @return Returns the a.
87  		 */
88  		public final String getA() {
89  			return m_a;
90  		}
91  
92  		/**
93  		 * @param a
94  		 *            The a to set.
95  		 */
96  		public final void setA(String a) {
97  			m_a = a;
98  		}
99  
100 		/**
101 		 * @return Returns the b.
102 		 */
103 		public final String getB() {
104 			return m_b;
105 		}
106 
107 		/**
108 		 * @param b
109 		 *            The b to set.
110 		 */
111 		public final void setB(String b) {
112 			m_b = b;
113 		}
114 
115 		/**
116 		 * @return Returns the c.
117 		 */
118 		public final String getC() {
119 			return m_c;
120 		}
121 
122 		/**
123 		 * @param c
124 		 *            The c to set.
125 		 */
126 		public final void setC(String c) {
127 			m_c = c;
128 		}
129 	}
130 	
131 	/**
132 	 * Test enrichment decorator.
133 	 *
134 	 * @author Martin Zeltner (MZE)
135 	 */
136 	public static class MyEnrichmentDecorator
137 		implements EnrichmentDecorator {
138 
139 		/**
140 		 * {@inheritDoc}
141 		 */
142 		public String changedInterfaceName(String originalInterfaceName) {
143 			return originalInterfaceName + "MyNew";
144 		}
145 
146 		/**
147 		 * {@inheritDoc}
148 		 */
149 		public Class[] changedExtendedInterface(Class[] extendedInterfaces) {
150 			InterfaceEnricher interfaceIndirector = new InterfaceEnricher();
151 			ClassLoader cl = Thread.currentThread().getContextClassLoader();
152 			
153 			Class[] changedInterfaces = new Class[extendedInterfaces.length + 1];
154 			for (int i = 0; i < extendedInterfaces.length; i++) {
155 				changedInterfaces[i] = interfaceIndirector.createShadowInterfaceAndLoadItDirectly(
156 					extendedInterfaces[i], this, cl);
157 			}
158 			changedInterfaces[extendedInterfaces.length] = Remote.class;
159 			
160 			return changedInterfaces;
161 		}
162 
163 		/**
164 		 * {@inheritDoc}
165 		 */
166 		public MethodDescriptor changedMethodSignature(
167 			MethodDescriptor method) {
168 			method.setThrownExceptions(new Class[] {RemoteException.class});
169 			return method;
170 		}
171 	}
172 
173 	/**
174 	 * Tests if enrichment of an interface with array parameter and array return
175 	 * type works correctly.
176 	 */
177 	@Test
178 	public void testInterfaceEnrichmentWithArrayParameter() {
179 		InterfaceEnricher ie = new InterfaceEnricher();
180 		Class oldInterface = MyArrayInterface.class;
181 		EnrichmentDecorator ed = new MyEnrichmentDecorator();
182 		ClassLoader cl = Thread.currentThread().getContextClassLoader();
183 		
184 		Class newInterface  = ie.createShadowInterfaceAndLoadItDirectly(
185 			oldInterface, ed, cl);
186 
187 		assertNotNull(newInterface);
188 		assertEquals(ClassUtils.getShortName(oldInterface) + "MyNew",
189 			ClassUtils.getShortName(newInterface));
190 		Method[] methods = newInterface.getMethods();
191 		assertEquals(2, methods.length);
192 		for (int i = 0; i < methods.length; i++) {
193 			assertEquals(1, methods[i].getExceptionTypes().length);
194 			assertEquals(RemoteException.class,
195 				methods[i].getExceptionTypes()[0]);
196 		}
197 		assertEquals(1, newInterface.getInterfaces().length);
198 		assertEquals(Remote.class, newInterface.getInterfaces()[0]);
199 	}
200 	
201 	@Test
202 	public void testSubInterfaceEnrichmentWithArrayParameter() {
203 		InterfaceEnricher ie = new InterfaceEnricher();
204 		Class oldInterface = SubInterface.class;
205 		EnrichmentDecorator ed = new MyEnrichmentDecorator();
206 		ClassLoader cl = Thread.currentThread().getContextClassLoader();
207 		
208 		Class newInterface  = ie.createShadowInterfaceAndLoadItDirectly(
209 			oldInterface, ed, cl);
210 
211 		assertNotNull(newInterface);
212 		assertEquals(ClassUtils.getShortName(oldInterface) + "MyNew",
213 			ClassUtils.getShortName(newInterface));
214 		Method[] methods = newInterface.getMethods();
215 		assertEquals(3, methods.length);
216 		for (int i = 0; i < methods.length; i++) {
217 			assertEquals(1, methods[i].getExceptionTypes().length);
218 			assertEquals(RemoteException.class,
219 				methods[i].getExceptionTypes()[0]);
220 		}
221 		assertEquals(2, newInterface.getInterfaces().length);
222 		
223 		// test if parent interface is enriched too
224 		Class parentInterface = ie.createShadowInterfaceAndLoadItDirectly(
225 			MyArrayInterface.class, ed, cl);
226 		assertEquals(parentInterface, newInterface.getInterfaces()[0]);
227 		assertEquals(Remote.class, newInterface.getInterfaces()[1]);
228 	}
229 }