1 package org.apache.commons.digester3.substitution;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25
26 import org.apache.commons.digester3.Substitutor;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.xml.sax.Attributes;
30 import org.xml.sax.helpers.AttributesImpl;
31
32 public final class CompoundSubstitutorTestCase
33 {
34
35
36 private static class SubstitutorStub
37 extends Substitutor
38 {
39
40 private String newBodyText;
41
42 private String uri;
43
44 private String localName;
45
46 private String type;
47
48 private String value;
49
50 public SubstitutorStub( String bodyText, String uri, String localName, String type, String value )
51 {
52 this.newBodyText = bodyText;
53 this.uri = uri;
54 this.localName = localName;
55 this.type = type;
56 this.value = value;
57 }
58
59
60
61
62 @Override
63 public Attributes substitute( Attributes attributes )
64 {
65 AttributesImpl attribs = new AttributesImpl( attributes );
66 attribs.addAttribute( uri, localName, uri + ":" + localName, type, value );
67 return attribs;
68 }
69
70
71
72
73 @Override
74 public String substitute( String bodyText )
75 {
76 return newBodyText;
77 }
78
79 }
80
81 private Attributes attrib;
82
83 private String bodyText;
84
85 @Before
86 public void setUp()
87 {
88 AttributesImpl aImpl = new AttributesImpl();
89 aImpl.addAttribute( "", "b", ":b", "", "bcd" );
90 aImpl.addAttribute( "", "c", ":c", "", "cde" );
91 aImpl.addAttribute( "", "d", ":d", "", "def" );
92
93 attrib = aImpl;
94 bodyText = "Amazing Body Text!";
95 }
96
97 @Test
98 public void testConstructors()
99 {
100 try
101 {
102 new CompoundSubstitutor( null, null );
103 fail();
104 }
105 catch ( IllegalArgumentException e )
106 {
107
108 }
109
110 Substitutor a = new SubstitutorStub( "XYZ", "", "a", "", "abc" );
111
112 try
113 {
114 new CompoundSubstitutor( a, null );
115 fail();
116 }
117 catch ( IllegalArgumentException e )
118 {
119
120 }
121
122 try
123 {
124 new CompoundSubstitutor( null, a );
125 fail();
126 }
127 catch ( IllegalArgumentException e )
128 {
129
130 }
131 }
132
133 @Test
134 public void testChaining()
135 {
136 Substitutor a = new SubstitutorStub( "XYZ", "", "a", "", "abc" );
137 Substitutor b = new SubstitutorStub( "STU", "", "b", "", "bcd" );
138
139 Substitutor test = new CompoundSubstitutor( a, b );
140
141 AttributesImpl attribFixture = new AttributesImpl( attrib );
142 attribFixture.addAttribute( "", "a", ":a", "", "abc" );
143 attribFixture.addAttribute( "", "b", ":b", "", "bcd" );
144
145 assertTrue( areEqual( test.substitute( attrib ), attribFixture ) );
146 assertEquals( test.substitute( bodyText ), "STU" );
147 }
148
149 private boolean areEqual( Attributes a, Attributes b )
150 {
151 if ( a.getLength() != b.getLength() )
152 {
153 return false;
154 }
155
156 boolean success = true;
157 for ( int i = 0; i < a.getLength() && success; i++ )
158 {
159 success = a.getLocalName( i ).equals( b.getLocalName( i ) )
160 && a.getQName( i ).equals( b.getQName( i ) )
161 && a.getType( i ).equals( b.getType( i ) )
162 && a.getURI( i ).equals( b.getURI( i ) )
163 && a.getValue( i ).equals( b.getValue( i ) );
164 }
165
166 return success;
167 }
168
169 }