001/* $Id: TestXmlRuleInfo.java 1102402 2011-05-12 18:03:26Z simonetripodi $
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements.  See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.commons.digester3.plugins;
020
021import static org.junit.Assert.*;
022
023import java.io.StringReader;
024
025import org.apache.commons.digester3.Digester;
026import org.apache.commons.digester3.plugins.PluginCreateRule;
027import org.apache.commons.digester3.plugins.PluginDeclarationRule;
028import org.apache.commons.digester3.plugins.PluginRules;
029import org.junit.Test;
030
031/**
032 * Test cases for the declaration of custom rules for a plugin using xmlrules format files.
033 */
034
035public class TestXmlRuleInfo
036{
037
038    // --------------------------------------------------------------- Test cases
039    @Test
040    public void testXmlRuleInfoExplicitFile()
041        throws Exception
042    {
043        // * tests that custom rules can be declared on a
044        // separate class by explicitly declaring a file containing
045        // the rules, using a relative or absolute path name.
046
047        StringBuilder input = new StringBuilder();
048        input.append( "<root>" );
049        input.append( " <plugin" );
050        input.append( "  id='testobject'" );
051        input.append( "  class='org.apache.commons.digester3.plugins.ObjectTestImpl'" );
052        input.append( "  file='src/test/resources/org/apache/commons/digester3/plugins/xmlrules1.xml'" );
053        input.append( "  />" );
054        input.append( "  <object plugin-id='testobject'/>" );
055        input.append( "</root>" );
056
057        Digester digester = new Digester();
058        PluginRules rc = new PluginRules();
059        digester.setRules( rc );
060
061        PluginDeclarationRule pdr = new PluginDeclarationRule();
062        digester.addRule( "root/plugin", pdr );
063
064        PluginCreateRule pcr = new PluginCreateRule( ObjectTestImpl.class );
065        digester.addRule( "root/object", pcr );
066
067        try
068        {
069            digester.parse( new StringReader( input.toString() ) );
070        }
071        catch ( Exception e )
072        {
073            throw e;
074        }
075
076        Object root = digester.getRoot();
077        assertEquals( ObjectTestImpl.class, root.getClass() );
078        ObjectTestImpl testObject = (ObjectTestImpl) root;
079        assertEquals( "xmlrules1", testObject.getValue() );
080    }
081
082    @Test
083    public void testXmlRuleInfoExplicitResource()
084        throws Exception
085    {
086        // * tests that custom rules can be declared on a
087        // separate class by explicitly declaring the rule class.
088        // and explicitly declaring a file which is somewhere in the
089        // classpath.
090
091        StringBuilder input = new StringBuilder();
092        input.append( "<root>" );
093        input.append( " <plugin" );
094        input.append( "  id='testobject'" );
095        input.append( "  class='org.apache.commons.digester3.plugins.ObjectTestImpl'" );
096        input.append( "  resource='org/apache/commons/digester3/plugins/xmlrules2.xml'" );
097        input.append( "  />" );
098        input.append( "  <object plugin-id='testobject'/>" );
099        input.append( "</root>" );
100
101        Digester digester = new Digester();
102        PluginRules rc = new PluginRules();
103        digester.setRules( rc );
104
105        PluginDeclarationRule pdr = new PluginDeclarationRule();
106        digester.addRule( "root/plugin", pdr );
107
108        PluginCreateRule pcr = new PluginCreateRule( ObjectTestImpl.class );
109        digester.addRule( "root/object", pcr );
110
111        try
112        {
113            digester.parse( new StringReader( input.toString() ) );
114        }
115        catch ( Exception e )
116        {
117            throw e;
118        }
119
120        Object root = digester.getRoot();
121        assertEquals( ObjectTestImpl.class, root.getClass() );
122        ObjectTestImpl testObject = (ObjectTestImpl) root;
123        assertEquals( "xmlrules2", testObject.getValue() );
124    }
125
126    @Test
127    public void testXmlRuleImplicitResource()
128        throws Exception
129    {
130        // * tests that custom rules can be declared on a
131        // separate class by explicitly declaring the rule class.
132        // and explicitly declaring a file which is somewhere in the
133        // classpath.
134
135        StringBuilder input = new StringBuilder();
136        input.append( "<root>" );
137        input.append( " <plugin" );
138        input.append( "  id='testobject'" );
139        input.append( "  class='org.apache.commons.digester3.plugins.ObjectTestImpl'" );
140        input.append( "  />" );
141        input.append( "  <object plugin-id='testobject'/>" );
142        input.append( "</root>" );
143
144        Digester digester = new Digester();
145        PluginRules rc = new PluginRules();
146        digester.setRules( rc );
147
148        PluginDeclarationRule pdr = new PluginDeclarationRule();
149        digester.addRule( "root/plugin", pdr );
150
151        PluginCreateRule pcr = new PluginCreateRule( ObjectTestImpl.class );
152        digester.addRule( "root/object", pcr );
153
154        try
155        {
156            digester.parse( new StringReader( input.toString() ) );
157        }
158        catch ( Exception e )
159        {
160            throw e;
161        }
162
163        Object root = digester.getRoot();
164        assertEquals( ObjectTestImpl.class, root.getClass() );
165        ObjectTestImpl testObject = (ObjectTestImpl) root;
166        assertEquals( "xmlrules-ruleinfo", testObject.getValue() );
167    }
168}