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