diff --git a/liquibase-cdi/src/main/java/liquibase/integration/cdi/CDILiquibase.java b/liquibase-cdi/src/main/java/liquibase/integration/cdi/CDILiquibase.java index 1b8462faf2d..5be39a08a7e 100644 --- a/liquibase-cdi/src/main/java/liquibase/integration/cdi/CDILiquibase.java +++ b/liquibase-cdi/src/main/java/liquibase/integration/cdi/CDILiquibase.java @@ -10,8 +10,8 @@ import liquibase.exception.DatabaseException; import liquibase.exception.LiquibaseException; import liquibase.exception.UnexpectedLiquibaseException; -import liquibase.integration.commandline.LiquibaseCommandLineConfiguration; import liquibase.integration.cdi.annotations.LiquibaseType; +import liquibase.integration.commandline.LiquibaseCommandLineConfiguration; import liquibase.logging.Logger; import liquibase.resource.ResourceAccessor; import liquibase.util.LiquibaseUtil; @@ -77,9 +77,12 @@ public class CDILiquibase implements Extension { @LiquibaseType ResourceAccessor resourceAccessor; - @Inject @LiquibaseType - private CDILiquibaseConfig config; - @Inject @LiquibaseType + @Inject + @LiquibaseType + protected CDILiquibaseConfig config; + + @Inject + @LiquibaseType private DataSource dataSource; private boolean initialized; private boolean updateSuccessful; @@ -94,38 +97,43 @@ public boolean isUpdateSuccessful() { @PostConstruct public void onStartup() { - Logger log = Scope.getCurrentScope().getLog(getClass()); - - log.info("Booting Liquibase " + LiquibaseUtil.getBuildVersionInfo()); - String hostName; try { - hostName = NetUtil.getLocalHostName(); - } catch (Exception e) { - log.warning("Cannot find hostname: " + e.getMessage()); - log.fine("", e); - return; - } + Logger log = Scope.getCurrentScope().getLog(getClass()); + + log.info("Booting Liquibase " + LiquibaseUtil.getBuildVersionInfo()); + String hostName; + try { + hostName = NetUtil.getLocalHostName(); + } catch (Exception e) { + log.warning("Cannot find hostname: " + e.getMessage()); + log.fine("", e); + return; + } - if (!LiquibaseCommandLineConfiguration.SHOULD_RUN.getCurrentValue()) { - log.info(String.format("Liquibase did not run on %s because %s was set to false.", - hostName, - LiquibaseCommandLineConfiguration.SHOULD_RUN.getKey() - )); - return; - } - if (!config.getShouldRun()) { - log.info(String.format("Liquibase did not run on %s because CDILiquibaseConfig.shouldRun was set to false.", hostName)); - return; - } - initialized = true; - try { + if (!LiquibaseCommandLineConfiguration.SHOULD_RUN.getCurrentValue()) { + log.info(String.format("Liquibase did not run on %s because %s was set to false.", + hostName, + LiquibaseCommandLineConfiguration.SHOULD_RUN.getKey() + )); + return; + } + if (!config.getShouldRun()) { + log.info(String.format("Liquibase did not run on %s because CDILiquibaseConfig.shouldRun was set to false.", hostName)); + return; + } + initialized = true; performUpdate(); - } catch (LiquibaseException e) { + } catch (Throwable e) { + Scope.getCurrentScope().getLog(getClass()).severe(e.getMessage(), e); + if (e instanceof RuntimeException) { + throw (RuntimeException) e; + } + throw new UnexpectedLiquibaseException(e); } } - private void performUpdate() throws LiquibaseException { + protected void performUpdate() throws LiquibaseException { Connection c = null; Liquibase liquibase = null; try { @@ -158,7 +166,7 @@ private void performUpdate() throws LiquibaseException { protected Liquibase createLiquibase(Connection c) throws LiquibaseException { Liquibase liquibase = new Liquibase(config.getChangeLog(), resourceAccessor, createDatabase(c)); if (config.getParameters() != null) { - for(Map.Entry entry: config.getParameters().entrySet()) { + for (Map.Entry entry : config.getParameters().entrySet()) { liquibase.setChangeLogParameter(entry.getKey(), entry.getValue()); } } @@ -173,6 +181,7 @@ protected Liquibase createLiquibase(Connection c) throws LiquibaseException { /** * Subclasses may override this method add change some database settings such as * default schema before returning the database object. + * * @param c * @return a Database implementation retrieved from the {@link liquibase.database.DatabaseFactory}. * @throws DatabaseException diff --git a/liquibase-cdi/src/test/java/liquibase/integration/cdi/CDILiquibaseTest.java b/liquibase-cdi/src/test/java/liquibase/integration/cdi/CDILiquibaseTest.java index c88d9d1bcf7..ea724699f47 100644 --- a/liquibase-cdi/src/test/java/liquibase/integration/cdi/CDILiquibaseTest.java +++ b/liquibase-cdi/src/test/java/liquibase/integration/cdi/CDILiquibaseTest.java @@ -1,12 +1,19 @@ package liquibase.integration.cdi; +import liquibase.Scope; import liquibase.configuration.LiquibaseConfiguration; +import liquibase.exception.LiquibaseException; +import liquibase.logging.core.BufferedLogService; +import liquibase.logging.core.CompositeLogService; +import liquibase.logging.core.CompositeLogger; import org.jboss.weld.environment.se.Weld; import org.jboss.weld.environment.se.WeldContainer; import org.junit.After; import org.junit.Before; import org.junit.Test; +import java.util.logging.Level; + import static org.junit.Assert.*; /** @@ -54,4 +61,27 @@ public void shouldRunWhenConfigShouldRunIsTrue() { System.setProperty("liquibase.config.shouldRun", "true"); validateRunningState(true); } + + @Test + public void onStartupExceptionsAreCorrectlyHandled() throws Exception { + System.setProperty("liquibase.config.shouldRun", "true"); + final CDILiquibase cdi = new CDILiquibase() { + @Override + protected void performUpdate() { + throw new IllegalArgumentException("Tested Exception"); + } + }; + cdi.config = new CDILiquibaseConfig(); + + BufferedLogService bufferLog = new BufferedLogService(); + Scope.child(Scope.Attr.logService.name(), bufferLog, () -> { + try { + cdi.onStartup(); + fail("Did not throw exception"); + } catch (IllegalArgumentException e) { + assert e.getMessage().equals("Tested Exception"); + assert bufferLog.getLogAsString(Level.SEVERE).contains("SEVERE Tested Exception"); + } + }); + } }