]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2094 Support backups without the field <enabled>
authorsimonbrandhof <simon.brandhof@gmail.com>
Wed, 2 Feb 2011 13:31:14 +0000 (14:31 +0100)
committersimonbrandhof <simon.brandhof@gmail.com>
Wed, 2 Feb 2011 13:32:36 +0000 (14:32 +0100)
sonar-plugin-api/src/main/java/org/sonar/api/profiles/RulesProfile.java
sonar-server/src/main/java/org/sonar/server/configuration/Backup.java
sonar-server/src/main/java/org/sonar/server/configuration/ProfilesBackup.java
sonar-server/src/test/java/org/sonar/server/configuration/ProfilesBackupTest.java
sonar-server/src/test/resources/org/sonar/server/configuration/ProfilesBackupTest/shouldSupportEnabledField.xml [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/configuration/ProfilesBackupTest/shouldSupportMissingEnabledField.xml [new file with mode: 0644]

index fbf2d2bea04801ba29e92924e54d0d853d98244a..4a5cfa80b62ded1e15faf0f35a2eccbbbfbd88a3 100644 (file)
@@ -70,7 +70,7 @@ public class RulesProfile implements Cloneable {
   private Boolean provided = Boolean.FALSE;
 
   @Column(name = "enabled", updatable = true, nullable = false)
-  private boolean enabled = true;
+  private Boolean enabled = Boolean.TRUE;
 
   @Column(name = "language", updatable = true, nullable = false)
   private String language;
@@ -179,11 +179,15 @@ public class RulesProfile implements Cloneable {
     this.provided = b;
   }
 
-  public boolean isEnabled() {
+  public Boolean getEnabled() {
     return enabled;
   }
 
-  public RulesProfile setEnabled(boolean b) {
+  public boolean isEnabled() {
+    return enabled==Boolean.TRUE;
+  }
+
+  public RulesProfile setEnabled(Boolean b) {
     this.enabled = b;
     return this;
   }
index eba4e48108305c6910dc47d872a09de4443d143e..c610f0655353761c50bc89c652629c261bce7c2d 100644 (file)
@@ -103,13 +103,17 @@ public class Backup {
   public void importXml(String xml) {
     try {
       startDb();
-      SonarConfig sonarConfig = getSonarConfigFromXml(xml);
-      importBackupablesXml(sonarConfig);
+      doImportXml(xml);
     } finally {
       stopDb();
     }
   }
 
+  void doImportXml(String xml) {
+    SonarConfig sonarConfig = getSonarConfigFromXml(xml);
+    importBackupablesXml(sonarConfig);
+  }
+
   protected void importBackupablesXml(SonarConfig sonarConfig) {
     for (Backupable backupable : backupables) {
       backupable.importXml(sonarConfig);
index 71d57d8c1481b22a73b283f1f373ee3b7d311106..4067d83d37a61de938a733cb651e3eb41183e4f8 100644 (file)
@@ -85,6 +85,10 @@ public class ProfilesBackup implements Backupable {
   }
 
   public void importProfile(RulesDao rulesDao, RulesProfile toImport) {
+    if (toImport.getEnabled()==null) {
+      // backward-compatibility with versions < 2.6. The field "enabled" did not exist. Default value is true.
+      toImport.setEnabled(true);
+    }
     importActiveRules(rulesDao, toImport);
     importAlerts(toImport);
     session.save(toImport);
index d129f7e524c811129d1d22c164ba43882dbfb495..dfec8d7d3a168649ed2ad41f477d3b5268547b4f 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.configuration;
 
+import org.apache.commons.io.FileUtils;
 import org.junit.Before;
 import org.junit.Test;
 import org.sonar.api.measures.Metric;
@@ -26,11 +27,14 @@ import org.sonar.api.profiles.Alert;
 import org.sonar.api.profiles.RulesProfile;
 import org.sonar.api.rules.*;
 import org.sonar.jpa.test.AbstractDbUnitTestCase;
+import org.sonar.test.TestUtils;
 
+import java.io.IOException;
 import java.util.Arrays;
 
 import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.collection.IsCollectionContaining.hasItem;
+import static org.hamcrest.core.Is.is;
 import static org.junit.Assert.*;
 
 public class ProfilesBackupTest extends AbstractDbUnitTestCase {
@@ -78,8 +82,7 @@ public class ProfilesBackupTest extends AbstractDbUnitTestCase {
   }
 
   @Test
-  public void testExportWithNoProfiles() {
-
+  public void shouldImportProvidedProfiles() {
     RulesProfile profileProvided = new RulesProfile("test provided", "lang", false, true);
     RulesProfile profileNotProvided = new RulesProfile("test not provided", "lang", false, false);
     getSession().save(profileProvided, profileNotProvided);
@@ -98,8 +101,7 @@ public class ProfilesBackupTest extends AbstractDbUnitTestCase {
   }
 
   @Test
-  public void testExportWithProfilesAndAlerts() {
-
+  public void shouldImportProfiles() {
     RulesProfile profileProvided = new RulesProfile("test provided", "lang", false, true);
     RulesProfile profileNotProvided = new RulesProfile("test not provided", "lang", false, false);
     getSession().save(profileProvided, profileNotProvided);
@@ -139,6 +141,30 @@ public class ProfilesBackupTest extends AbstractDbUnitTestCase {
     assertEquals(2, newProfile.getActiveRules().size());
     assertEquals(1, newProfile.getActiveRules(RulePriority.MAJOR).get(0).getActiveRuleParams().size());
     assertEquals(2, newProfile.getAlerts().size());
+  }
+
+  /**
+   * The field <profile><enabled> has been added in version 2.6. Profiles imported from backup of previous releases must
+   * be considered as enabled.
+   */
+  @Test
+  public void shouldSupportMissingEnabledField() throws IOException {
+    Backup backup = new Backup(getSession());
+    backup.doImportXml(FileUtils.readFileToString(TestUtils.getResource(getClass(), "shouldSupportMissingEnabledField.xml")));
+
+    RulesProfile profile = getSession().getSingleResult(RulesProfile.class, "name", "Missing enabled field");
+    assertThat(profile.getEnabled(), is(Boolean.TRUE));
+  }
+
+  @Test
+  public void shouldSupportEnabledField() throws IOException {
+    Backup backup = new Backup(getSession());
+    backup.doImportXml(FileUtils.readFileToString(TestUtils.getResource(getClass(), "shouldSupportEnabledField.xml")));
+
+    RulesProfile enabledProfile = getSession().getSingleResult(RulesProfile.class, "name", "Enabled");
+    assertThat(enabledProfile.getEnabled(), is(Boolean.TRUE));
 
+    RulesProfile disabledProfile = getSession().getSingleResult(RulesProfile.class, "name", "Disabled");
+    assertThat(disabledProfile.getEnabled(), is(Boolean.FALSE));
   }
 }
diff --git a/sonar-server/src/test/resources/org/sonar/server/configuration/ProfilesBackupTest/shouldSupportEnabledField.xml b/sonar-server/src/test/resources/org/sonar/server/configuration/ProfilesBackupTest/shouldSupportEnabledField.xml
new file mode 100644 (file)
index 0000000..cbd461b
--- /dev/null
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<sonar-config>
+  <version><![CDATA[59]]></version>
+  <date><![CDATA[2009-02-19]]></date>
+  <metrics/>
+  <profiles>
+    <profile>
+      <name><![CDATA[Disabled]]></name>
+      <default-profile><![CDATA[false]]></default-profile>
+      <provided><![CDATA[false]]></provided>
+      <language><![CDATA[java]]></language>
+      <enabled><![CDATA[false]]></enabled>
+      <active-rules>
+      </active-rules>
+      <alerts/>
+    </profile>
+    <profile>
+      <name><![CDATA[Enabled]]></name>
+      <default-profile><![CDATA[false]]></default-profile>
+      <provided><![CDATA[false]]></provided>
+      <language><![CDATA[java]]></language>
+      <enabled><![CDATA[true]]></enabled>
+      <active-rules>
+      </active-rules>
+      <alerts/>
+    </profile>
+  </profiles>
+</sonar-config>
diff --git a/sonar-server/src/test/resources/org/sonar/server/configuration/ProfilesBackupTest/shouldSupportMissingEnabledField.xml b/sonar-server/src/test/resources/org/sonar/server/configuration/ProfilesBackupTest/shouldSupportMissingEnabledField.xml
new file mode 100644 (file)
index 0000000..4ba3d1f
--- /dev/null
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<sonar-config>
+  <version><![CDATA[59]]></version>
+  <date><![CDATA[2009-02-19]]></date>
+  <metrics/>
+  <profiles>
+    <profile>
+      <name><![CDATA[Missing enabled field]]></name>
+      <default-profile><![CDATA[false]]></default-profile>
+      <provided><![CDATA[false]]></provided>
+      <language><![CDATA[java]]></language>
+      <active-rules>
+      </active-rules>
+      <alerts/>
+    </profile>
+  </profiles>
+</sonar-config>