1 //----------------------------------------------------------------------
2 //
3 // PerfectJPattern: "Design patterns are good but components are better!"
4 // TestProxy.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.proxy;
22
23 import junit.framework.*;
24
25 import org.perfectjpattern.core.api.structural.proxy.*;
26 import org.slf4j.*;
27
28 /**
29 * Test suite for the {@link AbstractProxy} implementation
30 *
31 * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
32 * @version $Revision: 1.0 $Date: Jun 18, 2008 10:57:21 PM $
33 */
34 public
35 class TestProxy
36 extends TestCase
37 {
38 //------------------------------------------------------------------------
39 // public
40 //------------------------------------------------------------------------
41 public void
42 testProxying()
43 {
44 theLogger.debug("Step #1: Create component and proxy");
45 SomeComponent mySubject = new SomeComponent();
46 IProxy<ISomeComponent> myProxy = new AbstractProxy<
47 ISomeComponent>(ISomeComponent.class, mySubject)
48 {
49 // empty
50 };
51
52 theLogger.debug("Step #2: Make sure that the 'Subject' is correct");
53 assertSame("IProxy.getSubject not correctly implemented", mySubject,
54 myProxy.getRealSubject());
55
56 theLogger.debug("Step #3: Get reference to proxied component");
57 ISomeComponent myComponent = myProxy.getSubject();
58
59 theLogger.debug("Step #4: Call methods through the proxied component");
60 myComponent.method1("Hello proxy world!");
61 myComponent.method2();
62
63 theLogger.debug("Running assertions");
64 assertTrue("method1 was not called", mySubject.isMethod1Called());
65
66 assertTrue("method2 was not called", mySubject.isMethod2Called());
67 }
68
69 //------------------------------------------------------------------------
70 // inner classes
71 //------------------------------------------------------------------------
72 /**
73 * Definition of an example Component interface.
74 */
75 public static
76 interface ISomeComponent
77 {
78 //--------------------------------------------------------------------
79 public void
80 method1(String aValue);
81
82 //--------------------------------------------------------------------
83 public void
84 method2();
85 }
86
87 //------------------------------------------------------------------------
88 /**
89 * Implementation of the interface defined above.
90 */
91 private static
92 class SomeComponent
93 implements ISomeComponent
94 {
95 //--------------------------------------------------------------------
96 public void
97 method1(String aValue)
98 {
99 theLogger.debug(aValue);
100
101 theMethod1Called = true;
102 }
103
104 //--------------------------------------------------------------------
105 public void
106 method2()
107 {
108 theMethod2Called = true;
109 }
110
111 //--------------------------------------------------------------------
112 public boolean
113 isMethod1Called()
114 {
115 return theMethod1Called;
116 }
117
118 //--------------------------------------------------------------------
119 public boolean
120 isMethod2Called()
121 {
122 return theMethod2Called;
123 }
124
125 //--------------------------------------------------------------------
126 // members
127 //--------------------------------------------------------------------
128 private boolean theMethod1Called;
129 private boolean theMethod2Called;
130 }
131
132 //------------------------------------------------------------------------
133 // members
134 //------------------------------------------------------------------------
135 /**
136 * Provides logging services for this class.
137 */
138 // CHECKSTYLE:OFF
139 private final static Logger theLogger = LoggerFactory.getLogger(TestProxy.class);
140 // CHECKSTYLE:ON
141 }