1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.execution.scope.internal;
20
21 import java.util.concurrent.atomic.AtomicInteger;
22
23 import com.google.inject.Key;
24 import org.apache.maven.execution.MojoExecutionEvent;
25 import org.apache.maven.execution.scope.WeakMojoExecutionListener;
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.junit.jupiter.api.Test;
28
29 import static org.junit.jupiter.api.Assertions.assertEquals;
30 import static org.junit.jupiter.api.Assertions.assertSame;
31 import static org.junit.jupiter.api.Assertions.assertThrows;
32
33 class MojoExecutionScopeTest {
34 @Test
35 void testNestedEnter() throws Exception {
36 MojoExecutionScope scope = new MojoExecutionScope();
37
38 scope.enter();
39
40 Object o1 = new Object();
41 scope.seed(Object.class, o1);
42 assertSame(o1, scope.scope(Key.get(Object.class), null).get());
43
44 scope.enter();
45 Object o2 = new Object();
46 scope.seed(Object.class, o2);
47 assertSame(o2, scope.scope(Key.get(Object.class), null).get());
48
49 scope.exit();
50 assertSame(o1, scope.scope(Key.get(Object.class), null).get());
51
52 scope.exit();
53
54 assertThrows(IllegalStateException.class, () -> scope.exit());
55 }
56
57 @Test
58 void testMultiKeyInstance() throws Exception {
59 MojoExecutionScope scope = new MojoExecutionScope();
60 scope.enter();
61
62 final AtomicInteger beforeExecution = new AtomicInteger();
63 final AtomicInteger afterExecutionSuccess = new AtomicInteger();
64 final AtomicInteger afterExecutionFailure = new AtomicInteger();
65 final WeakMojoExecutionListener instance = new WeakMojoExecutionListener() {
66 @Override
67 public void beforeMojoExecution(MojoExecutionEvent event) throws MojoExecutionException {
68 beforeExecution.incrementAndGet();
69 }
70
71 @Override
72 public void afterMojoExecutionSuccess(MojoExecutionEvent event) throws MojoExecutionException {
73 afterExecutionSuccess.incrementAndGet();
74 }
75
76 @Override
77 public void afterExecutionFailure(MojoExecutionEvent event) {
78 afterExecutionFailure.incrementAndGet();
79 }
80 };
81 assertSame(instance, scope.scope(Key.get(Object.class), () -> instance).get());
82 assertSame(
83 instance,
84 scope.scope(Key.get(WeakMojoExecutionListener.class), () -> instance)
85 .get());
86
87 final MojoExecutionEvent event = new MojoExecutionEvent(null, null, null, null);
88 scope.beforeMojoExecution(event);
89 scope.afterMojoExecutionSuccess(event);
90 scope.afterExecutionFailure(event);
91
92 assertEquals(1, beforeExecution.get());
93 assertEquals(1, afterExecutionSuccess.get());
94 assertEquals(1, afterExecutionFailure.get());
95
96 scope.exit();
97 }
98 }