1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.digester3.plugins;
20
21 import static org.junit.Assert.*;
22
23 import java.io.StringReader;
24
25 import org.apache.commons.digester3.Digester;
26 import org.apache.commons.digester3.plugins.PluginCreateRule;
27 import org.apache.commons.digester3.plugins.PluginDeclarationRule;
28 import org.apache.commons.digester3.plugins.PluginRules;
29 import org.junit.Test;
30
31
32
33
34
35 public class TestXmlRuleInfo
36 {
37
38
39 @Test
40 public void testXmlRuleInfoExplicitFile()
41 throws Exception
42 {
43
44
45
46
47 StringBuilder input = new StringBuilder();
48 input.append( "<root>" );
49 input.append( " <plugin" );
50 input.append( " id='testobject'" );
51 input.append( " class='org.apache.commons.digester3.plugins.ObjectTestImpl'" );
52 input.append( " file='src/test/resources/org/apache/commons/digester3/plugins/xmlrules1.xml'" );
53 input.append( " />" );
54 input.append( " <object plugin-id='testobject'/>" );
55 input.append( "</root>" );
56
57 Digester digester = new Digester();
58 PluginRules rc = new PluginRules();
59 digester.setRules( rc );
60
61 PluginDeclarationRule pdr = new PluginDeclarationRule();
62 digester.addRule( "root/plugin", pdr );
63
64 PluginCreateRule pcr = new PluginCreateRule( ObjectTestImpl.class );
65 digester.addRule( "root/object", pcr );
66
67 try
68 {
69 digester.parse( new StringReader( input.toString() ) );
70 }
71 catch ( Exception e )
72 {
73 throw e;
74 }
75
76 Object root = digester.getRoot();
77 assertEquals( ObjectTestImpl.class, root.getClass() );
78 ObjectTestImpl testObject = (ObjectTestImpl) root;
79 assertEquals( "xmlrules1", testObject.getValue() );
80 }
81
82 @Test
83 public void testXmlRuleInfoExplicitResource()
84 throws Exception
85 {
86
87
88
89
90
91 StringBuilder input = new StringBuilder();
92 input.append( "<root>" );
93 input.append( " <plugin" );
94 input.append( " id='testobject'" );
95 input.append( " class='org.apache.commons.digester3.plugins.ObjectTestImpl'" );
96 input.append( " resource='org/apache/commons/digester3/plugins/xmlrules2.xml'" );
97 input.append( " />" );
98 input.append( " <object plugin-id='testobject'/>" );
99 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
131
132
133
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 }