1 package dst.ass2.aop.tests;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
7 import java.lang.reflect.Method;
11 import dst.ass2.aop.IPluginExecutable;
12 import dst.ass2.aop.event.Event;
13 import dst.ass2.aop.event.EventBus;
14 import dst.ass2.aop.sample.InterruptedPluginExecutable;
15 import dst.ass2.aop.util.PluginUtils;
16 import org.aspectj.lang.annotation.After;
17 import org.aspectj.lang.annotation.Around;
18 import org.aspectj.lang.annotation.Aspect;
19 import org.aspectj.lang.annotation.Before;
20 import org.aspectj.weaver.internal.tools.PointcutExpressionImpl;
21 import org.aspectj.weaver.tools.ShadowMatch;
22 import org.junit.Test;
23 import org.springframework.aop.PointcutAdvisor;
24 import org.springframework.aop.framework.Advised;
25 import org.springframework.core.annotation.AnnotationUtils;
26 import org.springframework.util.ReflectionUtils;
28 import dst.ass2.aop.management.ManagementAspect;
30 public class Ass2_4_3Test {
31 final EventBus eventBus = EventBus.getInstance();
35 public void beforeAndAfter() {
40 * Verifies that the {@link ManagementAspect} is a valid AspectJ aspect i.e., {@link Aspect @Aspect} as well as
41 * {@link Around @Around} or {@link Before @Before} / {@link After @After}.
44 public void managementAspect_isValid() {
45 Aspect aspect = AnnotationUtils.findAnnotation(ManagementAspect.class, Aspect.class);
46 assertNotNull("ManagementAspect is not annotated with @Aspect", aspect);
48 Map<Method, Around> around = PluginUtils.findMethodAnnotation(ManagementAspect.class, Around.class);
49 Map<Method, Before> before = PluginUtils.findMethodAnnotation(ManagementAspect.class, Before.class);
50 Map<Method, After> after = PluginUtils.findMethodAnnotation(ManagementAspect.class, After.class);
52 boolean found = !around.isEmpty() || (!before.isEmpty() && !after.isEmpty());
53 assertEquals("ManagementAspect does not contain methods annotated with @Around OR @Before and @After", true, found);
57 * Verifies that the pointcut expression of the {@link ManagementAspect}
58 * does not match any method except the {@link IPluginExecutable#execute()} method.
61 public void pointcutExpression_matchesCorrectly() {
62 IPluginExecutable executable = PluginUtils.getExecutable(InterruptedPluginExecutable.class, ManagementAspect.class);
63 assertEquals("Executable must implement the Advised interface", true, executable instanceof Advised);
64 Advised advised = (Advised) executable;
66 PointcutAdvisor pointcutAdvisor = PluginUtils.getPointcutAdvisor(advised);
67 assertNotNull("PointcutAdvisor not found because there is no pointcut or the pointcut does not match", pointcutAdvisor);
69 PointcutExpressionImpl pointcutExpression = PluginUtils.getPointcutExpression(advised);
70 Method interruptedMethod = ReflectionUtils.findMethod(InterruptedPluginExecutable.class, PluginUtils.EXECUTE_METHOD.getName());
71 ShadowMatch shadowMatch = pointcutExpression.matchesMethodExecution(interruptedMethod);
72 assertEquals("Pointcut does not match InterruptedPluginExecutable.execute()", true, shadowMatch.alwaysMatches());
76 * Tests if the {@link ManagementAspect} interrupts the plugin after the given timeout.
78 @Test(timeout = PluginUtils.PLUGIN_TEST_TIMEOUT)
79 public void managementAspect_interruptsCorrectly() {
80 IPluginExecutable executable = PluginUtils.getExecutable(InterruptedPluginExecutable.class, ManagementAspect.class);
81 assertEquals("EventBus must be empty", 0, eventBus.size());
84 List<Event> events = eventBus.getEvents();
85 assertEquals("EventBus must contain 2 events", 2, events.size());
87 long duration = events.get(1).getTime() - events.get(0).getTime();
88 assertTrue("Plugin was not interrupted 2 seconds after starting it", duration < 3000);