From 2bc382aaeace31072352a0b7e18c5878815a692a Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Wed, 2 Feb 2011 14:31:14 +0100 Subject: [PATCH] SONAR-2094 Support backups without the field --- .../org/sonar/api/profiles/RulesProfile.java | 10 ++++-- .../sonar/server/configuration/Backup.java | 8 +++-- .../server/configuration/ProfilesBackup.java | 4 +++ .../configuration/ProfilesBackupTest.java | 34 ++++++++++++++++--- .../shouldSupportEnabledField.xml | 28 +++++++++++++++ .../shouldSupportMissingEnabledField.xml | 17 ++++++++++ 6 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 sonar-server/src/test/resources/org/sonar/server/configuration/ProfilesBackupTest/shouldSupportEnabledField.xml create mode 100644 sonar-server/src/test/resources/org/sonar/server/configuration/ProfilesBackupTest/shouldSupportMissingEnabledField.xml diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/RulesProfile.java b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/RulesProfile.java index fbf2d2bea04..4a5cfa80b62 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/RulesProfile.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/RulesProfile.java @@ -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; } diff --git a/sonar-server/src/main/java/org/sonar/server/configuration/Backup.java b/sonar-server/src/main/java/org/sonar/server/configuration/Backup.java index eba4e481083..c610f065535 100644 --- a/sonar-server/src/main/java/org/sonar/server/configuration/Backup.java +++ b/sonar-server/src/main/java/org/sonar/server/configuration/Backup.java @@ -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); diff --git a/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesBackup.java b/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesBackup.java index 71d57d8c148..4067d83d37a 100644 --- a/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesBackup.java +++ b/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesBackup.java @@ -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); diff --git a/sonar-server/src/test/java/org/sonar/server/configuration/ProfilesBackupTest.java b/sonar-server/src/test/java/org/sonar/server/configuration/ProfilesBackupTest.java index d129f7e524c..dfec8d7d3a1 100644 --- a/sonar-server/src/test/java/org/sonar/server/configuration/ProfilesBackupTest.java +++ b/sonar-server/src/test/java/org/sonar/server/configuration/ProfilesBackupTest.java @@ -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 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 index 00000000000..cbd461b32e2 --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/configuration/ProfilesBackupTest/shouldSupportEnabledField.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 index 00000000000..4ba3d1f7053 --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/configuration/ProfilesBackupTest/shouldSupportMissingEnabledField.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + -- 2.39.5