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.sun.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 AbstractSunModuleTestSupport extends AbstractItModuleTestSupport {
35  
36      private static final String XML_NAME = "/sun_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 ex) {
48              throw new IllegalStateException(ex);
49          }
50          try {
51              CHECKSTYLE_MODULES = CheckUtil.getCheckstyleModules();
52          }
53          catch (IOException ex) {
54              throw new IllegalStateException(ex);
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       * Returns {@link Configuration} instance for the given module name.
78       * This implementation uses {@link #getModuleConfig(String, String)} method inside.
79       *
80       * @param moduleName module name.
81       * @return {@link Configuration} instance for the given module name.
82       */
83      protected static Configuration getModuleConfig(String moduleName) {
84          return getModuleConfig(moduleName, null);
85      }
86  
87      /**
88       * Returns {@link Configuration} instance for the given module name.
89       * This implementation uses {@link #getModuleConfig(String)} method inside.
90       *
91       * @param moduleName module name.
92       * @param moduleId module id.
93       * @return {@link Configuration} instance for the given module name.
94       * @throws IllegalStateException if there is a problem retrieving the module or config.
95       */
96      protected static Configuration getModuleConfig(String moduleName, String moduleId) {
97          return getModuleConfig(CONFIGURATION, moduleName, moduleId);
98      }
99  
100 }