DigesterDefinitionsReader implements DefinitionsReader { public Map<String, Definition> read(Object source) { definitions = new LinkedHashMap<String, Definition>(); if (source == null) { return null; } InputStream input; try { input = (InputStream) source; } catch (ClassCastException e) { throw new DefinitionsFactoryException( "Invalid source type. Requires java.io.InputStream.", e); } try { digester.push(this); digester.parse(input); } catch (SAXException e) { throw new DefinitionsFactoryException( "XML error reading definitions.", e); } catch (IOException e) { throw new DefinitionsFactoryException( "I/O Error reading definitions.", e); } finally { digester.clear(); } return definitions; } DigesterDefinitionsReader(); void setValidating(boolean validating); Map<String, Definition> read(Object source); void addDefinition(Definition definition); static final String PARSER_VALIDATE_PARAMETER_NAME; }
@Test public void testRead() throws IOException { URL configFile = this.getClass().getClassLoader().getResource( "org/apache/tiles/config/tiles-defs.xml"); assertNotNull("Config file not found", configFile); InputStream source = configFile.openStream(); Map<String, Definition> definitions = reader.read(source); assertNotNull("Definitions not returned.", definitions); assertNotNull("Couldn't find doc.mainLayout tile.", definitions.get("doc.mainLayout")); assertNotNull("Couldn't Find title attribute.", definitions.get( "doc.mainLayout").getAttribute("title").getValue()); assertEquals("Incorrect Find title attribute.", "Tiles Library Documentation", definitions.get( "doc.mainLayout").getAttribute("title").getValue()); Definition def = definitions.get("doc.role.test"); assertNotNull("Couldn't find doc.role.test tile.", def); Attribute attribute = def.getAttribute("title"); assertNotNull("Couldn't Find title attribute.", attribute .getValue()); assertEquals("Role 'myrole' expected", attribute.getRole(), "myrole"); def = definitions.get("doc.listattribute.test"); assertNotNull("Couldn't find doc.listattribute.test tile.", def); attribute = def.getAttribute("items"); assertNotNull("Couldn't Find items attribute.", attribute); assertTrue("The class of the attribute is not right", attribute instanceof ListAttribute); assertTrue("The class of value of the attribute is not right", attribute.getValue() instanceof List); }
@Test public void testRead21Version() throws IOException { URL configFile = this.getClass().getClassLoader().getResource( "org/apache/tiles/config/tiles-defs-2.1.xml"); assertNotNull("Config file not found", configFile); InputStream source = configFile.openStream(); Map<String, Definition> definitions = reader.read(source); assertNotNull("Definitions not returned.", definitions); Definition def = definitions.get("doc.cascaded.test"); assertNotNull("Couldn't find doc.role.test tile.", def); Attribute attribute = def.getLocalAttribute("title"); assertNotNull("Couldn't Find title local attribute.", attribute); attribute = def.getCascadedAttribute("title2"); assertNotNull("Couldn't Find title2 cascaded attribute.", attribute); attribute = def.getLocalAttribute("items1"); assertNotNull("Couldn't Find items1 local attribute.", attribute); attribute = def.getCascadedAttribute("items2"); assertNotNull("Couldn't Find items2 cascaded attribute.", attribute); def = definitions.get("test.nesting.definitions"); assertNotNull("Couldn't find test.nesting.definitions tile.", def); assertEquals("/layout.jsp", def.getTemplateAttribute().getValue()); assertEquals("template", def.getTemplateAttribute().getRenderer()); attribute = def.getAttribute("body"); assertNotNull("Couldn't Find body attribute.", attribute); assertEquals("Attribute not of 'definition' type", "definition", attribute.getRenderer()); assertNotNull("Attribute value null", attribute.getValue()); String defName = attribute.getValue().toString(); def = definitions.get(defName); assertNotNull("Couldn't find " + defName + " tile.", def); def = definitions.get("test.nesting.list.definitions"); assertNotNull("Couldn't find test.nesting.list.definitions tile.", def); attribute = def.getAttribute("list"); assertNotNull("Couldn't Find list attribute.", attribute); assertTrue("Attribute not of valid type", attribute instanceof ListAttribute); ListAttribute listAttribute = (ListAttribute) attribute; List<Attribute> list = listAttribute.getValue(); assertEquals("The list is not of correct size", 1, list.size()); attribute = list.get(0); assertNotNull("Couldn't Find element attribute.", attribute); assertEquals("Attribute not of 'definition' type", "definition", attribute.getRenderer()); assertNotNull("Attribute value null", attribute.getValue()); defName = attribute.getValue().toString(); def = definitions.get(defName); assertNotNull("Couldn't find " + defName + " tile.", def); defName = "test.inherit.list.base"; def = definitions.get(defName); assertNotNull("Couldn't find " + defName + " tile.", def); defName = "test.inherit.list"; def = definitions.get(defName); assertNotNull("Couldn't find " + defName + " tile.", def); listAttribute = (ListAttribute) def.getAttribute("list"); assertEquals("This definition does not inherit its list attribute", true, listAttribute.isInherit()); defName = "test.noinherit.list"; def = definitions.get(defName); listAttribute = (ListAttribute) def.getAttribute("list"); assertEquals("This definition inherits its list attribute", false, listAttribute.isInherit()); defName = "test.new.attributes"; def = definitions.get(defName); assertNotNull("Couldn't find " + defName + " tile.", def); Attribute templateAttribute = def.getTemplateAttribute(); assertEquals(templateAttribute.getExpressionObject().getExpression(), "${my.expression}"); assertEquals("mytype", templateAttribute.getRenderer()); attribute = def.getAttribute("body"); assertNotNull("Couldn't Find body attribute.", attribute); assertEquals("${my.attribute.expression}", attribute .getExpressionObject().getExpression()); }
@Test public void testBadSource() { try { reader.read(new String("Bad Input")); fail("Should've thrown an exception."); } catch (DefinitionsFactoryException e) { if (log.isDebugEnabled()) { log.debug("Exception caught, it is OK", e); } } catch (Exception e) { fail("Exception reading configuration." + e); } }
@Test public void testBadXml() { try { URL configFile = this.getClass().getClassLoader().getResource( "org/apache/tiles/config/malformed-defs.xml"); assertNotNull("Config file not found", configFile); InputStream source = configFile.openStream(); reader.read(source); fail("Should've thrown an exception."); } catch (DefinitionsFactoryException e) { if (log.isDebugEnabled()) { log.debug("Exception caught, it is OK", e); } } catch (Exception e) { fail("Exception reading configuration." + e); } }
@Test public void testRegressionTiles352() throws IOException { URL configFile = this.getClass().getClassLoader().getResource( "org/apache/tiles/config/defs_regression_TILES-352.xml"); assertNotNull("Config file not found", configFile); InputStream source = configFile.openStream(); Map<String, Definition> name2defs = reader.read(source); source.close(); Definition root = name2defs.get("root"); Attribute attribute = root.getAttribute("body"); Definition child = name2defs.get(attribute.getValue()); ListAttribute listAttribute = (ListAttribute) child.getAttribute("list"); List<Attribute> list = listAttribute.getValue(); assertEquals((list.get(0)).getValue(), "This is a value"); }
@Test public void testReadNoSource() { assertNull(reader.read(null)); }