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.plugin.testing;
20  
21  import java.util.Properties;
22  
23  import org.apache.maven.api.di.Provides;
24  import org.apache.maven.api.plugin.testing.InjectMojo;
25  import org.apache.maven.api.plugin.testing.MojoTest;
26  import org.apache.maven.execution.MavenSession;
27  import org.apache.maven.plugin.MojoExecution;
28  import org.apache.maven.project.MavenProject;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.Nested;
31  import org.junit.jupiter.api.Test;
32  import org.junit.jupiter.api.extension.ExtendWith;
33  import org.mockito.Mock;
34  import org.mockito.junit.jupiter.MockitoExtension;
35  
36  import static org.junit.jupiter.api.Assertions.assertNotNull;
37  import static org.junit.jupiter.api.Assertions.assertSame;
38  import static org.mockito.Mockito.when;
39  
40  @ExtendWith(MockitoExtension.class)
41  @MojoTest
42  class ProvidesInjectOverrideMojoTest {
43  
44      private static final String POM = "<project></project>";
45  
46      @Mock
47      private MavenProject project;
48  
49      @Mock
50      private MavenSession session;
51  
52      @Mock
53      private MojoExecution mojoExecution;
54  
55      @BeforeEach
56      void setup() {
57          when(session.getUserProperties()).thenReturn(new Properties());
58          when(session.getSystemProperties()).thenReturn(new Properties());
59      }
60  
61      @Provides
62      public MavenProject mockMavenProject() {
63          return project;
64      }
65  
66      @Provides
67      public MavenSession mockMavenSession() {
68          return session;
69      }
70  
71      @Provides
72      MojoExecution mockMojoExecution() {
73          return mojoExecution;
74      }
75  
76      @Test
77      @InjectMojo(pom = POM, goal = "test:test-plugin:0.0.1-SNAPSHOT:provides")
78      void bennShouldBeInjected(ProvidesInjectMojo mojo) {
79          assertNotNull(mojo);
80          // session provided by the @Provides method should be used
81          assertSame(session, mojo.getSession());
82          assertSame(session, mojo.getSessionFromBean());
83  
84          assertSame(project, mojo.getProject());
85          assertSame(project, mojo.getProjectFromBean());
86  
87          assertSame(mojoExecution, mojo.getMojoExecution());
88          assertSame(mojoExecution, mojo.getMojoExecutionFromBean());
89      }
90  
91      @Nested
92      class NestedTest {
93  
94          @Mock
95          private MavenProject nestedProject;
96  
97          @Provides
98          public MavenProject mockMavenProject() {
99              return nestedProject;
100         }
101 
102         @Test
103         @InjectMojo(pom = POM, goal = "test:test-plugin:0.0.1-SNAPSHOT:provides")
104         void bennFromParentClassShouldBeInjected(ProvidesInjectMojo mojo) {
105 
106             assertNotNull(mojo);
107             // session provided by the @Provides method should be used
108             assertSame(session, mojo.getSession());
109             assertSame(session, mojo.getSessionFromBean());
110 
111             assertSame(nestedProject, mojo.getProject());
112             assertSame(nestedProject, mojo.getProjectFromBean());
113 
114             assertSame(mojoExecution, mojo.getMojoExecution());
115             assertSame(mojoExecution, mojo.getMojoExecutionFromBean());
116         }
117     }
118 }