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