KickJava   Java API By Example, From Geeks To Geeks.

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


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.service.BodyBuilder;
20 import org.apache.hivemind.service.MethodSignature;
21 import org.apache.hivemind.test.HiveMindTestCase;
22 import org.apache.tapestry.coerce.ValueConverter;
23 import org.apache.tapestry.services.ComponentPropertySource;
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.InjectMetaWorker}.
30  *
31  * @author Howard M. Lewis Ship
32  * @since 4.0
33  */

34 public class TestInjectMetaWorker extends HiveMindTestCase
35 {
36     private InjectSpecification newSpec(String JavaDoc propertyName, String JavaDoc object)
37     {
38         InjectSpecificationImpl result = new InjectSpecificationImpl();
39
40         result.setProperty(propertyName);
41         result.setObject(object);
42
43         return result;
44     }
45
46     private ComponentPropertySource newSource()
47     {
48         return (ComponentPropertySource) newMock(ComponentPropertySource.class);
49     }
50
51     public void testPrimitive()
52     {
53         InjectSpecification spec = newSpec("fooBar", "foo.bar");
54
55         ComponentPropertySource source = newSource();
56
57         MockControl control = newControl(EnhancementOperation.class);
58         EnhancementOperation op = (EnhancementOperation) control.getMock();
59
60         op.getPropertyType("fooBar");
61         control.setReturnValue(int.class);
62
63         op.claimProperty("fooBar");
64
65         MethodSignature sig = new MethodSignature(int.class, "getFooBar", null, null);
66
67         op.addInjectedField(InjectMetaWorker.SOURCE_NAME, ComponentPropertySource.class, source);
68         control.setReturnValue("_source");
69
70         op.getAccessorMethodName("fooBar");
71         control.setReturnValue("getFooBar");
72
73         BodyBuilder builder = new BodyBuilder();
74         builder.begin();
75         builder.addln("java.lang.String meta = _source.getComponentProperty(this, \"foo.bar\");");
76         builder.addln("return java.lang.Integer.parseInt(meta);");
77         builder.end();
78
79         op.addMethod(Modifier.PUBLIC, sig, builder.toString());
80
81         replayControls();
82
83         InjectMetaWorker worker = new InjectMetaWorker();
84
85         worker.setSource(source);
86
87         worker.performEnhancement(op, spec);
88
89         verifyControls();
90     }
91
92     public void testCharacter()
93     {
94         InjectSpecification spec = newSpec("fooBar", "foo.bar");
95
96         ComponentPropertySource source = newSource();
97
98         MockControl control = newControl(EnhancementOperation.class);
99         EnhancementOperation op = (EnhancementOperation) control.getMock();
100
101         op.getPropertyType("fooBar");
102         control.setReturnValue(char.class);
103
104         op.claimProperty("fooBar");
105
106         MethodSignature sig = new MethodSignature(char.class, "getFooBar", null, null);
107
108         op.addInjectedField(InjectMetaWorker.SOURCE_NAME, ComponentPropertySource.class, source);
109         control.setReturnValue("_source");
110
111         op.getAccessorMethodName("fooBar");
112         control.setReturnValue("getFooBar");
113
114         BodyBuilder builder = new BodyBuilder();
115         builder.begin();
116         builder.addln("java.lang.String meta = _source.getComponentProperty(this, \"foo.bar\");");
117         builder.addln("return meta.charAt(0);");
118         builder.end();
119
120         op.addMethod(Modifier.PUBLIC, sig, builder.toString());
121
122         replayControls();
123
124         InjectMetaWorker worker = new InjectMetaWorker();
125
126         worker.setSource(source);
127
128         worker.performEnhancement(op, spec);
129
130         verifyControls();
131     }
132
133     public void testObject()
134     {
135         InjectSpecification spec = newSpec("fooBar", "foo.bar");
136
137         ComponentPropertySource source = newSource();
138         ValueConverter converter = (ValueConverter) newMock(ValueConverter.class);
139
140         MockControl control = newControl(EnhancementOperation.class);
141         EnhancementOperation op = (EnhancementOperation) control.getMock();
142
143         op.getPropertyType("fooBar");
144         control.setReturnValue(Object JavaDoc.class);
145
146         op.claimProperty("fooBar");
147
148         MethodSignature sig = new MethodSignature(Object JavaDoc.class, "getFooBar", null, null);
149
150         op.addInjectedField(InjectMetaWorker.SOURCE_NAME, ComponentPropertySource.class, source);
151         control.setReturnValue("_source");
152
153         op.getAccessorMethodName("fooBar");
154         control.setReturnValue("getFooBar");
155
156         op.addInjectedField("_$valueConverter", ValueConverter.class, converter);
157         control.setReturnValue("vc");
158
159         op.getClassReference(Object JavaDoc.class);
160         control.setReturnValue("_$Object");
161
162         BodyBuilder builder = new BodyBuilder();
163         builder.begin();
164         builder.addln("java.lang.String meta = _source.getComponentProperty(this, \"foo.bar\");");
165         builder.addln("return (java.lang.Object) vc.coerceValue(meta, _$Object);");
166         builder.end();
167
168         op.addMethod(Modifier.PUBLIC, sig, builder.toString());
169
170         replayControls();
171
172         InjectMetaWorker worker = new InjectMetaWorker();
173
174         worker.setSource(source);
175         worker.setValueConverter(converter);
176
177         worker.performEnhancement(op, spec);
178
179         verifyControls();
180     }
181
182 }
183
Popular Tags