1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }