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.impl;
22
23 import java.io.IOException;
24 import java.io.StringWriter;
25 import java.net.MalformedURLException;
26 import java.net.URL;
27 import java.util.HashSet;
28 import java.util.Set;
29 import java.util.Vector;
30
31 import javax.servlet.ServletContext;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
35 import junit.framework.TestCase;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.apache.shale.test.mock.MockHttpServletRequest;
40 import org.apache.shale.test.mock.MockHttpServletResponse;
41 import org.apache.shale.test.mock.MockHttpSession;
42 import org.apache.tiles.Attribute;
43 import org.apache.tiles.TilesException;
44 import org.apache.tiles.factory.TilesContainerFactory;
45 import org.easymock.EasyMock;
46
47
48 /***
49 * @version $Rev: 556114 $ $Date: 2007-07-13 21:28:24 +0200 (Fri, 13 Jul 2007) $
50 */
51 public class BasicTilesContainerTest extends TestCase {
52
53 /***
54 * The logging object.
55 */
56 private static final Log LOG = LogFactory
57 .getLog(BasicTilesContainerTest.class);
58
59 /***
60 * A sample integer value to check object rendering.
61 */
62 private static final int SAMPLE_INT = 15;
63
64 /***
65 * The container.
66 */
67 private BasicTilesContainer container;
68
69 /*** {@inheritDoc} */
70 @Override
71 public void setUp() {
72 ServletContext context = EasyMock.createMock(ServletContext.class);
73 URL url = getClass().getResource("/org/apache/tiles/factory/test-defs.xml");
74
75 Vector<String> v = new Vector<String>();
76
77 EasyMock.expect(context.getInitParameter(TilesContainerFactory.CONTAINER_FACTORY_INIT_PARAM)).andReturn(null);
78 EasyMock.expect(context.getInitParameter(TilesContainerFactory.CONTEXT_FACTORY_INIT_PARAM)).andReturn(null);
79 EasyMock.expect(context.getInitParameter(TilesContainerFactory.DEFINITIONS_FACTORY_INIT_PARAM)).andReturn(null);
80 EasyMock.expect(context.getInitParameter(EasyMock.isA(String.class))).andReturn(null).anyTimes();
81 EasyMock.expect(context.getInitParameterNames()).andReturn(v.elements()).anyTimes();
82 try {
83 EasyMock.expect(context.getResource("/WEB-INF/tiles.xml")).andReturn(url);
84 } catch (MalformedURLException e) {
85 throw new RuntimeException("Error getting Tiles configuration URL",
86 e);
87 }
88 EasyMock.replay(context);
89 try {
90 TilesContainerFactory factory = TilesContainerFactory.getFactory(context);
91 container = (BasicTilesContainer) factory.createContainer(context);
92 } catch (TilesException e) {
93 throw new RuntimeException("Error initializing factory", e);
94 }
95 }
96
97 /***
98 * Tests basic Tiles container initialization.
99 */
100 public void testInitialization() {
101 assertNotNull(container);
102 assertNotNull(container.getContextFactory());
103 assertNotNull(container.getPreparerFactory());
104 assertNotNull(container.getDefinitionsFactory());
105 }
106
107 /***
108 * Tests that attributes of type "object" won't be rendered.
109 *
110 * @throws IOException If something goes wrong, but it's not a Tiles
111 * exception.
112 */
113 public void testObjectAttribute() throws IOException {
114 Attribute attribute = new Attribute();
115 HttpServletRequest request = new MockHttpServletRequest();
116 HttpServletResponse response = new MockHttpServletResponse();
117 boolean exceptionFound = false;
118
119 attribute.setValue(new Integer(SAMPLE_INT));
120 try {
121 container.render(attribute, null, request, response);
122 } catch (TilesException e) {
123 LOG.debug("Intercepted a TilesException, it is correct", e);
124 exceptionFound = true;
125 }
126
127 assertTrue("An attribute of 'object' type cannot be rendered",
128 exceptionFound);
129 }
130
131 /***
132 * Tests is attributes are rendered correctly according to users roles.
133 *
134 * @throws TilesException If a problem arises during rendering.
135 * @throws IOException If a problem arises during rendering or writing in the writer.
136 */
137 public void testAttributeCredentials() throws TilesException, IOException {
138 RoleMockHttpServletRequest request = new RoleMockHttpServletRequest("myrole");
139 MockHttpSession session = new MockHttpSession();
140 request.setHttpSession(session);
141 MockHttpServletResponse response = new MockHttpServletResponse();
142 Attribute attribute = new Attribute((Object) "This is the value", "myrole");
143 StringWriter writer = new StringWriter();
144 container.render(attribute, writer, request, response);
145 writer.close();
146 assertEquals("The attribute should have been rendered", writer
147 .toString(), "This is the value");
148 request = new RoleMockHttpServletRequest();
149 writer = new StringWriter();
150 container.render(attribute, writer, request, response);
151 writer.close();
152 assertNotSame("The attribute should have not been rendered", writer
153 .toString(), "This is the value");
154 }
155
156 /***
157 * Servlet request mock class that allows to choose the user roles.
158 */
159 private static class RoleMockHttpServletRequest extends MockHttpServletRequest {
160
161 /***
162 * Set containing the allowed roles.
163 */
164 private Set<String> roleSet;
165
166 /***
167 * Constructor.
168 *
169 * @param roles The roles to be allowed.
170 */
171 public RoleMockHttpServletRequest(String... roles) {
172 roleSet = new HashSet<String>();
173 for (String role : roles) {
174 roleSet.add(role);
175 }
176 }
177
178 /*** {@inheritDoc} */
179 @Override
180 public boolean isUserInRole(String role) {
181 return roleSet.contains(role);
182 }
183 }
184 }