Property constraint dictionary support

Constraint implementation support
Regular expression constraint


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2547 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2006-03-14 22:57:50 +00:00
parent f3658f2a58
commit 783de4f80a
24 changed files with 1066 additions and 54 deletions

View File

@@ -0,0 +1,88 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.dictionary.constraint;
import java.util.Collection;
import org.alfresco.service.cmr.dictionary.Constraint;
import org.alfresco.service.cmr.dictionary.DictionaryException;
/**
* Base services for constraints.
*
* @author Derek Hulley
*/
public abstract class AbstractConstraint implements Constraint
{
public static final String ERR_PROP_NOT_SET = "d_dictionary.model.err.property_not_set";
public static final String ERR_EVALUATE_EXCEPTION = "d_dictionary.model.err.evaluate_exception";
/**
* @see #evaluateSingleValue(Object)
* @see #evaluateCollection(Collection)
*/
@SuppressWarnings("unchecked")
public final void evaluate(Object value)
{
try
{
// check for collection
if (value instanceof Collection)
{
Collection collection = (Collection) value;
evaluateCollection(collection);
}
else
{
evaluateSingleValue(value);
}
}
catch (DictionaryException e)
{
// this can go
throw e;
}
catch (Throwable e)
{
throw new DictionaryException(AbstractConstraint.ERR_EVALUATE_EXCEPTION, this, e.getMessage());
}
}
/**
* Only override if there is some specific evaluation that needs to be performed on the
* collection as a whole.
*
* @param collection the collection of values to evaluate
*
* @see #evaluateSingleValue(Object)
*/
protected void evaluateCollection(Collection<Object> collection)
{
for (Object value : collection)
{
evaluateSingleValue(value);
}
}
/**
* Support for evaluation of properties. The value passed in will never be a
* collection.
*
* @throws DictionaryException throw this when the evaluation fails
*/
protected abstract void evaluateSingleValue(Object value);
}

View File

@@ -0,0 +1,142 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.dictionary.constraint;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
import org.alfresco.i18n.I18NUtil;
import org.alfresco.repo.dictionary.DictionaryDAOTest;
import org.alfresco.service.cmr.dictionary.DictionaryException;
/**
* @see org.alfresco.service.cmr.dictionary.Constraint
* @see org.alfresco.repo.dictionary.constraint.AbstractConstraint
* @see org.alfresco.repo.dictionary.constraint.RegexConstraint
*
* @author Derek Hulley
*/
public class ConstraintsTest extends TestCase
{
@Override
protected void setUp() throws Exception
{
// register resource bundles for messages
I18NUtil.registerResourceBundle(DictionaryDAOTest.TEST_RESOURCE_MESSAGES);
}
/**
* ensure that the default handling of checks on collections will work
*/
public void testCollections() throws Exception
{
DummyConstraint constraint = new DummyConstraint();
constraint.initialize();
List<Object> dummyObjects = new ArrayList<Object>(3);
dummyObjects.add("ABC"); // correct
dummyObjects.add("DEF"); // correct
dummyObjects.add(this); // NO
try
{
constraint.evaluate(dummyObjects);
fail("Failed to detected constraint violation in collection");
}
catch (DictionaryException e)
{
// expected
}
// check that the two strings were properly dealt with
assertEquals("String values not checked", 2, constraint.tested.size());
}
public void testRegexConstraint() throws Exception
{
RegexConstraint constraint = new RegexConstraint();
constraint.setExpression("[A-Z]*");
constraint.initialize();
// do some successful stuff
constraint.evaluate("ABC");
constraint.evaluate("DEF");
// now some failures
try
{
constraint.evaluate("abc");
fail("Regular expression evaluation should have failed: abc");
}
catch (DictionaryException e)
{
String msg = e.getMessage();
assertFalse("I18N of constraint message failed", msg.startsWith("d_dictionary.constraint"));
}
// now a case of passing in an object that could be a string
constraint.evaluate(DummyEnum.ABC);
constraint.evaluate(DummyEnum.DEF);
try
{
constraint.evaluate(DummyEnum.abc);
fail("Regular expression evaluation should have failed for enum: " + DummyEnum.abc);
}
catch (DictionaryException e)
{
}
}
private enum DummyEnum
{
ABC,
DEF,
abc;
}
private class DummyConstraint extends AbstractConstraint
{
private List<Object> tested;
public void initialize()
{
tested = new ArrayList<Object>(4);
}
/**
* Fails on everything but String values, which pass.
* Null values cause runtime exceptions and all other failures are by
* DictionaryException.
*/
@Override
protected void evaluateSingleValue(Object value)
{
if (value == null)
{
throw new NullPointerException("Null value in dummy test");
}
else if (value instanceof String)
{
tested.add(value);
}
else
{
throw new DictionaryException("Non-String value");
}
}
}
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.dictionary.constraint;
import org.alfresco.service.cmr.dictionary.DictionaryException;
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
/**
* Constraint implementation that performs regular expression comparisons.
* Where possible, the {@link org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter type converter}
* will be used to first convert the value to a <code>String</code>, so the evaluation
* will be against the value's <code>String</code> equivalent.
*
* @see java.lang.String#matches(java.lang.String)
*
* @author Derek Hulley
*/
public class RegexConstraint extends AbstractConstraint
{
public static final String CONSTRAINT_REGEX_NO_MATCH = "d_dictionary.constraint.regex.no_match";
private String expression;
/**
* Set the regular expression used to evaluate string values
* @param expression similar to the {@link String#matches(java.lang.String) argument
*/
public void setExpression(String expression)
{
this.expression = expression;
}
public void initialize()
{
if (expression == null)
{
throw new DictionaryException(AbstractConstraint.ERR_PROP_NOT_SET, "expression");
}
}
public void evaluateSingleValue(Object value)
{
// convert the value to a String
String valueStr = DefaultTypeConverter.INSTANCE.convert(String.class, value);
boolean matches = valueStr.matches(expression);
if (!matches)
{
throw new DictionaryException(RegexConstraint.CONSTRAINT_REGEX_NO_MATCH, value, expression);
}
}
}