View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional information regarding
4    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
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 }