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 javax.inject.Inject;
22  
23  import java.io.File;
24  
25  import org.apache.maven.api.plugin.testing.InjectMojo;
26  import org.apache.maven.api.plugin.testing.MojoExtension;
27  import org.apache.maven.api.plugin.testing.MojoTest;
28  import org.apache.maven.execution.MavenSession;
29  import org.apache.maven.plugin.MojoExecution;
30  import org.apache.maven.project.MavenProject;
31  import org.junit.jupiter.api.BeforeEach;
32  import org.junit.jupiter.api.Test;
33  
34  import static org.junit.jupiter.api.Assertions.assertEquals;
35  import static org.junit.jupiter.api.Assertions.assertFalse;
36  import static org.junit.jupiter.api.Assertions.assertNotNull;
37  import static org.junit.jupiter.api.Assertions.assertSame;
38  
39  @MojoTest
40  public class ProvidesInjectMojoTest {
41  
42      private static final String POM = "<project>" + "</project>";
43  
44      @Inject
45      private MavenSession session;
46  
47      @Inject
48      private MavenProject project;
49  
50      @Inject
51      private MojoExecution mojoExecution;
52  
53      @BeforeEach
54      void setup() {
55          assertEquals(new File(MojoExtension.getBasedir()), project.getBasedir());
56      }
57  
58      @Test
59      @InjectMojo(pom = POM, goal = "test:test-plugin:0.0.1-SNAPSHOT:provides")
60      public void bennShouldBeInjected(ProvidesInjectMojo mojo) {
61          assertNotNull(mojo);
62          assertSame(session, mojo.getSession());
63          assertSame(session, mojo.getSessionFromBean());
64  
65          assertSame(project, mojo.getProject());
66          assertSame(project, mojo.getProjectFromBean());
67  
68          assertSame(mojoExecution, mojo.getMojoExecution());
69          assertSame(mojoExecution, mojo.getMojoExecutionFromBean());
70      }
71  
72      @Test
73      @InjectMojo(pom = POM, goal = "test:test-plugin:0.0.1-SNAPSHOT:provides")
74      public void defaultValuesShouldBeProvided(ProvidesInjectMojo mojo) {
75          assertNotNull(mojo);
76          assertNotNull(mojo.getProject().getBasedir());
77          assertNotNull(mojo.getProject().getBuild().getOutputDirectory());
78          assertNotNull(mojo.getProject().getBuild().getTestOutputDirectory());
79          assertFalse(mojo.getProject().getBuild().getResources().isEmpty());
80          assertFalse(mojo.getProject().getBuild().getTestResources().isEmpty());
81          assertFalse(mojo.getProject().getCompileSourceRoots().isEmpty());
82          assertFalse(mojo.getProject().getTestCompileSourceRoots().isEmpty());
83          assertFalse(mojo.getProject().getResources().isEmpty());
84          assertFalse(mojo.getProject().getTestResources().isEmpty());
85      }
86  }