1 //----------------------------------------------------------------------
2 //
3 // PerfectJPattern: "Design patterns are good but components are better!"
4 // TestExactMatchAdapter.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 ExactMatchAdaptingStrategy}
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 TestExactMatchAdapter
40 extends TestCase
41 {
42 //------------------------------------------------------------------------
43 // public
44 //------------------------------------------------------------------------
45 public void
46 testValidate()
47 {
48 IAdaptingStrategy myExactMatchStrategy =
49 new ExactMatchAdaptingStrategy();
50
51 try
52 {
53 myExactMatchStrategy.validate(ITargetInterface.class,
54 new GoodAdaptee(), new Object());
55
56 // ok
57 }
58 catch (NoSuchMethodError anException)
59 {
60 fail("ExactMatch did not validate a good adaptee properly");
61 }
62
63 try
64 {
65 myExactMatchStrategy.validate(ITargetInterface.class,
66 new BadAdaptee(), new Object());
67
68 fail("ExactMatch did not validate a bad adaptee properly");
69 }
70 catch (NoSuchMethodError anException)
71 {
72 // ok
73 }
74 }
75
76 //------------------------------------------------------------------------
77 public void
78 testAdapter()
79 {
80 theLogger.debug("Step #1: Create Adapter with default " +
81 "AdaptingStrategy");
82 IAdapter<ITargetInterface, GoodAdaptee> myAdapter = new Adapter<
83 ITargetInterface, GoodAdaptee>(ITargetInterface.class,
84 new GoodAdaptee());
85
86 theLogger.debug("Step #2: Acquire the Target (Componet type view " +
87 "of the Adaptee)");
88 ITargetInterface myTarget = myAdapter.getTarget();
89
90 theLogger.debug("Step #3: Invoke methods on the Target interface");
91 myTarget.method1("argument", Integer.valueOf(1));
92 myTarget.method2();
93
94 theLogger.debug("Step #4: Run assertions");
95 assertEquals("Adapter did not invoke method1 as expected",
96 theInvokations.get(0), "method1");
97 assertEquals("Adapter did not invoke method2 as expected",
98 theInvokations.get(1), "method2");
99 }
100
101 //------------------------------------------------------------------------
102 // inner classes
103 //------------------------------------------------------------------------
104 private
105 interface ITargetInterface
106 {
107 //--------------------------------------------------------------------
108 public void
109 method1(String anArgument1, Integer anArgument2);
110
111 //--------------------------------------------------------------------
112 public void
113 method2();
114
115 //--------------------------------------------------------------------
116 public boolean
117 isSupported();
118 }
119
120 //------------------------------------------------------------------------
121 private
122 class GoodAdaptee
123 {
124 //--------------------------------------------------------------------
125 public void
126 method1(String anArgument1, Integer anArgument2)
127 {
128 theInvokations.add("method1");
129 }
130
131 //--------------------------------------------------------------------
132 public void
133 method2()
134 {
135 theInvokations.add("method2");
136 }
137
138 //--------------------------------------------------------------------
139 public boolean
140 isSupported()
141 {
142 return true;
143 }
144
145 //--------------------------------------------------------------------
146 public void
147 method3(Double anArgument1)
148 {
149 // must not be called
150 }
151 }
152
153 //------------------------------------------------------------------------
154 private
155 class BadAdaptee
156 {
157 //--------------------------------------------------------------------
158 public void
159 method1(String anArgument1, Integer anArgument2)
160 {
161
162 }
163 }
164
165 //------------------------------------------------------------------------
166 // members
167 //------------------------------------------------------------------------
168 private final List<String> theInvokations = new ArrayList<String>();
169
170 /**
171 * Provides logging services for this class.
172 */
173 // CHECKSTYLE:OFF
174 private final static Logger theLogger = LoggerFactory.getLogger(
175 TestExactMatchAdapter.class);
176 // CHECKSTYLE:ON
177 }