KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ArrayList JavaDoc;
19 import java.util.Collections JavaDoc;
20
21 import org.apache.hivemind.ApplicationRuntimeException;
22 import org.apache.hivemind.ErrorLog;
23 import org.apache.hivemind.Location;
24 import org.apache.hivemind.service.MethodSignature;
25 import org.apache.hivemind.test.HiveMindTestCase;
26 import org.apache.tapestry.BaseComponent;
27 import org.apache.tapestry.spec.BeanSpecification;
28 import org.apache.tapestry.spec.IBeanSpecification;
29 import org.apache.tapestry.spec.IComponentSpecification;
30 import org.easymock.MockControl;
31
32 /**
33  * Tests for {@link org.apache.tapestry.enhance.InjectBeanWorker}.
34  *
35  * @author Howard M. Lewis Ship
36  * @since 4.0
37  */

38 public class TestInjectBeanWorker extends HiveMindTestCase
39 {
40     private IComponentSpecification newSpec(String JavaDoc beanName, String JavaDoc propertyName, Location location)
41     {
42         IBeanSpecification bs = new BeanSpecification();
43         bs.setPropertyName(propertyName);
44         bs.setLocation(location);
45
46         MockControl control = newControl(IComponentSpecification.class);
47         IComponentSpecification spec = (IComponentSpecification) control.getMock();
48
49         spec.getBeanNames();
50         control.setReturnValue(Collections.singletonList(beanName));
51
52         spec.getBeanSpecification(beanName);
53         control.setReturnValue(bs);
54
55         return spec;
56     }
57
58     public void testNoWork()
59     {
60         IComponentSpecification spec = newSpec("fred", null, null);
61         EnhancementOperation op = (EnhancementOperation) newMock(EnhancementOperation.class);
62
63         replayControls();
64
65         new InjectBeanWorker().performEnhancement(op, spec);
66
67         verifyControls();
68     }
69
70     public void testSuccess()
71     {
72         IComponentSpecification spec = newSpec("fred", "barney", null);
73         MockControl control = newControl(EnhancementOperation.class);
74         EnhancementOperation op = (EnhancementOperation) control.getMock();
75
76         op.claimProperty("barney");
77
78         op.getPropertyType("barney");
79         control.setReturnValue(ArrayList JavaDoc.class);
80
81         op.getAccessorMethodName("barney");
82         control.setReturnValue("getBarney");
83
84         op.addMethod(
85                 Modifier.PUBLIC,
86                 new MethodSignature(ArrayList JavaDoc.class, "getBarney", null, null),
87                 "return (java.util.ArrayList) getBeans().getBean(\"fred\");");
88
89         replayControls();
90         
91         new InjectBeanWorker().performEnhancement(op, spec);
92         
93         verifyControls();
94     }
95
96     public void testFailure()
97     {
98         Location l = fabricateLocation(99);
99         Throwable JavaDoc ex = new ApplicationRuntimeException(EnhanceMessages.claimedProperty("barney"));
100
101         MockControl control = newControl(EnhancementOperation.class);
102         EnhancementOperation op = (EnhancementOperation) control.getMock();
103
104         IComponentSpecification spec = newSpec("fred", "barney", l);
105
106         ErrorLog log = (ErrorLog) newMock(ErrorLog.class);
107
108         op.claimProperty("barney");
109         control.setThrowable(ex);
110
111         op.getBaseClass();
112         control.setReturnValue(BaseComponent.class);
113
114         log.error(EnhanceMessages.errorAddingProperty("barney", BaseComponent.class, ex), l, ex);
115
116         replayControls();
117
118         InjectBeanWorker w = new InjectBeanWorker();
119
120         w.setErrorLog(log);
121
122         w.performEnhancement(op, spec);
123
124         verifyControls();
125     }
126
127 }
Popular Tags