Clover Coverage Report - perfectjpattern(Aggregated)
Coverage timestamp: Sat Feb 28 2009 14:35:07 CET
38   230   12   4.22
0   137   0.32   1.8
9     1.33  
5    
 
  TestNameMatchAdapter       Line # 39 34 0% 5 3 91.7% 0.9166667
  TestNameMatchAdapter.ITargetInterface       Line # 141 0 - 0 0 - -1.0
  TestNameMatchAdapter.SampleAdapter       Line # 154 2 0% 2 0 100% 1.0
  TestNameMatchAdapter.GoodAdaptee       Line # 175 2 0% 3 3 40% 0.4
  TestNameMatchAdapter.BadAdaptee       Line # 201 0 0% 2 2 0% 0.0
 
  (2)
 
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  1 toggle 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    // ok
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    // ok
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    // ok
102    }
103    }
104   
105    //------------------------------------------------------------------------
 
106  1 toggle 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    // 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  1 toggle public SampleAdapter(GoodAdaptee anAdaptee,
159    IAdaptingStrategy anAdaptingStrategy)
160    throws IllegalArgumentException
161    {
162  1 super(ITargetInterface.class, anAdaptee, anAdaptingStrategy);
163    }
164   
165    //--------------------------------------------------------------------
 
166  1 toggle public void
167    method2()
168    {
169  1 theInvokations.add("method2");
170    }
171    }
172   
173    //------------------------------------------------------------------------
174    private
 
175    class GoodAdaptee
176    {
177    //--------------------------------------------------------------------
 
178  1 toggle public void
179    method3(String anArgument1, Integer anArgument2)
180    {
181  1 theInvokations.add("method3");
182    }
183   
184    //--------------------------------------------------------------------
 
185  0 toggle public void
186    method4()
187    {
188  0 theInvokations.add("method4");
189    }
190   
191    //--------------------------------------------------------------------
 
192  0 toggle public void
193    method5(Double anArgument1)
194    {
195    // must not be called
196    }
197    }
198   
199    //------------------------------------------------------------------------
200    private
 
201    class BadAdaptee
202    {
203    //--------------------------------------------------------------------
 
204  0 toggle public void
205    method6(Integer anArgument1, String anArgument2)
206    {
207    // empty
208    }
209   
210    //--------------------------------------------------------------------
 
211  0 toggle 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    }