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 import junit.framework.Test;
22 import junit.framework.TestSuite;
23 import org.apache.commons.chain.CatalogFactory;
24 import org.apache.commons.chain.Context;
25 import org.apache.commons.chain.impl.CatalogBase;
26 import org.apache.shale.test.base.AbstractJsfTestCase;
27 import org.apache.shale.test.mock.MockPrintWriter;
28
29 /***
30 * <p>Test case for <code>org.apache.shale.remoting.impl.ClassResourceProcessor</code>.</p>
31 */
32 public class ChainProcessorTestCase extends AbstractJsfTestCase {
33
34
35
36
37
38
39 public ChainProcessorTestCase(String name) {
40 super(name);
41 }
42
43
44
45
46
47 /***
48 * <p>The valid content (up to the line delimiter) returned for the
49 * <code>testProcess()</code> test.</p>
50 */
51 private static final String PROCESS_CONTENT =
52 "This is a test. It is only a test.";
53
54
55
56
57
58
59 protected void setUp() throws Exception {
60
61 super.setUp();
62 processor = new ChainProcessor();
63
64 }
65
66
67
68 public static Test suite() {
69
70 return (new TestSuite(ChainProcessorTestCase.class));
71
72 }
73
74
75
76 protected void tearDown() throws Exception {
77
78 processor = null;
79 super.tearDown();
80
81 }
82
83
84
85
86
87
88 private ChainProcessor processor = null;
89
90
91
92
93
94
95 public void testCreateContext() {
96
97 Context context = processor.createContext(facesContext, "/foo/bar");
98 assertNotNull(context);
99 assertTrue(context instanceof ChainContext);
100 assertTrue(facesContext == context.get("facesContext"));
101 assertTrue(facesContext == ((ChainContext) context).getFacesContext());
102
103 }
104
105
106
107 public void testMapCataog() {
108
109 assertEquals("remoting", processor.mapCatalog(facesContext, "/foo/bar"));
110 assertEquals("remoting", processor.mapCatalog(facesContext, "/baz/bop"));
111
112 }
113
114
115
116 public void testMapCommand() {
117
118 assertEquals("foo.bar", processor.mapCommand(facesContext, "/foo/bar"));
119 assertEquals("baz.bop", processor.mapCommand(facesContext, "/baz/bop"));
120
121 }
122
123
124
125 public void testMissingCatalog() throws Exception {
126
127 processor.process(facesContext, "/foo/bar");
128 assertEquals(response.getStatus(), HttpServletResponse.SC_NOT_FOUND);
129 assertEquals(response.getMessage(), "/foo/bar");
130
131 }
132
133
134
135 public void testMissingCommand() throws Exception {
136
137 CatalogFactory.getInstance().addCatalog("remoting", new CatalogBase());
138 processor.process(facesContext, "/foo/bar");
139 assertEquals(response.getStatus(), HttpServletResponse.SC_NOT_FOUND);
140 assertEquals(response.getMessage(), "/foo/bar");
141
142 }
143
144
145
146 public void testPristine() throws Exception {
147
148 assertNotNull(processor);
149
150 }
151
152
153
154 public void testProcess() throws Exception {
155
156 CatalogFactory.getInstance().addCatalog("remoting", new CatalogBase());
157 CatalogFactory.getInstance().getCatalog("remoting").
158 addCommand("foo.bar", new ChainProcessorCommand());
159 processor.process(facesContext, "/foo/bar");
160 assertEquals(response.getStatus(), HttpServletResponse.SC_OK);
161 assertEquals("text/x-plain", response.getContentType());
162 MockPrintWriter writer = (MockPrintWriter) response.getWriter();
163 char content[] = writer.content();
164 assertNotNull(content);
165 assertTrue(content.length > PROCESS_CONTENT.length());
166 for (int i = 0; i < PROCESS_CONTENT.length(); i++) {
167 assertEquals("Character at position " + i, PROCESS_CONTENT.charAt(i), content[i]);
168 }
169
170 }
171
172
173
174 public void testSendNotFound() throws Exception {
175
176 processor.sendNotFound(facesContext, "/foo/bar");
177 assertEquals(response.getStatus(), HttpServletResponse.SC_NOT_FOUND);
178 assertEquals(response.getMessage(), "/foo/bar");
179
180 }
181
182
183
184 public void testServerError() throws Exception {
185
186 processor.sendServerError(facesContext, "/foo/bar", new NullPointerException());
187 assertEquals(response.getStatus(), HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
188 assertEquals(response.getMessage(), "/foo/bar");
189
190 }
191
192
193
194
195
196
197
198
199 }