KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
19
20 import org.apache.hivemind.Location;
21 import org.apache.hivemind.service.BodyBuilder;
22 import org.apache.hivemind.service.MethodSignature;
23 import org.apache.hivemind.test.HiveMindTestCase;
24 import org.apache.tapestry.engine.state.ApplicationStateManager;
25 import org.apache.tapestry.event.PageDetachListener;
26 import org.apache.tapestry.spec.InjectSpecification;
27 import org.apache.tapestry.spec.InjectSpecificationImpl;
28 import org.easymock.MockControl;
29
30 /**
31  * Tests for {@link org.apache.tapestry.enhance.InjectStateWorker}.
32  *
33  * @author Howard M. Lewis Ship
34  * @since 4.0
35  */

36 public class TestInjectStateWorker extends HiveMindTestCase
37 {
38     private ApplicationStateManager newASM()
39     {
40         return (ApplicationStateManager) newMock(ApplicationStateManager.class);
41     }
42
43     private InjectSpecification newSpec(String JavaDoc propertyName, String JavaDoc objectName, Location l)
44     {
45         InjectSpecification spec = new InjectSpecificationImpl();
46
47         spec.setProperty(propertyName);
48         spec.setObject(objectName);
49         spec.setLocation(l);
50
51         return spec;
52     }
53
54     public void testSuccess()
55     {
56         InjectSpecification spec = newSpec("fred", "barney", null);
57         MockControl opc = newControl(EnhancementOperation.class);
58         EnhancementOperation op = (EnhancementOperation) opc.getMock();
59
60         ApplicationStateManager asm = newASM();
61
62         op.getPropertyType("fred");
63         opc.setReturnValue(Map JavaDoc.class);
64
65         op.claimProperty("fred");
66         op.addField("_$fred", Map JavaDoc.class);
67
68         op.addInjectedField("_$applicationStateManager", ApplicationStateManager.class, asm);
69         opc.setReturnValue("_$applicationStateManager");
70
71         op.getAccessorMethodName("fred");
72         opc.setReturnValue("getFred");
73
74         BodyBuilder builder = new BodyBuilder();
75
76         // Accessor
77

78         builder.begin();
79         builder.addln("if (_$fred == null)");
80         builder.addln(" _$fred = (java.util.Map) _$applicationStateManager.get(\"barney\");");
81         builder.addln("return _$fred;");
82         builder.end();
83
84         MethodSignature sig = new MethodSignature(Map JavaDoc.class, "getFred", null, null);
85
86         op.addMethod(Modifier.PUBLIC, sig, builder.toString());
87
88         builder.clear();
89         builder.begin();
90         builder.addln("_$applicationStateManager.store(\"barney\", $1);");
91         builder.addln("_$fred = $1;");
92         builder.end();
93
94         sig = new MethodSignature(void.class, "setFred", new Class JavaDoc[]
95         { Map JavaDoc.class }, null);
96
97         op.addMethod(Modifier.PUBLIC, sig, builder.toString());
98         op.extendMethodImplementation(
99                 PageDetachListener.class,
100                 EnhanceUtils.PAGE_DETACHED_SIGNATURE,
101                 "_$fred = null;");
102
103         replayControls();
104
105         InjectStateWorker w = new InjectStateWorker();
106         w.setApplicationStateManager(asm);
107
108         w.performEnhancement(op, spec);
109
110         verifyControls();
111     }
112 }
Popular Tags