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.stubs;
20  
21  import java.util.Optional;
22  
23  import org.apache.maven.api.MojoExecution;
24  import org.apache.maven.api.Plugin;
25  import org.apache.maven.api.model.PluginExecution;
26  import org.apache.maven.api.plugin.descriptor.MojoDescriptor;
27  import org.apache.maven.api.xml.XmlNode;
28  
29  /**
30   * A stub implementation of {@link MojoExecution} for testing Maven plugins.
31   * This class provides a simplified representation of a Mojo execution context,
32   * allowing tests to simulate plugin executions without a full Maven environment.
33   *
34   * <p>Example usage:</p>
35   * <pre>
36   * MojoExecutionStub execution = new MojoExecutionStub("myExecution", "myGoal");
37   * execution.setPlugin(new PluginStub());
38   * execution.setDescriptor(mojoDescriptor);
39   * </pre>
40   *
41   * @see MojoExecution
42   * @see PluginStub
43   * @since 4.0.0
44   */
45  public class MojoExecutionStub implements MojoExecution {
46      private String executionId;
47      private String goal;
48      private XmlNode dom;
49      private Plugin plugin = new PluginStub();
50      private PluginExecution model;
51      private MojoDescriptor descriptor;
52      private String lifecyclePhase;
53  
54      public MojoExecutionStub(String executionId, String goal) {
55          this(executionId, goal, null);
56      }
57  
58      public MojoExecutionStub(String executionId, String goal, XmlNode dom) {
59          this.executionId = executionId;
60          this.goal = goal;
61          this.dom = dom;
62      }
63  
64      @Override
65      public Plugin getPlugin() {
66          return plugin;
67      }
68  
69      @Override
70      public PluginExecution getModel() {
71          return model;
72      }
73  
74      @Override
75      public MojoDescriptor getDescriptor() {
76          return descriptor;
77      }
78  
79      @Override
80      public String getLifecyclePhase() {
81          return lifecyclePhase;
82      }
83  
84      @Override
85      public String getExecutionId() {
86          return executionId;
87      }
88  
89      @Override
90      public String getGoal() {
91          return goal;
92      }
93  
94      @Override
95      public Optional<XmlNode> getConfiguration() {
96          return Optional.ofNullable(dom);
97      }
98  
99      public void setExecutionId(String executionId) {
100         this.executionId = executionId;
101     }
102 
103     public void setGoal(String goal) {
104         this.goal = goal;
105     }
106 
107     public void setDom(XmlNode dom) {
108         this.dom = dom;
109     }
110 
111     public void setPlugin(Plugin plugin) {
112         this.plugin = plugin;
113     }
114 
115     public void setModel(PluginExecution model) {
116         this.model = model;
117     }
118 
119     public void setDescriptor(MojoDescriptor descriptor) {
120         this.descriptor = descriptor;
121     }
122 
123     public void setLifecyclePhase(String lifecyclePhase) {
124         this.lifecyclePhase = lifecyclePhase;
125     }
126 }