1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package org.perfectjpattern.core.structural.adapter; |
22 |
|
|
23 |
|
import java.util.*; |
24 |
|
|
25 |
|
import junit.framework.*; |
26 |
|
|
27 |
|
import org.perfectjpattern.core.api.structural.adapter.*; |
28 |
|
import org.slf4j.*; |
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
@link@link |
33 |
|
|
34 |
|
|
35 |
|
@author |
36 |
|
@version |
37 |
|
|
38 |
|
public |
|
|
| 91.7% |
Uncovered Elements: 3 (36) |
Complexity: 5 |
Complexity Density: 0.15 |
|
39 |
|
class TestNameMatchAdapter |
40 |
|
extends TestCase |
41 |
|
{ |
42 |
|
|
43 |
|
|
44 |
|
|
|
|
| 85% |
Uncovered Elements: 3 (20) |
Complexity: 4 |
Complexity Density: 0.2 |
1
PASS
|
|
45 |
1
|
public void... |
46 |
|
testValidate() |
47 |
|
{ |
48 |
1
|
try |
49 |
|
{ |
50 |
1
|
Map<String, String> myMethodMapping = new HashMap<String, String>(); |
51 |
1
|
myMethodMapping.put("method1", "method3"); |
52 |
1
|
myMethodMapping.put("method2", "method4"); |
53 |
|
|
54 |
1
|
IAdaptingStrategy myNameMatchStrategy = |
55 |
|
new NameMatchAdaptingStrategy(myMethodMapping); |
56 |
|
|
57 |
1
|
myNameMatchStrategy.validate(ITargetInterface.class, |
58 |
|
new GoodAdaptee(), new Object()); |
59 |
|
|
60 |
|
|
61 |
|
} |
62 |
|
catch (NoSuchMethodError anException) |
63 |
|
{ |
64 |
0
|
fail("ExactMatch did not validate a good adaptee properly"); |
65 |
|
} |
66 |
|
|
67 |
1
|
try |
68 |
|
{ |
69 |
1
|
Map<String, String> myMethodMapping = new HashMap<String, String>(); |
70 |
1
|
myMethodMapping.put("method1", "method6"); |
71 |
|
|
72 |
1
|
IAdaptingStrategy myNameMatchStrategy = |
73 |
|
new NameMatchAdaptingStrategy(myMethodMapping); |
74 |
|
|
75 |
1
|
myNameMatchStrategy.validate(ITargetInterface.class, |
76 |
|
new BadAdaptee(), new Object()); |
77 |
|
|
78 |
0
|
fail("ExactMatch did not validate a bad adaptee properly"); |
79 |
|
} |
80 |
|
catch (NoSuchMethodError anException) |
81 |
|
{ |
82 |
|
|
83 |
|
} |
84 |
|
|
85 |
1
|
try |
86 |
|
{ |
87 |
1
|
Map<String, String> myMethodMapping = new HashMap<String, String>(); |
88 |
1
|
myMethodMapping.put("method1", "method6"); |
89 |
1
|
myMethodMapping.put("method2", "method7"); |
90 |
|
|
91 |
1
|
IAdaptingStrategy myNameMatchStrategy = |
92 |
|
new NameMatchAdaptingStrategy(myMethodMapping); |
93 |
|
|
94 |
1
|
myNameMatchStrategy.validate(ITargetInterface.class, |
95 |
|
new BadAdaptee(), new Object()); |
96 |
|
|
97 |
0
|
fail("ExactMatch did not validate a bad adaptee properly"); |
98 |
|
} |
99 |
|
catch (NoSuchMethodError anException) |
100 |
|
{ |
101 |
|
|
102 |
|
} |
103 |
|
} |
104 |
|
|
105 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
1
PASS
|
|
106 |
1
|
public void... |
107 |
|
testAdapter() |
108 |
|
{ |
109 |
1
|
theLogger.debug("Step #1: Define how to map Target methods to " + |
110 |
|
"Adaptee methods"); |
111 |
1
|
Map<String, String> myMethodsMapping = new HashMap<String, String>(); |
112 |
1
|
myMethodsMapping.put("method1", "method3"); |
113 |
|
|
114 |
1
|
IAdaptingStrategy myNameMatchStrategy = new NameMatchAdaptingStrategy( |
115 |
|
myMethodsMapping); |
116 |
|
|
117 |
1
|
theLogger.debug("Step #2: Create Adapter with " + |
118 |
|
"NameMatchAdaptingStrategy"); |
119 |
1
|
IAdapter<ITargetInterface, GoodAdaptee> myAdapter = new SampleAdapter( |
120 |
|
new GoodAdaptee(), myNameMatchStrategy); |
121 |
|
|
122 |
1
|
theLogger.debug("Step #3: Acquire the Target (Componet type view " + |
123 |
|
"of the Adaptee)"); |
124 |
1
|
ITargetInterface myTarget = myAdapter.getTarget(); |
125 |
|
|
126 |
1
|
theLogger.debug("Step #4: Invoke methods on the Target interface"); |
127 |
1
|
myTarget.method1("argument", Integer.valueOf(1)); |
128 |
1
|
myTarget.method2(); |
129 |
|
|
130 |
1
|
theLogger.debug("Step #5: Run assertions"); |
131 |
1
|
assertEquals("Adapter did not invoke method1 as expected", |
132 |
|
theInvokations.get(0), "method3"); |
133 |
1
|
assertEquals("Adapter did not invoke method2 as expected", |
134 |
|
theInvokations.get(1), "method2"); |
135 |
|
} |
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
private |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
141 |
|
interface ITargetInterface |
142 |
|
{ |
143 |
|
|
144 |
|
public void |
145 |
|
method1(String anArgument1, Integer anArgument2); |
146 |
|
|
147 |
|
|
148 |
|
public void |
149 |
|
method2(); |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
|
public |
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
154 |
|
class SampleAdapter |
155 |
|
extends Adapter<ITargetInterface, GoodAdaptee> |
156 |
|
{ |
157 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
158 |
1
|
public SampleAdapter(GoodAdaptee anAdaptee, ... |
159 |
|
IAdaptingStrategy anAdaptingStrategy) |
160 |
|
throws IllegalArgumentException |
161 |
|
{ |
162 |
1
|
super(ITargetInterface.class, anAdaptee, anAdaptingStrategy); |
163 |
|
} |
164 |
|
|
165 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
166 |
1
|
public void... |
167 |
|
method2() |
168 |
|
{ |
169 |
1
|
theInvokations.add("method2"); |
170 |
|
} |
171 |
|
} |
172 |
|
|
173 |
|
|
174 |
|
private |
|
|
| 40% |
Uncovered Elements: 3 (5) |
Complexity: 3 |
Complexity Density: 1.5 |
|
175 |
|
class GoodAdaptee |
176 |
|
{ |
177 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
178 |
1
|
public void... |
179 |
|
method3(String anArgument1, Integer anArgument2) |
180 |
|
{ |
181 |
1
|
theInvokations.add("method3"); |
182 |
|
} |
183 |
|
|
184 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
185 |
0
|
public void... |
186 |
|
method4() |
187 |
|
{ |
188 |
0
|
theInvokations.add("method4"); |
189 |
|
} |
190 |
|
|
191 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
192 |
0
|
public void... |
193 |
|
method5(Double anArgument1) |
194 |
|
{ |
195 |
|
|
196 |
|
} |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
|
private |
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 2 |
Complexity Density: - |
|
201 |
|
class BadAdaptee |
202 |
|
{ |
203 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
204 |
0
|
public void... |
205 |
|
method6(Integer anArgument1, String anArgument2) |
206 |
|
{ |
207 |
|
|
208 |
|
} |
209 |
|
|
210 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
211 |
0
|
public void... |
212 |
|
method7() |
213 |
|
{ |
214 |
|
|
215 |
|
} |
216 |
|
} |
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
private final List<String> theInvokations = new ArrayList<String>(); |
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
private final static Logger theLogger = LoggerFactory.getLogger( |
228 |
|
TestNameMatchAdapter.class); |
229 |
|
|
230 |
|
} |