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.meta;
21
22 import static com.google.common.truth.Truth.assertThat;
23
24 import java.io.InputStream;
25 import java.nio.charset.StandardCharsets;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.util.List;
29
30 import org.apache.commons.io.IOUtils;
31 import org.junit.jupiter.api.Test;
32
33 import com.puppycrawl.tools.checkstyle.AbstractPathTestSupport;
34
35 public class XmlMetaReaderTest extends AbstractPathTestSupport {
36
37 @Override
38 public String getPackageLocation() {
39 return "com/puppycrawl/tools/checkstyle/meta/xmlmetareader";
40 }
41
42 @Test
43 public void test() {
44 assertThat(XmlMetaReader.readAllModulesIncludingThirdPartyIfAny()).hasSize(219);
45 }
46
47 @Test
48 public void testDuplicatePackage() {
49 assertThat(XmlMetaReader
50 .readAllModulesIncludingThirdPartyIfAny("com.puppycrawl.tools.checkstyle.meta"))
51 .hasSize(219);
52 }
53
54 @Test
55 public void testBadPackage() {
56 assertThat(XmlMetaReader.readAllModulesIncludingThirdPartyIfAny("DOES.NOT.EXIST"))
57 .hasSize(219);
58 }
59
60 @Test
61 public void testReadXmlMetaCheckWithProperties() throws Exception {
62 final String path = getPath("InputXmlMetaReaderCheckWithProps.xml");
63 try (InputStream is = Files.newInputStream(Path.of(path))) {
64 final ModuleDetails result = XmlMetaReader.read(is, ModuleType.CHECK);
65 checkModuleProps(result, ModuleType.CHECK, "Some description for check",
66 "com.puppycrawl.tools.checkstyle.checks.misc.InputCheck",
67 "com.puppycrawl.tools.checkstyle.TreeWalker");
68 assertThat(result.getName()).isEqualTo("InputCheck");
69 final List<String> violationMessageKeys = result.getViolationMessageKeys();
70 assertThat(violationMessageKeys).hasSize(1);
71 assertThat(violationMessageKeys.getFirst()).isEqualTo("test.key");
72 final List<ModulePropertyDetails> props = result.getProperties();
73 assertThat(props).hasSize(2);
74 final ModulePropertyDetails prop1 = props.getFirst();
75 checkProperty(prop1, "propertyOne", "java.lang.String",
76 "propertyOneDefaultValue",
77 "Property wrapped\n description.");
78 assertThat(prop1.getValidationType()).isNull();
79
80 final ModulePropertyDetails prop2 = props.get(1);
81 checkProperty(prop2, "propertyTwo", "java.lang.String[]",
82 "", "Property two desc");
83 assertThat(prop2.getValidationType()).isEqualTo("tokenTypesSet");
84 }
85 }
86
87 @Test
88 public void testReadXmlMetaCheckNoProperties() throws Exception {
89 final String path = getPath("InputXmlMetaReaderCheckNoProps.xml");
90 try (InputStream is = Files.newInputStream(Path.of(path))) {
91 final ModuleDetails result = XmlMetaReader.read(is, ModuleType.CHECK);
92 checkModuleProps(result, ModuleType.CHECK,
93 "Some description for check with no properties",
94 "com.puppycrawl.tools.checkstyle.checks.misc.InputCheckNoProps",
95 "com.puppycrawl.tools.checkstyle.TreeWalker");
96 assertThat(result.getName()).isEqualTo("InputCheckNoProps");
97 final List<String> violationMessageKeys = result.getViolationMessageKeys();
98 assertThat(violationMessageKeys).hasSize(2);
99 assertThat(violationMessageKeys.getFirst()).isEqualTo("test.key1");
100 assertThat(violationMessageKeys.get(1)).isEqualTo("test.key2");
101 assertThat(result.getProperties()).isEmpty();
102 }
103 }
104
105 @Test
106 public void testReadXmlMetaFilter() throws Exception {
107 final String path = getPath("InputXmlMetaReaderFilter.xml");
108 try (InputStream is = Files.newInputStream(Path.of(path))) {
109 final ModuleDetails result = XmlMetaReader.read(is, ModuleType.FILTER);
110 checkModuleProps(result, ModuleType.FILTER, "Description for filter",
111 "com.puppycrawl.tools.checkstyle.filters.SomeFilter",
112 "com.puppycrawl.tools.checkstyle.TreeWalker");
113 assertThat(result.getName()).isEqualTo("SomeFilter");
114 assertThat(result.getViolationMessageKeys()).isEmpty();
115 final List<ModulePropertyDetails> props = result.getProperties();
116 assertThat(props).hasSize(1);
117 final ModulePropertyDetails prop1 = props.getFirst();
118 checkProperty(prop1, "propertyOne", "java.util.regex.Pattern",
119 "propertyDefaultValue", "Property description.");
120 assertThat(prop1.getValidationType()).isNull();
121 }
122 }
123
124 @Test
125 public void testReadXmlMetaFileFilter() throws Exception {
126 final String path = getPath("InputXmlMetaReaderFileFilter.xml");
127 try (InputStream is = Files.newInputStream(Path.of(path))) {
128 final ModuleDetails result = XmlMetaReader.read(is, ModuleType.FILEFILTER);
129 checkModuleProps(result, ModuleType.FILEFILTER,
130 "File filter description",
131 "com.puppycrawl.tools.checkstyle.filefilters.FileFilter",
132 "com.puppycrawl.tools.checkstyle.Checker");
133 assertThat(result.getName()).isEqualTo("FileFilter");
134 assertThat(result.getViolationMessageKeys()).isEmpty();
135 final List<ModulePropertyDetails> props = result.getProperties();
136 assertThat(props).hasSize(1);
137 final ModulePropertyDetails prop1 = props.getFirst();
138 assertThat(prop1.getName()).isEqualTo("fileNamePattern");
139 assertThat(prop1.getType()).isEqualTo("java.util.regex.Pattern");
140 assertThat(prop1.getDefaultValue()).isNull();
141 assertThat(prop1.getValidationType()).isNull();
142 assertThat(prop1.getDescription())
143 .isEqualTo("Define regular expression to match the file name against.");
144 }
145 }
146
147 @Test
148 public void testReadXmlMetaModuleTypeNull() throws Exception {
149 try (InputStream is = IOUtils.toInputStream("", StandardCharsets.UTF_8)) {
150 assertThat(XmlMetaReader.read(is, null)).isNull();
151 }
152 }
153
154 private static void checkModuleProps(ModuleDetails result, ModuleType moduleType,
155 String description,
156 String fullName, String parent) {
157 assertThat(result.getModuleType()).isEqualTo(moduleType);
158 assertThat(result.getDescription()).isEqualTo(description);
159 assertThat(result.getFullQualifiedName()).isEqualTo(fullName);
160 assertThat(result.getParent()).isEqualTo(parent);
161 }
162
163 private static void checkProperty(ModulePropertyDetails prop, String name,
164 String type, String defaultValue, String description) {
165 assertThat(prop.getName()).isEqualTo(name);
166 assertThat(prop.getType()).isEqualTo(type);
167 assertThat(prop.getDefaultValue()).isEqualTo(defaultValue);
168 assertThat(prop.getDescription()).isEqualTo(description);
169 }
170 }