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());
}
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));
}
Aggregations