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.checks.imports;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static org.junit.jupiter.api.Assertions.assertThrows;
24 import static org.mockito.Mockito.doThrow;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.lang.reflect.Method;
32 import java.net.MalformedURLException;
33 import java.net.URI;
34 import java.net.URL;
35
36 import org.junit.jupiter.api.Test;
37 import org.xml.sax.Attributes;
38 import org.xml.sax.InputSource;
39 import org.xml.sax.SAXException;
40 import org.xml.sax.SAXParseException;
41 import org.xml.sax.helpers.AttributesImpl;
42
43 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
44
45 public class ImportControlLoaderTest {
46
47 private static String getPath(String filename) {
48 return "src/test/resources/com/puppycrawl/tools/"
49 + "checkstyle/checks/imports/importcontrolloader/" + filename;
50 }
51
52 @Test
53 public void testLoad() throws CheckstyleException {
54 final AbstractImportControl root =
55 ImportControlLoader.load(
56 new File(getPath("InputImportControlLoaderComplete.xml")).toURI());
57 assertWithMessage("Import root should not be null")
58 .that(root)
59 .isNotNull();
60 }
61
62 @Test
63 public void testWrongFormatUri() throws Exception {
64 try {
65 ImportControlLoader.load(new URI("aaa://"
66 + getPath("InputImportControlLoaderComplete.xml")));
67 assertWithMessage("exception expected").fail();
68 }
69 catch (CheckstyleException ex) {
70 assertWithMessage("Invalid exception class")
71 .that(ex.getCause())
72 .isInstanceOf(MalformedURLException.class);
73 assertWithMessage("Invalid exception message")
74 .that(ex)
75 .hasCauseThat()
76 .hasMessageThat()
77 .isEqualTo("unknown protocol: aaa");
78 }
79 }
80
81 @Test
82 public void testExtraElementInConfig() throws Exception {
83 final AbstractImportControl root =
84 ImportControlLoader.load(
85 new File(getPath("InputImportControlLoaderWithNewElement.xml")).toURI());
86 assertWithMessage("Import root should not be null")
87 .that(root)
88 .isNotNull();
89 }
90
91 @Test
92
93 public void testSafeGetThrowsException() {
94 final AttributesImpl attr = new AttributesImpl() {
95 @Override
96 public String getValue(int index) {
97 return null;
98 }
99 };
100 try {
101 final Class<?> clazz = ImportControlLoader.class;
102 final Method privateMethod = clazz.getDeclaredMethod("safeGet",
103 Attributes.class, String.class);
104 privateMethod.setAccessible(true);
105 privateMethod.invoke(null, attr, "you_cannot_find_me");
106 assertWithMessage("exception expected").fail();
107 }
108 catch (ReflectiveOperationException ex) {
109 assertWithMessage("Invalid exception class")
110 .that(ex.getCause())
111 .isInstanceOf(SAXException.class);
112 assertWithMessage("Invalid exception message")
113 .that(ex)
114 .hasCauseThat()
115 .hasMessageThat()
116 .isEqualTo("missing attribute you_cannot_find_me");
117 }
118 }
119
120 @Test
121
122
123
124 public void testLoadThrowsException() {
125 final InputSource source = new InputSource();
126 try {
127 final Class<?> clazz = ImportControlLoader.class;
128 final Method privateMethod = clazz.getDeclaredMethod("load", InputSource.class,
129 URI.class);
130 privateMethod.setAccessible(true);
131 privateMethod.invoke(null, source,
132 new File(getPath("InputImportControlLoaderComplete.xml")).toURI());
133 assertWithMessage("exception expected").fail();
134 }
135 catch (ReflectiveOperationException ex) {
136 assertWithMessage("Invalid exception class")
137 .that(ex.getCause())
138 .isInstanceOf(CheckstyleException.class);
139 assertWithMessage("Invalid exception message: " + ex.getCause().getMessage())
140 .that(ex)
141 .hasCauseThat()
142 .hasMessageThat()
143 .startsWith("unable to read");
144 }
145 }
146
147 @Test
148 public void testInputStreamFailsOnRead() throws Exception {
149 try (InputStream inputStream = mock()) {
150 final int available = doThrow(IOException.class).when(inputStream).available();
151 final URL url = mock();
152 when(url.openStream()).thenReturn(inputStream);
153 final URI uri = mock();
154 when(uri.toURL()).thenReturn(url);
155
156 final CheckstyleException ex = assertThrows(CheckstyleException.class, () -> {
157 ImportControlLoader.load(uri);
158 });
159 assertWithMessage("Invalid exception class")
160 .that(ex)
161 .hasCauseThat()
162 .isInstanceOf(SAXParseException.class);
163
164 assertWithMessage("")
165 .that(available)
166 .isEqualTo(0);
167 }
168 }
169
170 }