]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6187 Replace MessageException instead of IllegalStateException and add more...
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 19 Feb 2015 14:25:11 +0000 (15:25 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 19 Feb 2015 14:25:11 +0000 (15:25 +0100)
server/sonar-server/src/main/java/org/sonar/server/db/migrations/v453/AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigration.java
server/sonar-server/src/test/java/org/sonar/server/db/migrations/v453/AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigrationTest.java

index 4595ae1d9f3066c5c5f641beaa85fd9056e2cf0b..f46e3bb4a53cd45c9fb17df2262299edcad1a00b 100644 (file)
@@ -23,6 +23,7 @@ import com.google.common.base.Predicate;
 import com.google.common.collect.Iterables;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.sonar.api.utils.MessageException;
 import org.sonar.api.utils.System2;
 import org.sonar.core.persistence.Database;
 import org.sonar.server.db.migrations.BaseDataChange;
@@ -54,6 +55,10 @@ public class AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigration
   private static final String COMPLIANCE_NAME = "Compliance";
   private static final String COMPLIANCE_KEY_SUFFIX = "_COMPLIANCE";
 
+  private static final String ERROR_SUFFIX = "Please restore your DB backup, start the previous version of SonarQube " +
+    "and update your SQALE model to fix this issue before trying again to run the migration.";
+
+
   private final System2 system;
 
   public AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigration(Database db, System2 system) {
@@ -230,7 +235,7 @@ public class AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigration
       }, null);
       if (characteristic != null) {
         if (characteristic.getParentId() != null) {
-          throw new IllegalStateException(String.format("'%s' must be a characteristic", characteristic.getName()));
+          throw MessageException.of(String.format("'%s' must be a characteristic. " + ERROR_SUFFIX, characteristic.getName()));
         }
       }
       return characteristic;
@@ -247,9 +252,9 @@ public class AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigration
       if (characteristic != null) {
         Integer parentId = characteristic.getParentId();
         if (parentId == null) {
-          throw new IllegalStateException(String.format("'%s' must be a sub-characteristic", characteristic.getName()));
+          throw MessageException.of(String.format("'%s' must be a sub-characteristic. " + ERROR_SUFFIX, characteristic.getName()));
         } else if (!characteristic.getParentId().equals(parent.getId())) {
-          throw new IllegalStateException(String.format("'%s' must be defined under '%s'", characteristic.getName(), parent.getName()));
+          throw MessageException.of(String.format("'%s' must be defined under '%s'. " + ERROR_SUFFIX, characteristic.getName(), parent.getName()));
         }
       }
       return characteristic;
index d91905e704359cc04a8e2f592ba212c915a6fe85..a1e3de0667cc7092c76babedff363437a624ac85 100644 (file)
@@ -23,6 +23,7 @@ import org.junit.Before;
 import org.junit.ClassRule;
 import org.junit.Test;
 import org.sonar.api.utils.DateUtils;
+import org.sonar.api.utils.MessageException;
 import org.sonar.api.utils.System2;
 import org.sonar.core.persistence.TestDatabase;
 import org.sonar.server.db.migrations.DatabaseMigration;
@@ -89,11 +90,13 @@ public class AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigrationT
   public void fail_if_usability_exists_as_sub_characteristic() throws Exception {
     db.prepareDbUnit(getClass(), "fail_if_usability_exists_as_sub_characteristic.xml");
 
-   try {
-     migration.execute();
-     fail();
-   } catch (Exception e) {
-      assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("'Usability' must be a characteristic");
+    try {
+      migration.execute();
+      fail();
+    } catch (Exception e) {
+      assertThat(e).isInstanceOf(MessageException.class).hasMessage(
+        "'Usability' must be a characteristic. Please restore your DB backup, start the previous version of SonarQube " +
+          "and update your SQALE model to fix this issue before trying again to run the migration.");
     }
   }
 
@@ -105,7 +108,9 @@ public class AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigrationT
       migration.execute();
       fail();
     } catch (Exception e) {
-      assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("'Compliance' must be a sub-characteristic");
+      assertThat(e).isInstanceOf(MessageException.class).hasMessage(
+        "'Compliance' must be a sub-characteristic. Please restore your DB backup, start the previous version of SonarQube " +
+          "and update your SQALE model to fix this issue before trying again to run the migration.");
     }
   }
 
@@ -117,7 +122,9 @@ public class AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigrationT
       migration.execute();
       fail();
     } catch (Exception e) {
-      assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("'Reusability Compliance' must be defined under 'Reusability'");
+      assertThat(e).isInstanceOf(MessageException.class).hasMessage(
+        "'Reusability Compliance' must be defined under 'Reusability'. Please restore your DB backup, start the previous version of SonarQube " +
+          "and update your SQALE model to fix this issue before trying again to run the migration.");
     }
   }