CheckList { public static List<Class<?>> getChecks() { return ImmutableList.<Class<?>>builder() .addAll(getJavaChecks()) .addAll(getJavaTestChecks()) .addAll(getXmlChecks()) .build(); } private  CheckList(); static List<Class<?>> getChecks(); static List<Class<? extends JavaCheck>> getJavaChecks(); static List<Class<? extends DebugCheck>> getDebugChecks(); static List<Class<? extends JavaCheck>> getJavaTestChecks(); static List<Class<? extends SonarXmlCheck>> getXmlChecks(); static final String REPOSITORY_KEY; }
@Test void count() { int count = 0; List<File> files = (List<File>) FileUtils.listFiles(new File("src/main/java/org/sonar/java/checks/"), new String[] {"java"}, true); for (File file : files) { if (file.getName().endsWith("Check.java") && !BLACK_LIST.contains(file.getName())) { count++; } } assertThat(CheckList.getChecks().size()).isEqualTo(count + SE_CHEKS.size()); }
@Test void test() { Map<String, String> keyMap = new HashMap<>(); for (Class<?> cls : CheckList.getChecks()) { String testName = '/' + cls.getName().replace('.', '/') + "Test.class"; String simpleName = cls.getSimpleName(); Rule ruleAnnotation = AnnotationUtils.getAnnotation(cls, Rule.class); String key = getKey(cls, ruleAnnotation); keyMap.put(ruleAnnotation.key(), key); if (SE_CHEKS.contains(simpleName)) { continue; } assertThat(getClass().getResource(testName)) .overridingErrorMessage("No test for " + simpleName) .isNotNull(); } Set<String> keys = new HashSet<>(); Set<String> names = Sets.newHashSet(); CustomRulesDefinition definition = new CustomRulesDefinition(); RulesDefinition.Context context = new RulesDefinition.Context(); definition.define(context); List<RulesDefinition.Rule> rules = context.repository(CheckList.REPOSITORY_KEY).rules(); for (RulesDefinition.Rule rule : rules) { assertThat(keys).as("Duplicate key " + rule.key()).doesNotContain(rule.key()); keys.add(rule.key()); names.add(rule.name()); assertThat(getClass().getResource("/org/sonar/l10n/java/rules/" + CheckList.REPOSITORY_KEY + "/" + keyMap.get(rule.key()) + "_java.html")) .overridingErrorMessage("No description for " + rule.key() + " " + keyMap.get(rule.key())) .isNotNull(); assertThat(getClass().getResource("/org/sonar/l10n/java/rules/" + CheckList.REPOSITORY_KEY + "/" + keyMap.get(rule.key()) + "_java.json")) .overridingErrorMessage("No json metadata file for " + rule.key() + " " + keyMap.get(rule.key())) .isNotNull(); assertThat(rule.htmlDescription()).isNull(); assertThat(rule.markdownDescription()).isEqualTo(ARTIFICIAL_DESCRIPTION); for (RulesDefinition.Param param : rule.params()) { assertThat(param.description()).overridingErrorMessage(rule.key() + " missing description for param " + param.key()).isNotEmpty(); } } }
@Test void enforce_CheckList_registration() { List<File> files = (List<File>) FileUtils.listFiles(new File("src/main/java/org/sonar/java/checks/"), new String[] {"java"}, false); List<Class<?>> checks = CheckList.getChecks(); for (File file : files) { String name = file.getName(); if (name.endsWith("Check.java")) { String className = name.substring(0, name.length() - 5); try { Class<?> aClass = Class.forName("org.sonar.java.checks." + className); assertThat(checks).as(className + " is not declared in CheckList").contains(aClass); } catch (ClassNotFoundException e) { throw new IllegalStateException(e); } } } }