alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons Digester example source code file (SetPropertiesRuleTestCase.java)

This example Commons Digester source code file (SetPropertiesRuleTestCase.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons Digester tags/keywords

beta, delta, exception, exception, io, nosuchmethodexception, sax, setpropertiesrule, should, simpletestbean, simpletestbean, string, string, test, value, value

The Commons Digester SetPropertiesRuleTestCase.java source code

/* $Id: SetPropertiesRuleTestCase.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.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import java.io.Reader;
import java.io.StringReader;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.xml.sax.SAXException;


/**
 * <p> Test case for SetPropertiesRule.

*/ public class SetPropertiesRuleTestCase { // ----------------------------------------------------- Instance Variables /** * Simple test xml document used in the tests. */ protected final static String TEST_XML_1 = "<?xml version='1.0'?>"; /** * Simple test xml document used in the tests. */ protected final static String TEST_XML_2 = "<?xml version='1.0'?>"; /** * Simple test xml document used in the tests. */ protected final static String TEST_XML_3 = "<?xml version='1.0'?>"; /** * The digester instance we will be processing. */ protected Digester digester = null; // --------------------------------------------------- Overall Test Methods /** * Set up instance variables required by this test case. */ @Before public void setUp() { digester = new Digester(); } /** * Tear down instance variables required by this test case. */ @After public void tearDown() { digester = null; } // ------------------------------------------------ Individual Test Methods /** * Positive test for SetPropertiesRule. */ @Test public void testPositive() throws Exception { // Set up the rules we need digester.addObjectCreate("root", "org.apache.commons.digester.SimpleTestBean"); digester.addSetProperties("root"); // Parse the input SimpleTestBean bean = (SimpleTestBean) digester.parse(xmlTestReader(TEST_XML_1)); // Check that the properties were set correctly assertEquals("alpha property set", "ALPHA VALUE", bean.getAlpha()); assertEquals("beta property set", "BETA VALUE", bean.getBeta()); assertNull("gamma property not set", bean.getGamma()); assertEquals("delta property set", "DELTA VALUE", bean.getDeltaValue()); } /** * Positive test for SetPropertyRule ignoring missing properties. */ @Test public void testIgnoreMissing() throws Exception { // Set up the rules we need digester.addObjectCreate("root", "org.apache.commons.digester.SimpleTestBean"); digester.addSetProperties("root"); // Parse the input SimpleTestBean bean = (SimpleTestBean) digester.parse(xmlTestReader(TEST_XML_2)); // Check that the properties were set correctly assertNull("alpha property not set", bean.getAlpha()); assertEquals("beta property set", "BETA VALUE", bean.getBeta()); assertNull("gamma property not set", bean.getGamma()); assertEquals("delta property set", "DELTA VALUE", bean.getDeltaValue()); } /** * Negative test for SetPropertyRule ignoring missing properties. */ @Test public void testNegativeNotIgnoreMissing() throws Exception { // Set up the rules we need digester.addObjectCreate("root", "org.apache.commons.digester.SimpleTestBean"); SetPropertiesRule rule = new SetPropertiesRule(); rule.setIgnoreMissingProperty(false); digester.addRule("root", rule); try { // Parse the input SimpleTestBean bean = (SimpleTestBean) digester.parse(xmlTestReader(TEST_XML_2)); fail("Should have thrown NoSuchMethodException"); assertNotNull(bean); // just to prevent compiler warning on unused var } catch (Exception e) { if (e instanceof NoSuchMethodException) { // Expected; } else if (e instanceof SAXException) { Exception ee = ((SAXException) e).getException(); if (ee != null) { if (ee instanceof NoSuchMethodException) { // Expected result } else { fail("Should have thrown SE->NoSuchMethodException, threw " + ee); } } else { fail("Should have thrown NoSuchMethodException, threw " + e.getClass().getName()); } } else { fail("Should have thrown NoSuchMethodException, threw " + e); } } } /** * Negative test for SetPropertyRule ignoring missing properties. */ @Test public void testPositiveNotIgnoreMissingWithIgnoreAttributes() throws Exception { // Set up the rules we need digester.addObjectCreate("root", "org.apache.commons.digester.SimpleTestBean"); SetPropertiesRule rule = new SetPropertiesRule(new String[] {"ignore"}, new String[] {}); rule.setIgnoreMissingProperty(false); digester.addRule("root", rule); // Parse the input SimpleTestBean bean = (SimpleTestBean) digester.parse(xmlTestReader(TEST_XML_3)); // Check that the properties were set correctly assertEquals("alpha property set", "ALPHA VALUE", bean.getAlpha()); assertEquals("beta property set", "BETA VALUE", bean.getBeta()); assertNull("gamma property not set", bean.getGamma()); assertEquals("delta property set", "DELTA VALUE", bean.getDeltaValue()); } /** * Get input stream from specified String containing XML data. */ private Reader xmlTestReader(String xml) { return new StringReader(xml); } }

Other Commons Digester examples (source code examples)

Here is a short list of links related to this Commons Digester SetPropertiesRuleTestCase.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

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.