KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright 2004, 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.util.Map JavaDoc;
18
19 import org.apache.hivemind.ApplicationRuntimeException;
20 import org.apache.hivemind.test.HiveMindTestCase;
21 import org.apache.tapestry.IComponent;
22 import org.apache.tapestry.IRender;
23 import org.easymock.MockControl;
24
25 /**
26  * @author Howard M. Lewis Ship
27  * @since 4.0
28  */

29 public class TestEnhanceUtils extends HiveMindTestCase
30 {
31     public void testTypeUnspecifiedWithNoExistingProperty()
32     {
33         MockControl opc = newControl(EnhancementOperation.class);
34         EnhancementOperation op = (EnhancementOperation) opc.getMock();
35
36         op.getPropertyType("wilma");
37         opc.setReturnValue(null);
38
39         replayControls();
40
41         Class JavaDoc result = EnhanceUtils.extractPropertyType(op, "wilma", null);
42
43         assertEquals(Object JavaDoc.class, result);
44
45         verifyControls();
46     }
47
48     public void testTypeUnspecifiedButExistingProperty()
49     {
50         MockControl opc = newControl(EnhancementOperation.class);
51         EnhancementOperation op = (EnhancementOperation) opc.getMock();
52
53         op.getPropertyType("fred");
54         opc.setReturnValue(Map JavaDoc.class);
55
56         replayControls();
57
58         Class JavaDoc result = EnhanceUtils.extractPropertyType(op, "fred", null);
59
60         assertEquals(Map JavaDoc.class, result);
61
62         verifyControls();
63     }
64
65     public void testTypeSpecified()
66     {
67         MockControl opc = newControl(EnhancementOperation.class);
68         EnhancementOperation op = (EnhancementOperation) opc.getMock();
69
70         op.convertTypeName("int[]");
71         opc.setReturnValue(int[].class);
72
73         op.validateProperty("betty", int[].class);
74
75         replayControls();
76
77         Class JavaDoc result = EnhanceUtils.extractPropertyType(op, "betty", "int[]");
78
79         assertEquals(int[].class, result);
80
81         verifyControls();
82     }
83
84     public void testCreateUnwrapForPrimitive()
85     {
86         EnhancementOperation op = (EnhancementOperation) newMock(EnhancementOperation.class);
87
88         replayControls();
89
90         String JavaDoc result = EnhanceUtils.createUnwrapExpression(op, "mybinding", int.class);
91
92         assertEquals("org.apache.tapestry.enhance.EnhanceUtils.toInt(mybinding)", result);
93
94         verifyControls();
95     }
96
97     public void testCreateUnwrapForObjectType()
98     {
99         MockControl opc = newControl(EnhancementOperation.class);
100         EnhancementOperation op = (EnhancementOperation) opc.getMock();
101
102         op.getClassReference(String JavaDoc.class);
103         opc.setReturnValue("_$class$String");
104
105         replayControls();
106
107         String JavaDoc result = EnhanceUtils.createUnwrapExpression(op, "thebinding", String JavaDoc.class);
108
109         assertEquals("(java.lang.String) thebinding.getObject(_$class$String)", result);
110
111         verifyControls();
112     }
113
114     public void testVerifyPropertyTypeNoProperty()
115     {
116         MockControl opc = newControl(EnhancementOperation.class);
117         EnhancementOperation op = (EnhancementOperation) opc.getMock();
118
119         op.getPropertyType("foo");
120         opc.setReturnValue(null);
121
122         replayControls();
123
124         assertEquals(Object JavaDoc.class, EnhanceUtils.verifyPropertyType(op, "foo", String JavaDoc.class));
125
126         verifyControls();
127     }
128
129     public void testVerifyPropertyTypeSuccess()
130     {
131         MockControl opc = newControl(EnhancementOperation.class);
132         EnhancementOperation op = (EnhancementOperation) opc.getMock();
133
134         op.getPropertyType("foo");
135         opc.setReturnValue(Object JavaDoc.class);
136
137         replayControls();
138
139         assertEquals(Object JavaDoc.class, EnhanceUtils.verifyPropertyType(op, "foo", String JavaDoc.class));
140
141         verifyControls();
142     }
143
144     public void testVerifyPropertyTypeWithDeclaredPropertyType()
145     {
146         MockControl opc = newControl(EnhancementOperation.class);
147         EnhancementOperation op = (EnhancementOperation) opc.getMock();
148
149         op.getPropertyType("foo");
150         opc.setReturnValue(IRender.class);
151
152         replayControls();
153
154         assertEquals(IRender.class, EnhanceUtils.verifyPropertyType(op, "foo", IComponent.class));
155
156         verifyControls();
157
158     }
159
160     public void testVerifyPropertyTypeFailure()
161     {
162         MockControl opc = newControl(EnhancementOperation.class);
163         EnhancementOperation op = (EnhancementOperation) opc.getMock();
164
165         op.getPropertyType("foo");
166         opc.setReturnValue(String JavaDoc.class);
167
168         replayControls();
169
170         try
171         {
172             EnhanceUtils.verifyPropertyType(op, "foo", IComponent.class);
173         }
174         catch (ApplicationRuntimeException ex)
175         {
176             assertEquals(
177                     "Property foo is type java.lang.String, which is not compatible with org.apache.tapestry.IComponent.",
178                     ex.getMessage());
179         }
180
181         verifyControls();
182
183     }
184
185 }
Popular Tags