Search in sources :

Example 1 with MethodInvocation

use of org.jboss.aop.joinpoint.MethodInvocation 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 MethodInvocation

use of org.jboss.aop.joinpoint.MethodInvocation 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 3 with MethodInvocation

use of org.jboss.aop.joinpoint.MethodInvocation in project jaffa-framework by jaffa-projects.

the class TestSupport method executeInterceptor.

/**
 * Executes the interceptor and confirms the correct exception is thrown with the field label
 *
 * @param interceptorClass The Interceptor class
 * @param persistent       The target object of the interceptor
 * @param fieldName        The Field Name label
 * @param exceptionClass   The class of the expected exception
 * @throws Throwable
 */
protected void executeInterceptor(Class interceptorClass, Object persistent, String fieldName, Class exceptionClass) throws Throwable {
    assertTrue(AbstractValidateInterceptor.class.isAssignableFrom(interceptorClass));
    AbstractRuleInterceptor interceptor = (AbstractRuleInterceptor) interceptorClass.newInstance();
    interceptor.setTargetClassName(persistent.getClass().getName());
    Method method = persistent.getClass().getMethod("validate");
    MethodInvocation invocation = new MethodInvocation(new Interceptor[0], 0, method, method, null);
    invocation.setTargetObject(persistent);
    List<String> actualLabels = new ArrayList<>();
    ApplicationException exception = null;
    try {
        interceptor.invoke(invocation);
    } catch (ApplicationExceptions e) {
        ApplicationException[] exceptions = e.getApplicationExceptionArray();
        exception = exceptions[0];
        for (ApplicationException sub : exceptions) {
            actualLabels.add((String) sub.getArguments()[0]);
        }
    }
    assertEquals(1, actualLabels.size());
    assertEquals(fieldName, actualLabels.get(0));
    assertTrue(exceptionClass.isInstance(exception));
}
Also used : ApplicationException(org.jaffa.exceptions.ApplicationException) ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) AbstractValidateInterceptor(org.jaffa.rules.jbossaop.interceptors.AbstractValidateInterceptor) AbstractRuleInterceptor(org.jaffa.rules.jbossaop.interceptors.AbstractRuleInterceptor) ArrayList(java.util.ArrayList) MethodInvocation(org.jboss.aop.joinpoint.MethodInvocation) Method(java.lang.reflect.Method)

Example 4 with MethodInvocation

use of org.jboss.aop.joinpoint.MethodInvocation in project jaffa-framework by jaffa-projects.

the class CaseTypeValidatorTest method testInterceptor.

/**
 * Demonstrate that there's a proper situation for the CaseTypeInterceptor to change the case of the field.
 */
@Test
public void testInterceptor() throws Throwable {
    testModel.setField1(TEST_STRING);
    CaseTypeInterceptor interceptor = new CaseTypeInterceptor();
    interceptor.setTargetClassName(TestModel.class.getName());
    Method method = TestModel.class.getMethod("validate");
    MethodInvocation invocation = new MethodInvocation(new Interceptor[0], 0, method, method, null);
    // "field1" is configured to be UPPER CASE, but is set to all lower case.
    invocation.setTargetObject(testModel);
    interceptor.invoke(invocation);
    assertEquals(TEST_STRING.toUpperCase(), testModel.getField1());
}
Also used : CaseTypeInterceptor(org.jaffa.rules.jbossaop.interceptors.CaseTypeInterceptor) MethodInvocation(org.jboss.aop.joinpoint.MethodInvocation) Method(java.lang.reflect.Method) TestModel(org.jaffa.rules.TestModel) Test(org.junit.Test)

Aggregations

Method (java.lang.reflect.Method)4 MethodInvocation (org.jboss.aop.joinpoint.MethodInvocation)4 TestModel (org.jaffa.rules.TestModel)3 Test (org.junit.Test)3 AbstractRuleInterceptor (org.jaffa.rules.jbossaop.interceptors.AbstractRuleInterceptor)2 ArrayList (java.util.ArrayList)1 ApplicationException (org.jaffa.exceptions.ApplicationException)1 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)1 AbstractValidateInterceptor (org.jaffa.rules.jbossaop.interceptors.AbstractValidateInterceptor)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