View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.isUtilsClassHasPrivateConstructor;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import javax.xml.parsers.ParserConfigurationException;
29  
30  import org.junit.jupiter.api.Test;
31  import org.xml.sax.SAXException;
32  import org.xml.sax.XMLReader;
33  
34  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
35  
36  public class XmlLoaderTest {
37  
38      @Test
39      public void testParserConfiguredSuccessfully() throws Exception {
40          final DummyLoader dummyLoader = new DummyLoader(new HashMap<>(1));
41          final XMLReader parser = TestUtil.getInternalState(dummyLoader, "parser");
42          assertWithMessage("Invalid entity resolver")
43              .that(parser.getEntityResolver())
44              .isEqualTo(dummyLoader);
45      }
46  
47      @Test
48      public void testIsProperUtilsClass() throws ReflectiveOperationException {
49          assertWithMessage("Constructor is not private")
50                  .that(isUtilsClassHasPrivateConstructor(
51                          XmlLoader.LoadExternalDtdFeatureProvider.class))
52                  .isTrue();
53      }
54  
55      @Test
56      public void testResolveEntityDefault() throws Exception {
57          final Map<String, String> map = new HashMap<>();
58          map.put("predefined", "/google.xml");
59          final DummyLoader dummyLoader = new DummyLoader(map);
60          assertWithMessage("Invalid entity")
61              .that(dummyLoader.resolveEntity("notPredefined", "BAD"))
62              .isNull();
63      }
64  
65      @Test
66      public void testResolveEntityMap() throws Exception {
67          final Map<String, String> map = new HashMap<>();
68          map.put("predefined", "/google.xml");
69          final DummyLoader dummyLoader = new DummyLoader(map);
70          assertWithMessage("Invalid entity")
71              .that(dummyLoader.resolveEntity("predefined", "BAD"))
72              .isNotNull();
73      }
74  
75      private static final class DummyLoader extends XmlLoader {
76  
77          private DummyLoader(Map<String, String> publicIdToResourceNameMap)
78                  throws SAXException, ParserConfigurationException {
79              super(publicIdToResourceNameMap);
80          }
81  
82      }
83  
84  }