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.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.IOException;
27 import java.io.InputStream;
28
29 import javax.xml.XMLConstants;
30 import javax.xml.validation.Schema;
31 import javax.xml.validation.SchemaFactory;
32
33 import org.apache.commons.digester3.Digester;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.xml.sax.ErrorHandler;
38 import org.xml.sax.SAXException;
39 import org.xml.sax.SAXParseException;
40
41
42
43
44
45
46 public class XMLSchemaTestCase
47 {
48
49
50
51
52
53
54 protected Digester digester = null;
55
56
57
58
59
60
61 @Before
62 public void setUp()
63 throws SAXException
64 {
65
66 digester = new Digester();
67
68
69 digester.setNamespaceAware( true );
70 Schema test13schema =
71 SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI ).newSchema( this.getClass().getClassLoader().getResource( "org/apache/commons/digester3/Test13.xsd" ) );
72 digester.setXMLSchema( test13schema );
73
74
75 digester.addObjectCreate( "employee", Employee.class );
76 digester.addCallMethod( "employee/firstName", "setFirstName", 0 );
77 digester.addCallMethod( "employee/lastName", "setLastName", 0 );
78
79 digester.addObjectCreate( "employee/address", Address.class );
80 digester.addCallMethod( "employee/address/type", "setType", 0 );
81 digester.addCallMethod( "employee/address/city", "setCity", 0 );
82 digester.addCallMethod( "employee/address/state", "setState", 0 );
83 digester.addSetNext( "employee/address", "addAddress" );
84
85 }
86
87
88
89
90 @After
91 public void tearDown()
92 {
93
94 digester = null;
95
96 }
97
98
99
100
101
102
103 @Test
104 public void testGoodDocument()
105 throws SAXException, IOException
106 {
107
108
109 TestErrorHandler teh = new TestErrorHandler();
110 digester.setErrorHandler( teh );
111
112
113 Employee employee = digester.parse( getInputStream( "Test13-01.xml" ) );
114 assertNotNull( "failed to parsed an employee", employee );
115 assertTrue( "Test13-01 should not generate errors in Schema validation", teh.clean );
116
117
118 Address ha = employee.getAddress( "home" );
119 assertNotNull( ha );
120 assertEquals( "Home City", ha.getCity() );
121 assertEquals( "HS", ha.getState() );
122
123 }
124
125 @Test
126 public void testBadDocument()
127 throws SAXException, IOException
128 {
129
130
131 TestErrorHandler teh = new TestErrorHandler();
132 digester.setErrorHandler( teh );
133
134
135 digester.parse( getInputStream( "Test13-02.xml" ) );
136 assertFalse( "Test13-02 should generate errors in Schema validation", teh.clean );
137
138 }
139
140
141
142
143
144
145
146
147
148 protected InputStream getInputStream( String name )
149 throws IOException
150 {
151
152 return ( this.getClass().getResourceAsStream( "/org/apache/commons/digester3/" + name ) );
153
154 }
155
156 static final class TestErrorHandler
157 implements ErrorHandler
158 {
159 public boolean clean = true;
160
161 public TestErrorHandler()
162 {
163 }
164
165 public void error( SAXParseException exception )
166 {
167 clean = false;
168 }
169
170 public void fatalError( SAXParseException exception )
171 {
172 clean = false;
173 }
174
175 public void warning( SAXParseException exception )
176 {
177 clean = false;
178 }
179 }
180
181 }