1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package catalog;
  25 
  26 import static catalog.CatalogTestUtils.catalogResolver;
  27 import static catalog.CatalogTestUtils.catalogUriResolver;
  28 import static catalog.ResolutionChecker.checkPubIdResolution;
  29 import static catalog.ResolutionChecker.checkSysIdResolution;
  30 import static catalog.ResolutionChecker.checkUriResolution;
  31 
  32 import javax.xml.catalog.CatalogResolver;
  33 import javax.xml.catalog.CatalogUriResolver;
  34 
  35 import org.testng.annotations.DataProvider;
  36 import org.testng.annotations.Test;
  37 
  38 /*
  39  * @test
  40  * @bug 8077931
  41  * @summary Before matching identifiers and URI references, it has to normalize
  42  *          the passed identifiers and URI references. And then the catalog
  43  *          resolver uses the normalized stuff to search the counterparts in
  44  *          catalog files.
  45  * @compile ../../libs/catalog/CatalogTestUtils.java
  46  * @compile ../../libs/catalog/ResolutionChecker.java
  47  */
  48 public class NormalizationTest {
  49 
  50     private static final String CATALOG_NORMALIZATION = "normalization.xml";
  51 
  52     @Test(dataProvider = "systemId_uri-matchedUri")
  53     public void testNormalizationOnSysId(String sytemId, String matchedUri) {
  54         checkSysIdResolution(createEntityResolver(), sytemId, matchedUri);
  55     }
  56 
  57     @Test(dataProvider = "publicId-matchedUri")
  58     public void testNormalizationOnPubId(String publicId, String matchedUri) {
  59         checkPubIdResolution(createEntityResolver(), publicId, matchedUri);
  60     }
  61 
  62     @Test(dataProvider = "systemId_uri-matchedUri")
  63     public void testNormalizationOnUri(String uri, String matchedUri) {
  64         checkUriResolution(createUriResolver(), uri, matchedUri);
  65     }
  66 
  67     @DataProvider(name = "systemId_uri-matchedUri")
  68     private Object[][] dataOnSysIdAndUri() {
  69         return new Object[][] {
  70                 // The specified system id/URI reference contains spaces. And
  71                 // the counterparts in system/uri entries also contain spaces.
  72                 { "  http://remote/dtd/alice/docAlice.dtd  ",
  73                         "http://local/base/dtd/docAliceSys.dtd" },
  74 
  75                 // The specified system id/URI reference doesn't contain space.
  76                 // But the counterparts in system/uri entries contain spaces.
  77                 { "http://remote/dtd/alice/docAlice.dtd",
  78                         "http://local/base/dtd/docAliceSys.dtd" },
  79 
  80                 // The specified system id/URI reference contains special chars.
  81                 { "http://remote/dtd/bob/docBob<>\\^`{|}.dtd",
  82                         "http://local/base/dtd/docBobSys.dtd" },
  83 
  84                 // The specified system identifier/uri contains normalized chars.
  85                 { "http://remote/dtd/bob/docBob%3C%3E%5C%5E%60%7B%7C%7D.dtd",
  86                         "http://local/base/dtd/docBobSys.dtd" } };
  87     }
  88 
  89     @DataProvider(name = "publicId-matchedUri")
  90     private Object[][] dataOnPubId() {
  91         return new Object[][] {
  92                 // The specified public id contains spaces. And the counterparts
  93                 // in public entry also contains spaces.
  94                 { "   -//REMOTE//DTD    ALICE DOCALICE   XML//EN  ",
  95                         "http://local/base/dtd/docAlicePub.dtd" },
  96 
  97                 // The specified public id doesn't contain space. But the
  98                 // counterpart in public entry contains spaces.
  99                 { "-//REMOTE//DTD ALICE DOCALICE XML//EN",
 100                         "http://local/base/dtd/docAlicePub.dtd" },
 101 
 102                 // The specified public id contains spaces.
 103                 { "  -//REMOTE//DTD   BOB  DOCBOB  XML//EN",
 104                         "http://local/base/dtd/docBobPub.dtd" } };
 105     }
 106 
 107     private CatalogResolver createEntityResolver() {
 108         return catalogResolver(CATALOG_NORMALIZATION);
 109     }
 110 
 111     private CatalogUriResolver createUriResolver() {
 112         return catalogUriResolver(CATALOG_NORMALIZATION);
 113     }
 114 }