} 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);
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;
}
}
+ @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);
--- /dev/null
+<?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