1 //----------------------------------------------------------------------
2 //
3 // PerfectJPattern: "Design patterns are good but components are better!"
4 // NameMatchAdaptingStrategy.java Copyright (c) 2009 Giovanni Azua Garcia
5 // bravegag@hotmail.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 3
10 // of the License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, see <http://www.gnu.org/licenses/>.
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 * Test suite for the {@link Adapter} and {@link NameMatchAdaptingStrategy}
33 * implementations
34 *
35 * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
36 * @version $Revision: 1.0 $Date: Jan 28, 2009 3:05:35 PM $
37 */
38 public
39 class TestNameMatchAdapter
40 extends TestCase
41 {
42 //------------------------------------------------------------------------
43 // public
44 //------------------------------------------------------------------------
45 public void
46 testValidate()
47 {
48 try
49 {
50 Map<String, String> myMethodMapping = new HashMap<String, String>();
51 myMethodMapping.put("method1", "method3");
52 myMethodMapping.put("method2", "method4");
53
54 IAdaptingStrategy myNameMatchStrategy =
55 new NameMatchAdaptingStrategy(myMethodMapping);
56
57 myNameMatchStrategy.validate(ITargetInterface.class,
58 new GoodAdaptee(), new Object());
59
60 // ok
61 }
62 catch (NoSuchMethodError anException)
63 {
64 fail("ExactMatch did not validate a good adaptee properly");
65 }
66
67 try
68 {
69 Map<String, String> myMethodMapping = new HashMap<String, String>();
70 myMethodMapping.put("method1", "method6");
71
72 IAdaptingStrategy myNameMatchStrategy =
73 new NameMatchAdaptingStrategy(myMethodMapping);
74
75 myNameMatchStrategy.validate(ITargetInterface.class,
76 new BadAdaptee(), new Object());
77
78 fail("ExactMatch did not validate a bad adaptee properly");
79 }
80 catch (NoSuchMethodError anException)
81 {
82 // ok
83 }
84
85 try
86 {
87 Map<String, String> myMethodMapping = new HashMap<String, String>();
88 myMethodMapping.put("method1", "method6");
89 myMethodMapping.put("method2", "method7");
90
91 IAdaptingStrategy myNameMatchStrategy =
92 new NameMatchAdaptingStrategy(myMethodMapping);
93
94 myNameMatchStrategy.validate(ITargetInterface.class,
95 new BadAdaptee(), new Object());
96
97 fail("ExactMatch did not validate a bad adaptee properly");
98 }
99 catch (NoSuchMethodError anException)
100 {
101 // ok
102 }
103 }
104
105 //------------------------------------------------------------------------
106 public void
107 testAdapter()
108 {
109 theLogger.debug("Step #1: Define how to map Target methods to " +
110 "Adaptee methods");
111 Map<String, String> myMethodsMapping = new HashMap<String, String>();
112 myMethodsMapping.put("method1", "method3");
113
114 IAdaptingStrategy myNameMatchStrategy = new NameMatchAdaptingStrategy(
115 myMethodsMapping);
116
117 theLogger.debug("Step #2: Create Adapter with " +
118 "NameMatchAdaptingStrategy");
119 IAdapter<ITargetInterface, GoodAdaptee> myAdapter = new SampleAdapter(
120 new GoodAdaptee(), myNameMatchStrategy);
121
122 theLogger.debug("Step #3: Acquire the Target (Componet type view " +
123 "of the Adaptee)");
124 ITargetInterface myTarget = myAdapter.getTarget();
125
126 theLogger.debug("Step #4: Invoke methods on the Target interface");
127 myTarget.method1("argument", Integer.valueOf(1));
128 myTarget.method2();
129
130 theLogger.debug("Step #5: Run assertions");
131 assertEquals("Adapter did not invoke method1 as expected",
132 theInvokations.get(0), "method3");
133 assertEquals("Adapter did not invoke method2 as expected",
134 theInvokations.get(1), "method2");
135 }
136
137 //------------------------------------------------------------------------
138 // inner classes
139 //------------------------------------------------------------------------
140 private
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
154 class SampleAdapter
155 extends Adapter<ITargetInterface, GoodAdaptee>
156 {
157 //--------------------------------------------------------------------
158 public SampleAdapter(GoodAdaptee anAdaptee,
159 IAdaptingStrategy anAdaptingStrategy)
160 throws IllegalArgumentException
161 {
162 super(ITargetInterface.class, anAdaptee, anAdaptingStrategy);
163 }
164
165 //--------------------------------------------------------------------
166 public void
167 method2()
168 {
169 theInvokations.add("method2");
170 }
171 }
172
173 //------------------------------------------------------------------------
174 private
175 class GoodAdaptee
176 {
177 //--------------------------------------------------------------------
178 public void
179 method3(String anArgument1, Integer anArgument2)
180 {
181 theInvokations.add("method3");
182 }
183
184 //--------------------------------------------------------------------
185 public void
186 method4()
187 {
188 theInvokations.add("method4");
189 }
190
191 //--------------------------------------------------------------------
192 public void
193 method5(Double anArgument1)
194 {
195 // must not be called
196 }
197 }
198
199 //------------------------------------------------------------------------
200 private
201 class BadAdaptee
202 {
203 //--------------------------------------------------------------------
204 public void
205 method6(Integer anArgument1, String anArgument2)
206 {
207 // empty
208 }
209
210 //--------------------------------------------------------------------
211 public void
212 method7()
213 {
214 // empty
215 }
216 }
217
218 //------------------------------------------------------------------------
219 // members
220 //------------------------------------------------------------------------
221 private final List<String> theInvokations = new ArrayList<String>();
222
223 /**
224 * Provides logging services for this class.
225 */
226 // CHECKSTYLE:OFF
227 private final static Logger theLogger = LoggerFactory.getLogger(
228 TestNameMatchAdapter.class);
229 // CHECKSTYLE:ON
230 }