Search in sources :

Example 1 with AbstractRuleInterceptor

use of org.jaffa.rules.jbossaop.interceptors.AbstractRuleInterceptor 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 AbstractRuleInterceptor

use of org.jaffa.rules.jbossaop.interceptors.AbstractRuleInterceptor 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)

Aggregations

Method (java.lang.reflect.Method)2 AbstractRuleInterceptor (org.jaffa.rules.jbossaop.interceptors.AbstractRuleInterceptor)2 MethodInvocation (org.jboss.aop.joinpoint.MethodInvocation)2 ArrayList (java.util.ArrayList)1 ApplicationException (org.jaffa.exceptions.ApplicationException)1 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)1 TestModel (org.jaffa.rules.TestModel)1 AbstractValidateInterceptor (org.jaffa.rules.jbossaop.interceptors.AbstractValidateInterceptor)1 InitializeInterceptor (org.jaffa.rules.jbossaop.interceptors.InitializeInterceptor)1 Test (org.junit.Test)1