2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shale.remoting.impl;
19
20 import javax.servlet.http.HttpServletResponse;
21
22 import junit.framework.Test;
23 import junit.framework.TestSuite;
24 import org.apache.shale.remoting.Constants;
25
26 import org.apache.shale.test.base.AbstractJsfTestCase;
27 import org.apache.shale.test.mock.MockServletOutputStream;
28
29 /***
30 * <p>Test case for <code>org.apache.shale.remoting.impl.ClassResourceProcessor</code>.</p>
31 */
32 public class ClassResourceProcessorTestCase extends AbstractJsfTestCase {
33
34
35
36
37
38
39 public ClassResourceProcessorTestCase(String name) {
40 super(name);
41 }
42
43
44
45
46
47 private static final String INVALID_RESOURCE_ID =
48 "/org/apache/shale/remoting/impl/MissingData.txt";
49
50 private static final String SENSITIVE_RESOURCE_ID =
51 "/org/apache/shale/remoting/Bundle.properties";
52
53 private static final String VALID_RESOURCE_ID =
54 "/org/apache/shale/remoting/impl/TestData.txt";
55
56 private static final String VALID_RESOURCE_CONTENT =
57 "This is a test. It is only a test.";
58
59
60
61
62
63 protected void setUp() throws Exception {
64
65 threadClassLoader = Thread.currentThread().getContextClassLoader();
66 Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
67 super.setUp();
68 servletContext.addMimeType("txt", "text/x-plain");
69 processor = new ClassResourceProcessor();
70 processor.setExcludes(Constants.CLASS_RESOURCES_EXCLUDES_DEFAULT);
71 processor.setIncludes(Constants.CLASS_RESOURCES_INCLUDES_DEFAULT);
72
73 }
74
75
76
77 public static Test suite() {
78
79 return (new TestSuite(ClassResourceProcessorTestCase.class));
80
81 }
82
83
84
85 protected void tearDown() throws Exception {
86
87 processor = null;
88 super.tearDown();
89 Thread.currentThread().setContextClassLoader(threadClassLoader);
90
91 }
92
93
94
95
96
97
98 private ClassResourceProcessor processor = null;
99
100
101
102 private ClassLoader threadClassLoader = null;
103
104
105
106
107
108
109 public void testInvalidResource() throws Exception {
110
111 processor.process(facesContext, INVALID_RESOURCE_ID);
112 assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus());
113 assertEquals(INVALID_RESOURCE_ID, response.getMessage());
114
115 }
116
117
118
119 public void testMapping() {
120
121 assertNotNull(processor.getResourceURL(facesContext, VALID_RESOURCE_ID));
122 assertNull(processor.getResourceURL(facesContext, INVALID_RESOURCE_ID));
123
124 }
125
126
127
128 public void testPatternExcluded() throws Exception {
129
130 processor.setExcludes("*.txt");
131 processor.process(facesContext, VALID_RESOURCE_ID);
132 assertEquals(404, response.getStatus());
133
134 }
135
136
137
138 public void testPatternIncludedInensitive() throws Exception {
139
140 processor.setExcludes(null);
141 processor.setIncludes("*.txt");
142 processor.process(facesContext, VALID_RESOURCE_ID);
143 assertEquals(200, response.getStatus());
144
145 }
146
147
148
149 public void testPatternIncludedSensitive() throws Exception {
150
151 processor.setExcludes(null);
152 processor.setIncludes("*.properties");
153 processor.process(facesContext, SENSITIVE_RESOURCE_ID);
154 assertEquals(404, response.getStatus());
155
156 }
157
158
159 public void testPatternMixed() throws Exception {
160
161 processor.setExcludes("*.properties");
162 processor.setIncludes("*.txt");
163 processor.process(facesContext, VALID_RESOURCE_ID);
164 assertEquals(200, response.getStatus());
165
166 }
167
168
169
170
171 public void testPatternSensitive() throws Exception {
172
173 assertEquals(Constants.CLASS_RESOURCES_EXCLUDES_DEFAULT + ","
174 + Constants.CLASS_RESOURCES_EXCLUDES_DEFAULT,
175 processor.getExcludes());
176 assertEquals(Constants.CLASS_RESOURCES_INCLUDES_DEFAULT, processor.getIncludes());
177 processor.process(facesContext, SENSITIVE_RESOURCE_ID);
178 assertEquals(404, response.getStatus());
179
180 }
181
182
183
184 public void testPristine() {
185
186 assertNotNull(processor);
187 assertEquals("text/x-plain", servletContext.getMimeType(VALID_RESOURCE_ID));
188 assertEquals("text/x-plain", servletContext.getMimeType(INVALID_RESOURCE_ID));
189
190 }
191
192
193
194 public void testNotModifiedResource() throws Exception {
195
196 long timestamp = processor.getLastModified();
197 request.addDateHeader("If-Modified-Since", timestamp);
198 processor.process(facesContext, VALID_RESOURCE_ID);
199 assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getStatus());
200
201 }
202
203
204
205 public void testValidResource() throws Exception {
206
207 processor.process(facesContext, VALID_RESOURCE_ID);
208 assertEquals("text/x-plain", response.getContentType());
209 MockServletOutputStream stream =
210 (MockServletOutputStream) response.getOutputStream();
211 assertNotNull(stream);
212 assertTrue(stream.size() > VALID_RESOURCE_CONTENT.length());
213 byte content[] = stream.content();
214 for (int i = 0; i < VALID_RESOURCE_CONTENT.length(); i++) {
215 byte b = (byte) ((int) VALID_RESOURCE_CONTENT.charAt(i));
216 assertEquals("Byte at position " + i, b, content[i]);
217 }
218
219
220 }
221
222
223
224
225
226 }