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