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 */
17package validation.jdk8037819;
18
19import com.sun.org.apache.xerces.internal.dom.PSVIElementNSImpl;
20import com.sun.org.apache.xerces.internal.xs.ItemPSVI;
21import org.xml.sax.SAXException;
22import org.testng.annotations.AfterClass;
23import org.testng.annotations.BeforeClass;
24import org.testng.annotations.Test;
25import validation.BaseTest;
26
27// duplicate IDs
28// reference to non-existent ID
29
30public class IdIdrefCheckingTest extends BaseTest {
31    public static final String DUPLICATE_ID = "cvc-id.2";
32
33    public static final String NO_ID_BINDING = "cvc-id.1";
34
35    protected String getXMLDocument() {
36        return "idIdref.xml";
37    }
38
39    protected String getSchemaFile() {
40        return "base.xsd";
41    }
42
43    protected String[] getRelevantErrorIDs() {
44        return new String[] { DUPLICATE_ID, NO_ID_BINDING };
45    }
46
47    public IdIdrefCheckingTest(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 testDefault() {
63        try {
64            reset();
65            validateDocument();
66        } catch (Exception e) {
67            fail("Validation failed: " + e.getMessage());
68        }
69
70        checkDefault();
71    }
72
73    @Test
74    public void testSetFalse() {
75        try {
76            reset();
77            fValidator.setFeature(ID_IDREF_CHECKING, false);
78            validateDocument();
79        } catch (Exception e) {
80            fail("Validation failed: " + e.getMessage());
81        }
82
83        checkValidResult();
84    }
85
86    @Test
87    public void testSetTrue() {
88        try {
89            reset();
90            fValidator.setFeature(ID_IDREF_CHECKING, true);
91            validateDocument();
92        } catch (Exception e) {
93            fail("Validation failed: " + e.getMessage());
94        }
95
96        checkDefault();
97    }
98
99    private void checkDefault() {
100        assertError(DUPLICATE_ID);
101        assertError(NO_ID_BINDING);
102
103        assertValidity(ItemPSVI.VALIDITY_INVALID, fRootNode.getValidity());
104        assertValidationAttempted(ItemPSVI.VALIDATION_FULL, fRootNode
105                .getValidationAttempted());
106        assertElementName("A", fRootNode.getElementDeclaration().getName());
107        assertTypeName("X", fRootNode.getTypeDefinition().getName());
108
109        PSVIElementNSImpl child = super.getChild(1);
110        assertValidity(ItemPSVI.VALIDITY_VALID, child.getValidity());
111        assertValidationAttempted(ItemPSVI.VALIDATION_FULL, child
112                .getValidationAttempted());
113        assertElementName("A", child.getElementDeclaration().getName());
114        assertTypeName("idType", child.getTypeDefinition().getName());
115
116        child = super.getChild(2);
117        assertValidity(ItemPSVI.VALIDITY_INVALID, child.getValidity());
118        assertValidationAttempted(ItemPSVI.VALIDATION_FULL, child
119                .getValidationAttempted());
120        assertElementName("A", child.getElementDeclaration().getName());
121        assertTypeName("idType", child.getTypeDefinition().getName());
122
123        child = super.getChild(3);
124        assertValidity(ItemPSVI.VALIDITY_VALID, child.getValidity());
125        assertValidationAttempted(ItemPSVI.VALIDATION_FULL, child
126                .getValidationAttempted());
127        assertElementName("A", child.getElementDeclaration().getName());
128        assertTypeName("idrefType", child.getTypeDefinition().getName());
129    }
130
131    private void checkValidResult() {
132        assertNoError(DUPLICATE_ID);
133        assertNoError(NO_ID_BINDING);
134
135        assertValidity(ItemPSVI.VALIDITY_VALID, fRootNode.getValidity());
136        assertValidationAttempted(ItemPSVI.VALIDATION_FULL, fRootNode
137                .getValidationAttempted());
138        assertElementName("A", fRootNode.getElementDeclaration().getName());
139        assertTypeName("X", fRootNode.getTypeDefinition().getName());
140
141        PSVIElementNSImpl child = super.getChild(1);
142        assertValidity(ItemPSVI.VALIDITY_VALID, child.getValidity());
143        assertValidationAttempted(ItemPSVI.VALIDATION_FULL, child
144                .getValidationAttempted());
145        assertElementName("A", child.getElementDeclaration().getName());
146        assertTypeName("idType", child.getTypeDefinition().getName());
147
148        child = super.getChild(2);
149        assertValidity(ItemPSVI.VALIDITY_VALID, child.getValidity());
150        assertValidationAttempted(ItemPSVI.VALIDATION_FULL, child
151                .getValidationAttempted());
152        assertElementName("A", child.getElementDeclaration().getName());
153        assertTypeName("idType", child.getTypeDefinition().getName());
154
155        child = super.getChild(3);
156        assertValidity(ItemPSVI.VALIDITY_VALID, child.getValidity());
157        assertValidationAttempted(ItemPSVI.VALIDATION_FULL, child
158                .getValidationAttempted());
159        assertElementName("A", child.getElementDeclaration().getName());
160        assertTypeName("idrefType", child.getTypeDefinition().getName());
161    }
162}
163