KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > jsf > DelegatingVariableResolverTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
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
17 package org.springframework.web.jsf;
18
19 import javax.faces.context.FacesContext;
20 import javax.faces.el.EvaluationException;
21 import javax.faces.el.VariableResolver;
22
23 import junit.framework.TestCase;
24
25 import org.springframework.beans.TestBean;
26 import org.springframework.web.context.WebApplicationContext;
27 import org.springframework.web.context.support.StaticWebApplicationContext;
28
29 /**
30  * @author Juergen Hoeller
31  * @since 02.08.2004
32  */

33 public class DelegatingVariableResolverTests extends TestCase {
34
35     public void testDelegatingVariableResolver() {
36         final StaticWebApplicationContext wac = new StaticWebApplicationContext();
37         wac.registerSingleton("bean1", TestBean.class, null);
38         wac.registerSingleton("var1", TestBean.class, null);
39         wac.refresh();
40         TestBean bean1 = (TestBean) wac.getBean("bean1");
41
42         // We need to override the getWebApplicationContext method here:
43
// FacesContext and ExternalContext are hard to mock.
44
DelegatingVariableResolver resolver = new DelegatingVariableResolver(new OriginalVariableResolver()) {
45             protected WebApplicationContext getWebApplicationContext(FacesContext facesContext) {
46                 return wac;
47             }
48         };
49         assertEquals("val1", resolver.resolveVariable(null, "var1"));
50         assertEquals(bean1, resolver.resolveVariable(null, "bean1"));
51     }
52
53
54     private static class OriginalVariableResolver extends VariableResolver {
55
56         public Object JavaDoc resolveVariable(FacesContext facesContext, String JavaDoc name) throws EvaluationException {
57             if ("var1".equals(name)) {
58                 return "val1";
59             }
60             return null;
61         }
62     }
63
64 }
65
Popular Tags