KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > annotations > TestBeanAnnotationWorker


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.annotations;
16
17 import java.lang.reflect.Method JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.apache.hivemind.Location;
22 import org.apache.tapestry.bean.LightweightBeanInitializer;
23 import org.apache.tapestry.enhance.EnhancementOperation;
24 import org.apache.tapestry.spec.BeanLifecycle;
25 import org.apache.tapestry.spec.ComponentSpecification;
26 import org.apache.tapestry.spec.IBeanSpecification;
27 import org.apache.tapestry.spec.IComponentSpecification;
28 import org.easymock.MockControl;
29
30 /**
31  * Tests for {@link org.apache.tapestry.annotations.BeanAnnotationWorker}.
32  *
33  * @author Howard M. Lewis Ship
34  * @since 4.0
35  */

36
37 public class TestBeanAnnotationWorker extends BaseAnnotationTestCase
38 {
39     public void testBeanClassSpecified()
40     {
41         Location l = newLocation();
42         EnhancementOperation op = newOp();
43         IComponentSpecification spec = new ComponentSpecification();
44
45         Method JavaDoc m = findMethod(AnnotatedPage.class, "getMapBean");
46
47         replayControls();
48
49         new BeanAnnotationWorker().performEnhancement(op, spec, m, l);
50
51         verifyControls();
52
53         IBeanSpecification bs = spec.getBeanSpecification("mapBean");
54
55         assertEquals("mapBean", bs.getPropertyName());
56         assertEquals(HashMap JavaDoc.class.getName(), bs.getClassName());
57         assertEquals(BeanLifecycle.REQUEST, bs.getLifecycle());
58         assertSame(l, bs.getLocation());
59         assertNull(bs.getInitializers());
60     }
61
62     private EnhancementOperation newOp(String JavaDoc propertyName, Class JavaDoc propertyType)
63     {
64         MockControl opc = newControl(EnhancementOperation.class);
65         EnhancementOperation op = (EnhancementOperation) opc.getMock();
66
67         op.getPropertyType(propertyName);
68         opc.setReturnValue(propertyType);
69
70         return op;
71     }
72
73     public void testBeanClassNotSpecified()
74     {
75         Location l = newLocation();
76         EnhancementOperation op = newOp("hashMapBean", HashMap JavaDoc.class);
77         IComponentSpecification spec = new ComponentSpecification();
78
79         Method JavaDoc m = findMethod(AnnotatedPage.class, "getHashMapBean");
80
81         replayControls();
82
83         new BeanAnnotationWorker().performEnhancement(op, spec, m, l);
84
85         verifyControls();
86
87         IBeanSpecification bs = spec.getBeanSpecification("hashMapBean");
88
89         assertEquals("hashMapBean", bs.getPropertyName());
90         assertEquals(HashMap JavaDoc.class.getName(), bs.getClassName());
91         assertEquals(BeanLifecycle.REQUEST, bs.getLifecycle());
92         assertSame(l, bs.getLocation());
93         assertNull(bs.getInitializers());
94     }
95
96     public void testInitializer()
97     {
98         EnhancementOperation op = newOp("beanWithInitializer", Target.class);
99         IComponentSpecification spec = new ComponentSpecification();
100
101         Method JavaDoc m = findMethod(AnnotatedPage.class, "getBeanWithInitializer");
102
103         replayControls();
104
105         new BeanAnnotationWorker().performEnhancement(op, spec, m, null);
106
107         verifyControls();
108
109         IBeanSpecification bs = spec.getBeanSpecification("beanWithInitializer");
110
111         List JavaDoc l = bs.getInitializers();
112         LightweightBeanInitializer lbi = (LightweightBeanInitializer) l.get(0);
113
114         assertEquals("intValue=10", lbi.getPropertyName());
115     }
116
117     public void testLifecycle()
118     {
119         EnhancementOperation op = newOp();
120         IComponentSpecification spec = new ComponentSpecification();
121
122         Method JavaDoc m = findMethod(AnnotatedPage.class, "getRenderLifecycleBean");
123
124         replayControls();
125
126         new BeanAnnotationWorker().performEnhancement(op, spec, m, null);
127
128         verifyControls();
129
130         IBeanSpecification bs = spec.getBeanSpecification("renderLifecycleBean");
131
132         assertEquals(BeanLifecycle.RENDER, bs.getLifecycle());
133     }
134 }
135
Popular Tags