1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.apache.maven.execution.scope.internal;
16
17 import java.util.concurrent.atomic.AtomicInteger;
18
19 import org.apache.maven.execution.MojoExecutionEvent;
20 import org.apache.maven.execution.scope.WeakMojoExecutionListener;
21 import org.apache.maven.plugin.MojoExecutionException;
22
23 import junit.framework.TestCase;
24
25 import com.google.inject.Key;
26 import com.google.inject.Provider;
27
28 public class MojoExecutionScopeTest
29 extends TestCase
30 {
31 public void testNestedEnter()
32 throws Exception
33 {
34 MojoExecutionScope scope = new MojoExecutionScope();
35
36 scope.enter();
37
38 Object o1 = new Object();
39 scope.seed( Object.class, o1 );
40 assertSame( o1, scope.scope( Key.get( Object.class ), null ).get() );
41
42 scope.enter();
43 Object o2 = new Object();
44 scope.seed( Object.class, o2 );
45 assertSame( o2, scope.scope( Key.get( Object.class ), null ).get() );
46
47 scope.exit();
48 assertSame( o1, scope.scope( Key.get( Object.class ), null ).get() );
49
50 scope.exit();
51
52 try
53 {
54 scope.exit();
55 fail();
56 }
57 catch ( IllegalStateException expected )
58 {
59 }
60 }
61
62 public void testMultiKeyInstance()
63 throws Exception
64 {
65 MojoExecutionScope scope = new MojoExecutionScope();
66 scope.enter();
67
68 final AtomicInteger beforeExecution = new AtomicInteger();
69 final AtomicInteger afterExecutionSuccess = new AtomicInteger();
70 final AtomicInteger afterExecutionFailure = new AtomicInteger();
71 final WeakMojoExecutionListener instance = new WeakMojoExecutionListener()
72 {
73 @Override
74 public void beforeMojoExecution( MojoExecutionEvent event )
75 throws MojoExecutionException
76 {
77 beforeExecution.incrementAndGet();
78 }
79
80 @Override
81 public void afterMojoExecutionSuccess( MojoExecutionEvent event )
82 throws MojoExecutionException
83 {
84 afterExecutionSuccess.incrementAndGet();
85 }
86
87 @Override
88 public void afterExecutionFailure( MojoExecutionEvent event )
89 {
90 afterExecutionFailure.incrementAndGet();
91 }
92 };
93 assertSame( instance, scope.scope( Key.get( Object.class ), new Provider<Object>()
94 {
95 @Override
96 public Object get()
97 {
98 return instance;
99 }
100 } ).get() );
101 assertSame( instance,
102 scope.scope( Key.get( WeakMojoExecutionListener.class ), new Provider<WeakMojoExecutionListener>()
103 {
104 @Override
105 public WeakMojoExecutionListener get()
106 {
107 return instance;
108 }
109 } ).get() );
110
111 final MojoExecutionEvent event = new MojoExecutionEvent( null, null, null, null );
112 scope.beforeMojoExecution( event );
113 scope.afterMojoExecutionSuccess( event );
114 scope.afterExecutionFailure( event );
115
116 assertEquals( 1, beforeExecution.get() );
117 assertEquals( 1, afterExecutionSuccess.get() );
118 assertEquals( 1, afterExecutionFailure.get() );
119
120 scope.exit();
121 }
122 }