diff --git a/config/alfresco/messages/system-messages.properties b/config/alfresco/messages/system-messages.properties index 98fa3eeb6e..02f5128b54 100644 --- a/config/alfresco/messages/system-messages.properties +++ b/config/alfresco/messages/system-messages.properties @@ -33,6 +33,7 @@ system.schema_comp.validation=Validation: {0} {1}="{2}" fails to match rule: {3} # Specific validator (implementations) messages... system.schema_comp.name_validator=name must match pattern ''{0}'' system.schema_comp.index_columns_validator=Number of columns in index doesn''t match. Was {0}, but expected {1} +system.schema_comp.column_names_validator=Column types do not match. Was {0}, but expected {1} system.schema_comp.schema_version_validator=version must be at least ''{0}'' # Clustering diff --git a/source/java/org/alfresco/util/schemacomp/validator/TypeNameOnlyValidator.java b/source/java/org/alfresco/util/schemacomp/validator/TypeNameOnlyValidator.java new file mode 100644 index 0000000000..946517fc42 --- /dev/null +++ b/source/java/org/alfresco/util/schemacomp/validator/TypeNameOnlyValidator.java @@ -0,0 +1,55 @@ +package org.alfresco.util.schemacomp.validator; + +import org.alfresco.error.AlfrescoRuntimeException; +import org.alfresco.util.schemacomp.DbProperty; +import org.alfresco.util.schemacomp.DiffContext; +import org.alfresco.util.schemacomp.ValidationResult; +import org.alfresco.util.schemacomp.model.DbObject; +import org.alfresco.util.schemacomp.model.Column; +import org.springframework.extensions.surf.util.I18NUtil; + + +/** + * Validates columns types without column size. + * + * @author sergei.shcherbovich + */ +public class TypeNameOnlyValidator extends NameValidator +{ + private static String TYPE_SIZE_SPLITTER = "("; + + @Override + public void validate(DbObject reference, DbObject target, DiffContext ctx) + { + if (!(target instanceof Column)) + { + throw new AlfrescoRuntimeException("TypeNameOnlyValidator could be used only in context of column object but was: " + target.toString()); + } + + String referenceTypeName = ((Column)reference).getType(); + String targetTypeName = ((Column)target).getType(); + + if (referenceTypeName.contains(TYPE_SIZE_SPLITTER)) + { + referenceTypeName = referenceTypeName.substring(0, referenceTypeName.indexOf(TYPE_SIZE_SPLITTER)); + } + if (targetTypeName.contains(TYPE_SIZE_SPLITTER)) + { + targetTypeName = targetTypeName.substring(0, targetTypeName.indexOf(TYPE_SIZE_SPLITTER)); + } + + if (!referenceTypeName.equals(targetTypeName)) + { + String message = I18NUtil.getMessage("system.schema_comp.column_names_validator", targetTypeName, referenceTypeName); + ValidationResult result = new ValidationResult(new DbProperty(target, "type"), message); + ctx.getComparisonResults().add(result); + } + } + + @Override + public boolean validatesFullObject() + { + return true; + } + +} diff --git a/source/test-java/org/alfresco/util/schemacomp/validator/TypeNameOnlyValidatorTest.java b/source/test-java/org/alfresco/util/schemacomp/validator/TypeNameOnlyValidatorTest.java new file mode 100644 index 0000000000..32c2a3e780 --- /dev/null +++ b/source/test-java/org/alfresco/util/schemacomp/validator/TypeNameOnlyValidatorTest.java @@ -0,0 +1,78 @@ +package org.alfresco.util.schemacomp.validator; + +import static org.junit.Assert.fail; +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; + +import org.alfresco.error.AlfrescoRuntimeException; +import org.alfresco.repo.domain.hibernate.dialect.AlfrescoSQLServerDialect; +import org.alfresco.util.schemacomp.DiffContext; +import org.alfresco.util.schemacomp.Results; +import org.alfresco.util.schemacomp.model.Column; +import org.alfresco.util.schemacomp.model.DbObject; +import org.alfresco.util.schemacomp.model.Index; +import org.junit.Before; +import org.junit.Test; + + +/** + * Tests for the TypeNameOnlyValidator class. + * + * @author sergei.shcherbovich + */ +public class TypeNameOnlyValidatorTest +{ + private TypeNameOnlyValidator validator; + private DiffContext ctx; + private Results validationResults; + + @Before + public void setUp() throws Exception + { + validator = new TypeNameOnlyValidator(); + validationResults = new Results(); + ctx = new DiffContext(new AlfrescoSQLServerDialect(), validationResults, null, null); + } + + @Test + public void validateOnlyColumnsTest() + { + try + { + validator.validate(null, new Index(null, null, new ArrayList()), ctx); + fail("TypeNameOnlyValidator should validate only Column"); + } + catch(AlfrescoRuntimeException e) + { + // should validate only Column + } + } + + @Test + public void validateColumnNamesTest() + { + // shouldn't fail + assertValidation(column("nvarchar(1)"), column("nvarchar(2)"), false); + // shouldn't fail + assertValidation(column("numeric"), column("numeric"), false); + // should fail + assertValidation(column("nvarchar(1)"), column("varchar(1)"), true); + // shouldn't fail + assertValidation(column("numeric() identity"), column("numeric() identity"), false); + } + + private void assertValidation(DbObject reference, DbObject target, boolean shouldFail) + { + int shouldFailInt = shouldFail ? 1 : 0; + int beforeValidationResultsSize = validationResults.size(); + + validator.validate(reference, target, ctx); + assertEquals(validationResults.size() - beforeValidationResultsSize, shouldFailInt); + } + + private Column column(String typeName) + { + return new Column(null, null, typeName, true); + } +}