Skip to content

Commit

Permalink
#60 Adding DSL support for passing in RuleExecutors. Allows for use o…
Browse files Browse the repository at this point in the history
…f java 8 lambdas when defining custom rules.
  • Loading branch information
Taylor Wicksell committed May 25, 2014
1 parent 2b60352 commit ca0e803
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.springjutsu.validation.dsl;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.beanutils.PropertyUtils;

import com.fluentinterface.proxy.impl.SetterAttributeAccessStrategy;

public class OverloadedPropertyAwareSetterAttributeAccessStrategy extends SetterAttributeAccessStrategy {

@Override
public void setPropertyValue(Object target, String property, Object value) throws IllegalAccessException,
NoSuchMethodException, InvocationTargetException {
MethodUtils.invokeMethod(target, PropertyUtils.getPropertyDescriptor(target, property).getWriteMethod().getName(), value);
}

}
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
package org.springjutsu.validation.dsl;

import org.springjutsu.validation.executors.RuleExecutor;
import org.springjutsu.validation.rules.RuleErrorMode;

import com.fluentinterface.ReflectionBuilder;

public class Validation {

public static ValidationEntityBuilder forEntity(Class<?> entityClass) {
return ReflectionBuilder.implementationFor(ValidationEntityBuilder.class).create().forValidationClass(entityClass);
return ReflectionBuilder.implementationFor(ValidationEntityBuilder.class)
.usingAttributeAccessStrategy(new OverloadedPropertyAwareSetterAttributeAccessStrategy()).create()
.forValidationClass(entityClass);
}

public static ValidationRuleBuilder rule(String path, String type)
{
return ReflectionBuilder.implementationFor(ValidationRuleBuilder.class).create().forPath(path).usingType(type).behaviorOnFail(RuleErrorMode.ERROR);
return ReflectionBuilder.implementationFor(ValidationRuleBuilder.class)
.usingAttributeAccessStrategy(new OverloadedPropertyAwareSetterAttributeAccessStrategy()).create()
.forPath(path).usingHandler(type).behaviorOnFail(RuleErrorMode.ERROR);
}

public static <M,A> ValidationRuleBuilder rule(String path, String message, RuleExecutor<M,A> handlerImpl)
{
return ReflectionBuilder.implementationFor(ValidationRuleBuilder.class)
.usingAttributeAccessStrategy(new OverloadedPropertyAwareSetterAttributeAccessStrategy()).create()
.forPath(path).withMessage(message).usingHandler(handlerImpl).behaviorOnFail(RuleErrorMode.ERROR);
}

public static ValidationContextBuilder context(String type, String... qualifiers)
{
return ReflectionBuilder.implementationFor(ValidationContextBuilder.class).create().ofType(type).withQualifiers(qualifiers);
return ReflectionBuilder.implementationFor(ValidationContextBuilder.class)
.usingAttributeAccessStrategy(new OverloadedPropertyAwareSetterAttributeAccessStrategy()).create()
.ofType(type).withQualifiers(qualifiers);
}

public static ValidationContextBuilder group(String... names)
{
return ReflectionBuilder.implementationFor(ValidationContextBuilder.class).create().ofType("group").withQualifiers(names);
return ReflectionBuilder.implementationFor(ValidationContextBuilder.class)
.usingAttributeAccessStrategy(new OverloadedPropertyAwareSetterAttributeAccessStrategy()).create()
.ofType("group").withQualifiers(names);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.springjutsu.validation.dsl;

import org.springjutsu.validation.executors.RuleExecutor;
import org.springjutsu.validation.rules.CollectionStrategy;
import org.springjutsu.validation.rules.RuleErrorMode;
import org.springjutsu.validation.rules.ValidationRule;
Expand All @@ -9,7 +10,8 @@
public interface ValidationRuleBuilder extends Builder<ValidationRule>
{
public ValidationRuleBuilder forPath(String path);
public ValidationRuleBuilder usingType(String type);
public ValidationRuleBuilder usingHandler(String handlerName);
public <M,A> ValidationRuleBuilder usingHandler(RuleExecutor<M, A> handlerImpl);
public ValidationRuleBuilder withValue(String value);
public ValidationRuleBuilder withErrorPath(String errorPath);
public ValidationRuleBuilder withMessage(String message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.validation.Errors;
import org.springjutsu.validation.dsl.Validation;
import org.springjutsu.validation.executors.impl.RequiredRuleExecutor;
import org.springjutsu.validation.rules.ValidationEntity;
import org.springjutsu.validation.test.entities.Address;
import org.springjutsu.validation.test.entities.Customer;
import org.springjutsu.validation.util.ValidationRulesUtils;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=ValidationJavaConfigurationIntegrationTestConfig.class)
Expand Down Expand Up @@ -61,7 +63,7 @@ class ValidationJavaConfigurationIntegrationTestConfig
ValidationEntity personValidation()
{
return Validation.forEntity(Customer.class)
.havingRules(Validation.rule("favoriteColor", "required"))
.havingRules(Validation.rule("favoriteColor", "required", new RequiredRuleExecutor()))
.havingValidationContexts(Validation.context("group", "dave")
.havingRules(
Validation.rule("firstName", "required").withMessage("blam"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.junit.Assert;
import org.junit.Test;
import org.springjutsu.validation.dsl.Validation;
import org.springjutsu.validation.executors.RuleExecutor;
import org.springjutsu.validation.executors.impl.RequiredRuleExecutor;
import org.springjutsu.validation.rules.ValidationEntity;
import org.springjutsu.validation.test.entities.Customer;
import org.springjutsu.validation.test.entities.Person;
Expand Down Expand Up @@ -89,6 +91,25 @@ public void testContextsAndGroups()
Assert.assertEquals("notNull", entity.getValidationContexts().get(0).getRules().get(2).getValidationContexts().get(0).getRules().get(0).getType());
Assert.assertEquals("p-p-p-pow", entity.getValidationContexts().get(0).getRules().get(2).getValidationContexts().get(0).getRules().get(0).getMessage());
}

@Test
public void testWithRuleExecutorArgument()
{
ValidationEntity entity = Validation.forEntity(Person.class)
.havingRules(
Validation.rule("firstName", "blam yo", new RequiredRuleExecutor())
)
.build();

Assert.assertEquals(Person.class, entity.getValidationClass());
Assert.assertEquals("firstName", entity.getRules().get(0).getPath());
Assert.assertEquals("blam yo", entity.getRules().get(0).getMessage());
Assert.assertNull(entity.getRules().get(0).getType());
Assert.assertNotNull(entity.getRules().get(0).getRuleExecutor());
Assert.assertTrue(entity.getRules().get(0).getRuleExecutor() instanceof RequiredRuleExecutor);

}


}

Expand Down

0 comments on commit ca0e803

Please sign in to comment.