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  
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      // ------------------------------------------------------------ Constructors
36  
37  
38      // Construct a new instance of this test case.
39      public ClassResourceProcessorTestCase(String name) {
40          super(name);
41      }
42  
43  
44      // ------------------------------------------------------ Manifest Constants
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."; // Not including line delimiters!
58  
59      // ----------------------------------------------------------- Setup Methods
60  
61  
62      // Set up instance variables for this test case.
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      // Return the tests included in this test case.
77      public static Test suite() {
78  
79          return (new TestSuite(ClassResourceProcessorTestCase.class));
80  
81      }
82  
83  
84      // Tear down instance variables for this test case.
85      protected void tearDown() throws Exception {
86  
87          processor = null;
88          super.tearDown();
89          Thread.currentThread().setContextClassLoader(threadClassLoader);
90  
91      }
92  
93  
94      // ------------------------------------------------------ Instance Variables
95  
96  
97      // The Processor instance to be tested
98      private ClassResourceProcessor processor = null;
99  
100 
101     // The cached thread context class loader
102     private ClassLoader threadClassLoader = null;
103 
104 
105     // ------------------------------------------------------------ Test Methods
106 
107 
108     // Test an invalid resource
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     // Test mapping of resource identifiers to URLs
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     // Test attempt to execute an expression for an excluded pattern
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     // Test attempt to execute an expression for an included pattern
137     // that is not sensitive (i.e. included on the default exclude list)
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     // Test attempt to execute an expression for an included pattern
148     // that is sensitive (i.e. included on the default exclude list)
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     // Test attempt to execute an expression for a mixed exclude/include case
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     // Test attempt to access an existing sensitive resource that should be
170     // blocked by the default configuration
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     // Test a pristine instance of the Processor to be tested
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     // Test a valid resource that has not been modified
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     // Test a valid resource
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     // --------------------------------------------------------- Support Methods
223 
224 
225 
226 }