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.google.checkstyle.test.base;
21  
22  import java.io.IOException;
23  import java.util.Properties;
24  import java.util.Set;
25  
26  import org.checkstyle.base.AbstractItModuleTestSupport;
27  
28  import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
29  import com.puppycrawl.tools.checkstyle.PropertiesExpander;
30  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
31  import com.puppycrawl.tools.checkstyle.api.Configuration;
32  import com.puppycrawl.tools.checkstyle.internal.utils.CheckUtil;
33  import com.puppycrawl.tools.checkstyle.utils.ModuleReflectionUtil;
34  
35  public abstract class AbstractGoogleModuleTestSupport extends AbstractItModuleTestSupport {
36  
37      private static final String XML_NAME = "/google_checks.xml";
38  
39      private static final Configuration CONFIGURATION;
40  
41      private static final Set<Class<?>> CHECKSTYLE_MODULES;
42  
43      static {
44          try {
45              final Properties properties = new Properties();
46              properties.put("org.checkstyle.google.severity", "error");
47              final PropertiesExpander expander = new PropertiesExpander(properties);
48              CONFIGURATION = ConfigurationLoader.loadConfiguration(XML_NAME,
49                      expander);
50          }
51          catch (CheckstyleException ex) {
52              throw new IllegalStateException(ex);
53          }
54          try {
55              CHECKSTYLE_MODULES = CheckUtil.getCheckstyleModules();
56          }
57          catch (IOException ex) {
58              throw new IllegalStateException(ex);
59          }
60      }
61  
62      @Override
63      protected ModuleCreationOption findModuleCreationOption(String moduleName) {
64          ModuleCreationOption moduleCreationOption = ModuleCreationOption.IN_CHECKER;
65  
66          for (Class<?> moduleClass : CHECKSTYLE_MODULES) {
67              if (moduleClass.getSimpleName().equals(moduleName)
68                      || moduleClass.getSimpleName().equals(moduleName + "Check")) {
69                  if (ModuleReflectionUtil.isCheckstyleTreeWalkerCheck(moduleClass)
70                          || ModuleReflectionUtil.isTreeWalkerFilterModule(moduleClass)) {
71                      moduleCreationOption = ModuleCreationOption.IN_TREEWALKER;
72                  }
73                  break;
74              }
75          }
76  
77          return moduleCreationOption;
78      }
79  
80      /**
81       * Performs verification of the file with the given file path against the whole config.
82       *
83       * @param filePath file path to verify.
84       * @throws Exception if exception occurs during verification process.
85       */
86      protected void verifyWithWholeConfig(String filePath) throws Exception {
87          verifyWithItConfig(CONFIGURATION, filePath);
88      }
89  }