KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > enhance > TestInjectPageWorker


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.enhance;
16
17 import java.lang.reflect.Modifier JavaDoc;
18
19 import org.apache.hivemind.ApplicationRuntimeException;
20 import org.apache.hivemind.Location;
21 import org.apache.hivemind.service.MethodSignature;
22 import org.apache.hivemind.test.HiveMindTestCase;
23 import org.apache.tapestry.html.BasePage;
24 import org.apache.tapestry.spec.InjectSpecification;
25 import org.apache.tapestry.spec.InjectSpecificationImpl;
26 import org.easymock.MockControl;
27
28 /**
29  * Tests for {@link org.apache.tapestry.enhance.InjectPageWorker}.
30  *
31  * @author Howard Lewis Ship
32  * @since 4.0
33  */

34 public class TestInjectPageWorker extends HiveMindTestCase
35 {
36     public void testPrimitivePropertyType()
37     {
38         Location l = newLocation();
39
40         MockControl opc = newControl(EnhancementOperation.class);
41         EnhancementOperation op = (EnhancementOperation) opc.getMock();
42
43         op.getPropertyType("somePage");
44         opc.setReturnValue(int.class);
45
46         replayControls();
47
48         InjectSpecification is = new InjectSpecificationImpl();
49         is.setProperty("somePage");
50         is.setObject("SomePage");
51         is.setLocation(l);
52
53         try
54         {
55             new InjectPageWorker().performEnhancement(op, is);
56             unreachable();
57         }
58         catch (ApplicationRuntimeException ex)
59         {
60             assertEquals(
61                     "Property somePage is type int, which is not compatible with injection. The property type should be Object, IPage, or a specific page class.",
62                     ex.getMessage());
63             assertSame(l, ex.getLocation());
64         }
65
66         verifyControls();
67     }
68
69     /**
70      * Test for when there's no existing property.
71      */

72
73     public void testNoPropertyType()
74     {
75         MockControl opc = newControl(EnhancementOperation.class);
76         EnhancementOperation op = (EnhancementOperation) opc.getMock();
77
78         op.getPropertyType("somePage");
79         opc.setReturnValue(null);
80
81         op.claimProperty("somePage");
82
83         op.getAccessorMethodName("somePage");
84         opc.setReturnValue("getSomePage");
85
86         MethodSignature sig = new MethodSignature(Object JavaDoc.class, "getSomePage", null, null);
87
88         op.addMethod(
89                 Modifier.PUBLIC,
90                 sig,
91                 "return getPage().getRequestCycle().getPage(\"SomePage\");");
92
93         replayControls();
94
95         InjectSpecification is = new InjectSpecificationImpl();
96         is.setProperty("somePage");
97         is.setObject("SomePage");
98
99         new InjectPageWorker().performEnhancement(op, is);
100
101         verifyControls();
102     }
103
104     public void testExistingPropertyType()
105     {
106         MockControl opc = newControl(EnhancementOperation.class);
107         EnhancementOperation op = (EnhancementOperation) opc.getMock();
108
109         op.getPropertyType("somePage");
110         opc.setReturnValue(BasePage.class);
111
112         op.claimProperty("somePage");
113
114         op.getAccessorMethodName("somePage");
115         opc.setReturnValue("getSomePage");
116
117         MethodSignature sig = new MethodSignature(BasePage.class, "getSomePage", null, null);
118
119         // BasePage is too specific (the getPage() method returns IPage),
120
// so we see a cast.
121

122         op
123                 .addMethod(
124                         Modifier.PUBLIC,
125                         sig,
126                         "return (org.apache.tapestry.html.BasePage)getPage().getRequestCycle().getPage(\"SomePage\");");
127
128         replayControls();
129
130         InjectSpecification is = new InjectSpecificationImpl();
131         is.setProperty("somePage");
132         is.setObject("SomePage");
133
134         new InjectPageWorker().performEnhancement(op, is);
135
136         verifyControls();
137     }
138 }
139
Popular Tags