KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > xml > cookies > SharedXMLSupportTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.spi.xml.cookies;
21
22 import java.net.URL JavaDoc;
23 import junit.framework.TestCase;
24 import org.netbeans.api.xml.cookies.CookieMessage;
25 import org.netbeans.api.xml.cookies.CookieObserver;
26 import org.xml.sax.InputSource JavaDoc;
27
28 /**
29  * Trivial golden type support tests.
30  * <p>
31  * It tests class that is exposed by CheckXMLSupport
32  * or ValidateXMLSupport.
33  *
34  * @author Petr Kuzel
35  */

36 public class SharedXMLSupportTest extends TestCase {
37
38     public SharedXMLSupportTest(String JavaDoc testName) {
39         super(testName);
40     }
41
42     /** Test of checkXML method, of class org.netbeans.spi.xml.cookies.SharedXMLSupport. */
43     public void testCheckXML() {
44         System.out.println("testCheckXML");
45                 
46         URL JavaDoc dtd = getClass().getResource("data/DTD.dtd");
47         URL JavaDoc entity = getClass().getResource("data/Entity.ent");
48         URL JavaDoc invalidDTD = getClass().getResource("data/InvalidDTD.dtd");
49         URL JavaDoc invalidDocument = getClass().getResource("data/InvalidDocument.xml");
50         URL JavaDoc invalidEntity = getClass().getResource("data/InvalidEntity.ent");
51         URL JavaDoc validDocument = getClass().getResource("data/ValidDocument.xml");
52         URL JavaDoc wellformedDocument = getClass().getResource("data/WellformedDocument.xml");
53         URL JavaDoc namespacesDocument = getClass().getResource("data/NamespacesDocument.xml");
54         
55         CheckXMLSupport support;
56         support = new CheckXMLSupport(new InputSource JavaDoc(dtd.toExternalForm()), CheckXMLSupport.CHECK_PARAMETER_ENTITY_MODE);
57         assertTrue("DTD check failed!", support.checkXML(null));
58
59         support = new CheckXMLSupport(new InputSource JavaDoc(entity.toExternalForm()), CheckXMLSupport.CHECK_ENTITY_MODE);
60         assertTrue("Entity check failed!", support.checkXML(null));
61
62         support = new CheckXMLSupport(new InputSource JavaDoc(invalidDTD.toExternalForm()), CheckXMLSupport.CHECK_PARAMETER_ENTITY_MODE);
63         assertTrue("Invalid DTD must not pass!", support.checkXML(null) == false);
64
65         support = new CheckXMLSupport(new InputSource JavaDoc(invalidDocument.toExternalForm()));
66         assertTrue("Invalid document must not pass", support.checkXML(null) == false);
67
68         support = new CheckXMLSupport(new InputSource JavaDoc(invalidEntity.toExternalForm()), CheckXMLSupport.CHECK_ENTITY_MODE);
69         assertTrue("Invalid rntity must not pass!", support.checkXML(null) == false);
70
71         support = new CheckXMLSupport(new InputSource JavaDoc(validDocument.toExternalForm()));
72         assertTrue("Valid document must pass!", support.checkXML(null));
73
74         support = new CheckXMLSupport(new InputSource JavaDoc(wellformedDocument.toExternalForm()));
75         assertTrue("Wellformed document must pass", support.checkXML(null));
76
77         Observer observer = new Observer();
78         support = new CheckXMLSupport(new InputSource JavaDoc(namespacesDocument.toExternalForm()));
79         assertTrue("Wellformed document with namespaces must pass", support.checkXML(observer));
80         assertTrue("Unexpected warnings!", observer.getWarnings() == 0);
81         
82     }
83     
84     /** Test of validateXML method, of class org.netbeans.spi.xml.cookies.SharedXMLSupport. */
85     public void testValidateXML() {
86         System.out.println("testValidateXML");
87
88         URL JavaDoc dtd = getClass().getResource("data/DTD.dtd");
89         URL JavaDoc entity = getClass().getResource("data/Entity.ent");
90         URL JavaDoc invalidDTD = getClass().getResource("data/InvalidDTD.dtd");
91         URL JavaDoc invalidDocument = getClass().getResource("data/InvalidDocument.xml");
92         URL JavaDoc invalidEntity = getClass().getResource("data/InvalidEntity.ent");
93         URL JavaDoc validDocument = getClass().getResource("data/ValidDocument.xml");
94         URL JavaDoc wellformedDocument = getClass().getResource("data/WellformedDocument.xml");
95         URL JavaDoc validNamespacesDocument = getClass().getResource("data/ValidNamespacesDocument.xml");
96         URL JavaDoc conformingNamespacesDocument = getClass().getResource("data/ConformingNamespacesDocument.xml");
97         
98         SharedXMLSupport support;
99         support = new ValidateXMLSupport(new InputSource JavaDoc(dtd.toExternalForm()));
100         assertTrue("DTD validation must fail!", support.validateXML(null) == false);
101
102         support = new ValidateXMLSupport(new InputSource JavaDoc(entity.toExternalForm()));
103         assertTrue("Entity validation must fail!", support.validateXML(null) == false);
104
105         support = new ValidateXMLSupport(new InputSource JavaDoc(invalidDTD.toExternalForm()));
106         assertTrue("Invalid DTD must not pass!", support.validateXML(null) == false);
107
108         support = new ValidateXMLSupport(new InputSource JavaDoc(invalidDocument.toExternalForm()));
109         assertTrue("Invalid document must not pass", support.validateXML(null) == false);
110
111         support = new ValidateXMLSupport(new InputSource JavaDoc(invalidEntity.toExternalForm()));
112         assertTrue("Invalid rntity must not pass!", support.validateXML(null) == false);
113
114         support = new ValidateXMLSupport(new InputSource JavaDoc(validDocument.toExternalForm()));
115         assertTrue("Valid document must pass!", support.validateXML(null));
116
117         support = new ValidateXMLSupport(new InputSource JavaDoc(wellformedDocument.toExternalForm()));
118         assertTrue("Wellformed document must not pass", support.validateXML(null) == false);
119
120         Observer observer = new Observer();
121         support = new ValidateXMLSupport(new InputSource JavaDoc(validNamespacesDocument.toExternalForm()));
122         assertTrue("Valid document with namespaces must pass", support.validateXML(observer));
123         assertTrue("Unexpected warnings!", observer.getWarnings() == 0);
124
125         observer = new Observer();
126         support = new ValidateXMLSupport(new InputSource JavaDoc(conformingNamespacesDocument.toExternalForm()));
127         assertTrue("Conforming document must pass", support.validateXML(observer));
128         assertTrue("Unexpected warnings!", observer.getWarnings() == 0);
129         
130     }
131     
132     private static class Observer implements CookieObserver {
133         private int warnings;
134         public void receive(CookieMessage msg) {
135             if (msg.getLevel() >= msg.WARNING_LEVEL) {
136                 warnings++;
137             }
138         }
139         public int getWarnings() {
140             return warnings;
141         }
142     };
143         
144 }
145
Popular Tags