1 19 20 package org.netbeans.spi.xml.cookies; 21 22 import java.net.URL ; 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 ; 27 28 36 public class SharedXMLSupportTest extends TestCase { 37 38 public SharedXMLSupportTest(String testName) { 39 super(testName); 40 } 41 42 43 public void testCheckXML() { 44 System.out.println("testCheckXML"); 45 46 URL dtd = getClass().getResource("data/DTD.dtd"); 47 URL entity = getClass().getResource("data/Entity.ent"); 48 URL invalidDTD = getClass().getResource("data/InvalidDTD.dtd"); 49 URL invalidDocument = getClass().getResource("data/InvalidDocument.xml"); 50 URL invalidEntity = getClass().getResource("data/InvalidEntity.ent"); 51 URL validDocument = getClass().getResource("data/ValidDocument.xml"); 52 URL wellformedDocument = getClass().getResource("data/WellformedDocument.xml"); 53 URL namespacesDocument = getClass().getResource("data/NamespacesDocument.xml"); 54 55 CheckXMLSupport support; 56 support = new CheckXMLSupport(new InputSource (dtd.toExternalForm()), CheckXMLSupport.CHECK_PARAMETER_ENTITY_MODE); 57 assertTrue("DTD check failed!", support.checkXML(null)); 58 59 support = new CheckXMLSupport(new InputSource (entity.toExternalForm()), CheckXMLSupport.CHECK_ENTITY_MODE); 60 assertTrue("Entity check failed!", support.checkXML(null)); 61 62 support = new CheckXMLSupport(new InputSource (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 (invalidDocument.toExternalForm())); 66 assertTrue("Invalid document must not pass", support.checkXML(null) == false); 67 68 support = new CheckXMLSupport(new InputSource (invalidEntity.toExternalForm()), CheckXMLSupport.CHECK_ENTITY_MODE); 69 assertTrue("Invalid rntity must not pass!", support.checkXML(null) == false); 70 71 support = new CheckXMLSupport(new InputSource (validDocument.toExternalForm())); 72 assertTrue("Valid document must pass!", support.checkXML(null)); 73 74 support = new CheckXMLSupport(new InputSource (wellformedDocument.toExternalForm())); 75 assertTrue("Wellformed document must pass", support.checkXML(null)); 76 77 Observer observer = new Observer(); 78 support = new CheckXMLSupport(new InputSource (namespacesDocument.toExternalForm())); 79 assertTrue("Wellformed document with namespaces must pass", support.checkXML(observer)); 80 assertTrue("Unexpected warnings!", observer.getWarnings() == 0); 81 82 } 83 84 85 public void testValidateXML() { 86 System.out.println("testValidateXML"); 87 88 URL dtd = getClass().getResource("data/DTD.dtd"); 89 URL entity = getClass().getResource("data/Entity.ent"); 90 URL invalidDTD = getClass().getResource("data/InvalidDTD.dtd"); 91 URL invalidDocument = getClass().getResource("data/InvalidDocument.xml"); 92 URL invalidEntity = getClass().getResource("data/InvalidEntity.ent"); 93 URL validDocument = getClass().getResource("data/ValidDocument.xml"); 94 URL wellformedDocument = getClass().getResource("data/WellformedDocument.xml"); 95 URL validNamespacesDocument = getClass().getResource("data/ValidNamespacesDocument.xml"); 96 URL conformingNamespacesDocument = getClass().getResource("data/ConformingNamespacesDocument.xml"); 97 98 SharedXMLSupport support; 99 support = new ValidateXMLSupport(new InputSource (dtd.toExternalForm())); 100 assertTrue("DTD validation must fail!", support.validateXML(null) == false); 101 102 support = new ValidateXMLSupport(new InputSource (entity.toExternalForm())); 103 assertTrue("Entity validation must fail!", support.validateXML(null) == false); 104 105 support = new ValidateXMLSupport(new InputSource (invalidDTD.toExternalForm())); 106 assertTrue("Invalid DTD must not pass!", support.validateXML(null) == false); 107 108 support = new ValidateXMLSupport(new InputSource (invalidDocument.toExternalForm())); 109 assertTrue("Invalid document must not pass", support.validateXML(null) == false); 110 111 support = new ValidateXMLSupport(new InputSource (invalidEntity.toExternalForm())); 112 assertTrue("Invalid rntity must not pass!", support.validateXML(null) == false); 113 114 support = new ValidateXMLSupport(new InputSource (validDocument.toExternalForm())); 115 assertTrue("Valid document must pass!", support.validateXML(null)); 116 117 support = new ValidateXMLSupport(new InputSource (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 (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 (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 |