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