RM-2319 Separate the field validation from ClassificationLevelValidation.

Create field validators that are small and potentially re-usable.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@106527 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Tom Page
2015-06-19 09:52:10 +00:00
parent 88ee670c81
commit 4ee3afa208
11 changed files with 488 additions and 96 deletions

View File

@@ -16,39 +16,32 @@
* 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.module.org_alfresco_module_rm.classification;
package org.alfresco.module.org_alfresco_module_rm.classification.validation;
import static java.util.Arrays.asList;
import static org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevelValidation.ILLEGAL_ABBREVIATION_CHARS;
import static org.alfresco.module.org_alfresco_module_rm.classification.validation.FilenameFieldValidator.ILLEGAL_ABBREVIATION_CHARS;
import static org.alfresco.module.org_alfresco_module_rm.test.util.ExceptionUtils.expectedException;
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalAbbreviationChars;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalConfiguration;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MissingConfiguration;
import org.junit.Test;
import java.util.Collections;
import java.util.List;
/**
* Unit tests for the {@link ClassificationLevelValidation}.
*
* @author Neil Mc Erlean
*/
public class ClassificationLevelValidationUnitTest
{
private final ClassificationLevelValidation validation = new ClassificationLevelValidation();
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalAbbreviationChars;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalConfiguration;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevel;
import org.junit.Test;
@Test(expected=MissingConfiguration.class)
public void classificationLevelsAreRequired()
{
validation.validateLevels(Collections.emptyList());
}
/**
* Unit tests for the {@link ClassificationLevelFieldsValidator}.
*
* @author Neil Mc Erlean
* @author tpage
*/
public class ClassificationLevelFieldsValidatorUnitTest
{
private final ClassificationLevelFieldsValidator validator = new ClassificationLevelFieldsValidator();
/** Ensures that null, empty or whitespace-only IDs are rejected. */
@Test public void nonEmptyAbbreviationsAreMandatory()
@@ -58,7 +51,7 @@ public class ClassificationLevelValidationUnitTest
{
expectedException(IllegalArgumentException.class, () ->
{
validation.validateLevel(new ClassificationLevel(illegalID, "value.does.not.matter"));
validator.validate(new ClassificationLevel(illegalID, "value.does.not.matter"));
return null;
});
}
@@ -67,29 +60,16 @@ public class ClassificationLevelValidationUnitTest
@Test(expected=IllegalConfiguration.class)
public void systemUnclassifiedAbbreviationIsReserved()
{
validation.validateLevel(new ClassificationLevel("U", "value.does.not.matter"));
validator.validate(new ClassificationLevel("U", "value.does.not.matter"));
}
@Test(expected=IllegalConfiguration.class)
public void longAbbreviationsAreIllegal()
{
validation.validateLevel(new ClassificationLevel("12345678901", "value.does.not.matter"));
validator.validate(new ClassificationLevel("12345678901", "value.does.not.matter"));
}
@Test public void ensureUniquenessOfAbbreviationIds()
{
IllegalConfiguration e = expectedException(IllegalConfiguration.class, () ->
{
validation.validateLevels(asList(new ClassificationLevel("FOO", "value.does.not.matter"),
new ClassificationLevel("BAR", "value.does.not.matter"),
new ClassificationLevel("---", "value.does.not.matter"),
new ClassificationLevel("BAR", "value.does.not.matter"),
new ClassificationLevel("FOO", "value.does.not.matter")));
return null;
});
assertThat("Exception message did not identify the duplicate IDs", e.getMessage(),
allOf(containsString("FOO"), containsString("BAR")));
}
/**
* This test ensures that validation will catch any and all illegal characters in a
@@ -101,7 +81,7 @@ public class ClassificationLevelValidationUnitTest
{
IllegalAbbreviationChars e = expectedException(IllegalAbbreviationChars.class, () ->
{
validation.validateLevel(new ClassificationLevel("Hello" + illegalChar, "value.does.not.matter"));
validator.validate(new ClassificationLevel("Hello" + illegalChar, "value.does.not.matter"));
return null;
});
assertTrue("Exception did not contain helpful example of illegal character",
@@ -113,7 +93,7 @@ public class ClassificationLevelValidationUnitTest
IllegalAbbreviationChars e = expectedException(IllegalAbbreviationChars.class, () ->
{
validation.validateLevel(new ClassificationLevel(someIllegalChars.toString(),
validator.validate(new ClassificationLevel(someIllegalChars.toString(),
"value.does.not.matter"));
return null;
});

View File

@@ -0,0 +1,64 @@
/*
* Copyright (C) 2005-2015 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.module.org_alfresco_module_rm.classification.validation;
import static java.util.Arrays.asList;
import static org.alfresco.module.org_alfresco_module_rm.test.util.ExceptionUtils.expectedException;
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
import java.util.Collections;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevel;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalConfiguration;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MissingConfiguration;
import org.junit.Test;
/**
* Unit tests for the {@link ClassificationLevelValidation}.
*
* @author Neil Mc Erlean
*/
public class ClassificationLevelValidationUnitTest
{
private final ClassificationLevelValidation validation = new ClassificationLevelValidation();
@Test(expected=MissingConfiguration.class)
public void classificationLevelsAreRequired()
{
validation.validateLevels(Collections.emptyList());
}
@Test public void ensureUniquenessOfAbbreviationIds()
{
IllegalConfiguration e = expectedException(IllegalConfiguration.class, () ->
{
validation.validateLevels(asList(new ClassificationLevel("FOO", "value.does.not.matter"),
new ClassificationLevel("BAR", "value.does.not.matter"),
new ClassificationLevel("---", "value.does.not.matter"),
new ClassificationLevel("BAR", "value.does.not.matter"),
new ClassificationLevel("FOO", "value.does.not.matter")));
return null;
});
assertThat("Exception message did not identify the duplicate IDs", e.getMessage(),
allOf(containsString("FOO"), containsString("BAR")));
}
}

View File

@@ -0,0 +1,77 @@
/*
* Copyright (C) 2005-2015 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.module.org_alfresco_module_rm.classification.validation;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalConfiguration;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MissingConfiguration;
import org.junit.Test;
/**
* Unit tests for the {@link LengthFieldValidator}.
*
* @author tpage
* @since 3.0
*/
public class LengthFieldValidatorUnitTest
{
private static final String FIELD_NAME = "FIELD_NAME";
/** The class under test. */
LengthFieldValidator lengthFieldValidator;
@Test
public void testValidate_passMinOnly()
{
lengthFieldValidator = new LengthFieldValidator(1);
lengthFieldValidator.validate("acceptable", FIELD_NAME);
}
@Test(expected = MissingConfiguration.class)
public void testValidate_missingConfiguration()
{
lengthFieldValidator = new LengthFieldValidator(1);
lengthFieldValidator.validate("", FIELD_NAME);
}
@Test(expected = IllegalConfiguration.class)
public void testValidate_tooShort()
{
lengthFieldValidator = new LengthFieldValidator(6);
lengthFieldValidator.validate("short", FIELD_NAME);
}
@Test
public void testValidate_passMinAndMax()
{
lengthFieldValidator = new LengthFieldValidator(5,5);
lengthFieldValidator.validate("super", FIELD_NAME);
}
@Test(expected = IllegalConfiguration.class)
public void testValidate_tooLong()
{
lengthFieldValidator = new LengthFieldValidator(6, 7);
lengthFieldValidator.validate("too long", FIELD_NAME);
}
@Test(expected = IllegalArgumentException.class)
public void testValidate_invalidArguments()
{
lengthFieldValidator = new LengthFieldValidator(3, 1);
}
}