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.faces;
19  
20  import javax.faces.application.ViewHandler;
21  
22  import junit.framework.Test;
23  import junit.framework.TestSuite;
24  
25  import org.apache.shale.remoting.Constants;
26  import org.apache.shale.remoting.Mapping;
27  import org.apache.shale.remoting.Mappings;
28  import org.apache.shale.remoting.Mechanism;
29  import org.apache.shale.remoting.Processor;
30  import org.apache.shale.remoting.impl.ClassResourceProcessor;
31  import org.apache.shale.remoting.impl.FilteringProcessor;
32  import org.apache.shale.remoting.impl.MappingImpl;
33  import org.apache.shale.remoting.impl.MappingsImpl;
34  import org.apache.shale.remoting.impl.MethodBindingProcessor;
35  import org.apache.shale.remoting.impl.WebResourceProcessor;
36  import org.apache.shale.test.base.AbstractJsfTestCase;
37  
38  /***
39   * <p>Test case for <code>org.apache.shale.remoting.MappingsHelper</code>.
40   * These tests focus on correct configuration, not on executable
41   * functionality.</p>
42   */
43  public class MappingsHelperTestCase extends AbstractJsfTestCase {
44      
45  
46      // ------------------------------------------------------------ Constructors
47  
48  
49      // Construct a new instance of this test case.
50      public MappingsHelperTestCase(String name) {
51          super(name);
52      }
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          helper = new MappingsHelper();
63          // mappings instance set after configuration, not here
64  
65      }
66  
67  
68      // Return the tests included in this test case.
69      public static Test suite() {
70  
71          return (new TestSuite(MappingsHelperTestCase.class));
72  
73      }
74  
75  
76      // Tear down instance variables for this test case.
77      protected void tearDown() throws Exception {
78  
79          mappings = null;
80          helper = null;
81          super.tearDown();
82  
83      }
84  
85  
86      // ------------------------------------------------------ Instance Variables
87  
88  
89      // The instance to be tested
90      private MappingsHelper helper = null;
91  
92  
93      // An individual Mapping instance to be validated
94      private Mapping mapping = null;
95  
96  
97      // The mappings instance retrieved after configuration
98      private Mappings mappings = null;
99  
100 
101     // An individual Processor instance to be validated
102     private Processor processor = null;
103 
104 
105     // ------------------------------------------------------------ Test Methods
106 
107 
108     // Test an invalid exclude pattern
109     public void testInvalidExclude() {
110 
111         servletContext.addInitParameter(Constants.CLASS_RESOURCES_EXCLUDES,
112                                         "*.class,/foo");
113         try {
114             mappings = helper.getMappings(facesContext);
115             fail("Should have thrown IllegalArgumentException");
116         } catch (IllegalArgumentException e) {
117             ; // Expected result
118         }
119 
120     }
121 
122 
123     // Test an invalid include pattern
124     public void testInvalidInclude() {
125 
126         servletContext.addInitParameter(Constants.DYNAMIC_RESOURCES_INCLUDES,
127                                         "/bar/*,*.x.y");
128         try {
129             mappings = helper.getMappings(facesContext);
130             fail("Should have thrown IllegalArgumentException");
131         } catch (IllegalArgumentException e) {
132             ; // Expected result
133         }
134 
135     }
136 
137 
138     // Test an invalid mechanism pattern
139     public void testInvalidMechanism() {
140 
141         servletContext.addInitParameter(Constants.WEBAPP_RESOURCES_PARAM,
142                                         "*/*:org.apache.shale.remoting.impl.MethodBindingProcessor");
143         try {
144             mappings = helper.getMappings(facesContext);
145             fail("Should have thrown IllegalArgumentException");
146         } catch (IllegalArgumentException e) {
147             ; // Expected result
148         }
149 
150     }
151 
152 
153     // Test a pristine instance with default configuration
154     public void testPristine() {
155 
156         assertNotNull(helper);
157 
158         // Acquire the configured mappings instance
159         mappings = helper.getMappings(facesContext);
160         assertNotNull(mappings);
161 
162         // Validate the characteristics of the Mappings instance itself
163         assertTrue(mappings instanceof MappingsImpl);
164         assertEquals(ViewHandler.DEFAULT_SUFFIX, mappings.getExtension());
165         assertEquals(3, mappings.getMappings().size());
166         assertEquals(0, mappings.getPatterns().length);
167 
168         // Validate the "/static/*" Mapping instance for valid configuration
169         mapping = mappings.getMapping("/static/*");
170         assertNotNull(mapping);
171         assertTrue(mapping instanceof MappingImpl);
172         assertTrue(mappings == mapping.getMappings());
173         assertEquals(Mechanism.CLASS_RESOURCE, mapping.getMechanism());
174         assertEquals("/static/*", mapping.getPattern());
175         processor = mapping.getProcessor();
176         assertNotNull(processor);
177         assertTrue(processor instanceof ClassResourceProcessor);
178         assertEquals(Constants.CLASS_RESOURCES_EXCLUDES_DEFAULT + ","
179                      + Constants.CLASS_RESOURCES_EXCLUDES_DEFAULT,
180                      ((FilteringProcessor) processor).getExcludes());
181         assertEquals(Constants.CLASS_RESOURCES_INCLUDES_DEFAULT,
182                      ((FilteringProcessor) processor).getIncludes());
183 
184         // Validate the "/dynamic/*" Mapping instance for valid configuration
185         mapping = mappings.getMapping("/dynamic/*");
186         assertNotNull(mapping);
187         assertTrue(mapping instanceof MappingImpl);
188         assertTrue(mappings == mapping.getMappings());
189         assertEquals(Mechanism.DYNAMIC_RESOURCE, mapping.getMechanism());
190         assertEquals("/dynamic/*", mapping.getPattern());
191         processor = mapping.getProcessor();
192         assertNotNull(processor);
193         assertTrue(processor instanceof MethodBindingProcessor);
194         assertEquals(Constants.DYNAMIC_RESOURCES_EXCLUDES_DEFAULT + ","
195                      + Constants.DYNAMIC_RESOURCES_EXCLUDES_DEFAULT,
196                      ((FilteringProcessor) processor).getExcludes());
197         assertEquals(Constants.DYNAMIC_RESOURCES_INCLUDES_DEFAULT,
198                      ((FilteringProcessor) processor).getIncludes());
199 
200         // Validate the "/webapp/*" Mapping instance for valid configuration
201         mapping = mappings.getMapping("/webapp/*");
202         assertNotNull(mapping);
203         assertTrue(mapping instanceof MappingImpl);
204         assertTrue(mappings == mapping.getMappings());
205         assertEquals(Mechanism.WEBAPP_RESOURCE, mapping.getMechanism());
206         assertEquals("/webapp/*", mapping.getPattern());
207         processor = mapping.getProcessor();
208         assertNotNull(processor);
209         assertTrue(processor instanceof WebResourceProcessor);
210         assertEquals(Constants.WEBAPP_RESOURCES_EXCLUDES_DEFAULT + ","
211                      + Constants.WEBAPP_RESOURCES_EXCLUDES_DEFAULT,
212                      ((FilteringProcessor) processor).getExcludes());
213         assertEquals(Constants.WEBAPP_RESOURCES_INCLUDES_DEFAULT,
214                      ((FilteringProcessor) processor).getIncludes());
215 
216     }
217 
218 
219     // --------------------------------------------------------- Support Methods
220 
221 
222 
223 }