The Commons Digester DigesterLoaderTest.java source code
/* $Id: DigesterLoaderTest.java 992060 2010-09-02 19:09:47Z simonetripodi $
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.digester.xmlrules;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.InputStream;
import java.io.StringReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.digester.Address;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.ObjectCreationFactoryTestImpl;
import org.junit.Test;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
/**
* Tests loading Digester rules from an XML file.
*
* @author David H. Martin - Initial Contribution
* @author Scott Sanders - Added ASL, removed external dependencies
*/
public class DigesterLoaderTest {
/**
* Tests the DigesterLoader.createDigester(), with multiple
* included rule sources: testrules.xml includes another rules xml
* file, and also includes programmatically created rules.
*/
@Test
public void testCreateDigester() throws Exception {
URL rules = getClass().getClassLoader().getResource("org/apache/commons/digester/xmlrules/testrules.xml");
URL input = getClass().getClassLoader().getResource("org/apache/commons/digester/xmlrules/test.xml");
assertNotNull("The test could not locate testrules.xml", rules);
assertNotNull("The test could not locate test.xml", input);
Digester digester = DigesterLoader.createDigester(rules);
digester.push(new ArrayList<Object>());
Object root = digester.parse(input.openStream());
assertEquals("[foo1 baz1 foo2, foo3 foo4]",root.toString());
}
/**
* Tests the DigesterLoader.load(), with multiple included rule
* sources: testrules.xml includes another rules xml file, and
* also includes programmatically created rules.
*/
@Test
public void testLoad1() throws Exception {
ClassLoader classLoader = getClass().getClassLoader();
URL rules = classLoader.getResource("org/apache/commons/digester/xmlrules/testrules.xml");
URL input = classLoader.getResource("org/apache/commons/digester/xmlrules/test.xml");
assertNotNull("The test could not locate testrules.xml", rules);
assertNotNull("The test could not locate test.xml", input);
Object root = DigesterLoader.load(rules, classLoader, input, new ArrayList<Object>());
if (!(root instanceof ArrayList<?>)) {
fail("Unexpected object returned from DigesterLoader. Expected ArrayList; got " + root.getClass().getName());
}
assertEquals( "[foo1 baz1 foo2, foo3 foo4]",root.toString());
@SuppressWarnings("unchecked") // root is an ArrayList
ArrayList<Object> al = (ArrayList