View Javadoc

1   /*
2    * $Id: TestDigesterDefinitionsReader.java 628347 2008-02-16 16:28:14Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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             // What happens if we don't call init?
134             // reader.init(new HashMap());
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             // correct.
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             // Create Digester Reader.
160             DefinitionsReader reader = new DigesterDefinitionsReader();
161             Map<String, String> params = new HashMap<String, String>();
162 
163             // Initialize reader.
164             reader.init(params);
165 
166             // Read definitions.
167             reader.read(new String("Bad Input"));
168             fail("Should've thrown an exception.");
169         } catch (DefinitionsFactoryException e) {
170             // correct.
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             // correct.
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         // Testing with default (validation ON).
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             // correct.
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         // Testing with validation OFF.
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 }