KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > listener > TestListenerMap


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.listener;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.hivemind.ApplicationRuntimeException;
22 import org.apache.hivemind.test.HiveMindTestCase;
23 import org.apache.tapestry.IActionListener;
24 import org.apache.tapestry.IRequestCycle;
25
26 /**
27  * Tests for {@link org.apache.tapestry.listener.ListenerMapImpl}.
28  *
29  * @author Howard M. Lewis Ship
30  * @since 4.0
31  */

32 public class TestListenerMap extends HiveMindTestCase
33 {
34     public void testGetListener()
35     {
36         Object JavaDoc target = new Object JavaDoc();
37         IRequestCycle cycle = newCycle();
38         ListenerMethodInvoker invoker = newInvoker();
39         Map JavaDoc map = newMap("method", invoker);
40
41         invoker.invokeListenerMethod(target, cycle);
42
43         replayControls();
44
45         ListenerMap lm = new ListenerMapImpl(target, map);
46
47         IActionListener l1 = lm.getListener("method");
48
49         l1.actionTriggered(null, cycle);
50
51         verifyControls();
52
53         IActionListener l2 = lm.getListener("method");
54
55         assertSame(l1, l2);
56     }
57
58     public void testGetListenerNames()
59     {
60         Object JavaDoc target = new Object JavaDoc();
61         ListenerMethodInvoker invoker = newInvoker();
62         Map JavaDoc map = newMap("method", invoker);
63
64         replayControls();
65
66         ListenerMap lm = new ListenerMapImpl(target, map);
67
68         // Copy both collections into ArrayLists for comparison purposes.
69

70         assertEquals(new ArrayList JavaDoc(map.keySet()), new ArrayList JavaDoc(lm.getListenerNames()));
71
72         verifyControls();
73
74         try
75         {
76             lm.getListenerNames().clear();
77             unreachable();
78         }
79         catch (UnsupportedOperationException JavaDoc ex)
80         {
81             // Ignore. Expected result.
82
}
83     }
84
85     public void testCanProvideListener()
86     {
87         Object JavaDoc target = new Object JavaDoc();
88         ListenerMethodInvoker invoker = newInvoker();
89         Map JavaDoc map = newMap("method", invoker);
90
91         replayControls();
92
93         ListenerMap lm = new ListenerMapImpl(target, map);
94
95         assertEquals(true, lm.canProvideListener("method"));
96         assertEquals(false, lm.canProvideListener("foobar"));
97
98         verifyControls();
99     }
100
101     public void testMissingListener()
102     {
103         Object JavaDoc target = "*TARGET*";
104         ListenerMethodInvoker invoker = newInvoker();
105         Map JavaDoc map = newMap("method", invoker);
106
107         replayControls();
108
109         ListenerMap lm = new ListenerMapImpl(target, map);
110
111         try
112         {
113             lm.getListener("foobar");
114             unreachable();
115         }
116         catch (ApplicationRuntimeException ex)
117         {
118             assertEquals("Object *TARGET* does not implement a listener method named 'foobar'.", ex
119                     .getMessage());
120             assertSame(target, ex.getComponent());
121         }
122
123         verifyControls();
124     }
125
126     private Map JavaDoc newMap(Object JavaDoc key, Object JavaDoc value)
127     {
128         Map JavaDoc result = new HashMap JavaDoc();
129
130         result.put(key, value);
131
132         return result;
133     }
134
135     private ListenerMethodInvoker newInvoker()
136     {
137         return (ListenerMethodInvoker) newMock(ListenerMethodInvoker.class);
138     }
139
140     private IRequestCycle newCycle()
141     {
142         return (IRequestCycle) newMock(IRequestCycle.class);
143     }
144 }
Popular Tags