KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > pageload > TestVerifyRequiredParametersVisitor


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.pageload;
16
17 import org.apache.hivemind.ApplicationRuntimeException;
18 import org.apache.hivemind.Location;
19 import org.apache.hivemind.test.HiveMindTestCase;
20 import org.apache.tapestry.IBinding;
21 import org.apache.tapestry.IComponent;
22 import org.apache.tapestry.spec.ComponentSpecification;
23 import org.apache.tapestry.spec.IComponentSpecification;
24 import org.apache.tapestry.spec.ParameterSpecification;
25 import org.easymock.MockControl;
26
27 /**
28  * Tests for {@link org.apache.tapestry.pageload.VerifyRequiredParametersVisitor}.
29  *
30  * @author Howard M. Lewis Ship
31  * @since 4.0
32  */

33 public class TestVerifyRequiredParametersVisitor extends HiveMindTestCase
34 {
35     private IComponent newComponent(IComponentSpecification spec)
36     {
37         MockControl control = newControl(IComponent.class);
38         IComponent component = (IComponent) control.getMock();
39
40         component.getSpecification();
41         control.setReturnValue(spec);
42
43         return component;
44     }
45
46     private IBinding newBinding()
47     {
48         return (IBinding) newMock(IBinding.class);
49     }
50
51     public void testNotRequired()
52     {
53         ParameterSpecification pspec = new ParameterSpecification();
54         pspec.setParameterName("fred");
55
56         ComponentSpecification cspec = new ComponentSpecification();
57         cspec.addParameter(pspec);
58
59         IComponent component = newComponent(cspec);
60
61         replayControls();
62
63         VerifyRequiredParametersVisitor visitor = new VerifyRequiredParametersVisitor();
64
65         visitor.visitComponent(component);
66
67         verifyControls();
68     }
69
70     public void testRequiredWithAlias()
71     {
72         ParameterSpecification pspec = new ParameterSpecification();
73         pspec.setParameterName("fred");
74         pspec.setAliases("barney");
75         pspec.setRequired(true);
76
77         ComponentSpecification cspec = new ComponentSpecification();
78         cspec.addParameter(pspec);
79
80         IBinding fredBinding = newBinding();
81
82         MockControl control = newControl(IComponent.class);
83         IComponent component = (IComponent) control.getMock();
84
85         component.getSpecification();
86         control.setReturnValue(cspec);
87
88         // Notice that we don't ever check for "barney", just
89
// "fred"
90

91         component.getBinding("fred");
92         control.setReturnValue(fredBinding);
93
94         replayControls();
95
96         VerifyRequiredParametersVisitor visitor = new VerifyRequiredParametersVisitor();
97
98         visitor.visitComponent(component);
99
100         verifyControls();
101     }
102
103     public void testRequiredNotBound()
104     {
105         ParameterSpecification pspec = new ParameterSpecification();
106         pspec.setParameterName("fred");
107         pspec.setRequired(true);
108
109         ComponentSpecification cspec = new ComponentSpecification();
110         cspec.addParameter(pspec);
111
112         Location l = newLocation();
113
114         MockControl control = newControl(IComponent.class);
115         IComponent component = (IComponent) control.getMock();
116
117         component.getSpecification();
118         control.setReturnValue(cspec);
119
120         component.getBinding("fred");
121         control.setReturnValue(null);
122
123         component.getExtendedId();
124         control.setReturnValue("Fred/flintstone");
125
126         component.getLocation();
127         control.setReturnValue(l);
128
129         replayControls();
130
131         VerifyRequiredParametersVisitor visitor = new VerifyRequiredParametersVisitor();
132
133         try
134         {
135             visitor.visitComponent(component);
136             unreachable();
137         }
138         catch (ApplicationRuntimeException ex)
139         {
140             assertEquals("Required parameter fred of component Fred/flintstone is not bound.", ex
141                     .getMessage());
142             assertSame(component, ex.getComponent());
143             assertSame(l, ex.getLocation());
144         }
145
146         verifyControls();
147     }
148 }
149
Popular Tags