mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-31 17:39:05 +00:00
ALF-10771: schema validation and differences rules
Laying some groundwork for the main code. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@31921 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -29,5 +29,5 @@ import org.alfresco.util.schemacomp.model.DbObject;
|
||||
*/
|
||||
public interface DbValidator
|
||||
{
|
||||
void validate(DbObject dbo, DiffContext ctx);
|
||||
void validate(DbObject reference, DbObject target, DiffContext ctx);
|
||||
}
|
||||
|
@@ -38,49 +38,24 @@ import org.hibernate.dialect.Dialect;
|
||||
*/
|
||||
public class NameValidator implements DbValidator
|
||||
{
|
||||
private Map<Class<? extends Dialect>, Pattern> namePatterns = new HashMap<Class<? extends Dialect>, Pattern>();
|
||||
private Pattern defaultNamePattern;
|
||||
private Pattern pattern;
|
||||
|
||||
@Override
|
||||
public void validate(DbObject dbo, DiffContext ctx)
|
||||
public void validate(DbObject reference, DbObject target, DiffContext ctx)
|
||||
{
|
||||
String name = dbo.getName();
|
||||
String name = target.getName();
|
||||
|
||||
Pattern pattern = namePatterns.get(ctx.getDialect().getClass());
|
||||
|
||||
ValidationResult result = new ValidationResult(new DbProperty(dbo, "name"));
|
||||
ValidationResult result = new ValidationResult(new DbProperty(target, "name"));
|
||||
|
||||
if (pattern != null && !pattern.matcher(name).matches())
|
||||
{
|
||||
ctx.getValidationResults().add(result);
|
||||
}
|
||||
else if (defaultNamePattern != null && !defaultNamePattern.matcher(name).matches())
|
||||
{
|
||||
ctx.getValidationResults().add(result);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Specify the set of mappings of database dialect to acceptable name patterns.
|
||||
*
|
||||
* @param namePatterns
|
||||
*/
|
||||
public void setNamePatterns(Map<Class<? extends Dialect>, Pattern> namePatterns)
|
||||
public void setPattern(Pattern pattern)
|
||||
{
|
||||
this.namePatterns = namePatterns;
|
||||
}
|
||||
|
||||
/**
|
||||
* If during validation, there is no specific name validation pattern for the supplied {@link Dialect}
|
||||
* then the defaultNamePattern property will be used - if not null.
|
||||
* <p>
|
||||
* If defaultNamePattern is null then a validation failure will be produced.
|
||||
*
|
||||
* @param defaultNamePattern
|
||||
*/
|
||||
public void setDefaultNamePattern(Pattern defaultNamePattern)
|
||||
{
|
||||
this.defaultNamePattern = defaultNamePattern;
|
||||
this.pattern = pattern;
|
||||
}
|
||||
}
|
||||
|
@@ -22,9 +22,7 @@ package org.alfresco.util.schemacomp.validator;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.alfresco.util.schemacomp.DiffContext;
|
||||
@@ -32,7 +30,6 @@ import org.alfresco.util.schemacomp.Results;
|
||||
import org.alfresco.util.schemacomp.ValidationResult;
|
||||
import org.alfresco.util.schemacomp.model.DbObject;
|
||||
import org.alfresco.util.schemacomp.model.Index;
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.dialect.Oracle10gDialect;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -53,17 +50,17 @@ public class NameValidatorTest
|
||||
{
|
||||
validator = new NameValidator();
|
||||
validationResults = new ArrayList<ValidationResult>();
|
||||
ctx = new DiffContext(new Oracle10gDialect(), new Results(), validationResults);
|
||||
ctx = new DiffContext(new Oracle10gDialect(), new Results(), validationResults, null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canSpecifyDefaultRequiredPattern()
|
||||
{
|
||||
validator.setDefaultNamePattern(Pattern.compile("SYS_[A-Z_]+"));
|
||||
validator.validate(indexForName("SYS_MYINDEX"), ctx);
|
||||
validator.validate(indexForName("SYS_"), ctx);
|
||||
validator.validate(indexForName("SYS_MY_INDEX"), ctx);
|
||||
validator.validate(indexForName("MY_INDEX"), ctx);
|
||||
validator.setPattern(Pattern.compile("SYS_[A-Z_]+"));
|
||||
validator.validate(null, indexForName("SYS_MYINDEX"), ctx);
|
||||
validator.validate(null, indexForName("SYS_"), ctx);
|
||||
validator.validate(null, indexForName("SYS_MY_INDEX"), ctx);
|
||||
validator.validate(null, indexForName("MY_INDEX"), ctx);
|
||||
|
||||
assertEquals(2, validationResults.size());
|
||||
assertEquals("SYS_", validationResults.get(0).getValue());
|
||||
@@ -73,12 +70,10 @@ public class NameValidatorTest
|
||||
@Test
|
||||
public void canValidateAgainstPatternForDialect()
|
||||
{
|
||||
Map<Class<? extends Dialect>, Pattern> patterns = new HashMap<Class<? extends Dialect>, Pattern>();
|
||||
patterns.put(Oracle10gDialect.class, Pattern.compile("ORA_[A-Z_]+"));
|
||||
validator.setNamePatterns(patterns);
|
||||
validator.setPattern(Pattern.compile("ORA_[A-Z_]+"));
|
||||
|
||||
validator.validate(indexForName("ORA_MYINDEX"), ctx);
|
||||
validator.validate(indexForName("SYS_MYINDEX"), ctx);
|
||||
validator.validate(null, indexForName("ORA_MYINDEX"), ctx);
|
||||
validator.validate(null, indexForName("SYS_MYINDEX"), ctx);
|
||||
|
||||
assertEquals(1, validationResults.size());
|
||||
assertEquals("SYS_MYINDEX", validationResults.get(0).getValue());
|
||||
|
@@ -29,7 +29,7 @@ import org.alfresco.util.schemacomp.model.DbObject;
|
||||
public class NullValidator implements DbValidator
|
||||
{
|
||||
@Override
|
||||
public void validate(DbObject dbo, DiffContext ctx)
|
||||
public void validate(DbObject reference, DbObject target, DiffContext ctx)
|
||||
{
|
||||
// Do nothing
|
||||
}
|
||||
|
Reference in New Issue
Block a user