View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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.utils.CommonUtils.EMPTY_BYTE_ARRAY;
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertNotEquals;
25  import static org.junit.Assert.assertTrue;
26  import static org.junit.Assert.fail;
27  import static org.mockito.ArgumentMatchers.any;
28  import static org.mockito.Mockito.times;
29  import static org.powermock.api.mockito.PowerMockito.doNothing;
30  import static org.powermock.api.mockito.PowerMockito.mock;
31  import static org.powermock.api.mockito.PowerMockito.mockStatic;
32  import static org.powermock.api.mockito.PowerMockito.verifyStatic;
33  import static org.powermock.api.mockito.PowerMockito.when;
34  
35  import java.io.ByteArrayInputStream;
36  import java.io.FileInputStream;
37  import java.io.IOException;
38  import java.io.InputStream;
39  import java.lang.reflect.Constructor;
40  import java.lang.reflect.Field;
41  import java.net.URL;
42  import java.net.URLConnection;
43  import java.net.URLStreamHandler;
44  import java.util.Arrays;
45  import java.util.Enumeration;
46  import java.util.HashSet;
47  import java.util.Set;
48  
49  import org.junit.Test;
50  import org.junit.runner.RunWith;
51  import org.mockito.Mockito;
52  import org.powermock.core.classloader.annotations.PrepareForTest;
53  import org.powermock.modules.junit4.PowerMockRunner;
54  import org.xml.sax.Attributes;
55  import org.xml.sax.SAXException;
56  
57  import com.google.common.io.Closeables;
58  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
59  
60  /**
61   * Enter a description of class PackageNamesLoaderTest.java.
62   * @author Rick Giles
63   * @author lkuehne
64   */
65  @RunWith(PowerMockRunner.class)
66  @PrepareForTest({PackageNamesLoader.class, Closeables.class})
67  public class PackageNamesLoaderTest extends AbstractPathTestSupport {
68  
69      @Override
70      protected String getPackageLocation() {
71          return "com/puppycrawl/tools/checkstyle/packagenamesloader";
72      }
73  
74      @Test
75      public void testDefault()
76              throws CheckstyleException {
77          final Set<String> packageNames = PackageNamesLoader
78                  .getPackageNames(Thread.currentThread()
79                          .getContextClassLoader());
80          assertEquals("pkgNames.length.", 0,
81                  packageNames.size());
82      }
83  
84      /**
85       * Tests the loading of package names. This test needs mocking, because the package names would
86       * have to be placed in {@literal checkstyle_packages.xml}, but this will affect every test,
87       * which is undesired.
88       *
89       * @throws Exception if error occurs
90       */
91      @Test
92      @SuppressWarnings("unchecked")
93      public void testPackagesFile() throws Exception {
94          mockStatic(Closeables.class);
95          doNothing().when(Closeables.class);
96          Closeables.closeQuietly(any(InputStream.class));
97  
98          final URLConnection mockConnection = Mockito.mock(URLConnection.class);
99          when(mockConnection.getInputStream()).thenReturn(
100             new FileInputStream(getPath("InputPackageNamesLoaderFile.xml")));
101 
102         final URL url = getMockUrl(mockConnection);
103 
104         final Enumeration<URL> enumeration = mock(Enumeration.class);
105         when(enumeration.hasMoreElements()).thenReturn(true).thenReturn(false);
106         when(enumeration.nextElement()).thenReturn(url);
107 
108         final ClassLoader classLoader = mock(ClassLoader.class);
109         when(classLoader.getResources("checkstyle_packages.xml")).thenReturn(enumeration);
110 
111         final Set<String> actualPackageNames = PackageNamesLoader.getPackageNames(classLoader);
112         final String[] expectedPackageNames = {
113             "com.puppycrawl.tools.checkstyle",
114             "com.puppycrawl.tools.checkstyle.checks",
115             "com.puppycrawl.tools.checkstyle.checks.annotation",
116             "com.puppycrawl.tools.checkstyle.checks.blocks",
117             "com.puppycrawl.tools.checkstyle.checks.coding",
118             "com.puppycrawl.tools.checkstyle.checks.design",
119             "com.puppycrawl.tools.checkstyle.checks.header",
120             "com.puppycrawl.tools.checkstyle.checks.imports",
121             "com.puppycrawl.tools.checkstyle.checks.indentation",
122             "com.puppycrawl.tools.checkstyle.checks.javadoc",
123             "com.puppycrawl.tools.checkstyle.checks.metrics",
124             "com.puppycrawl.tools.checkstyle.checks.modifier",
125             "com.puppycrawl.tools.checkstyle.checks.naming",
126             "com.puppycrawl.tools.checkstyle.checks.regexp",
127             "com.puppycrawl.tools.checkstyle.checks.sizes",
128             "com.puppycrawl.tools.checkstyle.checks.whitespace",
129             "com.puppycrawl.tools.checkstyle.filefilters",
130             "com.puppycrawl.tools.checkstyle.filters",
131         };
132 
133         assertEquals("Invalid package names length.", expectedPackageNames.length,
134             actualPackageNames.size());
135         final Set<String> checkstylePackagesSet =
136                 new HashSet<>(Arrays.asList(expectedPackageNames));
137         assertEquals("Invalid names set.", checkstylePackagesSet, actualPackageNames);
138 
139         verifyStatic(Closeables.class, times(1));
140         Closeables.closeQuietly(any(InputStream.class));
141     }
142 
143     @Test
144     @SuppressWarnings("unchecked")
145     public void testPackagesWithDots() throws Exception {
146         final Constructor<PackageNamesLoader> constructor =
147                 PackageNamesLoader.class.getDeclaredConstructor();
148         constructor.setAccessible(true);
149         final PackageNamesLoader loader = constructor.newInstance();
150 
151         final Attributes attributes = mock(Attributes.class);
152         when(attributes.getValue("name")).thenReturn("coding.");
153         loader.startElement("", "", "package", attributes);
154         loader.endElement("", "", "package");
155 
156         final Field field = PackageNamesLoader.class.getDeclaredField("packageNames");
157         field.setAccessible(true);
158         final Set<String> list = (Set<String>) field.get(loader);
159         assertEquals("Invalid package name", "coding.", list.iterator().next());
160     }
161 
162     @Test
163     @SuppressWarnings("unchecked")
164     public void testPackagesWithSaxException() throws Exception {
165         final URLConnection mockConnection = Mockito.mock(URLConnection.class);
166         when(mockConnection.getInputStream()).thenReturn(
167                 new ByteArrayInputStream(EMPTY_BYTE_ARRAY));
168 
169         final URL url = getMockUrl(mockConnection);
170 
171         final Enumeration<URL> enumeration = mock(Enumeration.class);
172         when(enumeration.hasMoreElements()).thenReturn(true);
173         when(enumeration.nextElement()).thenReturn(url);
174 
175         final ClassLoader classLoader = mock(ClassLoader.class);
176         when(classLoader.getResources("checkstyle_packages.xml")).thenReturn(enumeration);
177 
178         try {
179             PackageNamesLoader.getPackageNames(classLoader);
180             fail("CheckstyleException is expected");
181         }
182         catch (CheckstyleException ex) {
183             assertTrue("Invalid exception cause class", ex.getCause() instanceof SAXException);
184         }
185     }
186 
187     @Test
188     @SuppressWarnings("unchecked")
189     public void testPackagesWithIoException() throws Exception {
190         final URLConnection mockConnection = Mockito.mock(URLConnection.class);
191         when(mockConnection.getInputStream()).thenReturn(null);
192 
193         final URL url = getMockUrl(mockConnection);
194 
195         final Enumeration<URL> enumer = mock(Enumeration.class);
196         when(enumer.hasMoreElements()).thenReturn(true);
197         when(enumer.nextElement()).thenReturn(url);
198 
199         final ClassLoader classLoader = mock(ClassLoader.class);
200         when(classLoader.getResources("checkstyle_packages.xml")).thenReturn(enumer);
201 
202         try {
203             PackageNamesLoader.getPackageNames(classLoader);
204             fail("CheckstyleException is expected");
205         }
206         catch (CheckstyleException ex) {
207             assertTrue("Invalid exception cause class", ex.getCause() instanceof IOException);
208             assertNotEquals("Invalid exception message",
209                     "unable to get package file resources", ex.getMessage());
210         }
211     }
212 
213     @Test
214     public void testPackagesWithIoExceptionGetResources() throws Exception {
215         final ClassLoader classLoader = mock(ClassLoader.class);
216         when(classLoader.getResources("checkstyle_packages.xml")).thenThrow(IOException.class);
217 
218         try {
219             PackageNamesLoader.getPackageNames(classLoader);
220             fail("CheckstyleException is expected");
221         }
222         catch (CheckstyleException ex) {
223             assertTrue("Invalid exception cause class", ex.getCause() instanceof IOException);
224             assertEquals("Invalid exception message",
225                     "unable to get package file resources", ex.getMessage());
226         }
227     }
228 
229     private static URL getMockUrl(final URLConnection connection) throws IOException {
230         final URLStreamHandler handler = new URLStreamHandler() {
231             @Override
232             protected URLConnection openConnection(final URL url) {
233                 return connection;
234             }
235         };
236         return new URL("http://foo.bar", "foo.bar", 80, "", handler);
237     }
238 
239 }