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.context.enhanced;
22
23 import junit.framework.TestCase;
24 import org.apache.tiles.TilesApplicationContext;
25 import org.easymock.EasyMock;
26
27 import java.io.IOException;
28 import java.net.MalformedURLException;
29 import java.net.URL;
30 import java.util.Enumeration;
31 import java.util.Vector;
32 import java.util.HashSet;
33
34
35 /***
36 * @version $Rev: 527536 $ $Date: 2007-04-11 17:44:51 +0200 (Wed, 11 Apr 2007) $
37 */
38 public class EnhancedTilesApplicationContextTest extends TestCase {
39
40 /***
41 * Number of properties container inside the test.properties file.
42 */
43 private static final int TEST_PROPERTIES_SIZE = 4;
44
45 /***
46 * The root Tiles application context.
47 */
48 private TilesApplicationContext root;
49
50 /***
51 * The enhanced Tiles application context.
52 */
53 private EnhancedTilesApplicationContext context;
54
55 /*** {@inheritDoc} */
56 @Override
57 public void setUp() {
58 root = EasyMock.createMock(TilesApplicationContext.class);
59 context = new EnhancedTilesApplicationContext(root);
60 }
61
62 /***
63 * Tests resource getting.
64 *
65 * @throws IOException If something goes wrong.
66 */
67 public void testGetResources() throws IOException {
68 ClassLoader original = Thread.currentThread().getContextClassLoader();
69 try {
70 String url = "test.properties";
71 HashSet<URL> set = new HashSet<URL>();
72 URL u = new URL("file://tiles/test.properties");
73 set.add(u);
74 EasyMock.expect(root.getResources(url)).andReturn(set);
75 EasyMock.replay(root);
76 Thread.currentThread().setContextClassLoader(new MockClassLoader());
77
78 assertEquals(TEST_PROPERTIES_SIZE, context.getResources(
79 "test.properties").size());
80 EasyMock.verify(root);
81 } finally {
82 Thread.currentThread().setContextClassLoader(original);
83 }
84 }
85
86 /***
87 * An mock class loader.
88 */
89 public class MockClassLoader extends ClassLoader {
90
91 /***
92 * A vector of resources.
93 */
94 private Vector<URL> resources;
95
96 /***
97 * Constructor.
98 *
99 * @throws MalformedURLException If the URL is not valid (that should
100 * not happen).
101 */
102 public MockClassLoader() throws MalformedURLException {
103 resources = new Vector<URL>();
104 resources.add(new URL("file://tiles/test/test.properties"));
105 resources.add(new URL("file://tiles/two/test.properties"));
106 resources.add(new URL("file://tiles/three/test.properties"));
107 }
108
109 /*** {@inheritDoc} */
110 @Override
111 public Enumeration<URL> findResources(String path) {
112 return resources.elements();
113 }
114 }
115
116
117 }