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
25 import java.io.IOException;
26 import java.io.InputStream;
27
28 import org.apache.commons.digester3.binder.AbstractRulesModule;
29 import org.junit.Test;
30 import org.xml.sax.SAXException;
31
32
33
34
35
36
37 public class XIncludeTestCase
38 {
39
40
41
42
43
44
45 @Test
46 public void testXInclude()
47 throws SAXException, IOException
48 {
49 Digester digester = newLoader( new AbstractRulesModule()
50 {
51
52 @Override
53 protected void configure()
54 {
55 forPattern( "employee" ).createObject().ofType( Employee.class );
56 forPattern( "employee/firstName" ).callMethod( "setFirstName" ).usingElementBodyAsArgument();
57 forPattern( "employee/lastName" ).callMethod( "setLastName" ).usingElementBodyAsArgument();
58
59 forPattern( "employee/address" ).createObject().ofType( Address.class )
60 .then()
61 .setNext( "addAddress" );
62 forPattern( "employee/address/type" ).callMethod( "setType" ).usingElementBodyAsArgument();
63 forPattern( "employee/address/city" ).callMethod( "setCity" ).usingElementBodyAsArgument();
64 forPattern( "employee/address/state" ).callMethod( "setState" ).usingElementBodyAsArgument();
65 }
66
67 })
68 .setNamespaceAware( true )
69 .setXIncludeAware( true )
70 .newDigester();
71
72
73 Employee employee = digester.parse( getInputStream( "Test12.xml" ) );
74 assertNotNull( "failed to parsed an employee", employee );
75
76
77 assertEquals( "First Name", employee.getFirstName() );
78 assertEquals( "Last Name", employee.getLastName() );
79
80
81 Address ha = employee.getAddress( "home" );
82 assertNotNull( ha );
83 assertEquals( "Home City", ha.getCity() );
84 assertEquals( "HS", ha.getState() );
85 Address oa = employee.getAddress( "office" );
86 assertNotNull( oa );
87 assertEquals( "Office City", oa.getCity() );
88 assertEquals( "OS", oa.getState() );
89
90 }
91
92
93
94
95
96
97
98
99
100 protected InputStream getInputStream( String name )
101 throws IOException
102 {
103
104 return ( this.getClass().getResourceAsStream( "/org/apache/commons/digester3/" + name ) );
105
106 }
107
108 }