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.execution.DefaultMavenExecutionRequest;
24  import org.apache.maven.execution.MavenExecutionRequest;
25  import org.apache.maven.execution.MavenSession;
26  import org.apache.maven.plugin.MojoExecution;
27  import org.apache.maven.project.MavenProject;
28  import org.apache.maven.project.ProjectBuilder;
29  import org.apache.maven.project.ProjectBuildingException;
30  import org.apache.maven.project.ProjectBuildingRequest;
31  import org.eclipse.aether.DefaultRepositorySystemSession;
32  
33  public class ParametersMojoTest extends AbstractMojoTestCase {
34      public void testDefault() throws Exception {
35          MavenProject project = readMavenProject(new File("src/test/projects/default"));
36  
37          ParametersMojo mojo = (ParametersMojo) lookupConfiguredMojo(project, "parameters");
38  
39          assertNull(mojo.plain);
40          assertNull(mojo.withProperty);
41          assertEquals("default", mojo.withDefault);
42          assertEquals("default", mojo.withPropertyAndDefault);
43      }
44  
45      public void testExplicit() throws Exception {
46          MavenProject project = readMavenProject(new File("src/test/projects/explicit"));
47  
48          ParametersMojo mojo = (ParametersMojo) lookupConfiguredMojo(project, "parameters");
49  
50          assertEquals("explicitValue", mojo.plain);
51          assertEquals("explicitWithPropertyValue", mojo.withProperty);
52          assertEquals("explicitWithDefaultValue", mojo.withDefault);
53          assertEquals("explicitWithPropertyAndDefaultValue", mojo.withPropertyAndDefault);
54      }
55  
56      public void testDefaultWithProperty() throws Exception {
57          MavenProject project = readMavenProject(new File("src/test/projects/default"));
58          MavenSession session = newMavenSession(project);
59          MojoExecution execution = newMojoExecution("parameters");
60  
61          session.getUserProperties().put("property", "propertyValue");
62          ParametersMojo mojo = (ParametersMojo) lookupConfiguredMojo(session, execution);
63  
64          assertNull(mojo.plain);
65          assertEquals("propertyValue", mojo.withProperty);
66          assertEquals("default", mojo.withDefault);
67          assertEquals("propertyValue", mojo.withPropertyAndDefault);
68      }
69  
70      public void testExplicitWithProperty() throws Exception {
71          MavenProject project = readMavenProject(new File("src/test/projects/explicit"));
72          MavenSession session = newMavenSession(project);
73          MojoExecution execution = newMojoExecution("parameters");
74  
75          session.getUserProperties().put("property", "propertyValue");
76          ParametersMojo mojo = (ParametersMojo) lookupConfiguredMojo(session, execution);
77  
78          assertEquals("explicitValue", mojo.plain);
79          assertEquals("explicitWithPropertyValue", mojo.withProperty);
80          assertEquals("explicitWithDefaultValue", mojo.withDefault);
81          assertEquals("explicitWithPropertyAndDefaultValue", mojo.withPropertyAndDefault);
82      }
83  
84      protected MavenProject readMavenProject(File basedir) throws ProjectBuildingException, Exception {
85          File pom = new File(basedir, "pom.xml");
86          MavenExecutionRequest request = new DefaultMavenExecutionRequest();
87          request.setBaseDirectory(basedir);
88          ProjectBuildingRequest configuration = request.getProjectBuildingRequest();
89          configuration.setRepositorySession(new DefaultRepositorySystemSession());
90          MavenProject project =
91                  lookup(ProjectBuilder.class).build(pom, configuration).getProject();
92          assertNotNull(project);
93          return project;
94      }
95  }