View Javadoc
1   /*
2    * $Id$
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.tiles.servlet.wildcard;
22  
23  import junit.framework.TestCase;
24  
25  import org.apache.tiles.servlet.wildcard.WildcardServletTilesApplicationContext;
26  import org.easymock.EasyMock;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.net.MalformedURLException;
31  import java.net.URL;
32  import java.util.Enumeration;
33  import java.util.Set;
34  import java.util.Vector;
35  import java.util.HashSet;
36  
37  import javax.servlet.ServletContext;
38  
39  
40  /**
41   * Tests {@link WildcardServletTilesApplicationContext}.
42   *
43   * @version $Rev$ $Date$
44   */
45  public class WildcardServletTilesApplicationContextTest extends TestCase {
46  
47      /**
48       * Number of properties container inside the test.properties file.
49       */
50      private static final int TEST_PROPERTIES_SIZE = 3;
51  
52      /**
53       * The root Tiles application context.
54       */
55      private ServletContext servletContext;
56  
57      /**
58       * The enhanced Tiles application context.
59       */
60      private WildcardServletTilesApplicationContext context;
61  
62      /**
63       * The original class loader.
64       */
65      private ClassLoader original;
66  
67      /** {@inheritDoc} */
68      @Override
69      public void setUp() {
70          servletContext = EasyMock.createMock(ServletContext.class);
71          original = Thread.currentThread().getContextClassLoader();
72          try {
73              Thread.currentThread().setContextClassLoader(new MockClassLoader());
74          } catch (MalformedURLException e) {
75              throw new RuntimeException("Error when using the mock classloader");
76          }
77          context = new WildcardServletTilesApplicationContext(servletContext);
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      protected void tearDown() throws Exception {
83          Thread.currentThread().setContextClassLoader(original);
84      }
85  
86      /**
87       * Tests resource getting.
88       *
89       * @throws IOException If something goes wrong.
90       */
91      public void testGetResources() throws IOException {
92          String url = "test.properties";
93          HashSet<URL> set = new HashSet<URL>();
94          URL u = new URL("file://tiles/test.properties");
95          set.add(u);
96          EasyMock.expect(servletContext.getResource("/" + url)).andReturn(u)
97                  .anyTimes();
98          File dir = new File(".");
99          EasyMock.expect(servletContext.getResource("/WEB-INF/")).andReturn(
100                 dir.toURI().toURL());
101         URL pomUrl = new URL("file://tiles/pom.xml");
102         EasyMock.expect(servletContext.getResource("/WEB-INF/pom.xml"))
103                 .andReturn(pomUrl);
104         Set<String> elementSet = new HashSet<String>();
105         elementSet.add("/WEB-INF/pom.xml");
106         EasyMock.expect(servletContext.getResourcePaths("/WEB-INF/")).andReturn(elementSet);
107         EasyMock.replay(servletContext);
108 
109         assertEquals(u, context.getResource("/" + url));
110         assertEquals(pomUrl, context.getResource("/WEB-INF/*.xml"));
111         assertEquals(TEST_PROPERTIES_SIZE, context.getResources(
112                 "classpath*:/test.properties").size());
113 
114         assertEquals(1, context.getResources(
115                 "classpath*:/org/apache/tiles/servlet/wildcard/*Test.class").size());
116         EasyMock.verify(servletContext);
117     }
118 
119     /**
120      * An mock class loader.
121      */
122     public class MockClassLoader extends ClassLoader {
123 
124         /**
125          * A vector of resources.
126          */
127         private Vector<URL> testPropertiesResources;
128 
129         /**
130          * Constructor.
131          *
132          * @throws MalformedURLException If the URL is not valid (that should
133          * not happen).
134          */
135         public MockClassLoader() throws MalformedURLException {
136             testPropertiesResources = new Vector<URL>();
137             testPropertiesResources.add(new URL("file://tiles/test/test.properties"));
138             testPropertiesResources.add(new URL("file://tiles/two/test.properties"));
139             testPropertiesResources.add(new URL("file://tiles/three/test.properties"));
140         }
141 
142         /** {@inheritDoc} */
143         @Override
144         public Enumeration<URL> findResources(String path) throws IOException {
145             Enumeration<URL> retValue = null;
146             if ("test.properties".equals(path)) {
147                 retValue = testPropertiesResources.elements();
148             } else {
149                 retValue = super.findResources(path);
150             }
151 
152             return retValue;
153         }
154     }
155 }