KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > serverbeans > validation > RNGValidatorTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.config.serverbeans.validation;
25
26 import junit.framework.*;
27 import org.xml.sax.InputSource JavaDoc;
28 import java.io.StringReader JavaDoc;
29 import java.io.OutputStreamWriter JavaDoc;
30 import java.io.StringWriter JavaDoc;
31 import org.xml.sax.EntityResolver JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
33 import java.io.IOException JavaDoc;
34 import org.xml.sax.SAXParseException JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.io.Reader JavaDoc;
37 import java.io.FileReader JavaDoc;
38 import java.io.File JavaDoc;
39 /**
40  *
41  * @author <a HREF="mailto:toby.h.ferguson@sun.com">Toby H Ferguson</a>
42  * @version $Revision: 1.3 $
43  */

44
45 public class RNGValidatorTest extends TestCase {
46     public void testacceptanceTestNoEntityFile() throws Exception JavaDoc{
47         final RNGValidator r = new RNGValidator();
48         final VariableResolver vr = new VariableResolver();
49         vr.setEntityResolver(new MyEntityResolver());
50         final InputStream JavaDoc schema = this.getClass().getResourceAsStream("/domain.rng");
51         if (null ==schema ){
52             throw new IOException JavaDoc("couldn't get resource: /domain.rng");
53         }
54         StringWriter JavaDoc out = new StringWriter JavaDoc();
55         try {
56             r.validate(new InputSource JavaDoc(schema),
57                        new InputSource JavaDoc("domain.xml"),
58                        vr,
59                        out);
60             fail("Expected Error indicating no file");
61         }
62         catch (SAXParseException JavaDoc spe){
63             assertEquals("External entity not found: \"http://www.sun.com/software/appserver/dtds/sun-domain_1_1.dtd\".", spe.getMessage());
64         }
65         
66     }
67     public void testacceptanceTestNoInputFile() throws Exception JavaDoc{
68         final RNGValidator r = new RNGValidator();
69         final VariableResolver vr = new VariableResolver();
70         vr.setEntityResolver(new MyEntityResolver(new File JavaDoc ("sun-domain_1_1.dtd")));
71         final InputStream JavaDoc schema = this.getClass().getResourceAsStream("/domain.rng");
72         if (null ==schema ){
73             throw new IOException JavaDoc("couldn't get resource: /domain.rng");
74         }
75         StringWriter JavaDoc out = new StringWriter JavaDoc();
76         try {
77             r.validate(new InputSource JavaDoc(schema),
78                        new InputSource JavaDoc("domain.xml.notHere"),
79                        vr,
80                        out);
81             fail("Expected Error indicating no file");
82         }
83         catch (IOException JavaDoc ioe){
84             assertEquals("/opt/S1AS8/NewBuild/admin/validator/tests/com/sun/enterprise/config/serverbeans/validation/domain.xml.notHere (No such file or directory)", ioe.getMessage());
85         }
86     }
87     public void testacceptanceTest() throws Exception JavaDoc{
88         final RNGValidator r = new RNGValidator();
89         final VariableResolver vr = new VariableResolver();
90         vr.setEntityResolver(new MyEntityResolver(new File JavaDoc ("sun-domain_1_1.dtd")));
91         final InputStream JavaDoc schema = this.getClass().getResourceAsStream("/domain.rng");
92         if (null ==schema ){
93             throw new IOException JavaDoc("couldn't get resource: /domain.rng");
94         }
95         StringWriter JavaDoc out = new StringWriter JavaDoc();
96         r.validate(new InputSource JavaDoc(schema),
97                    new InputSource JavaDoc("domain.xml"),
98                    vr,
99                    out);
100         assertEquals("Error: URI=file:/opt/S1AS8/NewBuild/admin/validator/tests/com/sun/enterprise/config/serverbeans/validation/domain.xml Line=11: attribute \"load-order\" has a bad value: \"ABC\" does not satisfy the \"positiveInteger\" type\n", ""+out);
101     }
102
103     public void testEntityResolverWithError() throws Exception JavaDoc {
104         RNGValidator r = new RNGValidator();
105         final String JavaDoc schema =""+
106         "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"+
107         "<grammar xmlns='http://relaxng.org/ns/structure/1.0'>"+
108         "<define name='top'>"+
109         "<element name='top'>"+
110         "<empty/>"+
111         "</element>"+
112         "</define>"+
113         "<start>"+
114         "<ref name='top'/>"+
115         "</start>"+
116         "</grammar>";
117
118
119         final String JavaDoc source ="<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE top PUBLIC '-//Sun Microsystems Inc.//DTD Application Server 8.0 Domain//EN' 'http://www.sun.com/software/appserver/dtds/sun-domain_1_1.dtd'><top/>";
120         VariableResolver vr = new VariableResolver();
121         StringWriter JavaDoc out = new StringWriter JavaDoc();
122         
123         vr.setEntityResolver(new MyEntityResolver());
124         try {
125             
126             r.validate(new InputSource JavaDoc(new StringReader JavaDoc(schema)),
127                        new InputSource JavaDoc(new StringReader JavaDoc(source)),
128                        vr,
129                        out);
130             fail("Expected a SAX Exception indicating problems with the external entity");
131         }
132         catch (SAXParseException JavaDoc spe){
133             assertEquals("External parameter entity \"%[dtd];\" has characters after markup.", spe.getMessage());
134         }
135     }
136     public void testEntityResolver() throws Exception JavaDoc {
137         RNGValidator r = new RNGValidator();
138         final String JavaDoc schema =""+
139         "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"+
140         "<grammar xmlns='http://relaxng.org/ns/structure/1.0'>"+
141         "<define name='top'>"+
142         "<element name='top'>"+
143         "<empty/>"+
144         "</element>"+
145         "</define>"+
146         "<start>"+
147         "<ref name='top'/>"+
148         "</start>"+
149         "</grammar>";
150
151         final String JavaDoc dtd = ""+
152         "<?xml version='1.0' encoding='UTF-8'?>"+
153         "<!ELEMENT top (bottom)>"+
154         "<!ELEMENT bottom EMPTY>";
155
156         final String JavaDoc source ="<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE top PUBLIC '-//Sun Microsystems Inc.//DTD Application Server 8.0 Domain//EN' 'http://www.sun.com/software/appserver/dtds/sun-domain_1_1.dtd'><top/>";
157         VariableResolver vr = new VariableResolver();
158         StringWriter JavaDoc out = new StringWriter JavaDoc();
159         
160         vr.setEntityResolver(new MyEntityResolver(dtd));
161         r.validate(new InputSource JavaDoc(new StringReader JavaDoc(schema)),
162                    new InputSource JavaDoc(new StringReader JavaDoc(source)),
163                    vr,
164                    out);
165         assertEquals("", out+"");
166     }
167     public void testBasicWithSubstitutionError() throws Exception JavaDoc {
168         RNGValidator r = new RNGValidator();
169         final String JavaDoc schema =""+
170         "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"+
171         "<grammar xmlns='http://relaxng.org/ns/structure/1.0'>"+
172         "<define name='top'>"+
173         "<element name='top'>"+
174         "<ref name='system-property'/>"+
175         "<attribute name='id'>"+
176         "<value>VALUE</value>"+
177         "</attribute>"+
178         "</element>"+
179         "</define>"+
180         "<define name='system-property'>"+
181         "<element name='system-property'>"+
182         "<empty/>"+
183         "<attribute name='name'/>"+
184         "<attribute name='value'/>"+
185         "</element>"+
186         "</define>"+
187         "<start>"+
188         "<ref name='top'/>"+
189         "</start>"+
190         "</grammar>";
191
192         final String JavaDoc source ="<?xml version='1.0'?> <top id='${var}'><system-property name='var' value='Not Expected Value'/></top>";
193         StringWriter JavaDoc out = new StringWriter JavaDoc();
194         VariableResolver vr = new VariableResolver();
195         r.validate(new InputSource JavaDoc(new StringReader JavaDoc(schema)),
196                    new InputSource JavaDoc(new StringReader JavaDoc(source)),
197                    vr,
198                    out);
199         assertEquals("Error: URI=null Line=1: attribute \"id\" has a bad value\n", ""+out);
200                      
201     }
202     public void testBasicWithSubstitution() throws Exception JavaDoc {
203         RNGValidator r = new RNGValidator();
204         final String JavaDoc schema =""+
205         "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"+
206         "<grammar xmlns='http://relaxng.org/ns/structure/1.0'>"+
207         "<define name='top'>"+
208         "<element name='top'>"+
209         "<ref name='system-property'/>"+
210         "<attribute name='id'>"+
211         "<value>VALUE</value>"+
212         "</attribute>"+
213         "</element>"+
214         "</define>"+
215         "<define name='system-property'>"+
216         "<element name='system-property'>"+
217         "<empty/>"+
218         "<attribute name='name'/>"+
219         "<attribute name='value'/>"+
220         "</element>"+
221         "</define>"+
222         "<start>"+
223         "<ref name='top'/>"+
224         "</start>"+
225         "</grammar>";
226
227         final String JavaDoc source ="<?xml version='1.0'?> <top id='${var}'><system-property name='var' value='VALUE'/></top>";
228         StringWriter JavaDoc out = new StringWriter JavaDoc();
229         VariableResolver vr = new VariableResolver();
230         r.validate(new InputSource JavaDoc(new StringReader JavaDoc(schema)),
231                    new InputSource JavaDoc(new StringReader JavaDoc(source)),
232                    vr,
233                    out);
234         assertEquals("", ""+out);
235                      
236     }
237
238     public void testBasic() throws Exception JavaDoc {
239         RNGValidator r = new RNGValidator();
240         final String JavaDoc schema =""+
241         "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"+
242         "<grammar xmlns='http://relaxng.org/ns/structure/1.0'>"+
243         "<define name='top'>"+
244         "<element name='top'>"+
245         "<empty/>"+
246         "</element>"+
247         "</define>"+
248         "<start>"+
249         "<ref name='top'/>"+
250         "</start>"+
251         "</grammar>";
252
253         final String JavaDoc source ="<?xml version='1.0'?> <top></top>";
254         StringWriter JavaDoc out = new StringWriter JavaDoc();
255         r.validate(new InputSource JavaDoc(new StringReader JavaDoc(schema)),
256                    new InputSource JavaDoc(new StringReader JavaDoc(source)),
257                    out);
258         assertEquals("", ""+out);
259                      
260     }
261
262     public void testBasicError() throws Exception JavaDoc {
263         RNGValidator r = new RNGValidator();
264         final String JavaDoc schema =""+
265         "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"+
266         "<grammar xmlns='http://relaxng.org/ns/structure/1.0'>"+
267         "<define name='top'>"+
268         "<element name='top'>"+
269         "<empty/>"+
270         "</element>"+
271         "</define>"+
272         "<start>"+
273         "<ref name='top'/>"+
274         "</start>"+
275         "</grammar>";
276
277         final String JavaDoc source ="<?xml version='1.0'?> <top><bottom/></top>";
278         StringWriter JavaDoc out = new StringWriter JavaDoc();
279         r.validate(new InputSource JavaDoc(new StringReader JavaDoc(schema)),
280                    new InputSource JavaDoc(new StringReader JavaDoc(source)),
281                    out);
282         assertEquals("Error: URI=null Line=1: element \"bottom\" was found where no element may occur\n", ""+out);
283                      
284     }
285
286     public RNGValidatorTest(String JavaDoc name){
287         super(name);
288     }
289
290     protected void setUp() {
291     }
292
293     protected void tearDown() {
294     }
295
296     private void nyi(){
297         fail("Not Yet Implemented");
298     }
299
300     public static void main(String JavaDoc args[]){
301         if (args.length == 0){
302             junit.textui.TestRunner.run(RNGValidatorTest.class);
303         } else {
304             junit.textui.TestRunner.run(makeSuite(args));
305         }
306     }
307     private static TestSuite makeSuite(String JavaDoc args[]){
308         final TestSuite ts = new TestSuite();
309         for (int i = 0; i < args.length; i++){
310             ts.addTest(new RNGValidatorTest(args[i]));
311         }
312         return ts;
313     }
314
315     private static class MyEntityResolver implements EntityResolver JavaDoc
316     {
317         String JavaDoc string;
318         File JavaDoc file;
319
320         MyEntityResolver(){}
321         
322         
323         MyEntityResolver(String JavaDoc dtdSrc){
324             string = dtdSrc;
325         }
326
327         MyEntityResolver(File JavaDoc f) throws IOException JavaDoc {
328             file = f;
329         }
330         
331         
332         public InputSource JavaDoc resolveEntity(java.lang.String JavaDoc publicId, java.lang.String JavaDoc systemId)
333             throws SAXException JavaDoc, java.io.IOException JavaDoc {
334             if (null != string){
335                 return new InputSource JavaDoc(new StringReader JavaDoc(string));
336             } else if (null != file) {
337                 return new InputSource JavaDoc(new FileReader JavaDoc(file));
338             } else {
339                 return null;
340             }
341         }
342     }
343
344
345 }
346
Popular Tags