Polish "Add failure analyzer for missing Liquibase changelog"
See gh-22320
This commit is contained in:
parent
554a962a13
commit
0ce0c3a54a
|
@ -14,7 +14,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.diagnostics.analyzer;
|
||||
package org.springframework.boot.liquibase;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
|
@ -22,6 +22,7 @@ import liquibase.exception.ChangeLogParseException;
|
|||
|
||||
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
|
||||
import org.springframework.boot.diagnostics.FailureAnalysis;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* An {@link AbstractFailureAnalyzer} that analyzes exceptions of type
|
||||
|
@ -33,20 +34,23 @@ class LiquibaseChangelogMissingFailureAnalyzer extends AbstractFailureAnalyzer<C
|
|||
|
||||
@Override
|
||||
protected FailureAnalysis analyze(Throwable rootFailure, ChangeLogParseException cause) {
|
||||
FileNotFoundException exception = findCause(cause, FileNotFoundException.class);
|
||||
if (exception != null) {
|
||||
return new FailureAnalysis(getDescription(cause),
|
||||
"Make sure a Liquibase changelog is present at the configured path", cause);
|
||||
FileNotFoundException fileNotFound = findCause(cause, FileNotFoundException.class);
|
||||
if (fileNotFound != null) {
|
||||
String changelogPath = extractChangelogPath(cause);
|
||||
if (StringUtils.hasText(changelogPath)) {
|
||||
return new FailureAnalysis(getDescription(changelogPath),
|
||||
"Make sure a Liquibase changelog is present at the configured path.", cause);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getDescription(ChangeLogParseException cause) {
|
||||
return "Liquibase failed to start because no changelog could be found at: " + extractChangelogPath(cause);
|
||||
}
|
||||
|
||||
private String extractChangelogPath(ChangeLogParseException cause) {
|
||||
return cause.getMessage().substring("Error parsing ".length());
|
||||
}
|
||||
|
||||
private String getDescription(String changelogPath) {
|
||||
return "Liquibase failed to start because no changelog could be found at '" + changelogPath + "'.";
|
||||
}
|
||||
|
||||
}
|
|
@ -57,7 +57,7 @@ org.springframework.boot.diagnostics.analyzer.IncompatibleConfigurationFailureAn
|
|||
org.springframework.boot.diagnostics.analyzer.InvalidConfigurationPropertyNameFailureAnalyzer,\
|
||||
org.springframework.boot.diagnostics.analyzer.InvalidConfigurationPropertyValueFailureAnalyzer,\
|
||||
org.springframework.boot.diagnostics.analyzer.PatternParseFailureAnalyzer,\
|
||||
org.springframework.boot.diagnostics.analyzer.LiquibaseChangelogMissingFailureAnalyzer
|
||||
org.springframework.boot.liquibase.LiquibaseChangelogMissingFailureAnalyzer
|
||||
|
||||
# FailureAnalysisReporters
|
||||
org.springframework.boot.diagnostics.FailureAnalysisReporter=\
|
||||
|
|
|
@ -14,12 +14,17 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.diagnostics.analyzer;
|
||||
package org.springframework.boot.liquibase;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import liquibase.integration.spring.SpringLiquibase;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.boot.diagnostics.FailureAnalysis;
|
||||
|
@ -35,15 +40,26 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
*
|
||||
* @author Sebastiaan Fernandez
|
||||
*/
|
||||
class LiquibaseChangelogMissingFailureAnalyzerTest {
|
||||
class LiquibaseChangelogMissingFailureAnalyzerTests {
|
||||
|
||||
@BeforeAll
|
||||
static void configureDerbyLogLocation(@TempDir File temp) {
|
||||
System.setProperty("derby.stream.error.file", new File(temp, "derby.log").getAbsolutePath());
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void clearDerbyLogLocation(@TempDir File temp) {
|
||||
System.clearProperty("derby.stream.error.file");
|
||||
}
|
||||
|
||||
@Test
|
||||
void changelogParseExceptionDueToChangelogNotPresent() {
|
||||
FailureAnalysis analysis = performAnalysis();
|
||||
assertThat(analysis.getDescription())
|
||||
.isEqualTo("Liquibase failed to start because no changelog could be found at: "
|
||||
+ "classpath:/db/changelog/db.changelog-master.yaml");
|
||||
assertThat(analysis.getAction()).isEqualTo("Make sure a Liquibase changelog is present at the configured path");
|
||||
.isEqualTo("Liquibase failed to start because no changelog could be found at '"
|
||||
+ "classpath:/db/changelog/db.changelog-master.yaml'.");
|
||||
assertThat(analysis.getAction())
|
||||
.isEqualTo("Make sure a Liquibase changelog is present at the configured path.");
|
||||
}
|
||||
|
||||
private FailureAnalysis performAnalysis() {
|
||||
|
@ -53,11 +69,8 @@ class LiquibaseChangelogMissingFailureAnalyzerTest {
|
|||
}
|
||||
|
||||
private BeanCreationException createFailure() {
|
||||
try {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
context.register(LiquibaseConfiguration.class);
|
||||
context.refresh();
|
||||
context.close();
|
||||
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
LiquibaseConfiguration.class)) {
|
||||
return null;
|
||||
}
|
||||
catch (BeanCreationException ex) {
|
||||
|
@ -70,14 +83,13 @@ class LiquibaseChangelogMissingFailureAnalyzerTest {
|
|||
|
||||
@Bean
|
||||
DataSource dataSource() {
|
||||
return DataSourceBuilder.create().url("jdbc:hsqldb:mem:normal").username("sa").build();
|
||||
return DataSourceBuilder.create().url("jdbc:hsqldb:mem:test").username("sa").build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
SpringLiquibase springLiquibase(DataSource dataSource) {
|
||||
SpringLiquibase liquibase = new SpringLiquibase();
|
||||
liquibase.setChangeLog("classpath:/db/changelog/db.changelog-master.yaml");
|
||||
liquibase.setShouldRun(true);
|
||||
liquibase.setDataSource(dataSource);
|
||||
return liquibase;
|
||||
}
|
Loading…
Reference in New Issue