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