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.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      // UT uses Reflection to avoid removing null-validation from static method
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     // UT uses Reflection to cover IOException from 'loader.parseInputSource(source);'
122     // because this is possible situation (though highly unlikely), which depends on hardware
123     // and is difficult to emulate
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             // Workaround for warning "Result of InputStream.available() is ignored"
164             assertWithMessage("")
165                     .that(available)
166                     .isEqualTo(0);
167         }
168     }
169 
170 }