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.io.File;
22  
23  import org.apache.maven.api.plugin.testing.Basedir;
24  import org.apache.maven.api.plugin.testing.InjectMojo;
25  import org.apache.maven.api.plugin.testing.MojoExtension;
26  import org.apache.maven.api.plugin.testing.MojoParameter;
27  import org.apache.maven.api.plugin.testing.MojoTest;
28  import org.junit.jupiter.api.Nested;
29  import org.junit.jupiter.api.Test;
30  
31  import static org.junit.jupiter.api.Assertions.assertEquals;
32  import static org.junit.jupiter.api.Assertions.assertTrue;
33  
34  @MojoTest
35  @Basedir("class-basedir")
36  @MojoParameter(name = "plain", value = "class-value")
37  class AnnotationLevelMojoTest {
38  
39      private static final String FS = File.separator;
40  
41      @Test
42      @InjectMojo(goal = "parameters")
43      void classLevelValues(ParametersMojo mojo) {
44          assertEquals("class-value", mojo.getPlain());
45          assertTrue(
46                  MojoExtension.getBasedir().endsWith(FS + "class-basedir"),
47                  "Basedir value did not came from class annotation");
48      }
49  
50      @Test
51      @InjectMojo(goal = "parameters")
52      @Basedir("method-basedir")
53      @MojoParameter(name = "plain", value = "method-value")
54      void methodLevelValues(ParametersMojo mojo) {
55          assertEquals("method-value", mojo.getPlain());
56          assertTrue(
57                  MojoExtension.getBasedir().endsWith(FS + "method-basedir"),
58                  "Basedir value did not came from method annotation");
59      }
60  
61      @Test
62      void parameterLevelValues(
63              @InjectMojo(goal = "parameters") @MojoParameter(name = "plain", value = "param-level-param-value")
64                      ParametersMojo mojo) {
65          assertEquals("param-level-param-value", mojo.getPlain());
66      }
67  
68      @Test
69      @MojoParameter(name = "plain", value = "method-value")
70      void mojoParameterOnMethod(
71              @InjectMojo(goal = "parameters") ParametersMojo mojo,
72              @InjectMojo(goal = "parameters") @MojoParameter(name = "plain", value = "param-value")
73                      ParametersMojo alternateMojo) {
74          assertEquals("method-value", mojo.getPlain());
75          assertEquals("param-value", alternateMojo.getPlain());
76      }
77  
78      @Nested
79      class NestedTest {
80          // all tests are duplicated from parent class
81  
82          protected String nestedAnnotationValue() {
83              return "";
84          }
85  
86          @Test
87          @InjectMojo(goal = "parameters")
88          void classLevelValues(ParametersMojo mojo) {
89              assertEquals(nestedAnnotationValue() + "class-value", mojo.getPlain());
90              assertTrue(
91                      MojoExtension.getBasedir().endsWith(FS + nestedAnnotationValue() + "class-basedir"),
92                      "Basedir value did not came from class annotation: " + MojoExtension.getBasedir());
93          }
94  
95          @Test
96          @InjectMojo(goal = "parameters")
97          @Basedir("method-basedir")
98          @MojoParameter(name = "plain", value = "method-value")
99          void methodLevelValues(ParametersMojo mojo) {
100             assertEquals("method-value", mojo.getPlain());
101             assertTrue(
102                     MojoExtension.getBasedir().endsWith(FS + "method-basedir"),
103                     "Basedir value did not came from method annotation");
104         }
105 
106         @Test
107         void parameterLevelValues(
108                 @InjectMojo(goal = "parameters") @MojoParameter(name = "plain", value = "param-level-param-value")
109                         ParametersMojo mojo) {
110             assertEquals("param-level-param-value", mojo.getPlain());
111         }
112 
113         @Test
114         @MojoParameter(name = "plain", value = "method-value")
115         void mojoParameterOnMethod(
116                 @InjectMojo(goal = "parameters") ParametersMojo mojo,
117                 @InjectMojo(goal = "parameters") @MojoParameter(name = "plain", value = "param-value")
118                         ParametersMojo alternateMojo) {
119             assertEquals("method-value", mojo.getPlain());
120             assertEquals("param-value", alternateMojo.getPlain());
121         }
122     }
123 
124     @Nested
125     @Basedir("nested-class-basedir")
126     @MojoParameter(name = "plain", value = "nested-class-value")
127     class NestedAnnotationLevelTest extends NestedTest {
128 
129         @Override
130         protected String nestedAnnotationValue() {
131             return "nested-";
132         }
133     }
134 }