Path: blob/master/test/jdk/javax/xml/jaxp/testng/validation/jdk8036951/FeaturePropagationTest.java
41159 views
/*1* Licensed to the Apache Software Foundation (ASF) under one or more2* contributor license agreements. See the NOTICE file distributed with3* this work for additional information regarding copyright ownership.4* The ASF licenses this file to You under the Apache License, Version 2.05* (the "License"); you may not use this file except in compliance with6* the License. You may obtain a copy of the License at7*8* http://www.apache.org/licenses/LICENSE-2.09*10* Unless required by applicable law or agreed to in writing, software11* distributed under the License is distributed on an "AS IS" BASIS,12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13* See the License for the specific language governing permissions and14* limitations under the License.15*/1617package validation.jdk8036951;1819import java.io.File;20import java.io.FileNotFoundException;21import java.net.URL;22import javax.xml.XMLConstants;23import javax.xml.transform.Source;24import javax.xml.transform.stream.StreamSource;25import javax.xml.validation.Schema;26import javax.xml.validation.SchemaFactory;27import javax.xml.validation.Validator;28import org.testng.annotations.AfterClass;29import org.testng.annotations.BeforeClass;30import org.testng.annotations.Test;31import org.xml.sax.SAXException;32import org.xml.sax.SAXNotRecognizedException;33import org.xml.sax.SAXNotSupportedException;34import validation.BaseTest;3536/**37* @author Peter McCracken, IBM38* @version $Id$39*/40public class FeaturePropagationTest extends BaseTest {4142public final String FEATURE_STRING_DEFAULT_FALSE = "http://apache.org/xml/features/honour-all-schemaLocations";43public final String FEATURE_STRING_DEFAULT_TRUE = "http://apache.org/xml/features/validation/schema-full-checking";44public final String SECURITY_MANAGER = "http://apache.org/xml/properties/security-manager";4546public FeaturePropagationTest(String name) {47super(name);48}4950@BeforeClass51protected void setUp() throws Exception {52super.setUp();53}5455@AfterClass56protected void tearDown() throws Exception {57super.tearDown();58}5960@Test61public void testPropertyReset() throws Exception {62SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);63Schema schema = makeSchema(factory, null);64Validator validator = schema.newValidator();65Object beforeReset = validator.getProperty(SECURITY_MANAGER);66validator.setProperty(SECURITY_MANAGER, null);67Object changed = validator.getProperty(SECURITY_MANAGER);68//for JDK, this is changed since by default the security manager is set69assertTrue("Property value should have changed after calling setProperty().", beforeReset != changed);70validator.reset();71Object afterReset = validator.getProperty(SECURITY_MANAGER);72assertTrue("Property value should be the same after calling reset()", beforeReset == afterReset);73}7475@Test76public void testFeatureReset() throws Exception {77SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);78Schema schema = makeSchema(factory, null);79Validator validator = schema.newValidator();80validator.setFeature(FEATURE_STRING_DEFAULT_TRUE, false);81validator.setFeature(FEATURE_STRING_DEFAULT_FALSE, true);82validator.reset();83boolean value = validator.getFeature(FEATURE_STRING_DEFAULT_TRUE);84assertTrue("After reset, value of feature on Validator should be true.", value);85value = validator.getFeature(FEATURE_STRING_DEFAULT_FALSE);86assertFalse("After reset, value of feature on Validator should be false.", value);87}8889@Test90public void testSecureProcessingFeaturePropagationAndReset() throws Exception {91SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);92boolean value;93value = factory.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING);94//default is true for JDK95//assertFalse("Default value of feature on SchemaFactory should have been false.", value);96assertTrue("Default value of feature on SchemaFactory should have been false.", value);97factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);98Schema schema = makeSchema(factory, null);99Validator validator = schema.newValidator();100value = validator.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING);101assertTrue("Value of feature on Validator should have been true.", value);102validator.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, false);103value = validator.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING);104assertFalse("Value of feature on Validator should have been false.", value);105validator.reset();106value = validator.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING);107assertTrue("After reset, value of feature on Validator should be true.", value);108}109/*110* Using four basically identical tests to try out the different111* instance classes of Schema. They shouldn't differ, because the relevant112* code is in a common base class.113*/114115@Test116public void testFeaturePropagationNull() throws Exception {117checkFeaturesOnValidator(null);118}119120@Test121public void testFeaturePropagationEmpty() throws Exception {122checkFeaturesOnValidator(new Source[] {});123}124125@Test126public void testFeaturePropagationSingle() throws Exception {127checkFeaturesOnValidator(new Source[] {makeSource("base.xsd")});128}129130@Test131public void testFeaturePropagationMultiple() throws Exception {132checkFeaturesOnValidator(new Source[] {makeSource("base.xsd"), makeSource("idc.xsd")});133}134135private void checkFeaturesOnValidator(Source[] sources) throws Exception {136try {137SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);138Schema schema = makeSchema(factory, sources);139Validator validator = schema.newValidator();140boolean value;141value = validator.getFeature(FEATURE_STRING_DEFAULT_TRUE);142assertTrue("Default value of feature on Validator should have been true.", value);143value = validator.getFeature(FEATURE_STRING_DEFAULT_FALSE);144assertFalse("Default value of feature on Validator should have been false.", value);145146// checking that the value propagates to the validator147factory.setFeature(FEATURE_STRING_DEFAULT_TRUE, false);148factory.setFeature(FEATURE_STRING_DEFAULT_FALSE, true);149schema = makeSchema(factory, sources);150validator = schema.newValidator();151value = validator.getFeature(FEATURE_STRING_DEFAULT_TRUE);152assertFalse("Value of feature on Validator should have been false.", value);153value = validator.getFeature(FEATURE_STRING_DEFAULT_FALSE);154assertTrue("Value of feature on Validator should have been true.", value);155156// checking that the validator contains a copy of the features, not a reference157factory.setFeature(FEATURE_STRING_DEFAULT_TRUE, true);158factory.setFeature(FEATURE_STRING_DEFAULT_FALSE, false);159value = validator.getFeature(FEATURE_STRING_DEFAULT_TRUE);160assertFalse("Value of feature on Validator should have stayed false.", value);161value = validator.getFeature(FEATURE_STRING_DEFAULT_FALSE);162assertTrue("Value of feature on Validator should have stayed true.", value);163}164catch (SAXNotRecognizedException e) {165fail(e.getMessage());166}167catch (SAXNotSupportedException e) {168fail(e.getMessage());169}170}171172private Schema makeSchema(SchemaFactory factory, Source[] sources) throws SAXException {173if (sources == null) {174return factory.newSchema();175}176else {177return factory.newSchema(sources);178}179}180181private Source makeSource(String xsd) throws FileNotFoundException {182return new StreamSource(fSchemaURL.toExternalForm());183}184185@Override186protected String getSchemaFile() {187return "base.xsd";188}189190@Override191protected String getXMLDocument() {192//not needed193return null;194}195}196197198