Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id A39AC6524 for ; Thu, 26 May 2011 14:04:07 +0000 (UTC) Received: (qmail 75062 invoked by uid 500); 26 May 2011 14:04:07 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 74986 invoked by uid 500); 26 May 2011 14:04:07 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 74979 invoked by uid 99); 26 May 2011 14:04:07 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 26 May 2011 14:04:07 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 26 May 2011 14:04:03 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id CADBA2388906; Thu, 26 May 2011 14:03:41 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1127922 - in /commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/xmlrules: DigesterRulesSourceTestImpl.java FromXmlRuleSetTest.java Date: Thu, 26 May 2011 14:03:41 -0000 To: commits@commons.apache.org From: simonetripodi@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110526140341.CADBA2388906@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: simonetripodi Date: Thu May 26 14:03:41 2011 New Revision: 1127922 URL: http://svn.apache.org/viewvc?rev=1127922&view=rev Log: tests moved to the universal loader Modified: commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/xmlrules/DigesterRulesSourceTestImpl.java commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/xmlrules/FromXmlRuleSetTest.java Modified: commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/xmlrules/DigesterRulesSourceTestImpl.java URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/xmlrules/DigesterRulesSourceTestImpl.java?rev=1127922&r1=1127921&r2=1127922&view=diff ============================================================================== --- commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/xmlrules/DigesterRulesSourceTestImpl.java (original) +++ commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/xmlrules/DigesterRulesSourceTestImpl.java Thu May 26 14:03:41 2011 @@ -18,8 +18,7 @@ package org.apache.commons.digester3.xmlrules; -import org.apache.commons.digester3.Digester; -import org.apache.commons.digester3.xmlrules.DigesterRulesSource; +import org.apache.commons.digester3.binder.AbstractRulesModule; /** * A test class, for validating FromXmlRuleSet's ability to 'include' programmatically-created rules from within an XML @@ -29,19 +28,21 @@ import org.apache.commons.digester3.xmlr * @author Scott Sanders - Added ASL, removed external dependencies */ public class DigesterRulesSourceTestImpl - implements DigesterRulesSource + extends AbstractRulesModule { /** - * Creates and adds Digester Rules to a given Rules object - * - * @param digester the Digester to add the new Rule objects to + * {@inheritDoc} */ - public void getRules( Digester digester ) + @Override + protected void configure() { - digester.addObjectCreate( "/baz", ObjectTestImpl.class.getName() ); - digester.addSetNext( "/baz", "add", "java.lang.Object" ); - digester.addSetProperties( "/baz" ); + forPattern("baz") + .createObject().ofType( ObjectTestImpl.class ) + .then() + .setProperties() + .then() + .setNext( "add" ).withParameterType( "java.lang.Object" ); } } Modified: commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/xmlrules/FromXmlRuleSetTest.java URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/xmlrules/FromXmlRuleSetTest.java?rev=1127922&r1=1127921&r2=1127922&view=diff ============================================================================== --- commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/xmlrules/FromXmlRuleSetTest.java (original) +++ commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/xmlrules/FromXmlRuleSetTest.java Thu May 26 14:03:41 2011 @@ -18,12 +18,20 @@ package org.apache.commons.digester3.xmlrules; -import static org.junit.Assert.*; +import static org.apache.commons.digester3.binder.DigesterLoader.newLoader; +import static org.junit.Assert.assertEquals; +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 org.apache.commons.digester3.Address; import org.apache.commons.digester3.Digester; -import org.apache.commons.digester3.xmlrules.FromXmlRuleSet; +import org.apache.commons.digester3.ObjectCreationFactoryTestImpl; +import org.apache.commons.digester3.binder.RulesModule; import org.junit.Test; import org.xml.sax.InputSource; @@ -34,46 +42,314 @@ import org.xml.sax.InputSource; public class FromXmlRuleSetTest { + private final RulesModule createRules( final URL xmlRules ) + { + return new FromXmlRulesModule() + { + + @Override + protected void loadRules() + { + loadXMLRules( xmlRules ); + } + + }; + } + + private final RulesModule createRules( final String xmlText ) + { + return new FromXmlRulesModule() + { + + @Override + protected void loadRules() + { + loadXMLRulesFromText( xmlText ); + } + + }; + } + /** - * Test the FromXmlRules.addRuleInstances(digester, path) method, ie test loading rules at a base position other - * than the root. + * 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 testBasePath() + public void testCreateDigester() throws Exception { - String xmlRules = - "" + "" + " " + " " + " " - + ""; + URL rules = getClass().getResource( "testrules.xml" ); + URL input = getClass().getResource( "test.xml" ); + + Digester digester = newLoader( createRules( rules ) ).newDigester(); + digester.push( new ArrayList() ); + 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 = getClass().getResource( "testrules.xml" ); + URL input = getClass().getResource( "test.xml" ); - String xml = "" + "" + " success" + ""; + Digester digester = newLoader( createRules( rules ) ).setClassLoader( classLoader ).newDigester(); + digester.push( new ArrayList() ); - // First try with no base path. The rule shouldn't match, because - // foo is not the root element. + Object root = digester.parse( input ); + if ( !( root instanceof ArrayList ) ) { - ObjectTestImpl testObject = new ObjectTestImpl(); - FromXmlRuleSet ruleset = new FromXmlRuleSet( new InputSource( new StringReader( xmlRules ) ) ); - Digester digester = new Digester(); - ruleset.addRuleInstances( digester ); + 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 al = (ArrayList) root; + Object obj = al.get( 0 ); + if ( !( obj instanceof ObjectTestImpl ) ) + { + fail( "Unexpected object returned from DigesterLoader. Expected TestObject; got " + + obj.getClass().getName() ); + } + ObjectTestImpl to = (ObjectTestImpl) obj; + assertEquals( new Long( 555 ), to.getLongValue() ); + assertEquals( "foo", to.getMapValue( "test1" ) ); + assertEquals( "bar", to.getMapValue( "test2" ) ); + } - digester.push( testObject ); - digester.parse( new InputSource( new StringReader( xml ) ) ); + /** + * The same as testLoad1, exception the input file is passed to + * DigesterLoader as an InputStream instead of a URL. + */ + @Test + public void testLoad2() + throws Exception + { + URL rules = getClass().getResource( "testrules.xml" ); + InputStream input = getClass().getResourceAsStream( "test.xml" ); + Digester digester = + newLoader( createRules( rules ) ).setClassLoader( this.getClass().getClassLoader() ).newDigester(); + digester.push( new ArrayList() ); + + ArrayList list = digester.parse( input ); + + assertEquals( list.toString(), "[foo1 baz1 foo2, foo3 foo4]" ); + assertEquals( "Wrong number of classes created", 2, list.size() ); + assertEquals( "Pushed first", true, ( (ObjectTestImpl) list.get( 0 ) ).isPushed() ); + assertEquals( "Didn't push second", false, ( (ObjectTestImpl) list.get( 1 ) ).isPushed() ); + assertTrue( "Property was set properly", + ( (ObjectTestImpl) list.get( 0 ) ).getProperty().equals( "I am a property!" ) ); + } - assertEquals( "", testObject.getProperty() ); + /** + */ + @Test + public void testSetCustomProperties() + throws Exception + { + URL rules = this.getClass().getResource( "testPropertyAliasRules.xml" ); + InputStream input = + getClass().getClassLoader().getResource( "org/apache/commons/digester3/Test7.xml" ).openStream(); + + Digester digester = + newLoader( createRules( rules ) ).setClassLoader( this.getClass().getClassLoader() ).newDigester(); + digester.push( new ArrayList() ); + + Object obj = digester.parse( input ); + + if ( !( obj instanceof ArrayList ) ) + { + fail( "Unexpected object returned from DigesterLoader. Expected ArrayList; got " + obj.getClass().getName() ); } - // Now try with a base path. The rule should now match. + @SuppressWarnings("unchecked") // root is an ArrayList of Address + ArrayList
root = (ArrayList
) obj; + + assertEquals( "Wrong array size", 4, root.size() ); + + // note that the array is in popped order (rather than pushed) + + Address add = root.get( 0 ); + Address addressOne = add; + assertEquals( "(1) Street attribute", "New Street", addressOne.getStreet() ); + assertEquals( "(1) City attribute", "Las Vegas", addressOne.getCity() ); + assertEquals( "(1) State attribute", "Nevada", addressOne.getState() ); + + add = root.get( 1 ); + Address addressTwo = add; + assertEquals( "(2) Street attribute", "Old Street", addressTwo.getStreet() ); + assertEquals( "(2) City attribute", "Portland", addressTwo.getCity() ); + assertEquals( "(2) State attribute", "Oregon", addressTwo.getState() ); + + add = root.get( 2 ); + Address addressThree = add; + assertEquals( "(3) Street attribute", "4th Street", addressThree.getStreet() ); + assertEquals( "(3) City attribute", "Dayton", addressThree.getCity() ); + assertEquals( "(3) State attribute", "US", addressThree.getState() ); + + add = root.get( 3 ); + Address addressFour = add; + assertEquals( "(4) Street attribute", "6th Street", addressFour.getStreet() ); + assertEquals( "(4) City attribute", "Cleveland", addressFour.getCity() ); + assertEquals( "(4) State attribute", "Ohio", addressFour.getState() ); + } + + @Test + public void testFactoryCreateRule() + throws Exception + { + URL rules = getClass().getResource( "testfactory.xml" ); + String xml = ""; + + Digester digester = + newLoader( createRules( rules ) ).setClassLoader( this.getClass().getClassLoader() ).newDigester(); + digester.push( new ArrayList() ); + + Object obj = digester.parse( new StringReader( xml ) ); + if ( !( obj instanceof ArrayList ) ) { - ObjectTestImpl testObject = new ObjectTestImpl(); - FromXmlRuleSet ruleset = new FromXmlRuleSet( new InputSource( new StringReader( xmlRules ) ) ); - Digester digester = new Digester(); - ruleset.addRuleInstances( digester, "root" ); + fail( "Unexpected object returned from DigesterLoader. Expected ArrayList; got " + obj.getClass().getName() ); + } + + @SuppressWarnings("unchecked") // root is an ArrayList of TestObjectCreationFactory + ArrayList list = (ArrayList) obj; - digester.push( testObject ); - digester.parse( new InputSource( new StringReader( xml ) ) ); + assertEquals( "List should contain only the factory object", list.size(), 1 ); + ObjectCreationFactoryTestImpl factory = list.get( 0 ); + assertEquals( "Object create not called(1)", factory.called, true ); + assertEquals( "Attribute not passed (1)", factory.attributes.getValue( "one" ), "good" ); + assertEquals( "Attribute not passed (2)", factory.attributes.getValue( "two" ), "bad" ); + assertEquals( "Attribute not passed (3)", factory.attributes.getValue( "three" ), "ugly" ); + } - assertEquals( "success", testObject.getProperty() ); + @Test + public void testFactoryIgnoreCreateRule() + throws Exception + { + URL rules = getClass().getResource( "testfactoryignore.xml" ); + + String xml = ""; + try { + newLoader(createRules(rules)).newDigester().parse(new StringReader(xml)); + } catch (Exception e) { + fail("This exception should have been ignored: " + e.getClass().getName()); + } + } + + @Test + public void testFactoryNotIgnoreCreateRule() + throws Exception + { + URL rules = getClass().getResource( "testfactorynoignore.xml" ); + + String xml = ""; + try + { + newLoader( createRules( rules ) ).newDigester().parse( new StringReader( xml ) ); + fail( "Exception should have been propagated from create method." ); + } + catch ( Exception e ) + { + /* What we expected */ + assertEquals( org.xml.sax.SAXParseException.class, e.getClass() ); } } + + @Test + public void testCallParamRule() + throws Exception + { + URL rules = getClass().getResource( "test-call-param-rules.xml" ); + + String xml = "" + + "shorttosh"; + + CallParamTestObject testObject = new CallParamTestObject(); + + Digester digester = + newLoader( createRules( rules ) ).setClassLoader( this.getClass().getClassLoader() ).newDigester(); + digester.push( testObject ); + digester.parse( new StringReader( xml ) ); + + assertEquals( "Incorrect left value", "long", testObject.getLeft() ); + assertEquals( "Incorrect middle value", "short", testObject.getMiddle() ); + assertEquals( "Incorrect right value", "", testObject.getRight() ); + } + + @Test + public void testInputSourceLoader() throws Exception { + String rulesXml = "" + + "" + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + ""; + + String xml = "" + + "shorttosh"; + + CallParamTestObject testObject = new CallParamTestObject(); + + Digester digester = newLoader( createRules( rulesXml ) ).newDigester(); + digester.push( testObject ); + digester.parse( new StringReader( xml ) ); + + assertEquals( "Incorrect left value", "long", testObject.getLeft() ); + assertEquals( "Incorrect middle value", "short", testObject.getMiddle() ); + assertEquals( "Incorrect right value", "", testObject.getRight() ); + } + + /** + * Test the FromXmlRules.addRuleInstances(digester, path) method, ie + * test loading rules at a base position other than the root. + */ + @Test + public void testBasePath() + throws Exception + { + String xmlRules = + "" + + "" + + " " + + " " + + " " + + ""; + + String xml = + "" + + "" + + " success" + + ""; + + ObjectTestImpl testObject = new ObjectTestImpl(); + Digester digester = newLoader( createRules( xmlRules ) ).newDigester(); + + digester.push( testObject ); + digester.parse( new InputSource( new StringReader( xml ) ) ); + + assertEquals( "success", testObject.getProperty() ); + } }