KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > retest > test > XmlRegressionTestCase


1 package xdoclet.retest.test;
2
3 import junit.framework.Test;
4 import junit.framework.TestSuite;
5 import org.w3c.dom.Document JavaDoc;
6 import org.w3c.dom.Node JavaDoc;
7 import org.xml.sax.SAXParseException JavaDoc;
8 import org.xml.sax.SAXException JavaDoc;
9 import org.xml.sax.EntityResolver JavaDoc;
10 import org.xml.sax.InputSource JavaDoc;
11 import org.xml.sax.ErrorHandler JavaDoc;
12 import xdoclet.XDocletException;
13
14 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
15 import javax.xml.parsers.DocumentBuilder JavaDoc;
16 import javax.xml.parsers.ParserConfigurationException JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.File JavaDoc;
19 import java.io.FileNotFoundException JavaDoc;
20 import java.util.Hashtable JavaDoc;
21
22 /**
23  * @author Vincent Harcq (vincent.harcq@hubmethods.com)
24  * @created Mars 5, 2002
25  * @version $Revision: 1.4 $
26  */

27 public class XmlRegressionTestCase
28         extends XDocletRegressionTestCase
29 {
30
31     public XmlRegressionTestCase(String JavaDoc name)
32     {
33         super(name);
34     }
35
36     public XmlRegressionTestCase(String JavaDoc name, String JavaDoc cn)
37     {
38         super(name,cn);
39     }
40
41     public Test getSuite()
42     {
43         TestSuite suite= new TestSuite();
44         return suite;
45     }
46
47     protected static Document JavaDoc readDocument(String JavaDoc uri)
48     throws XDocletException,FileNotFoundException JavaDoc
49     {
50         Document JavaDoc doc;
51         try{
52             DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
53             dbf.setValidating(false);
54             DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
55             LocalResolver resolver = new LocalResolver();
56             LocalErrorHandler error = new LocalErrorHandler("",resolver);
57             db.setEntityResolver(resolver);
58             db.setErrorHandler(error);
59             doc = db.parse(uri);
60             return doc;
61         }catch (ParserConfigurationException JavaDoc e){
62             e.printStackTrace();
63             throw new XDocletException("Parser Config Error");
64         }catch (SAXParseException JavaDoc e){
65             e.printStackTrace();
66             throw new XDocletException("Parsing Error in "+uri+" at line "+e.getLineNumber());
67         }catch (SAXException JavaDoc e){
68             e.printStackTrace();
69             throw new XDocletException("Parsing Error in "+uri);
70         }catch (IOException JavaDoc e){
71             if (e instanceof FileNotFoundException JavaDoc)
72                 throw (FileNotFoundException JavaDoc)e;
73             e.printStackTrace();
74             throw new XDocletException("IO Error in "+uri);
75         }
76     }
77
78     protected Node JavaDoc readReferenceNode(String JavaDoc file)
79     throws XDocletException,FileNotFoundException JavaDoc
80     {
81         return readDocument(refXmlBase + File.separator + getClassName() + File.separator + file);
82     }
83
84     protected Node JavaDoc readGeneratedNode(String JavaDoc file)
85     throws XDocletException,FileNotFoundException JavaDoc
86     {
87         return readDocument(genXmlBase + File.separator + getClassName() + File.separator + file);
88     }
89
90
91     private static class LocalResolver implements EntityResolver JavaDoc
92     {
93         private Hashtable JavaDoc dtds = new Hashtable JavaDoc();
94         private boolean hasDTD = false;
95
96         public LocalResolver()
97         {
98             registerDTD("-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN", "../../dtds/ejb11-jar.dtd");
99             registerDTD("-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN", "../../dtds/ejb20-jar.dtd");
100             registerDTD("-//Sun Microsystems, Inc.//DTD J2EE Application 1.2//EN", "../../dtds/application_1_2.dtd");
101             registerDTD("-//Sun Microsystems, Inc.//DTD Connector 1.0//EN", "../../dtds/connector_1_0.dtd");
102             registerDTD("-//JBoss//DTD JAWS 2.4//EN", "../../dtds/jaws_2_4.dtd");
103             registerDTD("-//JBoss//DTD JAWS 3.0//EN", "../../dtds/jaws_3_0.dtd");
104             registerDTD("-//JBoss//DTD JBOSS 2.4//EN","../../dtds/jboss_2_4.dtd");
105             registerDTD("-//JBoss//DTD JBOSS 3.0//EN","../../dtds/jboss_3_0.dtd");
106         }
107
108         /**
109          * Registers available DTDs
110          * @param String publicId - Public ID of DTD
111          * @param String dtdFileName - the file name of DTD
112          */

113         public void registerDTD(String JavaDoc publicId, String JavaDoc dtdFileName)
114         {
115             dtds.put(publicId, dtdFileName);
116         }
117
118         /**
119          * Returns DTD inputSource. Is DTD was found in the hashtable and inputSource was created
120          * flad hasDTD is ser to true.
121          * @param String publicId - Public ID of DTD
122          * @param String dtdFileName - the file name of DTD
123          * @return InputSource of DTD
124          */

125         public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
126         {
127             hasDTD = false;
128             String JavaDoc dtd = (String JavaDoc)dtds.get(publicId);
129
130             if (dtd != null)
131             {
132                 hasDTD = true;
133                 try
134                 {
135                     InputSource JavaDoc aInputSource = new InputSource JavaDoc(dtd);
136                     return aInputSource;
137                 } catch( Exception JavaDoc ex )
138                 {
139                     // ignore
140
}
141             }
142             return null;
143         }
144
145         /**
146          * Returns the boolean value to inform id DTD was found in the XML file or not
147          * @return boolean - true if DTD was found in XML
148          */

149         public boolean hasDTD()
150         {
151             return hasDTD;
152         }
153
154     }
155
156     private static class LocalErrorHandler implements ErrorHandler JavaDoc
157     {
158         private String JavaDoc theFileName;
159         private LocalResolver localResolver;
160
161         public LocalErrorHandler( String JavaDoc inFileName, LocalResolver localResolver )
162         {
163             this.theFileName = inFileName;
164             this.localResolver = localResolver;
165         }
166
167         public void error(SAXParseException JavaDoc exception)
168         {
169             if ( localResolver.hasDTD() )
170             {
171                 System.err.println("File "
172                         + theFileName
173                         + " process error. Line: "
174                         + String.valueOf(exception.getLineNumber())
175                         + ". Error message: "
176                         + exception.getMessage()
177                 );
178             }
179         }
180
181         public void fatalError(SAXParseException JavaDoc exception)
182         {
183             if ( localResolver.hasDTD() )
184             {
185                 System.err.println("File "
186                         + theFileName
187                         + " process fatal error. Line: "
188                         + String.valueOf(exception.getLineNumber())
189                         + ". Error message: "
190                         + exception.getMessage()
191                 );
192             }
193         }
194
195         public void warning(SAXParseException JavaDoc exception)
196         {
197             if ( localResolver.hasDTD() )
198             {
199                 System.err.println("File "
200                         + theFileName
201                         + " process warning. Line: "
202                         + String.valueOf(exception.getLineNumber())
203                         + ". Error message: "
204                         + exception.getMessage()
205                 );
206             }
207         }
208     }
209
210 }
211
Popular Tags