1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package javax.faces.application;
20
21 import static org.easymock.EasyMock.*;
22
23 import java.io.IOException;
24 import java.io.UnsupportedEncodingException;
25 import java.lang.reflect.Method;
26 import java.util.Collections;
27 import java.util.Locale;
28 import java.util.Map;
29
30 import javax.faces.FacesException;
31 import javax.faces.component.UIViewRoot;
32 import javax.faces.context.ExternalContext;
33 import javax.faces.context.FacesContext;
34
35 import junit.framework.TestCase;
36
37 import org.apache.myfaces.Assert;
38 import org.apache.myfaces.TestRunner;
39 import org.apache.shale.test.mock.MockFacesContext12;
40 import org.easymock.classextension.EasyMock;
41 import org.easymock.classextension.IMocksControl;
42
43
44
45
46
47 public class ViewHandlerTest extends TestCase
48 {
49 private MockFacesContext12 _facesContext;
50 private IMocksControl _mocksControl;
51 private ExternalContext _externalContext;
52 private TestViewHandler _testimpl;
53
54 protected void setUp() throws Exception
55 {
56 _mocksControl = EasyMock.createControl();
57 _externalContext = _mocksControl.createMock(ExternalContext.class);
58 _facesContext = new MockFacesContext12(_externalContext);
59 _testimpl = new TestViewHandler();
60 }
61
62
63
64
65
66 public void testCalculateCharacterEncodingWithRequestHeaderContentType()
67 {
68 Map map = _mocksControl.createMock(Map.class);
69 expect(_externalContext.getRequestHeaderMap()).andReturn(map);
70 expect(map.get(eq("Content-Type"))).andReturn("text/html;charset=UTF-8");
71 _mocksControl.replay();
72 assertEquals("UTF-8", _testimpl.calculateCharacterEncoding(_facesContext));
73 _mocksControl.verify();
74 }
75
76
77
78
79
80 public void testCalculateCharacterEncodingWithNoRequestContentTypeAndNoSession()
81 {
82 expect(_externalContext.getRequestHeaderMap()).andReturn(Collections.EMPTY_MAP);
83 expect(_externalContext.getSession(eq(false))).andReturn(null);
84 _mocksControl.replay();
85 assertNull(_testimpl.calculateCharacterEncoding(_facesContext));
86 _mocksControl.verify();
87 }
88
89
90
91
92
93 public void testCalculateCharacterEncodingWithNoRequestContentTypeAndWithSessionButNoSessionValue()
94 {
95 expect(_externalContext.getRequestHeaderMap()).andReturn(Collections.EMPTY_MAP);
96 expect(_externalContext.getSession(eq(false))).andReturn(new Object());
97 Map map = _mocksControl.createMock(Map.class);
98 expect(_externalContext.getSessionMap()).andReturn(map);
99 expect(map.get(eq(ViewHandler.CHARACTER_ENCODING_KEY))).andReturn(null);
100 _mocksControl.replay();
101 assertNull(_testimpl.calculateCharacterEncoding(_facesContext));
102 _mocksControl.verify();
103 }
104
105
106
107
108
109 public void testCalculateCharacterEncodingWithNoRequestContentTypeAndWithSessionAndNoSessionValue()
110 {
111 expect(_externalContext.getRequestHeaderMap()).andReturn(Collections.EMPTY_MAP);
112 expect(_externalContext.getSession(eq(false))).andReturn(new Object());
113 Map map = _mocksControl.createMock(Map.class);
114 expect(_externalContext.getSessionMap()).andReturn(map);
115 expect(map.get(eq(ViewHandler.CHARACTER_ENCODING_KEY))).andReturn("UTF-8");
116 _mocksControl.replay();
117 assertEquals("UTF-8", _testimpl.calculateCharacterEncoding(_facesContext));
118 _mocksControl.verify();
119 }
120
121
122
123
124
125
126 public void testInitView() throws Exception
127 {
128 ViewHandler handler = _mocksControl.createMock(ViewHandler.class, new Method[] { ViewHandler.class.getMethod(
129 "calculateCharacterEncoding", new Class[] { FacesContext.class }) });
130 expect(handler.calculateCharacterEncoding(_facesContext)).andReturn("xxx");
131 _externalContext.setRequestCharacterEncoding(eq("xxx"));
132 _mocksControl.replay();
133 handler.initView(_facesContext);
134 _mocksControl.verify();
135 }
136
137
138
139
140
141
142 public void testInitViewWithUnsupportedEncodingException() throws Exception
143 {
144 final ViewHandler handler = _mocksControl.createMock(ViewHandler.class, new Method[] { ViewHandler.class.getMethod(
145 "calculateCharacterEncoding", new Class[] { FacesContext.class }) });
146 expect(handler.calculateCharacterEncoding(_facesContext)).andReturn("xxx");
147 _externalContext.setRequestCharacterEncoding(eq("xxx"));
148 expectLastCall().andThrow(new UnsupportedEncodingException());
149 _mocksControl.replay();
150 Assert.assertException(FacesException.class, new TestRunner()
151 {
152 public void run() throws Throwable
153 {
154 handler.initView(_facesContext);
155 }
156 });
157 _mocksControl.verify();
158 }
159
160 private static class TestViewHandler extends ViewHandler
161 {
162 @Override
163 public Locale calculateLocale(FacesContext context)
164 {
165 throw new UnsupportedOperationException();
166 }
167
168 @Override
169 public String calculateRenderKitId(FacesContext context)
170 {
171 throw new UnsupportedOperationException();
172 }
173
174 @Override
175 public UIViewRoot createView(FacesContext context, String viewId)
176 {
177 throw new UnsupportedOperationException();
178 }
179
180 @Override
181 public String getActionURL(FacesContext context, String viewId)
182 {
183 throw new UnsupportedOperationException();
184 }
185
186 @Override
187 public String getResourceURL(FacesContext context, String path)
188 {
189 throw new UnsupportedOperationException();
190 }
191
192 @Override
193 public void renderView(FacesContext context, UIViewRoot viewToRender) throws IOException, FacesException
194 {
195 throw new UnsupportedOperationException();
196 }
197
198 @Override
199 public UIViewRoot restoreView(FacesContext context, String viewId)
200 {
201 throw new UnsupportedOperationException();
202 }
203
204 @Override
205 public void writeState(FacesContext context) throws IOException
206 {
207 throw new UnsupportedOperationException();
208 }
209
210 }
211 }