KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.StringReader JavaDoc;
29 import java.io.FileReader JavaDoc;
30 import java.io.StringWriter JavaDoc;
31 import javax.xml.transform.Result JavaDoc;
32 import javax.xml.transform.TransformerException JavaDoc;
33 import javax.xml.transform.sax.SAXSource JavaDoc;
34 import javax.xml.transform.stream.StreamResult JavaDoc;
35 import junit.framework.TestCase;
36 import junit.framework.TestSuite;
37 import junit.textui.TestRunner;
38 import org.xml.sax.EntityResolver JavaDoc;
39 import org.xml.sax.InputSource JavaDoc;
40 import org.xml.sax.SAXException JavaDoc;
41
42 /**
43  *
44  * @author <a HREF="mailto:toby.h.ferguson@sun.com">Toby H Ferguson</a>
45  * @version $Revision: 1.3 $
46  */

47
48 public class SchematronTest extends TestCase {
49     public void testSimpleTransform() throws Exception JavaDoc {
50         Schematron s = new Schematron("/simple.xsl");
51         StringWriter JavaDoc actual = new StringWriter JavaDoc();
52         Result r = new StreamResult JavaDoc(actual);
53         VariableResolver vr = new VariableResolver();
54         vr.setEntityResolver(new MyEntityResolver("/sun-domain_1_1.dtd"));
55         SAXSource JavaDoc src = new SAXSource JavaDoc(vr,
56                                       new InputSource JavaDoc("domain.xml"));
57         s.analyze(src, r);
58     }
59
60     
61     public void testWithSAXSourceWithVRAndER() throws Exception JavaDoc {
62         Schematron s = new Schematron("/domain.xsl");
63         StringWriter JavaDoc actual = new StringWriter JavaDoc();
64         Result r = new StreamResult JavaDoc(actual);
65         VariableResolver vr = new VariableResolver();
66         vr.setEntityResolver(new MyEntityResolver("/sun-domain_1_1.dtd"));
67         SAXSource JavaDoc src = new SAXSource JavaDoc(vr,
68                                       new InputSource JavaDoc("domain.xml.no.dtd"));
69         s.analyze(src, r);
70         assertEquals(1540, actual.toString().length());
71
72     }
73         // This is the test that demonstrates the problem - note that
74
// the VR has no EntityResolver
75

76     public void testWithSAXSourceWithVRNoER() throws Exception JavaDoc {
77         Schematron s = new Schematron("/domain.xsl");
78         StringWriter JavaDoc actual = new StringWriter JavaDoc();
79         Result r = new StreamResult JavaDoc(actual);
80         VariableResolver vr = new VariableResolver();
81         SAXSource JavaDoc src = new SAXSource JavaDoc(vr,
82                                       new InputSource JavaDoc(new FileReader JavaDoc("domain.xml.no.dtd")));
83         s.analyze(src, r);
84         assertEquals(1540, actual.toString().length());
85     }
86         // This test demonstrates that a SAXSource object works OK
87
// without a VariableResolver
88
public void testWithSAXSourceNoVR() throws Exception JavaDoc {
89         Schematron s = new Schematron("/domain.xsl");
90         StringWriter JavaDoc actual = new StringWriter JavaDoc();
91         Result r = new StreamResult JavaDoc(actual);
92         SAXSource JavaDoc src = new SAXSource JavaDoc(new InputSource JavaDoc("domain.xml.no.dtd"));
93         s.analyze(src, r);
94         assertEquals(1540, actual.toString().length());
95     }
96     
97         // This test relies on the fact that the URL can be resolved,
98
// but is some funky page that is not a DTD
99
public void testWithEntityResolverError() throws Exception JavaDoc {
100         Schematron s = new Schematron("/schema2.xsl");
101         StringWriter JavaDoc actual = new StringWriter JavaDoc();
102         Result r = new StreamResult JavaDoc(actual);
103         String JavaDoc input = "<?xml version='1.0'?><!DOCTYPE root PUBLIC 'DO NOT MATCH' 'http://www.google.com'><root id=\"${var}\"><system-property name=\"var\" value=\"value\"/></root>";
104         VariableResolver vr = new VariableResolver();
105         vr.setEntityResolver(new MyEntityResolver("/schema2.dtd"));
106         SAXSource JavaDoc src = new SAXSource JavaDoc(vr,
107                                       new InputSource JavaDoc(new StringReader JavaDoc(input)));
108         try {
109             s.analyze(src, r);
110             fail("Expected a Transformer Exception");
111         }
112         catch (TransformerException JavaDoc te){
113             assertEquals("javax.xml.transform.TransformerException: com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: External parameter entity \"%[dtd];\" has characters after markup.", te.getMessage());
114         }
115
116     }
117         // This test doesn't reveal any errors with an external data
118
// source and an external entity resolution
119
public void testExternalWithEntityResolver() throws Exception JavaDoc {
120         Schematron s = new Schematron("/schema2.xsl");
121         StringWriter JavaDoc actual = new StringWriter JavaDoc();
122         Result r = new StreamResult JavaDoc(actual);
123         VariableResolver vr = new VariableResolver();
124         vr.setEntityResolver(new MyEntityResolver("/schema2.dtd"));
125         SAXSource JavaDoc src = new SAXSource JavaDoc(vr,
126                                       new InputSource JavaDoc("schema2.xml"));
127         
128         s.analyze(src, r);
129         assertEquals("This is the id: value", ""+actual);
130     }
131         // This test doesn't reveal any errors in handling an external
132
// data source
133
public void testExternal() throws Exception JavaDoc {
134         Schematron s = new Schematron("/schema2.xsl");
135         StringWriter JavaDoc actual = new StringWriter JavaDoc();
136         Result r = new StreamResult JavaDoc(actual);
137         SAXSource JavaDoc src = new SAXSource JavaDoc(new VariableResolver(),
138                                       new InputSource JavaDoc("schema2.xml"));
139         
140         s.analyze(src, r);
141         assertEquals("This is the id: value", ""+actual);
142     }
143         // This test shows that an EntityResolver doesn't break the
144
// test with internal data
145
public void testWithEntityResolver() throws Exception JavaDoc {
146         Schematron s = new Schematron("/schema2.xsl");
147         StringWriter JavaDoc actual = new StringWriter JavaDoc();
148         Result r = new StreamResult JavaDoc(actual);
149         String JavaDoc input = "<?xml version='1.0'?><!DOCTYPE domain PUBLIC '-//Sun Microsystems Inc.//DTD Application Server 8.0 Domain//EN' 'http://www.sun.com/software/appserver/dtds/sun-domain_1_1.dtd'><root id=\"${var}\"><system-property name=\"var\" value=\"value\"/></root>";
150         VariableResolver vr = new VariableResolver();
151         vr.setEntityResolver(new MyEntityResolver("/schema2.dtd"));
152         SAXSource JavaDoc src = new SAXSource JavaDoc(vr,
153                                       new InputSource JavaDoc(new StringReader JavaDoc(input)));
154         
155         s.analyze(src, r);
156         assertEquals("This is the id: value", ""+actual);
157     }
158
159         public void testComment() throws Exception JavaDoc {
160         Schematron s = new Schematron("/schema2.xsl");
161         StringWriter JavaDoc actual = new StringWriter JavaDoc();
162         Result r = new StreamResult JavaDoc(actual);
163         String JavaDoc input = "<?xml version='1.0'?><!-- comment --><root id=\"${var}\"><system-property name=\"var\" value=\"value\"/></root>";
164         SAXSource JavaDoc src = new SAXSource JavaDoc(new VariableResolver(),
165                                       new InputSource JavaDoc(new StringReader JavaDoc(input)));
166         s.analyze(src, r);
167         assertEquals("This is the id: value", ""+actual);
168     }
169
170         // This test shows that the Schematron works with an
171
// externally defined XSL file, a VariableResolver with no
172
// EntityResolution, and an internal stream.
173
public void testPipeline() throws Exception JavaDoc {
174         Schematron s = new Schematron("/schema2.xsl");
175         StringWriter JavaDoc actual = new StringWriter JavaDoc();
176         Result r = new StreamResult JavaDoc(actual);
177         String JavaDoc input = "<?xml version='1.0'?><root id=\"${var}\"><system-property name=\"var\" value=\"value\"/></root>";
178         SAXSource JavaDoc src = new SAXSource JavaDoc(new VariableResolver(),
179                                       new InputSource JavaDoc(new StringReader JavaDoc(input)));
180         s.analyze(src, r);
181         assertEquals("This is the id: value", ""+actual);
182     }
183         
184     public void testSchematron() throws Exception JavaDoc {
185         Schematron s = new Schematron("/schema.xsl");
186         StringWriter JavaDoc actual = new StringWriter JavaDoc();
187         Result r = new StreamResult JavaDoc(actual);
188         s.analyze(new SAXSource JavaDoc(new InputSource JavaDoc(new StringReader JavaDoc("<top><root id ='foo'/></top>"))), r);
189         assertEquals("This is the result", ""+actual);
190     }
191
192     public SchematronTest(String JavaDoc name){
193         super(name);
194     }
195
196     protected void setUp() {
197     }
198
199     protected void tearDown() {
200     }
201
202     private void nyi(){
203         fail("Not Yet Implemented");
204     }
205
206     public static void main(String JavaDoc args[]){
207         if (args.length == 0){
208             TestRunner.run(SchematronTest.class);
209         } else {
210             TestRunner.run(makeSuite(args));
211         }
212     }
213     private static TestSuite makeSuite(String JavaDoc args[]){
214         final TestSuite ts = new TestSuite();
215         for (int i = 0; i < args.length; i++){
216             ts.addTest(new SchematronTest(args[i]));
217         }
218         return ts;
219     }
220
221     private static class MyEntityResolver implements EntityResolver JavaDoc
222     {
223         private String JavaDoc myDtd = "";
224
225         MyEntityResolver(){}
226         
227         MyEntityResolver(String JavaDoc dtd){
228             myDtd = dtd;
229         }
230         
231         public InputSource JavaDoc resolveEntity(java.lang.String JavaDoc publicId, java.lang.String JavaDoc systemId) throws SAXException JavaDoc, IOException JavaDoc {
232             if (null != publicId && publicId.equals("-//Sun Microsystems Inc.//DTD Application Server 8.0 Domain//EN")) {
233                 final InputStream JavaDoc is = getClass().getResourceAsStream(myDtd);
234                 return (null != is ) ? new InputSource JavaDoc(is) : null;
235             } else {
236                 return null;
237             }
238         }
239     }
240
241                 
242 }
243
Popular Tags