@Override public boolean equals(Object obj) { if (!(obj instanceof RulesProfile)) { return false; } if (this == obj) { return true; } RulesProfile other = (RulesProfile) obj; return new EqualsBuilder().append(language, other.getLanguage()).append(name, other.getName()).isEquals(); }
private static void appendHeader(RulesProfile profile, Writer writer) throws IOException { writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<!-- Generated by SonarQube -->" + "<profile><name>"); StringEscapeUtils.escapeXml(writer, profile.getName()); writer.append("</name><language>"); StringEscapeUtils.escapeXml(writer, profile.getLanguage()); writer.append("</language>"); }
private static void checkProfile(RulesProfile profile, ValidationMessages messages) { if (StringUtils.isBlank(profile.getName())) { messages.addErrorText("The mandatory node <name> is missing."); } if (StringUtils.isBlank(profile.getLanguage())) { messages.addErrorText("The mandatory node <language> is missing."); } }
@Override public Object clone() { RulesProfile clone = RulesProfile.create(getName(), getLanguage()); clone.setDefaultProfile(getDefaultProfile()); if (activeRules != null && !activeRules.isEmpty()) { clone.setActiveRules(activeRules.stream() .map(ar -> (ActiveRule) ar.clone()) .collect(Collectors.toList())); } return clone; }
/** * @param optionalSeverity if null, then the default rule severity is used */ public ActiveRule activateRule(final Rule rule, @Nullable RulePriority optionalSeverity) { if (activeRules.stream().anyMatch(ar -> ar.getRule().equals(rule))) { throw MessageException.of(String.format( "The definition of the profile '%s' (language '%s') contains multiple occurrences of the '%s:%s' rule. The plugin which declares this profile should fix this.", getName(), getLanguage(), rule.getRepositoryKey(), rule.getKey())); } ActiveRule activeRule = new ActiveRule(this, rule, optionalSeverity); activeRules.add(activeRule); return activeRule; }
@Override public void exportProfile(RulesProfile profile, Writer writer) { try { writer.write(format("Profile %s/%s exported by %s", profile.getLanguage(), profile.getName(), key)); } catch (IOException ioe) { throw new RuntimeException(ioe); } } };
@Test public void importProfile() { ValidationMessages validation = ValidationMessages.create(); RulesProfile profile = parse("importProfile.xml", validation); assertThat(profile.getLanguage()).isEqualTo("java"); assertThat(profile.getName()).isEqualTo("sonar way"); assertThat(validation.hasErrors()).isFalse(); assertThat(profile).isNotNull(); assertThat(profile.getActiveRule("checkstyle", "IllegalRegexp").getSeverity()).isEqualTo(RulePriority.CRITICAL); }
@Test public void shouldParseAnnotatedClasses() { RuleFinder ruleFinder = mock(RuleFinder.class); when(ruleFinder.findByKey(anyString(), anyString())).thenAnswer(new Answer<Rule>() { public Rule answer(InvocationOnMock iom) throws Throwable { return Rule.create((String) iom.getArguments()[0], (String) iom.getArguments()[1], (String) iom.getArguments()[1]); } }); ValidationMessages messages = ValidationMessages.create(); RulesProfile profile = new AnnotationProfileParser(ruleFinder).parse("squid", "Foo way", "java", Lists.<Class>newArrayList(FakeRule.class), messages); assertThat(profile.getName()).isEqualTo("Foo way"); assertThat(profile.getLanguage()).isEqualTo("java"); assertThat(profile.getActiveRule("squid", "fake").getSeverity()).isEqualTo(RulePriority.BLOCKER); assertThat(messages.hasErrors()).isFalse(); }
@Override public void define(Context context) { Profiler profiler = Profiler.create(Loggers.get(getClass())); for (ProfileDefinition definition : definitions) { profiler.start(); ValidationMessages validation = ValidationMessages.create(); RulesProfile profile = definition.createProfile(validation); validation.log(LOGGER); if (profile == null) { profiler.stopDebug(format("Loaded definition %s that return no profile", definition)); } else { if (!validation.hasErrors()) { define(context, profile); } profiler.stopDebug(format("Loaded deprecated profile definition %s for language %s", profile.getName(), profile.getLanguage())); } } }
private static void define(Context context, RulesProfile profile) { NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile(profile.getName(), profile.getLanguage()) .setDefault(profile.getDefaultProfile().booleanValue()); for (org.sonar.api.rules.ActiveRule ar : profile.getActiveRules()) { NewBuiltInActiveRule newActiveRule = newQp.activateRule(ar.getRepositoryKey(), ar.getRuleKey()); RulePriority overriddenSeverity = ar.getOverriddenSeverity(); if (overriddenSeverity != null) { newActiveRule.overrideSeverity(overriddenSeverity.name()); } for (ActiveRuleParam param : ar.getActiveRuleParams()) { newActiveRule.overrideParam(param.getKey(), param.getValue()); } } newQp.done(); }
@Override public String getLanguage() { if (singleLanguageProfile == null) { // Multi-languages module // This is a hack for CommonChecksDecorator that call this method in its constructor LOG.debug(DEPRECATED_USAGE_MESSAGE, new SonarException(DEPRECATED_USAGE_MESSAGE)); return ""; } return singleLanguageProfile.getLanguage(); }
@Override public boolean equals(Object obj) { if (!(obj instanceof RulesProfile)) { return false; } if (this == obj) { return true; } RulesProfile other = (RulesProfile) obj; return new EqualsBuilder().append(language, other.getLanguage()).append(name, other.getName()).isEquals(); }
@Override public String getLanguage() { if (singleLanguageProfile == null) { // Multi-languages module // This is a hack for CommonChecksDecorator that call this method in its constructor LOG.debug(DEPRECATED_USAGE_MESSAGE, new SonarException(DEPRECATED_USAGE_MESSAGE)); return ""; } return singleLanguageProfile.getLanguage(); }
private void appendHeader(RulesProfile profile, Writer writer) throws IOException { writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<!-- Generated by SonarQube -->" + "<profile><name>"); StringEscapeUtils.escapeXml(writer, profile.getName()); writer.append("</name><language>"); StringEscapeUtils.escapeXml(writer, profile.getLanguage()); writer.append("</language>"); }
private static void appendHeader(RulesProfile profile, Writer writer) throws IOException { writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<!-- Generated by SonarQube -->" + "<profile><name>"); StringEscapeUtils.escapeXml(writer, profile.getName()); writer.append("</name><language>"); StringEscapeUtils.escapeXml(writer, profile.getLanguage()); writer.append("</language>"); }
private void checkProfile(RulesProfile profile, ValidationMessages messages) { if (StringUtils.isBlank(profile.getName())) { messages.addErrorText("The mandatory node <name> is missing."); } if (StringUtils.isBlank(profile.getLanguage())) { messages.addErrorText("The mandatory node <language> is missing."); } }
@Override public Object clone() { RulesProfile clone = RulesProfile.create(getName(), getLanguage()); clone.setDefaultProfile(getDefaultProfile()); if (activeRules != null && !activeRules.isEmpty()) { clone.setActiveRules(activeRules.stream() .map(ar -> (ActiveRule) ar.clone()) .collect(Collectors.toList())); } return clone; }
private static ListMultimap<String, RulesProfile> profilesByLanguage(ProfileDefinition[] profileDefinitions) { ListMultimap<String, RulesProfile> byLang = ArrayListMultimap.create(); for (ProfileDefinition definition : profileDefinitions) { ValidationMessages validation = ValidationMessages.create(); RulesProfile profile = definition.createProfile(validation); if (profile != null && !validation.hasErrors()) { byLang.put(StringUtils.lowerCase(profile.getLanguage()), profile); } } return byLang; }
private static void checkProfile(RulesProfile profile, ValidationMessages messages) { if (StringUtils.isBlank(profile.getName())) { messages.addErrorText("The mandatory node <name> is missing."); } if (StringUtils.isBlank(profile.getLanguage())) { messages.addErrorText("The mandatory node <language> is missing."); } }
@Override public boolean equals(Object obj) { if (!(obj instanceof RulesProfile)) { return false; } if (this == obj) { return true; } RulesProfile other = (RulesProfile) obj; return new EqualsBuilder().append(language, other.getLanguage()).append(name, other.getName()).isEquals(); }