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 package validation.jdk8037819; 18 19 import javax.xml.namespace.QName; 20 import com.sun.org.apache.xerces.internal.dom.PSVIElementNSImpl; 21 import com.sun.org.apache.xerces.internal.impl.xs.SchemaSymbols; 22 import com.sun.org.apache.xerces.internal.xs.ItemPSVI; 23 import org.testng.annotations.AfterClass; 24 import org.testng.annotations.BeforeClass; 25 import org.testng.annotations.Test; 26 import validation.BaseTest; 27 28 public class RootTypeDefinitionTest extends BaseTest { 29 private QName unknownType; 30 31 private QName typeX; 32 33 private QName typeY; 34 35 private QName typeZ; 36 37 private QName typeOtherNamespace; 38 39 private final static String UNKNOWN_TYPE_ERROR = "cvc-type.1"; 40 41 private final static String INVALID_DERIVATION_ERROR = "cvc-elt.4.3"; 42 getXMLDocument()43 protected String getXMLDocument() { 44 return "base.xml"; 45 } 46 getSchemaFile()47 protected String getSchemaFile() { 48 return "base.xsd"; 49 } 50 getRelevantErrorIDs()51 protected String[] getRelevantErrorIDs() { 52 return new String[] { UNKNOWN_TYPE_ERROR, INVALID_DERIVATION_ERROR }; 53 } 54 RootTypeDefinitionTest(String name)55 public RootTypeDefinitionTest(String name) { 56 super(name); 57 unknownType = new QName("W"); 58 typeX = new QName("X"); 59 typeY = new QName("Y"); 60 typeZ = new QName("Z"); 61 typeOtherNamespace = new QName("xslt.unittests", "W", "unit"); 62 } 63 64 65 @BeforeClass setUp()66 protected void setUp() throws Exception { 67 super.setUp(); 68 } 69 70 @AfterClass tearDown()71 protected void tearDown() throws Exception { 72 super.tearDown(); 73 } 74 75 @Test testDefault()76 public void testDefault() { 77 try { 78 reset(); 79 validateDocument(); 80 } catch (Exception e) { 81 fail("Validation failed: " + e.getMessage()); 82 } 83 84 checkDefault(); 85 } 86 87 @Test testSettingNull()88 public void testSettingNull() { 89 try { 90 reset(); 91 fValidator.setProperty(ROOT_TYPE, null); 92 validateDocument(); 93 } catch (Exception e) { 94 fail("Validation failed: " + e.getMessage()); 95 } 96 97 checkDefault(); 98 } 99 100 @Test testSettingToUnknownType()101 public void testSettingToUnknownType() { 102 try { 103 reset(); 104 fValidator.setProperty(ROOT_TYPE, unknownType); 105 validateDocument(); 106 } catch (Exception e) { 107 fail("Validation failed: " + e.getMessage()); 108 } 109 110 assertError(UNKNOWN_TYPE_ERROR); 111 checkDefault(); 112 } 113 114 @Test testSettingToEqualType()115 public void testSettingToEqualType() { 116 try { 117 reset(); 118 fValidator.setProperty(ROOT_TYPE, typeX); 119 validateDocument(); 120 } catch (Exception e) { 121 fail("Validation failed: " + e.getMessage()); 122 } 123 124 assertValidity(ItemPSVI.VALIDITY_VALID, fRootNode.getValidity()); 125 assertValidationAttempted(ItemPSVI.VALIDATION_FULL, fRootNode 126 .getValidationAttempted()); 127 assertElementNull(fRootNode.getElementDeclaration()); 128 assertTypeName("X", fRootNode.getTypeDefinition().getName()); 129 } 130 131 @Test testSettingToDerivedType()132 public void testSettingToDerivedType() { 133 try { 134 reset(); 135 // this is required to make it a valid type Y node 136 ((PSVIElementNSImpl) fRootNode).setAttributeNS(null, "attr", "typeY"); 137 fValidator.setProperty(ROOT_TYPE, typeY); 138 validateDocument(); 139 } catch (Exception e) { 140 fail("Validation failed: " + e.getMessage()); 141 } 142 143 assertValidity(ItemPSVI.VALIDITY_VALID, fRootNode.getValidity()); 144 assertValidationAttempted(ItemPSVI.VALIDATION_FULL, fRootNode 145 .getValidationAttempted()); 146 assertElementNull(fRootNode.getElementDeclaration()); 147 assertTypeName("Y", fRootNode.getTypeDefinition().getName()); 148 } 149 150 @Test testSettingToNonDerivedType()151 public void testSettingToNonDerivedType() { 152 try { 153 reset(); 154 fValidator.setProperty(ROOT_TYPE, typeZ); 155 validateDocument(); 156 } catch (Exception e) { 157 fail("Validation failed: " + e.getMessage()); 158 } 159 160 assertValidity(ItemPSVI.VALIDITY_VALID, fRootNode.getValidity()); 161 assertValidationAttempted(ItemPSVI.VALIDATION_FULL, fRootNode 162 .getValidationAttempted()); 163 assertElementNull(fRootNode.getElementDeclaration()); 164 assertTypeName("Z", fRootNode.getTypeDefinition().getName()); 165 } 166 167 @Test testSettingToOtherSchemaType()168 public void testSettingToOtherSchemaType() { 169 try { 170 reset(); 171 ((PSVIElementNSImpl) fRootNode).setAttributeNS(SchemaSymbols.URI_XSI, 172 SchemaSymbols.XSI_SCHEMALOCATION, 173 "xslt.unittests otherNamespace.xsd"); 174 fValidator.setProperty(ROOT_TYPE, typeOtherNamespace); 175 validateDocument(); 176 } catch (Exception e) { 177 fail("Validation failed: " + e.getMessage()); 178 } 179 180 assertValidity(ItemPSVI.VALIDITY_VALID, fRootNode.getValidity()); 181 assertValidationAttempted(ItemPSVI.VALIDATION_FULL, fRootNode 182 .getValidationAttempted()); 183 assertElementNull(fRootNode.getElementDeclaration()); 184 assertTypeName("W", fRootNode.getTypeDefinition().getName()); 185 assertTypeNamespace("xslt.unittests", fRootNode.getTypeDefinition() 186 .getNamespace()); 187 } 188 189 @Test testSettingTypeAndXSIType()190 public void testSettingTypeAndXSIType() { 191 try { 192 reset(); 193 // this is required to make it a valid type Y node 194 ((PSVIElementNSImpl) fRootNode).setAttributeNS(null, "attr", "typeY"); 195 ((PSVIElementNSImpl) fRootNode).setAttributeNS(SchemaSymbols.URI_XSI, 196 SchemaSymbols.XSI_TYPE, "Y"); 197 fValidator.setProperty(ROOT_TYPE, typeX); 198 validateDocument(); 199 } catch (Exception e) { 200 fail("Validation failed: " + e.getMessage()); 201 } 202 203 assertValidity(ItemPSVI.VALIDITY_VALID, fRootNode.getValidity()); 204 assertValidationAttempted(ItemPSVI.VALIDATION_FULL, fRootNode 205 .getValidationAttempted()); 206 assertElementNull(fRootNode.getElementDeclaration()); 207 assertTypeName("Y", fRootNode.getTypeDefinition().getName()); 208 } 209 210 @Test testSettingTypeAndInvalidXSIType()211 public void testSettingTypeAndInvalidXSIType() { 212 try { 213 reset(); 214 ((PSVIElementNSImpl) fRootNode).setAttributeNS(SchemaSymbols.URI_XSI, 215 SchemaSymbols.XSI_TYPE, "Z"); 216 fValidator.setProperty(ROOT_TYPE, typeX); 217 validateDocument(); 218 } catch (Exception e) { 219 fail("Validation failed: " + e.getMessage()); 220 } 221 222 assertError(INVALID_DERIVATION_ERROR); 223 assertValidity(ItemPSVI.VALIDITY_INVALID, fRootNode.getValidity()); 224 assertValidationAttempted(ItemPSVI.VALIDATION_FULL, fRootNode 225 .getValidationAttempted()); 226 assertElementNull(fRootNode.getElementDeclaration()); 227 assertTypeName("Z", fRootNode.getTypeDefinition().getName()); 228 } 229 checkDefault()230 private void checkDefault() { 231 assertValidity(ItemPSVI.VALIDITY_VALID, fRootNode.getValidity()); 232 assertValidationAttempted(ItemPSVI.VALIDATION_FULL, fRootNode 233 .getValidationAttempted()); 234 assertElementName("A", fRootNode.getElementDeclaration().getName()); 235 assertTypeName("X", fRootNode.getTypeDefinition().getName()); 236 } 237 } 238