|
Commons Digester example source code file (DigesterTestCase.java)
The Commons Digester DigesterTestCase.java source code/* $Id: DigesterTestCase.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; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.File; import java.io.InputStream; import java.io.Reader; import java.io.StringReader; import java.math.BigDecimal; import java.net.URL; import java.util.ArrayList; import java.util.EmptyStackException; import java.util.Map; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.helpers.AttributesImpl; /** * <p>Test Case for the Digester class. These tests exercise the individual * methods of a Digester, but do not attempt to process complete documents. * </p> * * @author Craig R. McClanahan * @version $Revision: 992060 $ $Date: 2010-09-02 21:09:47 +0200 (Thu, 02 Sep 2010) $ */ public class DigesterTestCase { // ----------------------------------------------------- Instance Variables /** * The digester instance we will be processing. */ protected Digester digester = null; /** * The set of public identifiers, and corresponding resource names, * for the versions of the DTDs that we know about. There * <strong>MUST be an even number of Strings in this array. */ protected static final String registrations[] = { "-//Netscape Communications//DTD RSS 0.9//EN", "/org/apache/commons/digester/rss/rss-0.9.dtd", "-//Netscape Communications//DTD RSS 0.91//EN", "/org/apache/commons/digester/rss/rss-0.91.dtd", }; // -------------------------------------------------- Overall Test Methods /** * Set up instance variables required by this test case. */ @Before public void setUp() { digester = new Digester(); digester.setRules(new RulesBase()); } /** * Tear down instance variables required by this test case. */ @After public void tearDown() { digester = null; } // ------------------------------------------------ Individual Test Methods /** * Test <code>null parsing. * (should lead to <code>IllegalArgumentExceptions) */ @Test public void testNullFileParse() throws Exception { try { digester.parse((File) null); fail("Expected IllegalArgumentException with null argument"); } catch (IllegalArgumentException e) { // expected } } @Test public void testNullInputSourceParse() throws Exception { try { digester.parse((InputSource) null); fail("Expected IllegalArgumentException with null argument"); } catch (IllegalArgumentException e) { // expected } } @Test public void testNullInputStreamParse() throws Exception { try { digester.parse((InputStream) null); fail("Expected IllegalArgumentException with null argument"); } catch (IllegalArgumentException e) { // expected } } @Test public void testNullReaderParse() throws Exception { try { digester.parse((Reader) null); fail("Expected IllegalArgumentException with null argument"); } catch (IllegalArgumentException e) { // expected } } @Test public void testNullStringParse() throws Exception { try { digester.parse((String) null); fail("Expected IllegalArgumentException with null argument"); } catch (IllegalArgumentException e) { // expected } } @Test public void testNullURLParse() throws Exception { try { digester.parse((URL) null); fail("Expected IllegalArgumentException with null argument"); } catch (IllegalArgumentException e) { // expected } } /** * Test the basic property getters and setters. */ @Test public void testProperties() { assertNull("Initial error handler is null", digester.getErrorHandler()); digester.setErrorHandler(digester); assertTrue("Set error handler is digester", digester.getErrorHandler() == digester); digester.setErrorHandler(null); assertNull("Reset error handler is null", digester.getErrorHandler()); assertTrue("Initial namespace aware is false", !digester.getNamespaceAware()); digester.setNamespaceAware(true); assertTrue("Set namespace aware is true", digester.getNamespaceAware()); digester.setNamespaceAware(false); assertTrue("Reset namespace aware is false", !digester.getNamespaceAware()); assertTrue("Initial validating is false", !digester.getValidating()); digester.setValidating(true); assertTrue("Set validating is true", digester.getValidating()); digester.setValidating(false); assertTrue("Reset validating is false", !digester.getValidating()); } /** * Test registration of URLs for specified public identifiers. */ @Test public void testRegistrations() { Map<String, URL> map = digester.getRegistrations(); assertEquals("Initially zero registrations", 0, map.size()); int n = 0; for (int i = 0; i < registrations.length; i += 2) { URL url = this.getClass().getResource(registrations[i + 1]); if (url != null) { digester.register(registrations[i], url); n++; } } map = digester.getRegistrations(); assertEquals("Registered two URLs", n, map.size()); int count[] = new int[n]; for (int i = 0; i < n; i++) count[i] = 0; for (String key : map.keySet()) { for (int i = 0; i < n; i++) { if (key.equals(registrations[i * 2])) { count[i]++; break; } } } for (int i = 0; i < n; i++) assertEquals("Count for key " + registrations[i * 2], 1, count[i]); } /** * Basic test for rule creation and matching. */ @Test public void testRules() { assertEquals("Initial rules list is empty", 0, digester.getRules().match(null, "a").size()); digester.addSetProperties("a"); assertEquals("Add a matching rule", 1, digester.getRules().match(null, "a").size()); digester.addSetProperties("b"); assertEquals("Add a non-matching rule", 1, digester.getRules().match(null, "a").size()); digester.addSetProperties("a/b"); assertEquals("Add a non-matching nested rule", 1, digester.getRules().match(null, "a").size()); digester.addSetProperties("a/b"); assertEquals("Add a second matching rule", 2, digester.getRules().match(null, "a/b").size()); } /** * <p>Test matching rules in {@link RulesBase}. * * <p>Tests: * <ul> * <li>exact match * <li>tail match * <li>longest pattern rule * </ul> */ @Test public void testRulesBase() { assertEquals("Initial rules list is empty", 0, digester.getRules().rules().size()); // We're going to set up digester.addRule("a/b/c/d", new TestRule("a/b/c/d")); digester.addRule("*/d", new TestRule("*/d")); digester.addRule("*/c/d", new TestRule("*/c/d")); // Test exact match assertEquals("Exact match takes precedence 1", 1, digester.getRules().match(null, "a/b/c/d").size()); assertEquals("Exact match takes precedence 2", "a/b/c/d", ((TestRule) digester.getRules().match(null, "a/b/c/d").iterator().next()).getIdentifier()); // Test wildcard tail matching assertEquals("Wildcard tail matching rule 1", 1, digester.getRules().match(null, "a/b/d").size()); assertEquals("Wildcard tail matching rule 2", "*/d", ((TestRule) digester.getRules().match(null, "a/b/d").iterator().next()).getIdentifier()); // Test the longest matching pattern rule assertEquals("Longest tail rule 1", 1, digester.getRules().match(null, "x/c/d").size()); assertEquals("Longest tail rule 2", "*/c/d", ((TestRule) digester.getRules().match(null, "x/c/d").iterator().next()).getIdentifier()); } /** * Test the basic stack mechanisms. */ @Test public void testStackMethods() { Object value = null; // New stack must be empty assertEquals("New stack is empty", 0, digester.getCount()); value = digester.peek(); assertNull("New stack peek() returns null", value); value = digester.pop(); assertNull("New stack pop() returns null", value); // Test pushing and popping activities digester.push("First Item"); assertEquals("Pushed one item size", 1, digester.getCount()); value = digester.peek(); assertNotNull("Peeked first item is not null", value); assertEquals("Peeked first item value", "First Item", (String) value); digester.push("Second Item"); assertEquals("Pushed two items size", 2, digester.getCount()); value = digester.peek(); assertNotNull("Peeked second item is not null", value); assertEquals("Peeked second item value", "Second Item", (String) value); value = digester.pop(); assertEquals("Popped stack size", 1, digester.getCount()); assertNotNull("Popped second item is not null", value); assertEquals("Popped second item value", "Second Item", (String) value); value = digester.peek(); assertNotNull("Remaining item is not null", value); assertEquals("Remaining item value", "First Item", (String) value); assertEquals("Remaining stack size", 1, digester.getCount()); // Cleared stack is empty digester.push("Dummy Item"); digester.clear(); assertEquals("Cleared stack is empty", 0, digester.getCount()); value = digester.peek(); assertNull("Cleared stack peek() returns null", value); value = digester.pop(); assertNull("Cleared stack pop() returns null", value); } @Test public void testOnceAndOnceOnly() throws Exception { class TestConfigureDigester extends Digester { public int called=0; public TestConfigureDigester() {} @Override protected void initialize() { called++; } } TestConfigureDigester digester = new TestConfigureDigester(); String xml = "<?xml version='1.0'?> Other Commons Digester examples (source code examples)Here is a short list of links related to this Commons Digester DigesterTestCase.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.