ALF-10771: adding validation to schema compare tool

Added support to DbObjects to accept visitors
Added ValidatingVisitor to invoke suitable validator on each DbObject
Added NameValidator and NullValidator to operate on DbObject types
Added test suites



git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@31494 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Matt Ward
2011-10-26 16:01:38 +00:00
parent 7764900451
commit ec302df6ed
33 changed files with 896 additions and 30 deletions

View File

@@ -0,0 +1,31 @@
/*
* Copyright (C) 2005-2011 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.util.schemacomp.validator;
import org.alfresco.util.schemacomp.DiffContext;
import org.alfresco.util.schemacomp.model.DbObject;
/**
* TODO: comment me!
* @author Matt Ward
*/
public interface DbValidator
{
void validate(DbObject dbo, DiffContext ctx);
}

View File

@@ -0,0 +1,85 @@
/*
* Copyright (C) 2005-2011 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.util.schemacomp.validator;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import org.alfresco.util.schemacomp.DiffContext;
import org.alfresco.util.schemacomp.ValidationResult;
import org.alfresco.util.schemacomp.model.DbObject;
import org.hibernate.dialect.Dialect;
/**
* Validates the name of a DbObject using a regular expression. A regular expression
* can be supplied for each supported {@link Dialect database dialect}. In addition to
* dialect specific regular expressions, a default may be supplied - comparisons will fall
* back to the default if no specific dialect is matched.
*
* @author Matt Ward
*/
public class NameValidator implements DbValidator
{
private Map<Class<? extends Dialect>, Pattern> namePatterns = new HashMap<Class<? extends Dialect>, Pattern>();
private Pattern defaultNamePattern;
@Override
public void validate(DbObject dbo, DiffContext ctx)
{
String name = dbo.getName();
Pattern pattern = namePatterns.get(ctx.getDialect().getClass());
ValidationResult result = new ValidationResult(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)
{
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;
}
}

View File

@@ -0,0 +1,93 @@
/*
* Copyright (C) 2005-2011 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.util.schemacomp.validator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.alfresco.util.schemacomp.DiffContext;
import org.alfresco.util.schemacomp.Differences;
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;
import static org.junit.Assert.*;
/**
* Tests for the NameValidator class.
*
* @author Matt Ward
*/
public class NameValidatorTest
{
private NameValidator validator;
private DiffContext ctx;
private List<ValidationResult> validationResults;
@Before
public void setUp() throws Exception
{
validator = new NameValidator();
validationResults = new ArrayList<ValidationResult>();
ctx = new DiffContext(new Oracle10gDialect(), new Differences(), validationResults);
}
@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);
assertEquals(2, validationResults.size());
assertEquals("SYS_", validationResults.get(0).getValue());
assertEquals("MY_INDEX", validationResults.get(1).getValue());
}
@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.validate(indexForName("ORA_MYINDEX"), ctx);
validator.validate(indexForName("SYS_MYINDEX"), ctx);
assertEquals(1, validationResults.size());
assertEquals("SYS_MYINDEX", validationResults.get(0).getValue());
}
private DbObject indexForName(String name)
{
return new Index(name, new ArrayList<String>());
}
}

View File

@@ -0,0 +1,35 @@
/*
* Copyright (C) 2005-2011 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.util.schemacomp.validator;
import org.alfresco.util.schemacomp.DiffContext;
import org.alfresco.util.schemacomp.model.DbObject;
/**
* TODO: comment me!
* @author Matt Ward
*/
public class NullValidator implements DbValidator
{
@Override
public void validate(DbObject dbo, DiffContext ctx)
{
// Do nothing
}
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright (C) 2005-2011 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.util.schemacomp.validator;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses(
{
NameValidatorTest.class
})
public class ValidatorTestSuite
{
}