]> source.dussan.org Git - sonarqube.git/commitdiff
XMLProfileImporter : check that the nodes <name> and <language> are defined
authorsimonbrandhof <simon.brandhof@gmail.com>
Tue, 28 Sep 2010 13:06:25 +0000 (13:06 +0000)
committersimonbrandhof <simon.brandhof@gmail.com>
Tue, 28 Sep 2010 13:06:25 +0000 (13:06 +0000)
sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileImporter.java
sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileImporterTest.java
sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileImporterTest/nameAndLanguageShouldBeMandatory.xml [new file with mode: 0644]

index a15be26864c7c31c266b860dc6b48ca3213c0393..3a13d82b8b0085bb5a4496dfaea41097a7d1f396 100644 (file)
@@ -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);
index 3fe4ef399029a435f78330a74524abb061bc1d07..6358f1d08c361adb443851ac4f1af4e5c87a8019 100644 (file)
@@ -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;
@@ -63,6 +64,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);
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 (file)
index 0000000..303494e
--- /dev/null
@@ -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