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 |
|
18 import com.sun.org.apache.xerces.internal.dom.PSVIElementNSImpl; |
|
19 import com.sun.org.apache.xerces.internal.xs.ItemPSVI; |
|
20 import org.xml.sax.SAXException; |
|
21 import org.testng.annotations.AfterClass; |
|
22 import org.testng.annotations.BeforeClass; |
|
23 import org.testng.annotations.Test; |
|
24 |
|
25 public class IdentityConstraintCheckingTest extends BaseTest { |
|
26 // These values are unstable, since they're not actually error keys, but |
|
27 // simply |
|
28 // the first part of the error message. |
|
29 public static final String DUPLICATE_UNIQUE = "cvc-identity-constraint.4.1"; |
|
30 |
|
31 public static final String DUPLICATE_KEY = "cvc-identity-constraint.4.2.2"; |
|
32 |
|
33 public static final String INVALID_KEYREF = "cvc-identity-constraint.4.3"; |
|
34 |
|
35 protected String getXMLDocument() { |
|
36 return "idc.xml"; |
|
37 } |
|
38 |
|
39 protected String getSchemaFile() { |
|
40 return "idc.xsd"; |
|
41 } |
|
42 |
|
43 protected String[] getRelevantErrorIDs() { |
|
44 return new String[] { DUPLICATE_UNIQUE, DUPLICATE_KEY, INVALID_KEYREF }; |
|
45 } |
|
46 |
|
47 public IdentityConstraintCheckingTest(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(IDC_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(IDC_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_UNIQUE); |
|
101 assertError(DUPLICATE_KEY); |
|
102 assertError(INVALID_KEYREF); |
|
103 |
|
104 assertValidity(ItemPSVI.VALIDITY_INVALID, fRootNode.getValidity()); |
|
105 assertValidationAttempted(ItemPSVI.VALIDATION_FULL, fRootNode |
|
106 .getValidationAttempted()); |
|
107 assertElementName("itemList", fRootNode.getElementDeclaration() |
|
108 .getName()); |
|
109 assertTypeName("itemListType", fRootNode.getTypeDefinition().getName()); |
|
110 |
|
111 // this one is valid because it's the first one to define the unique |
|
112 // value |
|
113 PSVIElementNSImpl child = super.getChild(1); |
|
114 assertValidity(ItemPSVI.VALIDITY_VALID, child.getValidity()); |
|
115 assertValidationAttempted(ItemPSVI.VALIDATION_FULL, child |
|
116 .getValidationAttempted()); |
|
117 assertElementName("item", child.getElementDeclaration().getName()); |
|
118 assertTypeName("itemType", child.getTypeDefinition().getName()); |
|
119 |
|
120 // invalid because it repeats the unique value |
|
121 child = super.getChild(2); |
|
122 assertValidity(ItemPSVI.VALIDITY_INVALID, child.getValidity()); |
|
123 assertValidationAttempted(ItemPSVI.VALIDATION_FULL, child |
|
124 .getValidationAttempted()); |
|
125 assertElementName("item", child.getElementDeclaration().getName()); |
|
126 assertTypeName("itemType", child.getTypeDefinition().getName()); |
|
127 |
|
128 // invalid because it repeats the key |
|
129 child = super.getChild(3); |
|
130 assertValidity(ItemPSVI.VALIDITY_INVALID, child.getValidity()); |
|
131 assertValidationAttempted(ItemPSVI.VALIDATION_FULL, child |
|
132 .getValidationAttempted()); |
|
133 assertElementName("item", child.getElementDeclaration().getName()); |
|
134 assertTypeName("itemType", child.getTypeDefinition().getName()); |
|
135 |
|
136 // valid because key references aren't figured out until the validation |
|
137 // root |
|
138 child = super.getChild(4); |
|
139 assertValidity(ItemPSVI.VALIDITY_VALID, child.getValidity()); |
|
140 assertValidationAttempted(ItemPSVI.VALIDATION_FULL, child |
|
141 .getValidationAttempted()); |
|
142 assertElementName("itemRef", child.getElementDeclaration().getName()); |
|
143 assertTypeName("string", child.getTypeDefinition().getName()); |
|
144 } |
|
145 |
|
146 private void checkValidResult() { |
|
147 assertNoError(DUPLICATE_UNIQUE); |
|
148 assertNoError(DUPLICATE_KEY); |
|
149 assertNoError(INVALID_KEYREF); |
|
150 |
|
151 assertValidity(ItemPSVI.VALIDITY_VALID, fRootNode.getValidity()); |
|
152 assertValidationAttempted(ItemPSVI.VALIDATION_FULL, fRootNode |
|
153 .getValidationAttempted()); |
|
154 assertElementName("itemList", fRootNode.getElementDeclaration() |
|
155 .getName()); |
|
156 assertTypeName("itemListType", fRootNode.getTypeDefinition().getName()); |
|
157 |
|
158 PSVIElementNSImpl child = super.getChild(1); |
|
159 assertValidity(ItemPSVI.VALIDITY_VALID, child.getValidity()); |
|
160 assertValidationAttempted(ItemPSVI.VALIDATION_FULL, child |
|
161 .getValidationAttempted()); |
|
162 assertElementName("item", child.getElementDeclaration().getName()); |
|
163 assertTypeName("itemType", child.getTypeDefinition().getName()); |
|
164 |
|
165 child = super.getChild(2); |
|
166 assertValidity(ItemPSVI.VALIDITY_VALID, child.getValidity()); |
|
167 assertValidationAttempted(ItemPSVI.VALIDATION_FULL, child |
|
168 .getValidationAttempted()); |
|
169 assertElementName("item", child.getElementDeclaration().getName()); |
|
170 assertTypeName("itemType", child.getTypeDefinition().getName()); |
|
171 |
|
172 child = super.getChild(3); |
|
173 assertValidity(ItemPSVI.VALIDITY_VALID, child.getValidity()); |
|
174 assertValidationAttempted(ItemPSVI.VALIDATION_FULL, child |
|
175 .getValidationAttempted()); |
|
176 assertElementName("item", child.getElementDeclaration().getName()); |
|
177 assertTypeName("itemType", child.getTypeDefinition().getName()); |
|
178 |
|
179 child = super.getChild(4); |
|
180 assertValidity(ItemPSVI.VALIDITY_VALID, child.getValidity()); |
|
181 assertValidationAttempted(ItemPSVI.VALIDATION_FULL, child |
|
182 .getValidationAttempted()); |
|
183 assertElementName("itemRef", child.getElementDeclaration().getName()); |
|
184 assertTypeName("string", child.getTypeDefinition().getName()); |
|
185 } |
|
186 } |
|