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