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
27public class IdentityConstraintCheckingTest extends BaseTest {
28    // These values are unstable, since they're not actually error keys, but
29    // simply
30    // the first part of the error message.
31    public static final String DUPLICATE_UNIQUE = "cvc-identity-constraint.4.1";
32
33    public static final String DUPLICATE_KEY = "cvc-identity-constraint.4.2.2";
34
35    public static final String INVALID_KEYREF = "cvc-identity-constraint.4.3";
36
37    protected String getXMLDocument() {
38        return "idc.xml";
39    }
40
41    protected String getSchemaFile() {
42        return "idc.xsd";
43    }
44
45    protected String[] getRelevantErrorIDs() {
46        return new String[] { DUPLICATE_UNIQUE, DUPLICATE_KEY, INVALID_KEYREF };
47    }
48
49    public IdentityConstraintCheckingTest(String name) {
50        super(name);
51    }
52
53    @BeforeClass
54    protected void setUp() throws Exception {
55        super.setUp();
56    }
57
58    @AfterClass
59    protected void tearDown() throws Exception {
60        super.tearDown();
61    }
62
63    @Test
64    public void testDefault() {
65        try {
66            reset();
67            validateDocument();
68        } catch (Exception e) {
69            fail("Validation failed: " + e.getMessage());
70        }
71
72        checkDefault();
73    }
74
75    @Test
76    public void testSetFalse() {
77        try {
78            reset();
79            fValidator.setFeature(IDC_CHECKING, false);
80            validateDocument();
81        } catch (Exception e) {
82            fail("Validation failed: " + e.getMessage());
83        }
84
85        checkValidResult();
86    }
87
88    @Test
89    public void testSetTrue() {
90        try {
91            reset();
92            fValidator.setFeature(IDC_CHECKING, true);
93            validateDocument();
94        } catch (Exception e) {
95            fail("Validation failed: " + e.getMessage());
96        }
97
98        checkDefault();
99    }
100
101    private void checkDefault() {
102        assertError(DUPLICATE_UNIQUE);
103        assertError(DUPLICATE_KEY);
104        assertError(INVALID_KEYREF);
105
106        assertValidity(ItemPSVI.VALIDITY_INVALID, fRootNode.getValidity());
107        assertValidationAttempted(ItemPSVI.VALIDATION_FULL, fRootNode
108                .getValidationAttempted());
109        assertElementName("itemList", fRootNode.getElementDeclaration()
110                .getName());
111        assertTypeName("itemListType", fRootNode.getTypeDefinition().getName());
112
113        // this one is valid because it's the first one to define the unique
114        // value
115        PSVIElementNSImpl child = super.getChild(1);
116        assertValidity(ItemPSVI.VALIDITY_VALID, child.getValidity());
117        assertValidationAttempted(ItemPSVI.VALIDATION_FULL, child
118                .getValidationAttempted());
119        assertElementName("item", child.getElementDeclaration().getName());
120        assertTypeName("itemType", child.getTypeDefinition().getName());
121
122        // invalid because it repeats the unique value
123        child = super.getChild(2);
124        assertValidity(ItemPSVI.VALIDITY_INVALID, child.getValidity());
125        assertValidationAttempted(ItemPSVI.VALIDATION_FULL, child
126                .getValidationAttempted());
127        assertElementName("item", child.getElementDeclaration().getName());
128        assertTypeName("itemType", child.getTypeDefinition().getName());
129
130        // invalid because it repeats the key
131        child = super.getChild(3);
132        assertValidity(ItemPSVI.VALIDITY_INVALID, child.getValidity());
133        assertValidationAttempted(ItemPSVI.VALIDATION_FULL, child
134                .getValidationAttempted());
135        assertElementName("item", child.getElementDeclaration().getName());
136        assertTypeName("itemType", child.getTypeDefinition().getName());
137
138        // valid because key references aren't figured out until the validation
139        // root
140        child = super.getChild(4);
141        assertValidity(ItemPSVI.VALIDITY_VALID, child.getValidity());
142        assertValidationAttempted(ItemPSVI.VALIDATION_FULL, child
143                .getValidationAttempted());
144        assertElementName("itemRef", child.getElementDeclaration().getName());
145        assertTypeName("string", child.getTypeDefinition().getName());
146    }
147
148    private void checkValidResult() {
149        assertNoError(DUPLICATE_UNIQUE);
150        assertNoError(DUPLICATE_KEY);
151        assertNoError(INVALID_KEYREF);
152
153        assertValidity(ItemPSVI.VALIDITY_VALID, fRootNode.getValidity());
154        assertValidationAttempted(ItemPSVI.VALIDATION_FULL, fRootNode
155                .getValidationAttempted());
156        assertElementName("itemList", fRootNode.getElementDeclaration()
157                .getName());
158        assertTypeName("itemListType", fRootNode.getTypeDefinition().getName());
159
160        PSVIElementNSImpl child = super.getChild(1);
161        assertValidity(ItemPSVI.VALIDITY_VALID, child.getValidity());
162        assertValidationAttempted(ItemPSVI.VALIDATION_FULL, child
163                .getValidationAttempted());
164        assertElementName("item", child.getElementDeclaration().getName());
165        assertTypeName("itemType", child.getTypeDefinition().getName());
166
167        child = super.getChild(2);
168        assertValidity(ItemPSVI.VALIDITY_VALID, child.getValidity());
169        assertValidationAttempted(ItemPSVI.VALIDATION_FULL, child
170                .getValidationAttempted());
171        assertElementName("item", child.getElementDeclaration().getName());
172        assertTypeName("itemType", child.getTypeDefinition().getName());
173
174        child = super.getChild(3);
175        assertValidity(ItemPSVI.VALIDITY_VALID, child.getValidity());
176        assertValidationAttempted(ItemPSVI.VALIDATION_FULL, child
177                .getValidationAttempted());
178        assertElementName("item", child.getElementDeclaration().getName());
179        assertTypeName("itemType", child.getTypeDefinition().getName());
180
181        child = super.getChild(4);
182        assertValidity(ItemPSVI.VALIDITY_VALID, child.getValidity());
183        assertValidationAttempted(ItemPSVI.VALIDATION_FULL, child
184                .getValidationAttempted());
185        assertElementName("itemRef", child.getElementDeclaration().getName());
186        assertTypeName("string", child.getTypeDefinition().getName());
187    }
188}
189