mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-31 17:39:05 +00:00
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:
@@ -22,6 +22,8 @@ import java.io.Serializable;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.alfresco.module.org_alfresco_module_rm.classification.validation.ClassificationLevelValidation;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MissingConfiguration;
|
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MissingConfiguration;
|
||||||
import org.alfresco.module.org_alfresco_module_rm.classification.model.ClassifiedContentModel;
|
import org.alfresco.module.org_alfresco_module_rm.classification.model.ClassifiedContentModel;
|
||||||
|
@@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* 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.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.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validator for the fields in {@link ClassificationLevel}.
|
||||||
|
*
|
||||||
|
* @author tpage
|
||||||
|
* @since 3.0
|
||||||
|
*/
|
||||||
|
public class ClassificationLevelFieldsValidator implements EntityFieldsValidator<ClassificationLevel>
|
||||||
|
{
|
||||||
|
/** The maximum number of characters allowed in a {@link ClassificationLevel#getId() level ID}. */
|
||||||
|
private static final int ABBREVIATION_LENGTH_LIMIT = 10;
|
||||||
|
/** Validator for the length of the level abbreviation. */
|
||||||
|
private FieldValidator<String> lengthFieldValidator = new LengthFieldValidator(1, ABBREVIATION_LENGTH_LIMIT);
|
||||||
|
/** Validator for the characters in the level abbreviation. */
|
||||||
|
private FieldValidator<String> filenameFieldValidator = new FilenameFieldValidator();
|
||||||
|
/** Validator that checks that the reserved symbol "U" is not configured as a classification level. */
|
||||||
|
private FieldValidator<String> classificationLevelIsNotUnclassifiedValidator = new ClassificationLevelIsNotUnclassifiedValidator();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates the provided {@link ClassificationLevel}.
|
||||||
|
*
|
||||||
|
* @param classificationLevel the level to validate.
|
||||||
|
* @throws MissingConfiguration if the level abbreviation is missing.
|
||||||
|
* @throws IllegalConfiguration if the level abbreviation violates the standard restrictions.
|
||||||
|
* @throws IllegalAbbreviationChars if the level abbreviation contains illegal characters.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void validate(ClassificationLevel classificationLevel)
|
||||||
|
{
|
||||||
|
final String levelId = classificationLevel.getId();
|
||||||
|
|
||||||
|
lengthFieldValidator.validate(levelId, "level abbreviation");
|
||||||
|
filenameFieldValidator.validate(levelId, "level abbreviation");
|
||||||
|
classificationLevelIsNotUnclassifiedValidator.validate(levelId, "level abbreviation");
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalConfiguration;
|
||||||
|
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevelManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A validator that checks that the reserved symbol "U" is not configured as a classification level.
|
||||||
|
*
|
||||||
|
* @author tpage
|
||||||
|
* @since 3.0
|
||||||
|
*/
|
||||||
|
public class ClassificationLevelIsNotUnclassifiedValidator implements FieldValidator<String>
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void validate(String levelId, String fieldName) throws ClassificationException
|
||||||
|
{
|
||||||
|
if (levelId.equals(ClassificationLevelManager.UNCLASSIFIED_ID))
|
||||||
|
{
|
||||||
|
throw new IllegalConfiguration("The unclassified abbreviation '" +
|
||||||
|
ClassificationLevelManager.UNCLASSIFIED_ID + "' is reserved for system use.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -16,14 +16,15 @@
|
|||||||
* You should have received a copy of the GNU Lesser General Public License
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
* 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.util.RMCollectionUtils.getDuplicateElements;
|
import static org.alfresco.module.org_alfresco_module_rm.util.RMCollectionUtils.getDuplicateElements;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
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.IllegalAbbreviationChars;
|
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.IllegalConfiguration;
|
||||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MissingConfiguration;
|
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MissingConfiguration;
|
||||||
@@ -36,47 +37,8 @@ import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationE
|
|||||||
*/
|
*/
|
||||||
public class ClassificationLevelValidation
|
public class ClassificationLevelValidation
|
||||||
{
|
{
|
||||||
/** The maximum number of characters allowed in a {@link ClassificationLevel#getId() level ID}. */
|
/** A validator for the fields within the classification level. */
|
||||||
private static final int ABBREVIATION_LENGTH_LIMIT = 10;
|
private EntityFieldsValidator<ClassificationLevel> classificationLevelFieldsValidator = new ClassificationLevelFieldsValidator();
|
||||||
|
|
||||||
/**
|
|
||||||
* Illegal characters in a {@link ClassificationLevel#getId() level ID}.
|
|
||||||
* Equals to Alfresco's disallowed filename characters.
|
|
||||||
*/
|
|
||||||
// See <constraint name="cm:filename" type="REGEX"> in contentModel.xml
|
|
||||||
static final List<Character> ILLEGAL_ABBREVIATION_CHARS = asList('"', '*', '\\', '>', '<', '?', '/', ':', '|');
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validates the provided {@link ClassificationLevel}.
|
|
||||||
* @param level the level to validate.
|
|
||||||
* @throws MissingConfiguration if the level abbreviation is missing.
|
|
||||||
* @throws IllegalConfiguration if the level abbreviation violates the standard restrictions.
|
|
||||||
* @throws IllegalAbbreviationChars if the level abbreviation contains illegal characters.
|
|
||||||
*/
|
|
||||||
void validateLevel(ClassificationLevel level)
|
|
||||||
{
|
|
||||||
final String levelId = level.getId();
|
|
||||||
|
|
||||||
if (levelId == null || levelId.equals(""))
|
|
||||||
{
|
|
||||||
throw new MissingConfiguration("Classification level ID is missing.");
|
|
||||||
}
|
|
||||||
else if (levelId.equals(ClassificationLevelManager.UNCLASSIFIED_ID))
|
|
||||||
{
|
|
||||||
throw new IllegalConfiguration("The Unclassified ID abbreviation '" +
|
|
||||||
ClassificationLevelManager.UNCLASSIFIED_ID + "' is reserved for system use.");
|
|
||||||
}
|
|
||||||
else if (levelId.length() > ABBREVIATION_LENGTH_LIMIT)
|
|
||||||
{
|
|
||||||
throw new IllegalConfiguration("Illegal classification level abbreviation. Length " +
|
|
||||||
levelId.length() + " > " + ABBREVIATION_LENGTH_LIMIT);
|
|
||||||
}
|
|
||||||
else if (!getIllegalAbbreviationChars(levelId).isEmpty())
|
|
||||||
{
|
|
||||||
throw new IllegalAbbreviationChars("Illegal character(s) in level abbreviation",
|
|
||||||
getIllegalAbbreviationChars(levelId));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validates the provided {@link ClassificationLevel}s as a whole and individually.
|
* Validates the provided {@link ClassificationLevel}s as a whole and individually.
|
||||||
@@ -100,17 +62,7 @@ public class ClassificationLevelValidation
|
|||||||
|
|
||||||
for (ClassificationLevel level : levels)
|
for (ClassificationLevel level : levels)
|
||||||
{
|
{
|
||||||
validateLevel(level);
|
classificationLevelFieldsValidator.validate(level);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Character> getIllegalAbbreviationChars(String s)
|
|
||||||
{
|
|
||||||
final List<Character> result = new ArrayList<>();
|
|
||||||
for (Character c : ILLEGAL_ABBREVIATION_CHARS)
|
|
||||||
{
|
|
||||||
if (s.contains(c.toString())) result.add(c);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
}
|
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* 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.ClassificationSchemeEntity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A validator for all the fields of a classification POJO.
|
||||||
|
*
|
||||||
|
* @author tpage
|
||||||
|
* @since 3.0
|
||||||
|
*/
|
||||||
|
public interface EntityFieldsValidator<T extends ClassificationSchemeEntity>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Validate the fields of the given POJO.
|
||||||
|
*
|
||||||
|
* @param pojo The object to validate.
|
||||||
|
*/
|
||||||
|
void validate(T pojo);
|
||||||
|
}
|
@@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate a field.
|
||||||
|
*
|
||||||
|
* @author tpage
|
||||||
|
* @since 3.0
|
||||||
|
*/
|
||||||
|
public interface FieldValidator<T>
|
||||||
|
{
|
||||||
|
void validate(T field, String fieldName) throws ClassificationException;
|
||||||
|
}
|
@@ -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 java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException;
|
||||||
|
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalAbbreviationChars;
|
||||||
|
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check that a field is suitable to be used as part of a filename.
|
||||||
|
*
|
||||||
|
* @author tpage
|
||||||
|
* @since 3.0
|
||||||
|
*/
|
||||||
|
public class FilenameFieldValidator implements FieldValidator<String>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Illegal characters in a {@link ClassificationLevel#getId() level ID}.
|
||||||
|
* Equals to Alfresco's disallowed filename characters.
|
||||||
|
*/
|
||||||
|
// See <constraint name="cm:filename" type="REGEX"> in contentModel.xml
|
||||||
|
static final List<Character> ILLEGAL_ABBREVIATION_CHARS = asList('"', '*', '\\', '>', '<', '?', '/', ':', '|');
|
||||||
|
|
||||||
|
private List<Character> getIllegalAbbreviationChars(String s)
|
||||||
|
{
|
||||||
|
final List<Character> result = new ArrayList<>();
|
||||||
|
for (Character c : ILLEGAL_ABBREVIATION_CHARS)
|
||||||
|
{
|
||||||
|
if (s.contains(c.toString())) result.add(c);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void validate(String field, String fieldName) throws ClassificationException
|
||||||
|
{
|
||||||
|
List<Character> illegalAbbreviationChars = getIllegalAbbreviationChars(field);
|
||||||
|
if (!illegalAbbreviationChars.isEmpty())
|
||||||
|
{
|
||||||
|
throw new IllegalAbbreviationChars("Illegal character(s) in " + fieldName, illegalAbbreviationChars);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalConfiguration;
|
||||||
|
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MissingConfiguration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate the length of a field.
|
||||||
|
*
|
||||||
|
* @author tpage
|
||||||
|
* @since 3.0
|
||||||
|
*/
|
||||||
|
public class LengthFieldValidator implements FieldValidator<String>
|
||||||
|
{
|
||||||
|
/** The minimum allowed length for the field. */
|
||||||
|
private int minimumLength;
|
||||||
|
/** The maximum allowed length for the field, or {@code null} if there is no maximum length. */
|
||||||
|
private Integer maximumLength = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a validator that only checks the minimum length.
|
||||||
|
*
|
||||||
|
* @param minimumLength The length of the shortest allowable field.
|
||||||
|
*/
|
||||||
|
public LengthFieldValidator(int minimumLength)
|
||||||
|
{
|
||||||
|
this.minimumLength = minimumLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a validator that checks the minimum and maximum length.
|
||||||
|
*
|
||||||
|
* @param minimumLength The length of the shortest allowable field.
|
||||||
|
* @param maximumLength The length of the longest allowable field.
|
||||||
|
*/
|
||||||
|
public LengthFieldValidator(int minimumLength, int maximumLength)
|
||||||
|
{
|
||||||
|
if (minimumLength > maximumLength)
|
||||||
|
{
|
||||||
|
throw new IllegalArgumentException("The minimum length may not be larger than the maximum length.");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.minimumLength = minimumLength;
|
||||||
|
this.maximumLength = maximumLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void validate(String field, String fieldName) throws ClassificationException
|
||||||
|
{
|
||||||
|
if (field == null || (field.length() == 0 && minimumLength > 0))
|
||||||
|
{
|
||||||
|
throw new MissingConfiguration(fieldName + " is missing.");
|
||||||
|
}
|
||||||
|
else if (field.length() < minimumLength)
|
||||||
|
{
|
||||||
|
throw new IllegalConfiguration("Illegal " + fieldName + ": Length " + field.length() + " < " + minimumLength);
|
||||||
|
}
|
||||||
|
else if (maximumLength != null && field.length() > maximumLength)
|
||||||
|
{
|
||||||
|
throw new IllegalConfiguration("Illegal " + fieldName + ": Length " + field.length() + " > " + maximumLength);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -16,39 +16,32 @@
|
|||||||
* You should have received a copy of the GNU Lesser General Public License
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
* 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 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.alfresco.module.org_alfresco_module_rm.test.util.ExceptionUtils.expectedException;
|
||||||
import static org.hamcrest.CoreMatchers.allOf;
|
import static org.hamcrest.CoreMatchers.allOf;
|
||||||
import static org.hamcrest.CoreMatchers.containsString;
|
|
||||||
import static org.hamcrest.core.IsCollectionContaining.hasItem;
|
import static org.hamcrest.core.IsCollectionContaining.hasItem;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.junit.Assert.assertTrue;
|
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;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalAbbreviationChars;
|
||||||
* Unit tests for the {@link ClassificationLevelValidation}.
|
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalConfiguration;
|
||||||
*
|
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevel;
|
||||||
* @author Neil Mc Erlean
|
import org.junit.Test;
|
||||||
*/
|
|
||||||
public class ClassificationLevelValidationUnitTest
|
|
||||||
{
|
|
||||||
private final ClassificationLevelValidation validation = new ClassificationLevelValidation();
|
|
||||||
|
|
||||||
@Test(expected=MissingConfiguration.class)
|
/**
|
||||||
public void classificationLevelsAreRequired()
|
* Unit tests for the {@link ClassificationLevelFieldsValidator}.
|
||||||
{
|
*
|
||||||
validation.validateLevels(Collections.emptyList());
|
* @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. */
|
/** Ensures that null, empty or whitespace-only IDs are rejected. */
|
||||||
@Test public void nonEmptyAbbreviationsAreMandatory()
|
@Test public void nonEmptyAbbreviationsAreMandatory()
|
||||||
@@ -58,7 +51,7 @@ public class ClassificationLevelValidationUnitTest
|
|||||||
{
|
{
|
||||||
expectedException(IllegalArgumentException.class, () ->
|
expectedException(IllegalArgumentException.class, () ->
|
||||||
{
|
{
|
||||||
validation.validateLevel(new ClassificationLevel(illegalID, "value.does.not.matter"));
|
validator.validate(new ClassificationLevel(illegalID, "value.does.not.matter"));
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -67,29 +60,16 @@ public class ClassificationLevelValidationUnitTest
|
|||||||
@Test(expected=IllegalConfiguration.class)
|
@Test(expected=IllegalConfiguration.class)
|
||||||
public void systemUnclassifiedAbbreviationIsReserved()
|
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)
|
@Test(expected=IllegalConfiguration.class)
|
||||||
public void longAbbreviationsAreIllegal()
|
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
|
* 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, () ->
|
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;
|
return null;
|
||||||
});
|
});
|
||||||
assertTrue("Exception did not contain helpful example of illegal character",
|
assertTrue("Exception did not contain helpful example of illegal character",
|
||||||
@@ -113,7 +93,7 @@ public class ClassificationLevelValidationUnitTest
|
|||||||
|
|
||||||
IllegalAbbreviationChars e = expectedException(IllegalAbbreviationChars.class, () ->
|
IllegalAbbreviationChars e = expectedException(IllegalAbbreviationChars.class, () ->
|
||||||
{
|
{
|
||||||
validation.validateLevel(new ClassificationLevel(someIllegalChars.toString(),
|
validator.validate(new ClassificationLevel(someIllegalChars.toString(),
|
||||||
"value.does.not.matter"));
|
"value.does.not.matter"));
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
@@ -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")));
|
||||||
|
}
|
||||||
|
}
|
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user