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.api.plugin.testing;
20  
21  import javax.inject.Named;
22  
23  import java.nio.file.Paths;
24  import java.util.Properties;
25  
26  import com.google.inject.Provides;
27  import org.apache.maven.api.Session;
28  import org.apache.maven.api.plugin.MojoException;
29  import org.apache.maven.api.plugin.testing.stubs.SessionStub;
30  import org.junit.jupiter.api.Test;
31  
32  import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
33  import static org.junit.jupiter.api.Assertions.assertEquals;
34  import static org.junit.jupiter.api.Assertions.assertNotNull;
35  import static org.mockito.Mockito.doAnswer;
36  import static org.mockito.Mockito.doReturn;
37  
38  /**
39   * @author Edwin Punzalan
40   */
41  @MojoTest
42  public class ExpressionEvaluatorTest {
43  
44      private static final String LOCAL_REPO = "target/local-repo/";
45      private static final String ARTIFACT_ID = "maven-test-mojo";
46      private static final String COORDINATES = "groupId:" + ARTIFACT_ID + ":version:goal";
47      private static final String CONFIG = "<project>\n"
48              + "    <build>\n"
49              + "        <plugins>\n"
50              + "            <plugin>\n"
51              + "                <artifactId>" + ARTIFACT_ID + "</artifactId>\n"
52              + "                <configuration>\n"
53              + "                    <basedir>${basedir}</basedir>\n"
54              + "                    <workdir>${basedir}/workDirectory</workdir>\n"
55              + "                </configuration>\n"
56              + "            </plugin>\n"
57              + "        </plugins>\n"
58              + "    </build>\n"
59              + "</project>\n";
60  
61      @Test
62      @InjectMojo(goal = COORDINATES, pom = CONFIG)
63      public void testInjection(ExpressionEvaluatorMojo mojo) {
64          assertNotNull(mojo.basedir);
65          assertNotNull(mojo.workdir);
66          assertDoesNotThrow(mojo::execute);
67      }
68  
69      @Test
70      @InjectMojo(goal = COORDINATES, pom = CONFIG)
71      @MojoParameter(name = "param", value = "paramValue")
72      public void testParam(ExpressionEvaluatorMojo mojo) {
73          assertNotNull(mojo.basedir);
74          assertNotNull(mojo.workdir);
75          assertEquals("paramValue", mojo.param);
76          assertDoesNotThrow(mojo::execute);
77      }
78  
79      @Test
80      @InjectMojo(goal = COORDINATES, pom = CONFIG)
81      @MojoParameter(name = "param", value = "paramValue")
82      @MojoParameter(name = "param2", value = "param2Value")
83      public void testParams(ExpressionEvaluatorMojo mojo) {
84          assertNotNull(mojo.basedir);
85          assertNotNull(mojo.workdir);
86          assertEquals("paramValue", mojo.param);
87          assertEquals("param2Value", mojo.param2);
88          assertDoesNotThrow(mojo::execute);
89      }
90  
91      @Named(COORDINATES)
92      public static class ExpressionEvaluatorMojo implements org.apache.maven.api.plugin.Mojo {
93          private String basedir;
94  
95          private String workdir;
96  
97          private String param;
98  
99          private String param2;
100 
101         /** {@inheritDoc} */
102         @Override
103         public void execute() throws MojoException {
104             if (basedir == null || basedir.isEmpty()) {
105                 throw new MojoException("basedir was not injected.");
106             }
107 
108             if (workdir == null || workdir.isEmpty()) {
109                 throw new MojoException("workdir was not injected.");
110             } else if (!workdir.startsWith(basedir)) {
111                 throw new MojoException("workdir does not start with basedir.");
112             }
113         }
114     }
115 
116     @Provides
117     @SuppressWarnings("unused")
118     Session session() {
119         Session session = SessionStub.getMockSession(LOCAL_REPO);
120         doReturn(new Properties()).when(session).getSystemProperties();
121         doReturn(new Properties()).when(session).getUserProperties();
122         doAnswer(iom -> Paths.get(MojoExtension.getBasedir())).when(session).getRootDirectory();
123         return session;
124     }
125 }