1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package validation.jdk8036951;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import validation.BaseTest;
/**
* @author Peter McCracken, IBM
* @version $Id$
*/
public class FeaturePropagationTest extends BaseTest {
public final String FEATURE_STRING_DEFAULT_FALSE = "http://apache.org/xml/features/honour-all-schemaLocations";
public final String FEATURE_STRING_DEFAULT_TRUE = "http://apache.org/xml/features/validation/schema-full-checking";
public final String SECURITY_MANAGER = "http://apache.org/xml/properties/security-manager";
public FeaturePropagationTest(String name) {
super(name);
}
@BeforeClass
protected void setUp() throws Exception {
super.setUp();
}
@AfterClass
protected void tearDown() throws Exception {
super.tearDown();
}
@Test
public void testPropertyReset() throws Exception {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = makeSchema(factory, null);
Validator validator = schema.newValidator();
Object beforeReset = validator.getProperty(SECURITY_MANAGER);
validator.setProperty(SECURITY_MANAGER, null);
Object changed = validator.getProperty(SECURITY_MANAGER);
//for JDK, this is changed since by default the security manager is set
assertTrue("Property value should have changed after calling setProperty().", beforeReset != changed);
validator.reset();
Object afterReset = validator.getProperty(SECURITY_MANAGER);
assertTrue("Property value should be the same after calling reset()", beforeReset == afterReset);
}
@Test
public void testFeatureReset() throws Exception {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = makeSchema(factory, null);
Validator validator = schema.newValidator();
validator.setFeature(FEATURE_STRING_DEFAULT_TRUE, false);
validator.setFeature(FEATURE_STRING_DEFAULT_FALSE, true);
validator.reset();
boolean value = validator.getFeature(FEATURE_STRING_DEFAULT_TRUE);
assertTrue("After reset, value of feature on Validator should be true.", value);
value = validator.getFeature(FEATURE_STRING_DEFAULT_FALSE);
assertFalse("After reset, value of feature on Validator should be false.", value);
}
@Test
public void testSecureProcessingFeaturePropagationAndReset() throws Exception {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
boolean value;
value = factory.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING);
//default is true for JDK
//assertFalse("Default value of feature on SchemaFactory should have been false.", value);
assertTrue("Default value of feature on SchemaFactory should have been false.", value);
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
Schema schema = makeSchema(factory, null);
Validator validator = schema.newValidator();
value = validator.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING);
assertTrue("Value of feature on Validator should have been true.", value);
validator.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, false);
value = validator.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING);
assertFalse("Value of feature on Validator should have been false.", value);
validator.reset();
value = validator.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING);
assertTrue("After reset, value of feature on Validator should be true.", value);
}
/*
* Using four basically identical tests to try out the different
* instance classes of Schema. They shouldn't differ, because the relevant
* code is in a common base class.
*/
@Test
public void testFeaturePropagationNull() throws Exception {
checkFeaturesOnValidator(null);
}
@Test
public void testFeaturePropagationEmpty() throws Exception {
checkFeaturesOnValidator(new Source[] {});
}
@Test
public void testFeaturePropagationSingle() throws Exception {
checkFeaturesOnValidator(new Source[] {makeSource("base.xsd")});
}
@Test
public void testFeaturePropagationMultiple() throws Exception {
checkFeaturesOnValidator(new Source[] {makeSource("base.xsd"), makeSource("idc.xsd")});
}
private void checkFeaturesOnValidator(Source[] sources) throws Exception {
try {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = makeSchema(factory, sources);
Validator validator = schema.newValidator();
boolean value;
value = validator.getFeature(FEATURE_STRING_DEFAULT_TRUE);
assertTrue("Default value of feature on Validator should have been true.", value);
value = validator.getFeature(FEATURE_STRING_DEFAULT_FALSE);
assertFalse("Default value of feature on Validator should have been false.", value);
// checking that the value propagates to the validator
factory.setFeature(FEATURE_STRING_DEFAULT_TRUE, false);
factory.setFeature(FEATURE_STRING_DEFAULT_FALSE, true);
schema = makeSchema(factory, sources);
validator = schema.newValidator();
value = validator.getFeature(FEATURE_STRING_DEFAULT_TRUE);
assertFalse("Value of feature on Validator should have been false.", value);
value = validator.getFeature(FEATURE_STRING_DEFAULT_FALSE);
assertTrue("Value of feature on Validator should have been true.", value);
// checking that the validator contains a copy of the features, not a reference
factory.setFeature(FEATURE_STRING_DEFAULT_TRUE, true);
factory.setFeature(FEATURE_STRING_DEFAULT_FALSE, false);
value = validator.getFeature(FEATURE_STRING_DEFAULT_TRUE);
assertFalse("Value of feature on Validator should have stayed false.", value);
value = validator.getFeature(FEATURE_STRING_DEFAULT_FALSE);
assertTrue("Value of feature on Validator should have stayed true.", value);
}
catch (SAXNotRecognizedException e) {
fail(e.getMessage());
}
catch (SAXNotSupportedException e) {
fail(e.getMessage());
}
}
private Schema makeSchema(SchemaFactory factory, Source[] sources) throws SAXException {
if (sources == null) {
return factory.newSchema();
}
else {
return factory.newSchema(sources);
}
}
private Source makeSource(String xsd) throws FileNotFoundException {
return new StreamSource(fSchemaURL.toExternalForm());
}
@Override
protected String getSchemaFile() {
return "base.xsd";
}
@Override
protected String getXMLDocument() {
//not needed
return null;
}
}
|