]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1987: Improve test
authorGodin <mandrikov@gmail.com>
Wed, 8 Dec 2010 16:13:31 +0000 (16:13 +0000)
committerGodin <mandrikov@gmail.com>
Wed, 8 Dec 2010 16:13:31 +0000 (16:13 +0000)
sonar-server/src/test/java/org/sonar/server/configuration/BackupTest.java
sonar-server/src/test/resources/org/sonar/server/configuration/BackupTest/backup-restore-valid.xml
sonar-server/src/test/resources/org/sonar/server/configuration/BackupTest/backup-valid.xml

index 6b28a22366ebb4cba32b4fbde721f76a5f384be7..0e008fe836206b0a6c36e0b8caacd33529dcafe0 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.configuration;
 
+import com.google.common.collect.Lists;
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.CharEncoding;
@@ -30,10 +31,7 @@ import org.sonar.api.database.configuration.Property;
 import org.sonar.api.measures.Metric;
 import org.sonar.api.profiles.Alert;
 import org.sonar.api.profiles.RulesProfile;
-import org.sonar.api.rules.ActiveRule;
-import org.sonar.api.rules.ActiveRuleParam;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RulePriority;
+import org.sonar.api.rules.*;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -42,10 +40,11 @@ import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.*;
 
-import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.nullValue;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
@@ -66,11 +65,12 @@ public class BackupTest {
   @Test
   public void shouldReturnAValidXml() throws Exception {
     Backup backup = new Backup(Arrays.asList(new MetricsBackup(null), new PropertiesBackup(null),
-        new ProfilesBackup((DatabaseSession) null)));
+        new RulesBackup((DatabaseSession) null), new ProfilesBackup((DatabaseSession) null)));
     SonarConfig sonarConfig = getSonarConfig();
     sonarConfig.setMetrics(getMetrics());
     sonarConfig.setProperties(getProperties());
     sonarConfig.setProfiles(getProfiles());
+    sonarConfig.setRules(getUserRules());
 
     String xml = backup.getXmlFromSonarConfig(sonarConfig);
     assertXmlAreSimilar(xml, "backup-valid.xml");
@@ -99,7 +99,7 @@ public class BackupTest {
   @Test
   public void shouldImportXml() {
     Backup backup = new Backup(Arrays.asList(new MetricsBackup(null), new PropertiesBackup(null),
-        new ProfilesBackup((DatabaseSession) null)));
+        new RulesBackup((DatabaseSession) null), new ProfilesBackup((DatabaseSession) null)));
 
     String xml = getFileFromClasspath("backup-restore-valid.xml");
     SonarConfig sonarConfig = backup.getSonarConfigFromXml(xml);
@@ -141,6 +141,21 @@ public class BackupTest {
     assertEquals("testWarn", testAlert.getValueWarning());
     assertNotNull(testAlert.getMetric());
     assertEquals("test key", testAlert.getMetric().getKey());
+
+    Collection<Rule> rules = sonarConfig.getRules();
+    assertThat(rules.size(), is(1));
+    Rule rule = rules.iterator().next();
+    assertThat(rule.getParent().getRepositoryKey(), is("test plugin"));
+    assertThat(rule.getParent().getKey(), is("test key"));
+    assertThat(rule.getRepositoryKey(), is("test plugin"));
+    assertThat(rule.getKey(), is("test key2"));
+    assertThat(rule.getName(), is("test name"));
+    assertThat(rule.getDescription(), is("test description"));
+    assertThat(rule.getSeverity(), is(RulePriority.INFO));
+    assertThat(rule.getParams().size(), is(1));
+    RuleParam param = rule.getParams().get(0);
+    assertThat(param.getKey(), is("test param key"));
+    assertThat(param.getDefaultValue(), is("test param value"));
   }
 
   @Test
@@ -244,6 +259,18 @@ public class BackupTest {
     return profiles;
   }
 
+  private List<Rule> getUserRules() {
+    List<Rule> rules = Lists.newArrayList();
+    Rule parentRule = Rule.create("test plugin", "test key", null);
+    Rule rule = Rule.create("test plugin", "test key2", "test name")
+        .setDescription("test description")
+        .setSeverity(RulePriority.INFO)
+        .setParent(parentRule);
+    rule.createParameter().setKey("test param key").setDefaultValue("test param value");
+    rules.add(rule);
+    return rules;
+  }
+
   private List<Property> getProperties() {
     List<Property> properties = new ArrayList<Property>();
     properties.add(new Property("key1", "value1"));
index a18ba48b425465649d156c80a060126c8fdcf8ce..a7761157cb281973be0019634b92806a216429ba 100644 (file)
       <alerts/>
     </profile>
   </profiles>
+  <rules>
+    <rule>
+      <parentRepositoryKey><![CDATA[test plugin]]></parentRepositoryKey>
+      <parentKey><![CDATA[test key]]></parentKey>
+      <repositoryKey><![CDATA[test plugin]]></repositoryKey>
+      <key><![CDATA[test key2]]></key>
+      <level><![CDATA[INFO]]></level>
+      <name><![CDATA[test name]]></name>
+      <description><![CDATA[test description]]></description>
+      <params>
+        <param>
+          <key><![CDATA[test param key]]></key>
+          <value><![CDATA[test param value]]></value>
+        </param>
+      </params>
+    </rule>
+  </rules>
 </sonar-config>
index 2ab59320a07ba213117f100c4246819cfb411bfa..37235bd376507b692de49a7fd4f714af411d65b9 100644 (file)
       <alerts/>
     </profile>
   </profiles>
+  <rules>
+    <rule>
+      <parentRepositoryKey><![CDATA[test plugin]]></parentRepositoryKey>
+      <parentKey><![CDATA[test key]]></parentKey>
+      <repositoryKey><![CDATA[test plugin]]></repositoryKey>
+      <key><![CDATA[test key2]]></key>
+      <level><![CDATA[INFO]]></level>
+      <name><![CDATA[test name]]></name>
+      <description><![CDATA[test description]]></description>
+      <params>
+        <param>
+          <key><![CDATA[test param key]]></key>
+          <value><![CDATA[test param value]]></value>
+        </param>
+      </params>
+    </rule>
+  </rules>
 </sonar-config>