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 import org.apache.tiles.impl.KeyedDefinitionsFactoryTilesContainer;
33
34 import java.util.Map;
35 import java.util.Vector;
36 import java.util.HashMap;
37 import java.net.URL;
38 import java.net.MalformedURLException;
39
40
41 /***
42 * @version $Rev: 532678 $ $Date: 2007-04-26 11:13:49 +0200 (Thu, 26 Apr 2007) $
43 */
44 public class KeyedDefinitionsFactoryTilesContainerFactoryTest extends TestCase {
45
46 /***
47 * The logging object.
48 */
49 private static final Log LOG = LogFactory
50 .getLog(KeyedDefinitionsFactoryTilesContainerFactoryTest.class);
51
52 /***
53 * The servlet context.
54 */
55 private ServletContext context;
56
57 /***
58 * Default configuration parameters.
59 */
60 private Map<String, String> defaults;
61
62 /*** {@inheritDoc} */
63 @Override
64 public void setUp() {
65 context = EasyMock.createMock(ServletContext.class);
66 defaults = new HashMap<String, String>();
67 defaults.put(TilesContainerFactory.CONTAINER_FACTORY_INIT_PARAM,
68 KeyedDefinitionsFactoryTilesContainerFactory.class.getName());
69 }
70
71 /***
72 * Tests getting a container factory.
73 *
74 * @throws TilesException If something goes wrong.
75 */
76 public void testGetFactory() throws TilesException {
77 Vector<String> v = new Vector<String>();
78
79 EasyMock.expect(context.getInitParameterNames()).andReturn(v.elements());
80 EasyMock.expect(context.getInitParameter(TilesContainerFactory.CONTAINER_FACTORY_INIT_PARAM)).andReturn(null);
81 EasyMock.replay(context);
82 TilesContainerFactory factory = TilesContainerFactory.getFactory(context,
83 defaults);
84 assertNotNull(factory);
85 assertEquals(KeyedDefinitionsFactoryTilesContainerFactory.class,
86 factory.getClass());
87 }
88
89 /***
90 * Tests creating a container.
91 *
92 * @throws TilesException If something goes wrong.
93 * @throws MalformedURLException If the resources have an invalid form (that
94 * should not happen).
95 */
96 public void testCreateContainer() throws TilesException, MalformedURLException {
97 Vector<String> enumeration = new Vector<String>();
98 EasyMock.expect(context.getInitParameter(TilesContainerFactory.CONTAINER_FACTORY_INIT_PARAM)).andReturn(null);
99 EasyMock.expect(context.getInitParameter(TilesContainerFactory.CONTEXT_FACTORY_INIT_PARAM)).andReturn(null);
100 EasyMock.expect(context.getInitParameter(TilesContainerFactory.DEFINITIONS_FACTORY_INIT_PARAM)).andReturn(null);
101 EasyMock.expect(context.getInitParameter(
102 KeyedDefinitionsFactoryTilesContainerFactory.CONTAINER_KEYS_INIT_PARAM))
103 .andReturn("one,two").anyTimes();
104 EasyMock.expect(context.getInitParameter(
105 KeyedDefinitionsFactoryTilesContainer.DEFINITIONS_CONFIG_PREFIX
106 + "one"))
107 .andReturn("/WEB-INF/tiles-one.xml").anyTimes();
108 EasyMock.expect(context.getInitParameter(
109 KeyedDefinitionsFactoryTilesContainer.DEFINITIONS_CONFIG_PREFIX
110 + "@two")).andReturn("/WEB-INF/tiles-two.xml").anyTimes();
111 EasyMock.expect(context.getInitParameter(EasyMock.isA(String.class))).andReturn(null).anyTimes();
112 EasyMock.expect(context.getInitParameterNames()).andReturn(enumeration.elements()).anyTimes();
113 URL url = getClass().getResource("test-defs.xml");
114 EasyMock.expect(context.getResource("/WEB-INF/tiles.xml")).andReturn(url);
115 url = getClass().getResource("test-defs-key-one.xml");
116 EasyMock.expect(context.getResource("/WEB-INF/tiles-one.xml")).andReturn(url);
117 url = getClass().getResource("test-defs-key-two.xml");
118 EasyMock.expect(context.getResource("/WEB-INF/tiles-two.xml")).andReturn(url);
119 EasyMock.replay(context);
120
121 TilesContainerFactory factory = TilesContainerFactory.getFactory(context, defaults);
122 TilesContainer container = factory.createContainer(context);
123
124 assertNotNull(container);
125 assertTrue("The container is not an instance of KeyedDefinitionsFactoryTilesContainer",
126 container instanceof KeyedDefinitionsFactoryTilesContainer);
127 KeyedDefinitionsFactoryTilesContainer keyedContainer =
128 (KeyedDefinitionsFactoryTilesContainer) container;
129 assertNotNull(keyedContainer.getDefinitionsFactory());
130 assertNotNull(keyedContainer.getDefinitionsFactory("one"));
131 assertNotNull(keyedContainer.getDefinitionsFactory("two"));
132
133 try {
134 container.init(new HashMap<String, String>());
135 fail("Container should have already been initialized");
136 } catch (IllegalStateException te) {
137 if (LOG.isDebugEnabled()) {
138 LOG.debug("The container has been initialized, the exception is ok", te);
139 }
140 }
141
142 }
143 }