Search in sources :

Example 1 with TestModel

use of org.jaffa.rules.TestModel in project jaffa-framework by jaffa-projects.

the class FieldInitializerTest method testInterceptor.

/**
 * Tests the interceptor based initializer
 */
@Test
public void testInterceptor() throws Throwable {
    TestModel testModel = new TestModel();
    AbstractRuleInterceptor interceptor = new InitializeInterceptor();
    interceptor.setTargetClassName(testModel.getClass().getName());
    // this has to be a valid method, in production code it would be the constructor
    Method method = TestModel.class.getMethod("validate");
    MethodInvocation invocation = new MethodInvocation(new Interceptor[0], 0, method, method, null);
    invocation.setTargetObject(testModel);
    assertNull(testModel.getField1());
    interceptor.invoke(invocation);
    assertEquals(FIELD1_INIT_VALUE, testModel.getField1());
}
Also used : AbstractRuleInterceptor(org.jaffa.rules.jbossaop.interceptors.AbstractRuleInterceptor) MethodInvocation(org.jboss.aop.joinpoint.MethodInvocation) InitializeInterceptor(org.jaffa.rules.jbossaop.interceptors.InitializeInterceptor) Method(java.lang.reflect.Method) TestModel(org.jaffa.rules.TestModel) Test(org.junit.Test)

Example 2 with TestModel

use of org.jaffa.rules.TestModel in project jaffa-framework by jaffa-projects.

the class RuleInitializerFactoryTest method testCaching.

/**
 * Multiple hits to the same class/realm/variation should return the same instance.
 */
@Test
public void testCaching() {
    Initializer<TestModel> initializer1 = target.getInitializer(new TestModel());
    Initializer<TestModel> initializer2 = target.getInitializer(new TestModel());
    assertSame(initializer1, initializer2);
}
Also used : TestModel(org.jaffa.rules.TestModel) Test(org.junit.Test)

Example 3 with TestModel

use of org.jaffa.rules.TestModel in project jaffa-framework by jaffa-projects.

the class RuleInitializerFactoryTest method testInitializer.

/**
 * If initialize rules are present for an object, an initializer should be returned.
 */
@Test
public void testInitializer() throws FrameworkException, ApplicationException {
    TestModel model = new TestModel();
    Initializer<TestModel> initializer = target.getInitializer(model);
    initializer.initialize(model);
    assertEquals(FieldInitializerTest.FIELD1_INIT_VALUE, model.getField1());
}
Also used : TestModel(org.jaffa.rules.TestModel) Test(org.junit.Test)

Example 4 with TestModel

use of org.jaffa.rules.TestModel in project jaffa-framework by jaffa-projects.

the class CommentValidatorTest method testInterceptor.

/**
 * Demonstrate that there's a proper situation for the CommentInterceptor to change the value of the field.
 */
@Test
public void testInterceptor() throws Throwable {
    testModel.setField1(NEW_COMMENT);
    testModel.setOriginalValue("");
    CommentInterceptor interceptor = new CommentInterceptor();
    interceptor.setTargetClassName(TestModel.class.getName());
    Method method = TestModel.class.getMethod("validate");
    MethodInvocation invocation = new MethodInvocation(new Interceptor[0], 0, method, method, null);
    // "field3" is configured for the comment
    invocation.setTargetObject(testModel);
    interceptor.invoke(invocation);
    // if field1 has changed, them the comment was changed by the interceptor.
    assertNotEquals(NEW_COMMENT, testModel.getField1());
}
Also used : CommentInterceptor(org.jaffa.rules.jbossaop.interceptors.CommentInterceptor) MethodInvocation(org.jboss.aop.joinpoint.MethodInvocation) Method(java.lang.reflect.Method) TestModel(org.jaffa.rules.TestModel) Test(org.junit.Test)

Example 5 with TestModel

use of org.jaffa.rules.TestModel in project jaffa-framework by jaffa-projects.

the class MaxLengthValidatorTest method testNoFailure.

/**
 * If the one rule is satisfied, there should be no validation exception.
 */
@Test
public void testNoFailure() throws ApplicationException, FrameworkException {
    TestModel testModel = new TestModel();
    // max length not exceeded
    testModel.setField1("123");
    targetMaxLength.validate(testModel);
}
Also used : TestModel(org.jaffa.rules.TestModel) Test(org.junit.Test)

Aggregations

TestModel (org.jaffa.rules.TestModel)22 Test (org.junit.Test)22 Method (java.lang.reflect.Method)3 MethodInvocation (org.jboss.aop.joinpoint.MethodInvocation)3 RuleMetaData (org.jaffa.rules.meta.RuleMetaData)2 IRuleEvaluator (org.jaffa.rules.rulemeta.IRuleEvaluator)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 FakeModel (org.jaffa.persistence.FakeModel)1 AbstractRuleInterceptor (org.jaffa.rules.jbossaop.interceptors.AbstractRuleInterceptor)1 CaseTypeInterceptor (org.jaffa.rules.jbossaop.interceptors.CaseTypeInterceptor)1 CommentInterceptor (org.jaffa.rules.jbossaop.interceptors.CommentInterceptor)1 InitializeInterceptor (org.jaffa.rules.jbossaop.interceptors.InitializeInterceptor)1