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 java.util.Locale;
21  import javax.faces.component.UIViewRoot;
22  import junit.framework.Test;
23  import junit.framework.TestSuite;
24  import org.apache.shale.remoting.Constants;
25  import org.apache.shale.test.base.AbstractJsfTestCase;
26  
27  /***
28   * <p>Test case for <code>org.apache.shale.remoting.impl.MappingImpl</code>.</p>
29   */
30  public class MappingImplTestCase extends AbstractJsfTestCase {
31      
32  
33      // ------------------------------------------------------------ Constructors
34  
35  
36      // Construct a new instance of this test case.
37      public MappingImplTestCase(String name) {
38          super(name);
39      }
40  
41  
42      // ----------------------------------------------------------- Setup Methods
43  
44  
45      // Set up instance variables for this test case.
46      protected void setUp() throws Exception {
47  
48          super.setUp();
49          facesContext.setViewRoot(new UIViewRoot());
50          mappings = new MappingsImpl();
51          mappings.setExtension(".jsp");
52          mappings.setPatterns(new String[] { "*.faces" });
53          mapping = new MappingImpl();
54          mapping.setMappings(mappings);
55          mappings.addMapping(mapping);
56          facesContext.getExternalContext().getApplicationMap().
57                  put(Constants.MAPPINGS_ATTR, mappings);
58          facesContext.getViewRoot().setLocale(Locale.getDefault());
59          request.setPathElements("/webapp", "/foo", null, null);
60  
61      }
62  
63  
64      // Return the tests included in this test case.
65      public static Test suite() {
66  
67          return (new TestSuite(MappingImplTestCase.class));
68  
69      }
70  
71  
72      // Tear down instance variables for this test case.
73      protected void tearDown() throws Exception {
74  
75          mapping = null;
76          mappings = null;
77          super.tearDown();
78  
79      }
80  
81  
82      // ------------------------------------------------------ Instance Variables
83  
84  
85      // The instance to be tested
86      private MappingImpl mapping = null;
87  
88  
89      // The parent instance we must configure for testing
90      private MappingsImpl mappings = null;
91  
92  
93      // ------------------------------------------------------------ Test Methods
94  
95  
96      // Test extension mapping
97      public void testExtension() {
98  
99          mapping.setPattern("*.foo");
100         facesContext.getViewRoot().setViewId("foo");
101         assertNull(mapping.mapViewId(facesContext));
102         facesContext.getViewRoot().setViewId(".foo");
103         assertNull(mapping.mapViewId(facesContext));
104         facesContext.getViewRoot().setViewId("/foo");
105         assertNull(mapping.mapViewId(facesContext));
106         facesContext.getViewRoot().setViewId("/foo.bar");
107         assertNull(mapping.mapViewId(facesContext));
108         facesContext.getViewRoot().setViewId("/bar.foo");
109         assertEquals("/bar", mapping.mapViewId(facesContext));
110         facesContext.getViewRoot().setViewId("/bar/baz.foo");
111         assertEquals("/bar/baz", mapping.mapViewId(facesContext));
112         facesContext.getViewRoot().setViewId("/foo/bar.foo" + mappings.getExtension());
113         assertEquals("/foo/bar", mapping.mapViewId(facesContext));
114 
115     }
116 
117 
118     // Test extension mapping workaround for JSF RI bug that leaves
119     // "*.faces" on the view id after Restore View phase
120     public void testExtensionExtra() {
121 
122         mapping.setPattern("*.bop");
123         facesContext.getViewRoot().setViewId("/foo/bar.bop" + mappings.getPatterns()[0].substring(1));
124         assertEquals("/foo/bar", mapping.mapViewId(facesContext));
125 
126     }
127 
128 
129     // Test invalid patterns
130     public void testInvalid() {
131 
132         try {
133             mapping.setPattern("");
134             fail("Should have thrown IllegalArgumentException");
135         } catch (IllegalArgumentException e) {
136             ; // Expected result
137         }
138 
139         try {
140             mapping.setPattern("foo");
141             fail("Should have thrown IllegalArgumentException");
142         } catch (IllegalArgumentException e) {
143             ; // Expected result
144         }
145 
146         try {
147             mapping.setPattern("/foo");
148             fail("Should have thrown IllegalArgumentException");
149         } catch (IllegalArgumentException e) {
150             ; // Expected result
151         }
152 
153         try {
154             mapping.setPattern("/foo/");
155             fail("Should have thrown IllegalArgumentException");
156         } catch (IllegalArgumentException e) {
157             ; // Expected result
158         }
159 
160         try {
161             mapping.setPattern(".foo");
162             fail("Should have thrown IllegalArgumentException");
163         } catch (IllegalArgumentException e) {
164             ; // Expected result
165         }
166 
167         try {
168             mapping.setPattern("*.foo.");
169             fail("Should have thrown IllegalArgumentException");
170         } catch (IllegalArgumentException e) {
171             ; // Expected result
172         }
173 
174         try {
175             mapping.setPattern("*.foo.bar");
176             fail("Should have thrown IllegalArgumentException");
177         } catch (IllegalArgumentException e) {
178             ; // Expected result
179         }
180 
181     }
182 
183 
184     // Test prefix mapping
185     public void testPrefix() {
186 
187         mapping.setPattern("/foo/*");
188         facesContext.getViewRoot().setViewId("foo");
189         assertNull(mapping.mapViewId(facesContext));
190         facesContext.getViewRoot().setViewId("/bar");
191         assertNull(mapping.mapViewId(facesContext));
192         facesContext.getViewRoot().setViewId("/bar/foo");
193         assertNull(mapping.mapViewId(facesContext));
194         facesContext.getViewRoot().setViewId("/foo");
195         assertNull(mapping.mapViewId(facesContext));
196         facesContext.getViewRoot().setViewId("/foo/bar");
197         assertEquals("/bar", mapping.mapViewId(facesContext));
198         facesContext.getViewRoot().setViewId("/foo/bar.baz");
199         assertEquals("/bar.baz", mapping.mapViewId(facesContext));
200         facesContext.getViewRoot().setViewId("/foo/bar/baz");
201         assertEquals("/bar/baz", mapping.mapViewId(facesContext));
202         facesContext.getViewRoot().setViewId("/foo/bar/baz.bop");
203         assertEquals("/bar/baz.bop", mapping.mapViewId(facesContext));
204 
205     }
206 
207 
208     // Test a pristine instance
209     public void testPristine() {
210 
211         assertNull(mapping.getMechanism());
212         assertNull(mapping.getPattern());
213         assertNull(mapping.getProcessor());
214 
215     }
216 
217 
218     // Test mapping resource identifiers to view identifiers
219     public void testResourceIdentifiers() throws Exception {
220 
221         String resourceId = "/foo/bar.baz";
222 
223         // FacesServlet=prefix Mapping=prefix
224         mappings.setPatterns(new String[] { "/faces/*" });
225         mapping.setPattern("/bop/*");
226         assertEquals("/webapp/faces/bop/foo/bar.baz",
227                      mapping.mapResourceId(facesContext, resourceId));
228 
229         // FacesServlet=prefix Mapping=extension
230         mappings.setPatterns(new String[] { "/faces/*" });
231         mapping.setPattern("*.bop");
232         assertEquals("/webapp/faces/foo/bar.baz.bop",
233                      mapping.mapResourceId(facesContext, resourceId));
234 
235         // FacesServlet=extension Mapping=prefix
236         mappings.setPatterns(new String[] { "*.faces" });
237         mapping.setPattern("/bop/*");
238         assertEquals("/webapp/bop/foo/bar.baz.faces",
239                      mapping.mapResourceId(facesContext, resourceId));
240 
241         // FacesServlet=extension Mapping=prefix
242         mappings.setPatterns(new String[] { "*.faces" });
243         mapping.setPattern("*.bop");
244         assertEquals("/webapp/foo/bar.baz.bop.faces",
245                      mapping.mapResourceId(facesContext, resourceId));
246 
247     }
248 
249 
250     // --------------------------------------------------------- Support Methods
251 
252 
253 
254 }