1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
36
37
38
39
40
41 public class InterfaceEnrichmentTest {
42
43
44
45
46
47 public interface MyArrayInterface {
48
49
50
51 public MyValueObject[] getMyArray();
52
53
54
55
56
57 public void setMyArray(MyValueObject[] myArray);
58 }
59
60 public interface SubInterface extends MyArrayInterface {
61 public int getInt();
62 }
63
64
65
66
67
68
69 public static class MyValueObject {
70
71
72
73 private String m_a;
74
75
76
77
78 private String m_b;
79
80
81
82
83 private String m_c;
84
85
86
87
88 public final String getA() {
89 return m_a;
90 }
91
92
93
94
95
96 public final void setA(String a) {
97 m_a = a;
98 }
99
100
101
102
103 public final String getB() {
104 return m_b;
105 }
106
107
108
109
110
111 public final void setB(String b) {
112 m_b = b;
113 }
114
115
116
117
118 public final String getC() {
119 return m_c;
120 }
121
122
123
124
125
126 public final void setC(String c) {
127 m_c = c;
128 }
129 }
130
131
132
133
134
135
136 public static class MyEnrichmentDecorator
137 implements EnrichmentDecorator {
138
139
140
141
142 public String changedInterfaceName(String originalInterfaceName) {
143 return originalInterfaceName + "MyNew";
144 }
145
146
147
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
165
166 public MethodDescriptor changedMethodSignature(
167 MethodDescriptor method) {
168 method.setThrownExceptions(new Class[] {RemoteException.class});
169 return method;
170 }
171 }
172
173
174
175
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
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 }