From: simonbrandhof Date: Tue, 28 Sep 2010 13:06:25 +0000 (+0000) Subject: XMLProfileImporter : check that the nodes and are defined X-Git-Tag: 2.6~927 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=cae310e8493a20bbfe16e9b9cab5f9bdb73d090d;p=sonarqube.git XMLProfileImporter : check that the nodes and are defined --- 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 is missing."); + } + if (StringUtils.isBlank(profile.getLanguage())) { + messages.addErrorText("The mandatory node 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; @@ -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 index 00000000000..303494eb8bb --- /dev/null +++ b/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileImporterTest/nameAndLanguageShouldBeMandatory.xml @@ -0,0 +1,11 @@ + + + + + + checkstyle + IllegalRegexp + CRITICAL + + + \ No newline at end of file