1 /* 2 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package javax.xml.validation.ptests; 24 25 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; 26 import static javax.xml.validation.ptests.ValidationTestConst.XML_DIR; 27 import static org.testng.Assert.assertFalse; 28 import static org.testng.Assert.assertNotNull; 29 import static org.testng.Assert.assertNull; 30 import static org.testng.Assert.assertSame; 31 import static org.testng.Assert.assertTrue; 32 33 import java.io.File; 34 35 import javax.xml.validation.Schema; 36 import javax.xml.validation.SchemaFactory; 37 import javax.xml.validation.ValidatorHandler; 38 39 import org.testng.annotations.BeforeClass; 40 import org.testng.annotations.Test; 41 import org.xml.sax.ContentHandler; 42 import org.xml.sax.ErrorHandler; 43 import org.xml.sax.SAXException; 44 import org.xml.sax.SAXNotRecognizedException; 45 import org.xml.sax.SAXNotSupportedException; 46 import org.xml.sax.helpers.DefaultHandler; 47 48 /* 49 * @summary Class containing the test cases for ValidatorHandler API 50 */ 51 public class ValidatorHandlerTest { 52 @BeforeClass 53 public void setup() throws SAXException { 54 schema = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI).newSchema(new File(XML_DIR + "test.xsd")); 55 56 assertNotNull(schema); 57 } 58 59 @Test 60 public void testErrorHandler() { 61 ValidatorHandler validatorHandler = getValidatorHandler(); 62 assertNull(validatorHandler.getErrorHandler(), "When ValidatorHandler is created, initially ErrorHandler should not be set."); 63 64 ErrorHandler handler = new MyErrorHandler(); 65 validatorHandler.setErrorHandler(handler); 66 assertSame(validatorHandler.getErrorHandler(), handler); 67 68 } 69 70 @Test(expectedExceptions = SAXNotRecognizedException.class) 71 public void testGetUnrecognizedProperty() throws SAXNotRecognizedException, SAXNotSupportedException { 72 ValidatorHandler validatorHandler = getValidatorHandler(); 73 validatorHandler.getProperty(FEATURE_NAME); 74 75 } 76 77 @Test(expectedExceptions = SAXNotRecognizedException.class) 78 public void testSetUnrecognizedProperty() throws SAXNotRecognizedException, SAXNotSupportedException { 79 ValidatorHandler validatorHandler = getValidatorHandler(); 80 validatorHandler.setProperty(FEATURE_NAME, "test"); 81 } 82 83 @Test(expectedExceptions = NullPointerException.class) 84 public void testGetNullProperty() throws SAXNotRecognizedException, SAXNotSupportedException { 85 ValidatorHandler validatorHandler = getValidatorHandler(); 86 assertNotNull(validatorHandler); 87 validatorHandler.getProperty(null); 88 89 } 90 91 @Test(expectedExceptions = NullPointerException.class) 92 public void testSetNullProperty() throws SAXNotRecognizedException, SAXNotSupportedException { 93 ValidatorHandler validatorHandler = getValidatorHandler(); 94 assertNotNull(validatorHandler); 95 validatorHandler.setProperty(null, "test"); 96 } 97 98 public void testFeature() throws SAXNotRecognizedException, SAXNotSupportedException { 99 ValidatorHandler validatorHandler = getValidatorHandler(); 100 assertFalse(validatorHandler.getFeature(FEATURE_NAME), "The feature should be false by default."); 101 102 validatorHandler.setFeature(FEATURE_NAME, true); 103 assertTrue(validatorHandler.getFeature(FEATURE_NAME), "The feature should be false by default."); 104 105 } 106 107 @Test(expectedExceptions = NullPointerException.class) 108 public void testGetNullFeature() throws SAXNotRecognizedException, SAXNotSupportedException { 109 ValidatorHandler validatorHandler = getValidatorHandler(); 110 assertNotNull(validatorHandler); 111 validatorHandler.getFeature(null); 112 113 } 114 115 @Test(expectedExceptions = NullPointerException.class) 116 public void testSetNullFeature() throws SAXNotRecognizedException, SAXNotSupportedException { 117 ValidatorHandler validatorHandler = getValidatorHandler(); 118 assertNotNull(validatorHandler); 119 validatorHandler.setFeature(null, true); 120 } 121 122 @Test 123 public void testContentHandler() { 124 ValidatorHandler validatorHandler = getValidatorHandler(); 125 assertNull(validatorHandler.getContentHandler(), "When ValidatorHandler is created, initially ContentHandler should not be set."); 126 127 ContentHandler handler = new DefaultHandler(); 128 validatorHandler.setContentHandler(handler); 129 assertSame(validatorHandler.getContentHandler(), handler); 130 131 validatorHandler.setContentHandler(null); 132 assertNull(validatorHandler.getContentHandler()); 133 134 } 135 136 private ValidatorHandler getValidatorHandler() { 137 return schema.newValidatorHandler(); 138 } 139 140 private static final String FEATURE_NAME = "http://xml.org/sax/features/namespace-prefixes"; 141 142 private Schema schema; 143 144 }