KickJava   Java API By Example, From Geeks To Geeks.

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


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.ApplicationRuntimeException;
20 import org.apache.hivemind.ErrorLog;
21 import org.apache.hivemind.Messages;
22 import org.apache.hivemind.service.BodyBuilder;
23 import org.apache.hivemind.test.HiveMindTestCase;
24 import org.apache.tapestry.BaseComponent;
25 import org.apache.tapestry.services.ComponentMessagesSource;
26 import org.apache.tapestry.spec.IComponentSpecification;
27 import org.easymock.MockControl;
28
29 /**
30  * Tests for {@link org.apache.tapestry.enhance.InjectMessagesWorker}.
31  *
32  * @author Howard M. Lewis Ship
33  * @since 4.0
34  */

35 public class TestInjectMessagesWorker extends HiveMindTestCase
36 {
37     private IComponentSpecification newSpec()
38     {
39         return (IComponentSpecification) newMock(IComponentSpecification.class);
40     }
41
42     private ComponentMessagesSource newSource()
43     {
44         return (ComponentMessagesSource) newMock(ComponentMessagesSource.class);
45     }
46
47     public void testSuccess()
48     {
49
50         InjectMessagesWorker w = new InjectMessagesWorker();
51
52         IComponentSpecification spec = newSpec();
53         ComponentMessagesSource source = newSource();
54
55         MockControl control = newControl(EnhancementOperation.class);
56         EnhancementOperation op = (EnhancementOperation) control.getMock();
57
58         op.claimProperty(w.MESSAGES_PROPERTY);
59         op.addInjectedField("_$componentMessagesSource", ComponentMessagesSource.class, source);
60         control.setReturnValue("fred");
61
62         BodyBuilder builder = new BodyBuilder();
63         builder.begin();
64         builder.addln("if (_$messages == null)");
65         builder.addln(" _$messages = fred.getMessages(this);");
66         builder.addln("return _$messages;");
67         builder.end();
68
69         op.addField("_$messages", Messages.class);
70         op.addMethod(Modifier.PUBLIC, w.METHOD_SIGNATURE, builder.toString());
71
72         replayControls();
73
74         w.setComponentMessagesSource(source);
75
76         w.performEnhancement(op, spec);
77
78         verifyControls();
79     }
80
81     public void testFailure()
82     {
83
84         InjectMessagesWorker w = new InjectMessagesWorker();
85
86         Throwable JavaDoc ex = new ApplicationRuntimeException(EnhanceMessages
87                 .claimedProperty(w.MESSAGES_PROPERTY));
88
89         IComponentSpecification spec = newSpec();
90
91         MockControl control = newControl(EnhancementOperation.class);
92         EnhancementOperation op = (EnhancementOperation) control.getMock();
93
94         op.claimProperty(w.MESSAGES_PROPERTY);
95         control.setThrowable(ex);
96
97         op.getBaseClass();
98         control.setReturnValue(BaseComponent.class);
99
100         ErrorLog log = (ErrorLog) newMock(ErrorLog.class);
101
102         log.error(
103                 EnhanceMessages.errorAddingProperty(w.MESSAGES_PROPERTY, BaseComponent.class, ex),
104                 null,
105                 ex);
106
107         replayControls();
108
109         w.setErrorLog(log);
110
111         w.performEnhancement(op, spec);
112
113         verifyControls();
114     }
115 }
Popular Tags