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.factory;
22
23 import junit.framework.TestCase;
24
25 import javax.servlet.ServletContext;
26
27 import org.easymock.EasyMock;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.tiles.TilesContainer;
31 import org.apache.tiles.TilesException;
32
33 import java.util.Map;
34 import java.util.Vector;
35 import java.util.HashMap;
36 import java.net.URL;
37 import java.net.MalformedURLException;
38
39
40 /***
41 * @version $Rev: 527870 $ $Date: 2007-04-12 12:19:41 +0200 (Thu, 12 Apr 2007) $
42 */
43 public class TilesContainerFactoryTest extends TestCase {
44
45 /***
46 * The logging object.
47 */
48 private static final Log LOG = LogFactory
49 .getLog(TilesContainerFactoryTest.class);
50
51 /***
52 * The servlet context.
53 */
54 private ServletContext context;
55
56 /*** {@inheritDoc} */
57 @Override
58 public void setUp() {
59 context = EasyMock.createMock(ServletContext.class);
60 }
61
62 /***
63 * Tests getting the factory.
64 *
65 * @throws TilesException If something goes wrong.
66 */
67 public void testGetFactory() throws TilesException {
68 Vector<String> v = new Vector<String>();
69 Vector<String> emptyVector = new Vector<String>();
70 v.add(TilesContainerFactory.CONTAINER_FACTORY_INIT_PARAM);
71
72 EasyMock.expect(context.getInitParameterNames()).andReturn(v.elements());
73 EasyMock.expect(context.getInitParameter(TilesContainerFactory.CONTAINER_FACTORY_INIT_PARAM)).andReturn(null);
74 EasyMock.replay(context);
75 TilesContainerFactory factory = TilesContainerFactory.getFactory(context);
76 assertNotNull(factory);
77 assertEquals(TilesContainerFactory.class, factory.getClass());
78
79 EasyMock.reset(context);
80 EasyMock.expect(context.getInitParameterNames()).andReturn(v.elements());
81 EasyMock.expect(context.getInitParameter(TilesContainerFactory
82 .CONTAINER_FACTORY_INIT_PARAM)).andReturn(
83 TestFactory.class.getName());
84 EasyMock.replay(context);
85 factory = TilesContainerFactory.getFactory(context);
86 assertNotNull(factory);
87 assertEquals(TestFactory.class, factory.getClass());
88
89 Map<String, String> defaults = new HashMap<String, String>();
90 defaults.put(TilesContainerFactory.CONTAINER_FACTORY_INIT_PARAM,
91 TestFactory.class.getName());
92 EasyMock.reset(context);
93 EasyMock.expect(context.getInitParameterNames()).andReturn(emptyVector.elements());
94 EasyMock.expect(context.getInitParameter(TilesContainerFactory.CONTAINER_FACTORY_INIT_PARAM)).andReturn(null);
95 EasyMock.replay(context);
96 factory = TilesContainerFactory.getFactory(context, defaults);
97 assertNotNull(factory);
98 assertEquals(TestFactory.class, factory.getClass());
99
100 EasyMock.reset(context);
101 EasyMock.expect(context.getInitParameterNames()).andReturn(v.elements());
102 EasyMock.expect(context.getInitParameter(TilesContainerFactory
103 .CONTAINER_FACTORY_INIT_PARAM)).andReturn("org.missing.Class");
104 EasyMock.replay(context);
105 try {
106 TilesContainerFactory.getFactory(context);
107 fail("Invalid classname. Exception should have been thrown.");
108 } catch (TilesException e) {
109 if (LOG.isDebugEnabled()) {
110 LOG.debug("The classname is invalid, it is ok", e);
111 }
112 }
113 }
114
115
116 /***
117 * Tests the creation of a container.
118 *
119 * @throws TilesException If something goes wrong during execution of
120 * Tiles-specific code.
121 * @throws MalformedURLException If something goes wrong when obtaining URL
122 * resources.
123 */
124 public void testCreateContainer() throws TilesException, MalformedURLException {
125 URL url = getClass().getResource("test-defs.xml");
126 Vector<String> enumeration = new Vector<String>();
127 EasyMock.expect(context.getInitParameter(TilesContainerFactory.CONTAINER_FACTORY_INIT_PARAM)).andReturn(null);
128 EasyMock.expect(context.getInitParameter(TilesContainerFactory.CONTEXT_FACTORY_INIT_PARAM)).andReturn(null);
129 EasyMock.expect(context.getInitParameter(TilesContainerFactory.DEFINITIONS_FACTORY_INIT_PARAM)).andReturn(null);
130 EasyMock.expect(context.getInitParameter(EasyMock.isA(String.class))).andReturn(null).anyTimes();
131 EasyMock.expect(context.getInitParameterNames()).andReturn(enumeration.elements()).anyTimes();
132 EasyMock.expect(context.getResource("/WEB-INF/tiles.xml")).andReturn(url);
133 EasyMock.replay(context);
134
135 TilesContainerFactory factory = TilesContainerFactory.getFactory(context);
136 TilesContainer container = factory.createContainer(context);
137
138 assertNotNull(container);
139
140 try {
141 container.init(new HashMap<String, String>());
142 fail("Container should have already been initialized");
143 } catch (IllegalStateException te) {
144 if (LOG.isDebugEnabled()) {
145 LOG.debug("Intercepted an exception, it is OK", te);
146 }
147 }
148
149 }
150
151
152 /***
153 * Tests getting init parameter map.
154 *
155 * @throws TilesException If something goes wrong.
156 */
157 public void testGetInitParameterMap() throws TilesException {
158 Vector<String> keys = new Vector<String>();
159 keys.add("one");
160 keys.add("two");
161
162 EasyMock.expect(context.getInitParameter(TilesContainerFactory.CONTAINER_FACTORY_INIT_PARAM)).andReturn(null);
163 EasyMock.expect(context.getInitParameterNames()).andReturn(keys.elements());
164 EasyMock.expect(context.getInitParameterNames()).andReturn(keys.elements());
165 EasyMock.expect(context.getInitParameter("one")).andReturn("oneValue").anyTimes();
166 EasyMock.expect(context.getInitParameter("two")).andReturn("twoValue").anyTimes();
167 EasyMock.replay(context);
168
169 Map<String, String> map = TilesContainerFactory.getInitParameterMap(context);
170
171 assertEquals(2, map.size());
172 assertTrue(map.containsKey("one"));
173 assertTrue(map.containsKey("two"));
174 assertEquals("oneValue", map.get("one"));
175 assertEquals("twoValue", map.get("two"));
176 }
177
178 /***
179 * A test factory extending directly from TilesContainerFactory.
180 */
181 public static class TestFactory extends TilesContainerFactory {
182
183 }
184 }