1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 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 org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertSame;
25  import static org.junit.Assert.assertTrue;
26  import static org.junit.Assert.fail;
27  
28  import java.io.File;
29  import java.lang.reflect.InvocationTargetException;
30  import java.lang.reflect.Method;
31  import java.net.MalformedURLException;
32  import java.net.URI;
33  
34  import org.junit.Test;
35  import org.xml.sax.Attributes;
36  import org.xml.sax.InputSource;
37  import org.xml.sax.SAXException;
38  import org.xml.sax.helpers.AttributesImpl;
39  
40  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
41  
42  public class ImportControlLoaderTest {
43  
44      private static String getPath(String filename) {
45          return "src/test/resources/com/puppycrawl/tools/"
46                  + "checkstyle/checks/imports/importcontrolloader/" + filename;
47      }
48  
49      @Test
50      public void testLoad() throws CheckstyleException {
51          final AbstractImportControl root =
52                  ImportControlLoader.load(
53                  new File(getPath("InputImportControlLoaderComplete.xml")).toURI());
54          assertNotNull("Import root should not be null", root);
55      }
56  
57      @Test
58      public void testWrongFormatUri() throws Exception {
59          try {
60              ImportControlLoader.load(new URI("aaa://"
61                      + getPath("InputImportControlLoaderComplete.xml")));
62              fail("exception expected");
63          }
64          catch (CheckstyleException ex) {
65              assertSame("Invalid exception class",
66                      MalformedURLException.class, ex.getCause().getClass());
67              assertEquals("Invalid exception message",
68                      "unknown protocol: aaa", ex.getCause().getMessage());
69          }
70      }
71  
72      @Test
73      public void testExtraElementInConfig() throws Exception {
74          final AbstractImportControl root =
75                  ImportControlLoader.load(
76                      new File(getPath("InputImportControlLoaderWithNewElement.xml")).toURI());
77          assertNotNull("Import root should not be null", root);
78      }
79  
80      @Test
81      // UT uses Reflection to avoid removing null-validation from static method
82      public void testSafeGetThrowsException() throws Exception {
83          final AttributesImpl attr = new AttributesImpl() {
84              @Override
85              public String getValue(int index) {
86                  return null;
87                  }
88              };
89          try {
90              final Class<?> clazz = ImportControlLoader.class;
91              final Method privateMethod = clazz.getDeclaredMethod("safeGet",
92                  Attributes.class, String.class);
93              privateMethod.setAccessible(true);
94              privateMethod.invoke(null, attr, "you_cannot_find_me");
95              fail("exception expected");
96          }
97          catch (InvocationTargetException ex) {
98              assertSame("Invalid exception class", SAXException.class, ex.getCause().getClass());
99              assertEquals("Invalid exception message",
100                     "missing attribute you_cannot_find_me", ex.getCause().getMessage());
101         }
102     }
103 
104     @Test
105     // UT uses Reflection to cover IOException from 'loader.parseInputSource(source);'
106     // because this is possible situation (though highly unlikely), which depends on hardware
107     // and is difficult to emulate
108     public void testLoadThrowsException() throws Exception {
109         final InputSource source = new InputSource();
110         try {
111             final Class<?> clazz = ImportControlLoader.class;
112             final Method privateMethod = clazz.getDeclaredMethod("load", InputSource.class,
113                 URI.class);
114             privateMethod.setAccessible(true);
115             privateMethod.invoke(null, source,
116                     new File(getPath("InputImportControlLoaderComplete.xml")).toURI());
117             fail("exception expected");
118         }
119         catch (InvocationTargetException ex) {
120             assertSame("Invalid exception class",
121                     CheckstyleException.class, ex.getCause().getClass());
122             assertTrue("Invalid exception message: " + ex.getCause().getMessage(),
123                     ex.getCause().getMessage().startsWith("unable to read"));
124         }
125     }
126 
127 }