1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
42
43
44
45 public class WildcardServletTilesApplicationContextTest extends TestCase {
46
47
48
49
50 private static final int TEST_PROPERTIES_SIZE = 3;
51
52
53
54
55 private ServletContext servletContext;
56
57
58
59
60 private WildcardServletTilesApplicationContext context;
61
62
63
64
65 private ClassLoader original;
66
67
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
81 @Override
82 protected void tearDown() throws Exception {
83 Thread.currentThread().setContextClassLoader(original);
84 }
85
86
87
88
89
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
121
122 public class MockClassLoader extends ClassLoader {
123
124
125
126
127 private Vector<URL> testPropertiesResources;
128
129
130
131
132
133
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
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 }