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 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
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
106
107
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 }