2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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      // ------------------------------------------------------------ Constructors
36  
37  
38      // Construct a new instance of this test case.
39      public ChainProcessorTestCase(String name) {
40          super(name);
41      }
42  
43  
44      // ------------------------------------------------------ Manifest Constants
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      // ----------------------------------------------------------- Setup Methods
56  
57  
58      // Set up instance variables for this test case.
59      protected void setUp() throws Exception {
60  
61          super.setUp();
62          processor = new ChainProcessor();
63  
64      }
65  
66  
67      // Return the tests included in this test case.
68      public static Test suite() {
69  
70          return (new TestSuite(ChainProcessorTestCase.class));
71  
72      }
73  
74  
75      // Tear down instance variables for this test case.
76      protected void tearDown() throws Exception {
77  
78          processor = null;
79          super.tearDown();
80  
81      }
82  
83  
84      // ------------------------------------------------------ Instance Variables
85  
86  
87      // The Processor instance to be tested
88      private ChainProcessor processor = null;
89  
90  
91      // ------------------------------------------------------------ Test Methods
92  
93  
94      // Test the createContext() method
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     // Test the mapCatalog() method
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     // Test the mapCommand() method
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     // Test the situation where the mapped catalog is missing
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     // Test the situation where the mapped command is missing
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     // Test a pristine instance
146     public void testPristine() throws Exception {
147 
148         assertNotNull(processor);
149 
150     }
151 
152 
153     // Test processing of a configured command
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     // Test the sendNotFound() method
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     // Test the sendServerError() method
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     // --------------------------------------------------------- Support Methods
196 
197 
198 
199 }