KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > services > impl > TestEngineFactory


1 // Copyright 2004, 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.services.impl;
16
17 import java.util.Locale JavaDoc;
18
19 import org.apache.hivemind.ApplicationRuntimeException;
20 import org.apache.hivemind.impl.DefaultClassResolver;
21 import org.apache.hivemind.test.HiveMindTestCase;
22 import org.apache.tapestry.IEngine;
23 import org.apache.tapestry.engine.AbstractEngine;
24 import org.apache.tapestry.engine.BaseEngine;
25 import org.apache.tapestry.spec.IApplicationSpecification;
26 import org.easymock.MockControl;
27
28 /**
29  * Tests for {@link org.apache.tapestry.services.impl.EngineFactoryImpl}.
30  *
31  * @author Howard Lewis Ship
32  * @since 4.0
33  */

34 public class TestEngineFactory extends HiveMindTestCase
35 {
36     public void testUseDefault()
37     {
38         MockControl specControl = newControl(IApplicationSpecification.class);
39         IApplicationSpecification spec = (IApplicationSpecification) specControl.getMock();
40
41         // Training
42

43         spec.getEngineClassName();
44         specControl.setReturnValue(null);
45
46         EngineFactoryImpl f = new EngineFactoryImpl();
47
48         f.setApplicationSpecification(spec);
49         f.setClassResolver(new DefaultClassResolver());
50         f.setDefaultEngineClassName(BaseEngine.class.getName());
51
52         replayControls();
53
54         f.initializeService();
55
56         IEngine result = f.constructNewEngineInstance(Locale.CANADA_FRENCH);
57
58         assertTrue(result instanceof BaseEngine);
59         assertEquals(Locale.CANADA_FRENCH, result.getLocale());
60
61         verifyControls();
62     }
63
64     public void testDefinedInSpec()
65     {
66         MockControl specControl = newControl(IApplicationSpecification.class);
67         IApplicationSpecification spec = (IApplicationSpecification) specControl.getMock();
68
69         // Training
70

71         spec.getEngineClassName();
72         specControl.setReturnValue(EngineFixture.class.getName());
73
74         EngineFactoryImpl f = new EngineFactoryImpl();
75
76         f.setApplicationSpecification(spec);
77         f.setClassResolver(new DefaultClassResolver());
78
79         replayControls();
80
81         f.initializeService();
82
83         IEngine result = f.constructNewEngineInstance(Locale.CHINESE);
84
85         assertTrue(result instanceof EngineFixture);
86         assertEquals(Locale.CHINESE, result.getLocale());
87
88         verifyControls();
89     }
90
91     public void testUnableToInstantiate()
92     {
93         MockControl specControl = newControl(IApplicationSpecification.class);
94         IApplicationSpecification spec = (IApplicationSpecification) specControl.getMock();
95
96         // Training
97

98         spec.getEngineClassName();
99         specControl.setReturnValue(AbstractEngine.class.getName());
100
101         EngineFactoryImpl f = new EngineFactoryImpl();
102
103         f.setApplicationSpecification(spec);
104         f.setClassResolver(new DefaultClassResolver());
105
106         replayControls();
107
108         f.initializeService();
109
110         try
111         {
112             f.constructNewEngineInstance(Locale.CHINESE);
113             unreachable();
114         }
115         catch (ApplicationRuntimeException ex)
116         {
117             assertExceptionSubstring(
118                     ex,
119                     "Unable to instantiate engine as instance of class org.apache.tapestry.engine.AbstractEngine");
120         }
121
122         verifyControls();
123     }
124
125     public void testInvalidClass()
126     {
127         MockControl specControl = newControl(IApplicationSpecification.class);
128         IApplicationSpecification spec = (IApplicationSpecification) specControl.getMock();
129
130         // Training
131

132         spec.getEngineClassName();
133         specControl.setReturnValue("foo.XyzzYx");
134
135         EngineFactoryImpl f = new EngineFactoryImpl();
136
137         f.setApplicationSpecification(spec);
138         f.setClassResolver(new DefaultClassResolver());
139
140         replayControls();
141
142         try
143         {
144             f.initializeService();
145             unreachable();
146         }
147         catch (ApplicationRuntimeException ex)
148         {
149             assertExceptionSubstring(ex, "Could not load class foo.XyzzYx");
150         }
151
152         verifyControls();
153     }
154 }
Popular Tags