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.Listeners;
  41 import org.testng.annotations.Test;
  42 import org.xml.sax.ContentHandler;
  43 import org.xml.sax.ErrorHandler;
  44 import org.xml.sax.SAXException;
  45 import org.xml.sax.SAXNotRecognizedException;
  46 import org.xml.sax.SAXNotSupportedException;
  47 import org.xml.sax.helpers.DefaultHandler;
  48 
  49 /*
  50  * @summary Class containing the test cases for ValidatorHandler API
  51  */
  52 @Listeners({jaxp.library.FilePolicy.class})
  53 public class ValidatorHandlerTest {
  54     @BeforeClass
  55     public void setup() throws SAXException {
  56         schema = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI).newSchema(new File(XML_DIR + "test.xsd"));
  57 
  58         assertNotNull(schema);
  59     }
  60 
  61     @Test
  62     public void testErrorHandler() {
  63         ValidatorHandler validatorHandler = getValidatorHandler();
  64         assertNull(validatorHandler.getErrorHandler(), "When ValidatorHandler is created, initially ErrorHandler should not be set.");
  65 
  66         ErrorHandler handler = new MyErrorHandler();
  67         validatorHandler.setErrorHandler(handler);
  68         assertSame(validatorHandler.getErrorHandler(), handler);
  69 
  70     }
  71 
  72     @Test(expectedExceptions = SAXNotRecognizedException.class)
  73     public void testGetUnrecognizedProperty() throws SAXNotRecognizedException, SAXNotSupportedException {
  74         ValidatorHandler validatorHandler = getValidatorHandler();
  75         validatorHandler.getProperty(FEATURE_NAME);
  76 
  77     }
  78 
  79     @Test(expectedExceptions = SAXNotRecognizedException.class)
  80     public void testSetUnrecognizedProperty() throws SAXNotRecognizedException, SAXNotSupportedException {
  81         ValidatorHandler validatorHandler = getValidatorHandler();
  82         validatorHandler.setProperty(FEATURE_NAME, "test");
  83     }
  84 
  85     @Test(expectedExceptions = NullPointerException.class)
  86     public void testGetNullProperty() throws SAXNotRecognizedException, SAXNotSupportedException {
  87         ValidatorHandler validatorHandler = getValidatorHandler();
  88         assertNotNull(validatorHandler);
  89         validatorHandler.getProperty(null);
  90 
  91     }
  92 
  93     @Test(expectedExceptions = NullPointerException.class)
  94     public void testSetNullProperty() throws SAXNotRecognizedException, SAXNotSupportedException {
  95         ValidatorHandler validatorHandler = getValidatorHandler();
  96         assertNotNull(validatorHandler);
  97         validatorHandler.setProperty(null, "test");
  98     }
  99 
 100     public void testFeature() throws SAXNotRecognizedException, SAXNotSupportedException {
 101         ValidatorHandler validatorHandler = getValidatorHandler();
 102         assertFalse(validatorHandler.getFeature(FEATURE_NAME), "The feature should be false by default.");
 103 
 104         validatorHandler.setFeature(FEATURE_NAME, true);
 105         assertTrue(validatorHandler.getFeature(FEATURE_NAME), "The feature should be false by default.");
 106 
 107     }
 108 
 109     @Test(expectedExceptions = NullPointerException.class)
 110     public void testGetNullFeature() throws SAXNotRecognizedException, SAXNotSupportedException {
 111         ValidatorHandler validatorHandler = getValidatorHandler();
 112         assertNotNull(validatorHandler);
 113         validatorHandler.getFeature(null);
 114 
 115     }
 116 
 117     @Test(expectedExceptions = NullPointerException.class)
 118     public void testSetNullFeature() throws SAXNotRecognizedException, SAXNotSupportedException {
 119         ValidatorHandler validatorHandler = getValidatorHandler();
 120         assertNotNull(validatorHandler);
 121         validatorHandler.setFeature(null, true);
 122     }
 123 
 124     @Test
 125     public void testContentHandler() {
 126         ValidatorHandler validatorHandler = getValidatorHandler();
 127         assertNull(validatorHandler.getContentHandler(), "When ValidatorHandler is created, initially ContentHandler should not be set.");
 128 
 129         ContentHandler handler = new DefaultHandler();
 130         validatorHandler.setContentHandler(handler);
 131         assertSame(validatorHandler.getContentHandler(), handler);
 132 
 133         validatorHandler.setContentHandler(null);
 134         assertNull(validatorHandler.getContentHandler());
 135 
 136     }
 137 
 138     private ValidatorHandler getValidatorHandler() {
 139         return schema.newValidatorHandler();
 140     }
 141 
 142     private static final String FEATURE_NAME = "http://xml.org/sax/features/namespace-prefixes";
 143 
 144     private Schema schema;
 145 
 146 }