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