]> git.somenet.org - pub/jan/dst18.git/blob - ass2-aop/src/test/java/dst/ass2/aop/tests/Ass2_4_3Test.java
Add template for assignment 2
[pub/jan/dst18.git] / ass2-aop / src / test / java / dst / ass2 / aop / tests / Ass2_4_3Test.java
1 package dst.ass2.aop.tests;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6
7 import java.lang.reflect.Method;
8 import java.util.List;
9 import java.util.Map;
10
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;
27
28 import dst.ass2.aop.management.ManagementAspect;
29
30 public class Ass2_4_3Test {
31     final EventBus eventBus = EventBus.getInstance();
32
33     @org.junit.Before
34     @org.junit.After
35     public void beforeAndAfter() {
36         eventBus.reset();
37     }
38
39     /**
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}.
42      */
43     @Test
44     public void managementAspect_isValid() {
45         Aspect aspect = AnnotationUtils.findAnnotation(ManagementAspect.class, Aspect.class);
46         assertNotNull("ManagementAspect is not annotated with @Aspect", aspect);
47
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);
51
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);
54     }
55
56     /**
57      * Verifies that the pointcut expression of the {@link ManagementAspect}
58      * does not match any method except the {@link IPluginExecutable#execute()} method.
59      */
60     @Test
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;
65
66         PointcutAdvisor pointcutAdvisor = PluginUtils.getPointcutAdvisor(advised);
67         assertNotNull("PointcutAdvisor not found because there is no pointcut or the pointcut does not match", pointcutAdvisor);
68
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());
73     }
74
75     /**
76      * Tests if the {@link ManagementAspect} interrupts the plugin after the given timeout.
77      */
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());
82         executable.execute();
83
84         List<Event> events = eventBus.getEvents();
85         assertEquals("EventBus must contain 2 events", 2, events.size());
86
87         long duration = events.get(1).getTime() - events.get(0).getTime();
88         assertTrue("Plugin was not interrupted 2 seconds after starting it", duration < 3000);
89     }
90 }