KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > config > ConfigurationCactusTest


1 /*
2  * Copyright 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.config;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 import javax.faces.FactoryFinder;
22 import javax.faces.application.ApplicationFactory;
23 import javax.faces.application.NavigationHandler;
24 import javax.faces.application.StateManager;
25 import javax.faces.application.ViewHandler;
26 import javax.faces.context.ExternalContext;
27 import javax.faces.el.PropertyResolver;
28 import javax.faces.el.VariableResolver;
29 import javax.faces.event.PhaseListener;
30 import javax.faces.lifecycle.Lifecycle;
31 import javax.faces.lifecycle.LifecycleFactory;
32 import javax.faces.render.RenderKit;
33 import javax.faces.render.RenderKitFactory;
34 import javax.faces.render.Renderer;
35 import javax.faces.webapp.FacesServlet;
36 import javax.servlet.ServletContext JavaDoc;
37
38 import org.apache.myfaces.application.NavigationHandlerImpl;
39 import org.apache.myfaces.application.jsp.JspStateManagerImpl;
40 import org.apache.myfaces.application.jsp.JspViewHandlerImpl;
41 import org.apache.myfaces.cactus.MyFacesServletTestCase;
42 import org.apache.myfaces.config.element.ManagedBean;
43 import org.apache.myfaces.config.element.NavigationCase;
44 import org.apache.myfaces.config.element.NavigationRule;
45 import org.apache.myfaces.el.PropertyResolverImpl;
46 import org.apache.myfaces.el.VariableResolverImpl;
47 import org.apache.myfaces.lifecycle.LifecycleFactoryImpl;
48 import org.apache.myfaces.renderkit.html.util.AutoScrollPhaseListener;
49 import org.apache.myfaces.component.html.ext.HtmlDataTablePhaseListener;
50
51
52 public class ConfigurationCactusTest extends MyFacesServletTestCase
53 {
54
55     protected void setUp() throws Exception JavaDoc
56     {
57         super.setUp();
58     }
59
60
61     protected void tearDown() throws Exception JavaDoc
62     {
63         super.tearDown();
64     }
65
66
67     public ConfigurationCactusTest(String JavaDoc name)
68     {
69         super(name);
70     }
71
72
73     public void testConfiguration() throws Exception JavaDoc
74     {
75         ExternalContext externalContext = getContext().getExternalContext();
76
77         // test decorator pattern support
78

79         // application factory
80
ApplicationFactory applicationFactory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
81         assertEquals(TestApplicationFactory.class, applicationFactory.getClass());
82
83         javax.faces.application.Application application = applicationFactory.getApplication();
84
85         assertEquals("messageBundleA", application.getMessageBundle());
86
87         LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
88         assertEquals(TestLifecycleFactory.class, lifecycleFactory.getClass());
89
90         LifecycleFactory lifecycleFactoryDelegate = ((TestLifecycleFactory) lifecycleFactory).getDelegate();
91
92         assertNotNull(lifecycleFactoryDelegate);
93         assertEquals(LifecycleFactoryImpl.class, lifecycleFactoryDelegate.getClass());
94
95         // view handler
96
ViewHandler viewHandler = application.getViewHandler();
97
98         assertEquals(TestViewHandlerA.class, viewHandler.getClass());
99         assertEquals(TestViewHandlerB.class, ((TestViewHandlerA) viewHandler).getDelegate().getClass());
100         assertEquals(JspViewHandlerImpl.class, ((TestViewHandlerB) ((TestViewHandlerA) viewHandler).getDelegate()).getDelegate().getClass());
101
102         // navigation handler
103
NavigationHandler navigationHandler = application.getNavigationHandler();
104
105         assertEquals(TestNavigationHandler.class, navigationHandler.getClass());
106         assertEquals(NavigationHandlerImpl.class, ((TestNavigationHandler) navigationHandler).getDelegate().getClass());
107
108         // state manager
109
StateManager stateManager = application.getStateManager();
110
111         assertEquals(TestStateManager.class, stateManager.getClass());
112         assertEquals(JspStateManagerImpl.class, ((TestStateManager) stateManager).getDelegate().getClass());
113
114         // PropvertyResolver
115
PropertyResolver propertyResolver = application.getPropertyResolver();
116
117         assertEquals(TestPropertyResolver.class, propertyResolver.getClass());
118         assertEquals(PropertyResolverImpl.class, ((TestPropertyResolver) propertyResolver).getDelegate().getClass());
119
120         // VariableResolver
121
VariableResolver variableResolver = application.getVariableResolver();
122
123         assertEquals(TestVariableResolver.class, variableResolver.getClass());
124         assertEquals(VariableResolverImpl.class, ((TestVariableResolver) variableResolver).getDelegate().getClass());
125
126
127         // RuntimeConfig
128

129         RuntimeConfig runtimeConfig = RuntimeConfig.getCurrentInstance(externalContext);
130
131         ManagedBean bean = runtimeConfig.getManagedBean("testMap");
132         assertNotNull(bean);
133         assertNull(runtimeConfig.getManagedBean("doesNotExist"));
134
135         // Navigation rules
136
List JavaDoc navigationRules = new ArrayList JavaDoc(runtimeConfig.getNavigationRules());
137
138         assertEquals(1, navigationRules.size());
139         NavigationRule navRule = (NavigationRule) navigationRules.get(0);
140         NavigationCase navCase = (NavigationCase) navRule.getNavigationCases().iterator().next();
141         assertTrue(navCase.isRedirect());
142         assertEquals("Foo", navCase.getFromOutcome());
143         assertEquals("/Foo.jsp", navCase.getToViewId());
144
145         // Lifecycle config
146
ServletContext JavaDoc context = (ServletContext JavaDoc) externalContext.getContext();
147         String JavaDoc lifecycleId = context.getInitParameter(FacesServlet.LIFECYCLE_ID_ATTR);
148
149         if (lifecycleId == null)
150         {
151             lifecycleId = LifecycleFactory.DEFAULT_LIFECYCLE;
152         }
153
154         Lifecycle lifecycle = (Lifecycle) lifecycleFactory.getLifecycle(lifecycleId);
155
156         javax.faces.event.PhaseListener[] phaseListeners = lifecycle.getPhaseListeners();
157
158         assertEquals(4, phaseListeners.length);
159
160         for (int i = 0; i < phaseListeners.length; i++)
161         {
162             PhaseListener listener = phaseListeners[i];
163
164             assertTrue(listener instanceof TestPhaseListenerA || listener instanceof TestPhaseListenerB || listener instanceof AutoScrollPhaseListener || listener instanceof HtmlDataTablePhaseListener);
165         }
166     }
167
168
169     public void testRenderKit()
170     {
171         RenderKitFactory renderKitFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
172         RenderKit basicHtml = renderKitFactory.getRenderKit(getContext(), RenderKitFactory.HTML_BASIC_RENDER_KIT);
173
174         Renderer renderer = basicHtml.getRenderer("javax.faces.Command", "javax.faces.Button");
175         assertEquals(org.apache.myfaces.renderkit.html.HtmlButtonRenderer.class, renderer.getClass());
176
177         RenderKit testRenderKit = renderKitFactory.getRenderKit(getContext(), "TEST_RENDER_KIT");
178
179         assertNull(basicHtml.getRenderer("test.Command", "test.Button"));
180         renderer = testRenderKit.getRenderer("test.Command", "test.Button");
181         assertEquals(org.apache.myfaces.renderkit.html.HtmlButtonRenderer.class, renderer.getClass());
182
183     }
184 }
185
Popular Tags