1 //----------------------------------------------------------------------
2 //
3 // PerfectJPattern: "Design patterns are good but components are better!"
4 // TestDecorator.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.decorator;
22
23 import junit.framework.*;
24
25 import org.perfectjpattern.core.api.structural.decorator.*;
26 import org.slf4j.*;
27
28 /**
29 * Test Suite for <code>AbstarctDecorator</code> implementation.
30 *
31 * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
32 * @version $Revision: 1.0 $ $Date: Nov 25, 2007 4:28:45 PM $
33 */
34 public class TestDecorator
35 extends TestCase
36 {
37 //------------------------------------------------------------------------
38 // public
39 //------------------------------------------------------------------------
40 public void
41 testDecorator()
42 {
43 theLogger.debug("Step #1: Create a testing Component");
44 ISomeComponent myComponent = new SomeComponent();
45
46 theLogger.debug("Step #2: Create Component Decorator");
47 IDecorator<ISomeComponent, ISomeComponent> myDecorator =
48 new ComponentDecorator(myComponent);
49
50 theLogger.debug("Step #3: Use the Component reference of Decorator ");
51 myDecorator.getComponent().printValue("Hello World!");
52
53 theLogger.debug("Running assertions");
54 assertTrue("printValue(...) must have been called in the Decorator",
55 ((ComponentDecorator) myDecorator).isCalled());
56
57 assertTrue("printValue(...) must have been called in the Component",
58 ((SomeComponent) myComponent).isCalled());
59 }
60
61 //------------------------------------------------------------------------
62 // inner classes
63 //------------------------------------------------------------------------
64 /**
65 * Definition of an example Component interface.
66 */
67 public static
68 interface ISomeComponent
69 {
70 //--------------------------------------------------------------------
71 public void
72 printValue(String aValue);
73 }
74
75 //------------------------------------------------------------------------
76 /**
77 * Implementation of the interface defined above.
78 */
79 public static
80 class SomeComponent
81 implements ISomeComponent
82 {
83 //--------------------------------------------------------------------
84 public void
85 printValue(String aValue)
86 {
87 theLogger.debug(aValue);
88
89 theCalled = true;
90 }
91
92 //--------------------------------------------------------------------
93 public boolean
94 isCalled()
95 {
96 return theCalled;
97 }
98
99 //--------------------------------------------------------------------
100 // members
101 //--------------------------------------------------------------------
102 private boolean theCalled;
103 }
104
105 //------------------------------------------------------------------------
106 public static
107 class ComponentDecorator
108 extends AbstractDecorator<ISomeComponent, ISomeComponent>
109 {
110 //--------------------------------------------------------------------
111 public
112 ComponentDecorator(ISomeComponent aComponent)
113 {
114 super(ISomeComponent.class, aComponent);
115 }
116
117 //--------------------------------------------------------------------
118 public void
119 printValue(String aValue)
120 {
121 theLogger.debug(aValue);
122
123 theCalled = true;
124
125 getDecorated().printValue(aValue);
126 }
127
128 //--------------------------------------------------------------------
129 public boolean
130 isCalled()
131 {
132 return theCalled;
133 }
134
135 //--------------------------------------------------------------------
136 // members
137 //--------------------------------------------------------------------
138 private boolean theCalled;
139 }
140
141 //------------------------------------------------------------------------
142 // members
143 //------------------------------------------------------------------------
144 /**
145 * Provides logging services for this class.
146 */
147 // CHECKSTYLE:OFF
148 private static final Logger theLogger = LoggerFactory.getLogger(
149 TestDecorator.class);
150 // CHECKSTYLE:ON
151 }