|
18 | 18 | */
|
19 | 19 | package org.sonar.plugins.groovy;
|
20 | 20 |
|
21 |
| -import static org.assertj.core.api.Assertions.assertThat; |
22 |
| - |
23 |
| -import java.util.HashSet; |
24 |
| -import java.util.List; |
25 |
| -import java.util.Set; |
26 | 21 | import org.junit.Test;
|
27 | 22 | import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
|
28 | 23 | import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInActiveRule;
|
29 | 24 | import org.sonar.api.server.rule.RulesDefinition;
|
30 | 25 | import org.sonar.api.utils.ValidationMessages;
|
31 | 26 | import org.sonar.plugins.groovy.codenarc.CodeNarcRulesDefinition;
|
32 | 27 | import org.sonar.plugins.groovy.foundation.Groovy;
|
| 28 | +import org.w3c.dom.Document; |
| 29 | +import org.w3c.dom.NodeList; |
| 30 | +import org.xml.sax.SAXException; |
| 31 | + |
| 32 | +import javax.xml.parsers.DocumentBuilder; |
| 33 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 34 | +import javax.xml.parsers.ParserConfigurationException; |
| 35 | +import java.io.File; |
| 36 | +import java.io.IOException; |
| 37 | +import java.util.HashSet; |
| 38 | +import java.util.List; |
| 39 | +import java.util.Set; |
| 40 | + |
| 41 | +import static org.assertj.core.api.Assertions.assertThat; |
33 | 42 |
|
34 | 43 | public class GroovySonarWayProfileTest {
|
35 | 44 |
|
36 |
| - @Test |
37 |
| - public void shouldCreateSonarWayProfile() { |
38 |
| - ValidationMessages messages = ValidationMessages.create(); |
| 45 | + @Test |
| 46 | + public void shouldCreateSonarWayProfile() throws ParserConfigurationException, IOException, SAXException { |
| 47 | + ValidationMessages messages = ValidationMessages.create(); |
39 | 48 |
|
40 |
| - GroovySonarWayProfile profileDef = new GroovySonarWayProfile(); |
41 |
| - BuiltInQualityProfilesDefinition.Context profileContext = |
42 |
| - new BuiltInQualityProfilesDefinition.Context(); |
43 |
| - profileDef.define(profileContext); |
44 |
| - BuiltInQualityProfilesDefinition.BuiltInQualityProfile profile = |
45 |
| - profileContext.profile(Groovy.KEY, Groovy.PROFILE_NAME); |
46 |
| - assertThat(profile.language()).isEqualTo(Groovy.KEY); |
47 |
| - List<BuiltInActiveRule> activeRules = profile.rules(); |
48 |
| - // TODO The Number of Custom profile rules are set here, |
49 |
| - // we need to change this to get the size dynamically |
50 |
| - assertThat(activeRules).as("Expected number of rules in profile").hasSize(3); |
51 |
| - assertThat(profile.name()).isEqualTo(Groovy.PROFILE_NAME); |
| 49 | + GroovySonarWayProfile profileDef = new GroovySonarWayProfile(); |
| 50 | + BuiltInQualityProfilesDefinition.Context profileContext = new BuiltInQualityProfilesDefinition.Context(); |
| 51 | + profileDef.define(profileContext); |
| 52 | + BuiltInQualityProfilesDefinition.BuiltInQualityProfile profile = profileContext.profile(Groovy.KEY, Groovy.PROFILE_NAME); |
| 53 | + assertThat(profile.language()).isEqualTo(Groovy.KEY); |
| 54 | + List<BuiltInActiveRule> activeRules = profile.rules(); |
52 | 55 |
|
53 |
| - // Check that we use severity from the read rule and not default one. |
54 |
| - assertThat(activeRules.get(0).overriddenSeverity()).isNull(); |
55 |
| - assertThat(messages.hasErrors()).isFalse(); |
| 56 | + // Here we check the rules.xml file to get the number of rules in it |
| 57 | + File profile_default = new File("src/main/resources/org/sonar/plugins/groovy/rules.xml"); |
| 58 | + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
| 59 | + DocumentBuilder db; |
| 60 | + db = dbf.newDocumentBuilder(); |
| 61 | + Document doc = db.parse(profile_default); |
| 62 | + doc.getDocumentElement().normalize(); |
| 63 | + NodeList ruleList = doc.getElementsByTagName("rule"); |
| 64 | + assertThat(activeRules).as("Expected number of rules in profile").hasSize(ruleList.getLength()); |
| 65 | + assertThat(profile.name()).isEqualTo(Groovy.PROFILE_NAME); |
56 | 66 |
|
57 |
| - // Check that all rules in "Sonar way" actually exist |
58 |
| - CodeNarcRulesDefinition definition = new CodeNarcRulesDefinition(); |
59 |
| - RulesDefinition.Context rulesContext = new RulesDefinition.Context(); |
60 |
| - definition.define(rulesContext); |
61 |
| - RulesDefinition.Repository repository = |
62 |
| - rulesContext.repository(CodeNarcRulesDefinition.REPOSITORY_KEY); |
| 67 | + // Check that we use severity from the read rule and not default one. |
| 68 | + assertThat(activeRules.get(0).overriddenSeverity()).isNull(); |
| 69 | + assertThat(messages.hasErrors()).isFalse(); |
63 | 70 |
|
64 |
| - Set<String> rules = new HashSet<>(); |
65 |
| - for (RulesDefinition.Rule rule : repository.rules()) { |
66 |
| - rules.add(rule.key()); |
67 |
| - } |
68 |
| - for (BuiltInActiveRule activeRule : profile.rules()) { |
69 |
| - assertThat(rules.contains(activeRule.ruleKey())) |
70 |
| - .as("No such rule: " + activeRule.ruleKey()) |
71 |
| - .isTrue(); |
| 71 | + // Check that all rules in "Sonar way" actually exist |
| 72 | + CodeNarcRulesDefinition definition = new CodeNarcRulesDefinition(); |
| 73 | + RulesDefinition.Context rulesContext = new RulesDefinition.Context(); |
| 74 | + definition.define(rulesContext); |
| 75 | + RulesDefinition.Repository repository = |
| 76 | + rulesContext.repository(CodeNarcRulesDefinition.REPOSITORY_KEY); |
| 77 | + |
| 78 | + Set<String> rules = new HashSet<>(); |
| 79 | + for (RulesDefinition.Rule rule : repository.rules()) { |
| 80 | + rules.add(rule.key()); |
| 81 | + } |
| 82 | + for (BuiltInActiveRule activeRule : profile.rules()) { |
| 83 | + assertThat(rules.contains(activeRule.ruleKey())) |
| 84 | + .as("No such rule: " + activeRule.ruleKey()) |
| 85 | + .isTrue(); |
| 86 | + } |
72 | 87 | }
|
73 |
| - } |
74 | 88 | }
|
0 commit comments