1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 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.puppycrawl.tools.checkstyle.internal.utils.TestUtil.isUtilsClassHasPrivateConstructor;
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertNull;
26  import static org.junit.Assert.assertTrue;
27  
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  import javax.xml.parsers.ParserConfigurationException;
32  
33  import org.junit.Test;
34  import org.powermock.reflect.Whitebox;
35  import org.xml.sax.SAXException;
36  import org.xml.sax.XMLReader;
37  
38  public class XmlLoaderTest {
39  
40      @Test
41      public void testParserConfiguredSuccessfully() throws Exception {
42          final DummyLoader dummyLoader = new DummyLoader(new HashMap<>(1));
43          final XMLReader parser = Whitebox.getInternalState(dummyLoader, "parser");
44          assertEquals("Invalid entity resolver", dummyLoader, parser.getEntityResolver());
45      }
46  
47      @Test
48      public void testIsProperUtilsClass() throws ReflectiveOperationException {
49          assertTrue("Constructor is not private", isUtilsClassHasPrivateConstructor(
50                  XmlLoader.LoadExternalDtdFeatureProvider.class, true));
51      }
52  
53      @Test
54      public void testResolveEntityDefault() throws Exception {
55          final Map<String, String> map = new HashMap<>();
56          map.put("predefined", "/google.xml");
57          final DummyLoader dummyLoader = new DummyLoader(map);
58          assertNull("Invalid entity", dummyLoader.resolveEntity("notPredefined", "BAD"));
59      }
60  
61      @Test
62      public void testResolveEntityMap() throws Exception {
63          final Map<String, String> map = new HashMap<>();
64          map.put("predefined", "/google.xml");
65          final DummyLoader dummyLoader = new DummyLoader(map);
66          assertNotNull("Invalid entity", dummyLoader.resolveEntity("predefined", "BAD"));
67      }
68  
69      private static final class DummyLoader extends XmlLoader {
70  
71          /* package */ DummyLoader(Map<String, String> publicIdToResourceNameMap)
72                  throws SAXException, ParserConfigurationException {
73              super(publicIdToResourceNameMap);
74          }
75  
76      }
77  
78  }