KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > wsdd > TestXSD


1 package test.wsdd;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import org.w3c.dom.Document JavaDoc;
7
8 import javax.xml.parsers.DocumentBuilder JavaDoc;
9 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
10 import java.io.File JavaDoc;
11
12 /**
13  * Make sure that WSDD.xsd is up-to-date
14  */

15 public class TestXSD extends TestCase {
16
17     static final String JavaDoc JAXP_SCHEMA_LANGUAGE =
18             "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
19     static final String JavaDoc W3C_XML_SCHEMA =
20             "http://www.w3.org/2001/XMLSchema";
21     static final String JavaDoc JAXP_SCHEMA_SOURCE =
22             "http://java.sun.com/xml/jaxp/properties/schemaSource";
23
24     DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
25
26     public TestXSD(String JavaDoc name) {
27         super(name);
28     }
29
30     public static Test suite() {
31         return new TestSuite(TestXSD.class);
32     }
33
34     protected void setUp() throws Exception JavaDoc {
35         String JavaDoc schemaSource = "wsdd/WSDD.xsd";
36
37         // Set namespaceAware to true to get a DOM Level 2 tree with nodes
38
// containing namesapce information. This is necessary because the
39
// default value from JAXP 1.0 was defined to be false.
40
dbf.setNamespaceAware(true);
41
42         dbf.setValidating(true);
43         dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
44         // Specify other factory configuration settings
45
File JavaDoc f = new File JavaDoc(schemaSource);
46         dbf.setAttribute(JAXP_SCHEMA_SOURCE, f.toURL().toExternalForm());
47     }
48
49     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
50         TestXSD tester = new TestXSD("TestXSD");
51         tester.setUp();
52         tester.testWSDD();
53     }
54
55     public void testWSDD() throws Exception JavaDoc {
56         File JavaDoc f = new File JavaDoc(".");
57         recurse(f);
58     }
59
60     private void recurse(File JavaDoc f) throws Exception JavaDoc {
61         if (f.isDirectory()) {
62             File JavaDoc[] files = f.listFiles();
63             for (int i = 0; i < files.length; i++) {
64                 recurse(files[i]);
65             }
66         } else if (f.getName().endsWith(".wsdd")) {
67             checkValidity(f);
68         }
69     }
70
71     private void checkValidity(File JavaDoc f) throws Exception JavaDoc {
72         System.out.println("========== Checking " + f.getAbsolutePath() + "=================");
73         DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
74         Document JavaDoc doc = db.parse(f);
75         assertTrue(doc != null);
76     }
77 }
78
Popular Tags