As a gesture of good faith, here’s a custom precondition class that allows you to test some bean property of the Database. Stand back.
This code is in the public domain.
Best,
Laird
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import liquibase.database.Database;
import liquibase.exception.CustomPreconditionFailedException;
import liquibase.exception.CustomPreconditionErrorException;
import liquibase.preconditions.CustomPrecondition;
import liquibase.util.StringUtils;
public class DatabasePropertyPrecondition implements CustomPrecondition {
private String propertyName;
private String expectedPropertyValue;
public DatabasePropertyPrecondition() {
super();
}
@Override
public void check(final Database database) throws CustomPreconditionFailedException, CustomPreconditionErrorException {
final String expectedPropertyValue = StringUtils.trimToNull(this.getExpectedPropertyValue());
Object rawValue = null;
try {
rawValue = this.getPropertyValue(database, StringUtils.trimToNull(this.getPropertyName()));
} catch (final IllegalAccessException illegalAccessException) {
throw new CustomPreconditionErrorException(String.format(“There was an IllegalAccessException encountered while getting the value of the Database property named %s.”, this.getPropertyName()), illegalAccessException);
} catch (final InvocationTargetException invocationTargetException) {
throw new CustomPreconditionErrorException(String.format(“There was an InvocationTargetException encountered while getting the value of the Database property named %s.”, this.getPropertyName()), invocationTargetException);
} catch (final IntrospectionException introspectionException) {
throw new CustomPreconditionErrorException(String.format(“There was an IntrospectionException encountered while getting the value of the Database property named %s.”, this.getPropertyName()), introspectionException);
}
if (expectedPropertyValue == null) {
if (rawValue != null) {
throw new CustomPreconditionFailedException(String.format(“The Database property %s with value %s did not equal the expected property value %s.”, this.getPropertyName(), rawValue, expectedPropertyValue));
}
} else if (rawValue == null || !expectedPropertyValue.equals(rawValue)) {
throw new CustomPreconditionFailedException(String.format(“The Database property %s with value %s did not equal the expected property value %s.”, this.getPropertyName(), rawValue, expectedPropertyValue));
}
}
protected Object getPropertyValue(final Database database, final String propertyName) throws IllegalAccessException, IntrospectionException, InvocationTargetException {
if (database == null || propertyName == null) {
return null;
}
final BeanInfo beanInfo = Introspector.getBeanInfo(database.getClass());
assert beanInfo != null;
Object returnValue = null;
final PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
if (pds != null && pds.length > 0) {
PropertyDescriptor pd = null;
for (final PropertyDescriptor p : pds) {
if (p != null && propertyName.equals(p.getName())) {
pd = p;
break;
}
}
if (pd != null) {
final Method readMethod = pd.getReadMethod();
if (readMethod != null) {
returnValue = readMethod.invoke(database);
}
}
}
return returnValue;
}
public String getPropertyName() {
return this.propertyName;
}
public void setPropertyName(final String propertyName) {
this.propertyName = propertyName;
}
public String getExpectedPropertyValue() {
return this.expectedPropertyValue;
}
public void setExpectedPropertyValue(final String expectedPropertyValue) {
this.expectedPropertyValue = expectedPropertyValue;
}
}