1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.tiles.definition.digester;
23
24 import java.io.InputStream;
25 import java.net.URL;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import junit.framework.Test;
30 import junit.framework.TestCase;
31 import junit.framework.TestSuite;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.tiles.Attribute;
36 import org.apache.tiles.Definition;
37 import org.apache.tiles.context.ListAttribute;
38 import org.apache.tiles.definition.DefinitionsFactoryException;
39 import org.apache.tiles.definition.DefinitionsReader;
40
41 /***
42 * Tests the <code>org.apache.tiles.definition.digester.DigesterDefinitionsReader</code> class.
43 *
44 * @version $Rev: 628347 $ $Date: 2008-02-16 17:28:14 +0100 (Sat, 16 Feb 2008) $
45 */
46 public class TestDigesterDefinitionsReader extends TestCase {
47
48 /***
49 * The logging object.
50 */
51 private static final Log LOG = LogFactory
52 .getLog(TestDigesterDefinitionsReader.class);
53
54 /***
55 * Creates a new instance of TestDigesterDefinitionsReader.
56 *
57 * @param name The name of the test.
58 */
59 public TestDigesterDefinitionsReader(String name) {
60 super(name);
61 }
62
63 /***
64 * Start the tests.
65 *
66 * @param theArgs the arguments. Not used
67 */
68 public static void main(String[] theArgs) {
69 junit.textui.TestRunner.main(
70 new String[] { TestDigesterDefinitionsReader.class.getName()});
71 }
72
73 /***
74 * @return a test suite (<code>TestSuite</code>) that includes all methods
75 * starting with "test"
76 */
77 public static Test suite() {
78 return new TestSuite(TestDigesterDefinitionsReader.class);
79 }
80
81 /***
82 * Tests the read method under normal conditions.
83 */
84 public void testRead() {
85 try {
86 DefinitionsReader reader = new DigesterDefinitionsReader();
87 reader.init(new HashMap<String, String>());
88
89 URL configFile = this.getClass().getClassLoader().getResource(
90 "org/apache/tiles/config/tiles-defs.xml");
91 assertNotNull("Config file not found", configFile);
92
93 InputStream source = configFile.openStream();
94 Map<String, Definition> definitions = reader.read(source);
95
96 assertNotNull("Definitions not returned.", definitions);
97 assertNotNull("Couldn't find doc.mainLayout tile.",
98 definitions.get("doc.mainLayout"));
99 assertNotNull("Couldn't Find title attribute.", definitions.get(
100 "doc.mainLayout").getAttribute("title").getValue());
101 assertEquals("Incorrect Find title attribute.",
102 "Tiles Library Documentation", definitions.get(
103 "doc.mainLayout").getAttribute("title").getValue());
104
105 Definition def = definitions.get("doc.role.test");
106 assertNotNull("Couldn't find doc.role.test tile.", def);
107 Attribute attribute = def.getAttribute("title");
108 assertNotNull("Couldn't Find title attribute.", attribute
109 .getValue());
110 assertEquals("Role 'myrole' expected", attribute.getRole(),
111 "myrole");
112
113 def = definitions.get("doc.listattribute.test");
114 assertNotNull("Couldn't find doc.listattribute.test tile.", def);
115 attribute = def.getAttribute("items");
116 assertNotNull("Couldn't Find items attribute.", attribute);
117 assertTrue("The class of the attribute is not right",
118 attribute instanceof ListAttribute);
119 assertTrue("The class of value of the attribute is not right",
120 attribute.getValue() instanceof List);
121 } catch (Exception e) {
122 fail("Exception reading configuration." + e);
123 }
124 }
125
126 /***
127 * Tests calling read without calling init.
128 */
129 public void testNoInit() {
130 try {
131 DefinitionsReader reader = new DigesterDefinitionsReader();
132
133
134
135
136 URL configFile = this.getClass().getClassLoader().getResource(
137 "org/apache/tiles/config/tiles-defs.xml");
138 assertNotNull("Config file not found", configFile);
139
140 InputStream source = configFile.openStream();
141 reader.read(source);
142
143 fail("Should've thrown exception.");
144 } catch (DefinitionsFactoryException e) {
145
146 if (LOG.isDebugEnabled()) {
147 LOG.debug("Exception caught, it is OK", e);
148 }
149 } catch (Exception e) {
150 fail("Exception reading configuration." + e);
151 }
152 }
153
154 /***
155 * Tests read with bad input source.
156 */
157 public void testBadSource() {
158 try {
159
160 DefinitionsReader reader = new DigesterDefinitionsReader();
161 Map<String, String> params = new HashMap<String, String>();
162
163
164 reader.init(params);
165
166
167 reader.read(new String("Bad Input"));
168 fail("Should've thrown an exception.");
169 } catch (DefinitionsFactoryException e) {
170
171 if (LOG.isDebugEnabled()) {
172 LOG.debug("Exception caught, it is OK", e);
173 }
174 } catch (Exception e) {
175 fail("Exception reading configuration." + e);
176 }
177 }
178
179 /***
180 * Tests read with bad XML source.
181 */
182 public void testBadXml() {
183 try {
184 DefinitionsReader reader = new DigesterDefinitionsReader();
185 reader.init(new HashMap<String, String>());
186
187 URL configFile = this.getClass().getClassLoader().getResource(
188 "org/apache/tiles/config/malformed-defs.xml");
189 assertNotNull("Config file not found", configFile);
190
191 InputStream source = configFile.openStream();
192 reader.read(source);
193 fail("Should've thrown an exception.");
194 } catch (DefinitionsFactoryException e) {
195
196 if (LOG.isDebugEnabled()) {
197 LOG.debug("Exception caught, it is OK", e);
198 }
199 } catch (Exception e) {
200 fail("Exception reading configuration." + e);
201 }
202 }
203
204 /***
205 * Tests the validating input parameter.
206 *
207 * This test case enables Digester's validating property then passes in a
208 * configuration file with invalid XML.
209 */
210 public void testValidatingParameter() {
211
212 try {
213 DefinitionsReader reader = new DigesterDefinitionsReader();
214 Map<String, String> params = new HashMap<String, String>();
215 reader.init(params);
216
217 URL configFile = this.getClass().getClassLoader().getResource(
218 "org/apache/tiles/config/invalid-defs.xml");
219 assertNotNull("Config file not found", configFile);
220
221 InputStream source = configFile.openStream();
222 reader.read(source);
223 fail("Should've thrown an exception.");
224 } catch (DefinitionsFactoryException e) {
225
226 if (LOG.isDebugEnabled()) {
227 LOG.debug("Exception caught, it is OK", e);
228 }
229 } catch (Exception e) {
230 fail("Exception reading configuration." + e);
231 }
232
233
234 try {
235 DefinitionsReader reader = new DigesterDefinitionsReader();
236 Map<String, String> params = new HashMap<String, String>();
237 params.put(DigesterDefinitionsReader.PARSER_VALIDATE_PARAMETER_NAME,
238 "false");
239 reader.init(params);
240
241 URL configFile = this.getClass().getClassLoader().getResource(
242 "org/apache/tiles/config/invalid-defs.xml");
243 assertNotNull("Config file not found", configFile);
244
245 InputStream source = configFile.openStream();
246 reader.read(source);
247 } catch (DefinitionsFactoryException e) {
248 fail("Should not have thrown an exception." + e);
249 } catch (Exception e) {
250 fail("Exception reading configuration." + e);
251 }
252 }
253 }