KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > callback > TestDirectCallback


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.callback;
16
17 import org.apache.hivemind.ApplicationRuntimeException;
18 import org.apache.hivemind.Location;
19 import org.apache.tapestry.IComponent;
20 import org.apache.tapestry.IDirect;
21 import org.apache.tapestry.IPage;
22 import org.apache.tapestry.IRequestCycle;
23 import org.apache.tapestry.components.BaseComponentTestCase;
24 import org.easymock.MockControl;
25
26 /**
27  * @author Howard M. Lewis Ship
28  */

29 public class TestDirectCallback extends BaseComponentTestCase
30 {
31     public void testNoParams()
32     {
33         MockControl pagec = newControl(IPage.class);
34         IPage page = (IPage) pagec.getMock();
35
36         MockControl componentc = newControl(IDirect.class);
37         IDirect component = (IDirect) componentc.getMock();
38
39         component.getPage();
40         componentc.setReturnValue(page);
41
42         page.getPageName();
43         pagec.setReturnValue("Fred");
44
45         component.getIdPath();
46         componentc.setReturnValue("foo.bar");
47
48         replayControls();
49
50         DirectCallback callback = new DirectCallback(component, null);
51
52         assertEquals("DirectCallback[Fred/foo.bar]", callback.toString());
53
54         verifyControls();
55
56         IRequestCycle cycle = newCycle("Fred", page);
57
58         page.getNestedComponent("foo.bar");
59         pagec.setReturnValue(component);
60
61         cycle.setListenerParameters(null);
62
63         component.trigger(cycle);
64
65         replayControls();
66
67         callback.performCallback(cycle);
68
69         verifyControls();
70     }
71
72     public void testWithParams()
73     {
74         Object JavaDoc[] params = new Object JavaDoc[]
75         { "p1", "p2" };
76
77         MockControl pagec = newControl(IPage.class);
78         IPage page = (IPage) pagec.getMock();
79
80         MockControl componentc = newControl(IDirect.class);
81         IDirect component = (IDirect) componentc.getMock();
82
83         component.getPage();
84         componentc.setReturnValue(page);
85
86         page.getPageName();
87         pagec.setReturnValue("Barney");
88
89         component.getIdPath();
90         componentc.setReturnValue("foo.bar");
91
92         replayControls();
93
94         DirectCallback callback = new DirectCallback(component, params);
95
96         assertEquals("DirectCallback[Barney/foo.bar p1, p2]", callback.toString());
97
98         verifyControls();
99
100         IRequestCycle cycle = newCycle("Barney", page);
101
102         page.getNestedComponent("foo.bar");
103         pagec.setReturnValue(component);
104
105         cycle.setListenerParameters(params);
106
107         component.trigger(cycle);
108
109         replayControls();
110
111         callback.performCallback(cycle);
112
113         verifyControls();
114     }
115
116     public void testNotDirect()
117     {
118         MockControl pagec = newControl(IPage.class);
119         IPage page = (IPage) pagec.getMock();
120
121         MockControl componentc = newControl(IDirect.class);
122         IDirect component = (IDirect) componentc.getMock();
123
124         component.getPage();
125         componentc.setReturnValue(page);
126
127         page.getPageName();
128         pagec.setReturnValue("Fred");
129
130         component.getIdPath();
131         componentc.setReturnValue("foo.bar");
132
133         replayControls();
134
135         DirectCallback callback = new DirectCallback(component, null);
136
137         assertEquals("DirectCallback[Fred/foo.bar]", callback.toString());
138
139         verifyControls();
140
141         IRequestCycle cycle = newCycle("Fred", page);
142
143         Location l = newLocation();
144         IComponent component2 = newComponent("Fred/foo.bar", l);
145
146         page.getNestedComponent("foo.bar");
147         pagec.setReturnValue(component2);
148
149         replayControls();
150
151         try
152         {
153             callback.performCallback(cycle);
154         }
155         catch (ApplicationRuntimeException ex)
156         {
157             assertEquals("Component Fred/foo.bar does not implement the IDirect interface.", ex
158                     .getMessage());
159             assertSame(component2, ex.getComponent());
160             assertSame(l, ex.getLocation());
161         }
162
163         verifyControls();
164     }
165 }
166
Popular Tags