1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.application;
20
21 import static org.apache.myfaces.test.AssertThrowables.assertThrowable;
22 import static org.easymock.EasyMock.eq;
23 import static org.easymock.EasyMock.expect;
24 import static org.easymock.EasyMock.isA;
25 import static org.easymock.EasyMock.isNull;
26 import static org.easymock.classextension.EasyMock.createMock;
27 import static org.easymock.classextension.EasyMock.replay;
28
29 import java.util.ListResourceBundle;
30 import java.util.Locale;
31 import java.util.ResourceBundle;
32
33 import javax.el.ELContext;
34 import javax.el.ELResolver;
35 import javax.el.ValueExpression;
36 import javax.faces.FacesException;
37 import javax.faces.application.Application;
38 import javax.faces.component.UIComponent;
39 import javax.faces.component.UIOutput;
40 import javax.faces.component.UIViewRoot;
41 import javax.faces.context.FacesContext;
42 import javax.faces.convert.Converter;
43 import javax.faces.convert.ConverterException;
44 import javax.faces.convert.EnumConverter;
45 import javax.faces.el.ReferenceSyntaxException;
46 import javax.faces.el.VariableResolver;
47
48 import junit.framework.TestCase;
49
50 import org.apache.myfaces.config.RuntimeConfig;
51 import org.apache.myfaces.test.TestRunnable;
52 import org.apache.myfaces.test.el.MockELContext;
53 import org.apache.myfaces.test.mock.MockFacesContext12;
54 import org.easymock.classextension.EasyMock;
55 import org.easymock.classextension.IMocksControl;
56
57
58
59
60
61 public class ApplicationImplTest extends TestCase
62 {
63
64
65
66 private ApplicationImpl app;
67 private MockFacesContext12 context;
68
69 protected void setUp() throws Exception
70 {
71 app = new ApplicationImpl(new RuntimeConfig());
72 context = new MockFacesContext12();
73 }
74
75 public void testCreateMethodBinding() throws Exception
76 {
77 assertThrowable(ReferenceSyntaxException.class, new TestRunnable() {
78 public void run() throws Throwable
79 {
80 app.createMethodBinding("xxx", null);
81 }
82 });
83 }
84
85
86
87
88
89 public void testGetResourceBundleNPE()
90 {
91 assertThrowable(NullPointerException.class, new TestRunnable()
92 {
93 public void run()
94 {
95 app.getResourceBundle(null, "xxx");
96 }
97 });
98 assertThrowable(NullPointerException.class, new TestRunnable()
99 {
100 public void run()
101 {
102 app.getResourceBundle(context, null);
103 }
104 });
105 }
106
107
108
109
110
111
112
113
114 public void testGetResourceBundleFacesException()
115 {
116 final ApplicationImpl myApp = new ApplicationImpl(new RuntimeConfig())
117 {
118 @Override
119 String getBundleName(FacesContext facesContext, String name)
120 {
121 return "bundleName";
122 }
123 };
124 assertThrowable(FacesException.class, new TestRunnable()
125 {
126 public void run()
127 {
128 myApp.getResourceBundle(context, "xxx");
129 }
130 });
131 }
132
133
134
135
136
137 public void testGetResourceBundleWithDefaultLocale()
138 {
139 assertGetResourceBundleWithLocale(Locale.getDefault());
140 }
141
142
143
144
145
146 public void testGetResourceBundleWithUIViewRootLocale()
147 {
148 Locale locale = new Locale("xx");
149 UIViewRoot viewRoot = new UIViewRoot();
150 context.setViewRoot(viewRoot);
151 viewRoot.setLocale(locale);
152 assertGetResourceBundleWithLocale(locale);
153 }
154
155 public void testCreateComponentCallSetValueOnExpressionIfValueNull() throws Exception
156 {
157 ValueExpression expr = createMock(ValueExpression.class);
158 FacesContext context = createMock(FacesContext.class);
159 ELContext elcontext = createMock(ELContext.class);
160 expect(context.getELContext()).andReturn(elcontext);
161 expect(expr.getValue(elcontext)).andReturn(null);
162 expr.setValue(eq(elcontext), isA(UIOutput.class));
163 app.addComponent("testComponent", UIOutput.class.getName());
164 replay(context);
165 replay(expr);
166 assertTrue(UIOutput.class.isAssignableFrom(app.createComponent(expr, context, "testComponent").getClass()));
167 }
168
169 public void testCreateComponentExpressionFacesExceptionTest() throws Exception
170 {
171 ValueExpression expr = createMock(ValueExpression.class);
172 FacesContext context = createMock(FacesContext.class);
173 ELContext elcontext = createMock(ELContext.class);
174 expect(context.getELContext()).andReturn(elcontext);
175 expect(expr.getValue(elcontext)).andThrow(new IllegalArgumentException());
176 replay(context);
177 replay(expr);
178 try
179 {
180 app.createComponent(expr, context, "testComponent");
181 }
182 catch (FacesException e)
183 {
184
185 }
186 catch (Throwable e)
187 {
188 fail("FacesException expected: " + e.getMessage());
189 }
190 }
191
192 public void testGetVariableResolver() throws Exception
193 {
194 VariableResolver variableResolver = app.getVariableResolver();
195 assertNotNull(variableResolver);
196 IMocksControl mocksControl = EasyMock.createControl();
197 Application mockApp = mocksControl.createMock(Application.class);
198 context.setApplication(mockApp);
199 ELResolver elResolver = mocksControl.createMock(ELResolver.class);
200 expect(mockApp.getELResolver()).andReturn(elResolver);
201 context.setELContext(new MockELContext());
202 expect(elResolver.getValue(eq(context.getELContext()), isNull(), eq("xxx"))).andReturn("testValue");
203 mocksControl.replay();
204 assertEquals("testValue", variableResolver.resolveVariable(context, "xxx"));
205 }
206
207 private void assertGetResourceBundleWithLocale(final Locale expectedLocale)
208 {
209 final String var = "test";
210 final String bundleName = "bundleName";
211 final ResourceBundle bundle = new ListResourceBundle()
212 {
213 @Override
214 protected Object[][] getContents()
215 {
216 return null;
217 }
218 };
219 ApplicationImpl myapp = new ApplicationImpl(new RuntimeConfig())
220 {
221 @Override
222 String getBundleName(FacesContext facesContext, String name)
223 {
224 assertEquals(var, name);
225 return bundleName;
226 }
227
228 @Override
229 ResourceBundle getResourceBundle(String name, Locale locale, ClassLoader loader)
230 {
231 assertEquals(Thread.currentThread().getContextClassLoader(), loader);
232 assertEquals(bundleName, name);
233 assertEquals(expectedLocale, locale);
234 return bundle;
235 }
236 };
237 assertSame(bundle, myapp.getResourceBundle(context, var));
238 }
239
240 private enum MyEnum {VALUE1, VALUE2};
241
242
243
244
245
246 public void testCreateEnumConverter() throws Exception
247 {
248 app.addConverter(Enum.class, EnumConverter.class.getName());
249
250 Converter converter = app.createConverter(MyEnum.class);
251 assertNotNull(converter);
252 assertEquals(converter.getClass(), EnumConverter.class);
253 }
254
255
256 private interface EnumCoded { public int getCode(); }
257 private enum AnotherEnum implements EnumCoded {
258 VALUE1, VALUE2;
259 public int getCode() {return 0;}
260 };
261
262 public static class EnumCodedTestConverter implements Converter
263 {
264
265 public EnumCodedTestConverter()
266 {
267 }
268
269 public Object getAsObject(FacesContext context, UIComponent component,
270 String value) throws ConverterException
271 {
272 return null;
273 }
274
275 public String getAsString(FacesContext context, UIComponent component,
276 Object value) throws ConverterException
277 {
278 return null;
279 }
280 }
281
282
283
284
285
286
287
288
289
290
291 public void testCreateConverterForInterface() throws Exception
292 {
293 app.addConverter(Enum.class, EnumConverter.class.getName());
294 app.addConverter(EnumCoded.class, EnumCodedTestConverter.class.getName());
295
296 Converter converter = app.createConverter(AnotherEnum.class);
297 assertNotNull(converter);
298 assertEquals(converter.getClass(), EnumCodedTestConverter.class);
299 }
300 }