1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.digester3;
20
21 import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.IOException;
27 import java.io.StringReader;
28 import java.util.ArrayList;
29
30 import org.apache.commons.digester3.binder.AbstractRulesModule;
31 import org.junit.Test;
32 import org.xml.sax.SAXException;
33
34
35
36
37
38
39
40 public class ObjectParamRuleTestCase
41 {
42
43
44
45 private StringBuilder sb =
46 new StringBuilder().append( "<arraylist><A/><B/><C/><D desc=\"the fourth\"/><E/></arraylist>" );
47
48
49
50
51
52 @Test
53 public void testBasic()
54 throws SAXException, IOException
55 {
56 Digester digester = newLoader( new AbstractRulesModule()
57 {
58
59 @Override
60 protected void configure()
61 {
62 forPattern( "arraylist" ).createObject().ofType( ArrayList.class );
63 forPattern( "arraylist/A" ).callMethod( "add" ).withParamCount( 1 )
64 .then()
65 .objectParam( new Integer( -9 ) );
66 forPattern( "arraylist/B" ).callMethod( "add" ).withParamCount( 1 )
67 .then()
68 .objectParam( new Float( 3.14159 ) );
69 forPattern( "arraylist/C" ).callMethod( "add" ).withParamCount( 1 )
70 .then()
71 .objectParam( new Long( 999999999 ) );
72 forPattern( "arraylist/D" ).callMethod( "add" ).withParamCount( 1 )
73 .then()
74 .objectParam( new String( "foobarbazbing" ) ).matchingAttribute( "desc" );
75 forPattern( "arraylist/E" ).callMethod( "add" ).withParamCount( 1 )
76 .then()
77 .objectParam( new String( "ignore" ) ).matchingAttribute( "nonexistentattribute" );
78 }
79
80 }).newDigester();
81
82
83 ArrayList<?> al = digester.parse( new StringReader( sb.toString() ) );
84 assertNotNull( al );
85 assertEquals( al.size(), 4 );
86 assertTrue( al.contains( new Integer( -9 ) ) );
87 assertTrue( al.contains( new Float( 3.14159 ) ) );
88 assertTrue( al.contains( new Long( 999999999 ) ) );
89 assertTrue( al.contains( new String( "foobarbazbing" ) ) );
90 assertTrue( !( al.contains( new String( "ignore" ) ) ) );
91 }
92
93 }