aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2010-09-28 13:06:25 +0000
committersimonbrandhof <simon.brandhof@gmail.com>2010-09-28 13:06:25 +0000
commitcae310e8493a20bbfe16e9b9cab5f9bdb73d090d (patch)
treefe57b090746f18c2d845abb8bef366161142e2e3
parent3faeeaeb37725caeade62fad7bc5a13c918d7cc6 (diff)
downloadsonarqube-cae310e8493a20bbfe16e9b9cab5f9bdb73d090d.tar.gz
sonarqube-cae310e8493a20bbfe16e9b9cab5f9bdb73d090d.zip
XMLProfileImporter : check that the nodes <name> and <language> are defined
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileImporter.java10
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileImporterTest.java17
-rw-r--r--sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileImporterTest/nameAndLanguageShouldBeMandatory.xml11
3 files changed, 38 insertions, 0 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileImporter.java b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileImporter.java
index a15be26864c..3a13d82b8b0 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileImporter.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileImporter.java
@@ -74,9 +74,19 @@ public final class XMLProfileImporter {
} catch (XMLStreamException e) {
messages.addErrorText("XML is not valid: " + e.getMessage());
}
+ checkProfile(profile, messages);
return profile;
}
+ 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.");
+ }
+ }
+
private SMInputFactory initStax() {
XMLInputFactory xmlFactory = XMLInputFactory2.newInstance();
xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileImporterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileImporterTest.java
index 3fe4ef39902..6358f1d08c3 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileImporterTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileImporterTest.java
@@ -38,6 +38,7 @@ import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
+import static org.junit.internal.matchers.StringContains.containsString;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -64,6 +65,22 @@ public class XMLProfileImporterTest {
}
@Test
+ public void nameAndLanguageShouldBeMandatory() throws UnsupportedEncodingException {
+ Reader reader = new InputStreamReader(getClass().getResourceAsStream("/org/sonar/api/profiles/XMLProfileImporterTest/nameAndLanguageShouldBeMandatory.xml"), CharEncoding.UTF_8);
+ try {
+ ValidationMessages validation = ValidationMessages.create();
+ RuleFinder ruleFinder = newRuleFinder();
+ RulesProfile profile = XMLProfileImporter.create(ruleFinder).importProfile(reader, validation);
+
+ assertThat(validation.getErrors().size(), is(2));
+ assertThat(validation.getErrors().get(0) , containsString(""));
+
+ } finally {
+ IOUtils.closeQuietly(reader);
+ }
+ }
+
+ @Test
public void importProfileWithRuleParameters() throws UnsupportedEncodingException {
Reader reader = new InputStreamReader(getClass().getResourceAsStream("/org/sonar/api/profiles/XMLProfileImporterTest/importProfileWithRuleParameters.xml"), CharEncoding.UTF_8);
try {
diff --git a/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileImporterTest/nameAndLanguageShouldBeMandatory.xml b/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileImporterTest/nameAndLanguageShouldBeMandatory.xml
new file mode 100644
index 00000000000..303494eb8bb
--- /dev/null
+++ b/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileImporterTest/nameAndLanguageShouldBeMandatory.xml
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Generated by Sonar -->
+<profile>
+ <rules>
+ <rule>
+ <repositoryKey>checkstyle</repositoryKey>
+ <key>IllegalRegexp</key>
+ <priority>CRITICAL</priority>
+ </rule>
+ </rules>
+</profile> \ No newline at end of file