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