]> 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:30:26 +0000 (15:30 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 19 Feb 2015 14:30:26 +0000 (15:30 +0100)
server/sonar-server/src/main/java/org/sonar/server/db/migrations/v501/AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigration.java
server/sonar-server/src/test/java/org/sonar/server/db/migrations/v501/AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigrationTest.java

index 23d2e79e95f10674068f1a43942f2c5c6e4f39e9..fbdc4b17b94c27c864c7db23bbddbde9bfc7bada 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;
@@ -53,6 +54,9 @@ 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) {
@@ -229,7 +233,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;
@@ -246,9 +250,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 a1ca4e5fe1c13832c31caaca14a892801c900393..bc53a8a43c0eef911f01c5f1c78bb675f25bac63 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;
@@ -93,7 +94,9 @@ public class AddCharacteristicUsabilityAndSubCharacteristicsComplianceMigrationT
       migration.execute();
       fail();
     } catch (Exception e) {
-      assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("'Usability' must be a characteristic");
+      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.");
     }
   }