1 //----------------------------------------------------------------------
2 //
3 // PerfectJPattern: "Design patterns are good but components are better!"
4 // TestFancyObserver.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.behavioral.observer;
22
23 import java.util.*;
24
25 import junit.framework.*;
26
27 import org.perfectjpattern.core.api.behavioral.observer.*;
28 import org.perfectjpattern.core.api.behavioral.observer.data.*;
29 import org.slf4j.*;
30
31
32 /**
33 * Test suite for <code>ObserverProxy</code> implementation.
34 *
35 * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
36 * @version $Revision: 1.0 $ $Date: Jun 30, 2007 10:17:51 PM $
37 */
38 public
39 class TestProxyObserver
40 extends TestCase
41 {
42 //------------------------------------------------------------------------
43 // public
44 //------------------------------------------------------------------------
45 @SuppressWarnings("unchecked")
46 public void
47 testObserverPOJO()
48 {
49 // create the subjects
50 ISubject<EventDataOne> mySubjectOne = new Subject<EventDataOne>();
51 ISubject<EventDataTwo> mySubjectTwo = new Subject<EventDataTwo>();
52 ISubject<EventDataThree> mySubjectThree =
53 new Subject<EventDataThree>();
54
55 // create a plain agnostic POJO that does not have any abstract
56 // coupling to org.perfectjpattern's Observer implementation.
57 MyObserverPOJO myPOJO = new MyObserverPOJO();
58
59 mySubjectOne.attach(new ObserverProxy<EventDataOne>(myPOJO,
60 "onEventOne", EventDataOne.class));
61
62 mySubjectTwo.attach(new ObserverProxy<EventDataTwo>(myPOJO,
63 "onEventTwo", EventDataTwo.class));
64
65 mySubjectThree.attach(new ObserverProxy<EventDataThree>(myPOJO,
66 "onEventThree", EventDataThree.class));
67
68 mySubjectOne.notifyObservers(new EventDataOne());
69 mySubjectTwo.notifyObservers(new EventDataTwo());
70 mySubjectThree.notifyObservers(new EventDataThree());
71
72 String[] myExpected = new String[]
73 {
74 "onEventOne",
75 "onEventTwo",
76 "onEventThree"
77 };
78
79 assertTrue("ObserverProxy did not work as expected, not all methods " +
80 "were successfully called.", Arrays.deepEquals(myExpected,
81 myPOJO.getCalledMethods().toArray()));
82 }
83
84 //------------------------------------------------------------------------
85 // inner classes
86 //------------------------------------------------------------------------
87 public static
88 class MyObserverPOJO
89 {
90 //--------------------------------------------------------------------
91 public void
92 onEventOne(EventDataOne anEventData)
93 {
94 theLogger.debug("Received event " + anEventData.toString());
95
96 theCalledMethods.add("onEventOne");
97 }
98
99 //--------------------------------------------------------------------
100 public void
101 onEventTwo(EventDataTwo anEventData)
102 {
103 theLogger.debug("Received event " + anEventData.toString());
104
105 theCalledMethods.add("onEventTwo");
106 }
107
108 //--------------------------------------------------------------------
109 public void
110 onEventThree(EventDataThree anEventData)
111 {
112 theLogger.debug("Received event " + anEventData.toString());
113
114 theCalledMethods.add("onEventThree");
115 }
116
117 //--------------------------------------------------------------------
118 public List<String>
119 getCalledMethods()
120 {
121 return theCalledMethods;
122 }
123
124 //--------------------------------------------------------------------
125 // members
126 //--------------------------------------------------------------------
127 /**
128 * Attribute required for testing purposes: keep track of what calls
129 * completed successfully.
130 */
131 private List<String> theCalledMethods = new ArrayList<String>();
132 }
133
134 //------------------------------------------------------------------------
135 private static
136 class EventDataOne
137 implements IEventData
138 {
139 // empty
140 }
141
142 //------------------------------------------------------------------------
143 private static
144 class EventDataTwo
145 implements IEventData
146 {
147 // empty
148 }
149
150 //------------------------------------------------------------------------
151 private static
152 class EventDataThree
153 implements IEventData
154 {
155 // empty
156 }
157
158 //------------------------------------------------------------------------
159 // members
160 //------------------------------------------------------------------------
161 /**
162 * Provides logging services for this class.
163 */
164 // CHECKSTYLE:OFF
165 private final static Logger theLogger = LoggerFactory.getLogger(
166 TestProxyObserver.class);
167 // CHECKSTYLE:ON
168 }