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.project;
20  
21  import org.apache.maven.model.Build;
22  import org.apache.maven.model.Model;
23  import org.apache.maven.model.Plugin;
24  import org.junit.jupiter.api.Test;
25  
26  import static org.junit.jupiter.api.Assertions.assertEquals;
27  import static org.junit.jupiter.api.Assertions.assertNotNull;
28  
29  /**
30   * Simple test to verify that Plugin objects returned by MavenProject.getPlugin() are connected to the project model.
31   * This test specifically verifies the fix for the <a href="https://github.com/apache/maven/pull/2530">issue</a> where
32   * getPlugin() was returning disconnected Plugin objects.
33   */
34  class PluginConnectionSimpleTest {
35  
36      @Test
37      void testPluginModificationPersistsInModel() {
38          // Create a test project with a plugin
39          Model model = new Model();
40          model.setGroupId("test.group");
41          model.setArtifactId("test-artifact");
42          model.setVersion("1.0.0");
43  
44          Build build = new Build();
45          model.setBuild(build);
46  
47          // Add a test plugin
48          Plugin originalPlugin = new Plugin();
49          originalPlugin.setGroupId("org.apache.maven.plugins");
50          originalPlugin.setArtifactId("maven-compiler-plugin");
51          originalPlugin.setVersion("3.8.1");
52          build.addPlugin(originalPlugin);
53  
54          MavenProject project = new MavenProject(model);
55  
56          // Get the plugin using getPlugin() method
57          Plugin retrievedPlugin = project.getPlugin("org.apache.maven.plugins:maven-compiler-plugin");
58          assertNotNull(retrievedPlugin, "Plugin should be found");
59          assertEquals("3.8.1", retrievedPlugin.getVersion(), "Initial version should match");
60  
61          // Modify the plugin version
62          retrievedPlugin.setVersion("3.11.0");
63  
64          // Verify the change persists when getting the plugin again
65          Plugin pluginAfterModification = project.getPlugin("org.apache.maven.plugins:maven-compiler-plugin");
66          assertEquals(
67                  "3.11.0",
68                  pluginAfterModification.getVersion(),
69                  "Version change should persist - this verifies the plugin is connected to the model");
70  
71          // Also verify the change is reflected in the build plugins list
72          Plugin pluginFromBuildList = project.getBuild().getPlugins().stream()
73                  .filter(p -> "org.apache.maven.plugins:maven-compiler-plugin".equals(p.getKey()))
74                  .findFirst()
75                  .orElse(null);
76          assertNotNull(pluginFromBuildList, "Plugin should be found in build plugins list");
77          assertEquals(
78                  "3.11.0", pluginFromBuildList.getVersion(), "Version change should be reflected in build plugins list");
79      }
80  
81      @Test
82      void testPluginConnectionBeforeAndAfterFix() {
83          // This test demonstrates the difference between the old broken behavior and the new fixed behavior
84  
85          Model model = new Model();
86          model.setGroupId("test.group");
87          model.setArtifactId("test-artifact");
88          model.setVersion("1.0.0");
89  
90          Build build = new Build();
91          model.setBuild(build);
92  
93          Plugin originalPlugin = new Plugin();
94          originalPlugin.setGroupId("org.apache.maven.plugins");
95          originalPlugin.setArtifactId("maven-surefire-plugin");
96          originalPlugin.setVersion("2.22.2");
97          build.addPlugin(originalPlugin);
98  
99          MavenProject project = new MavenProject(model);
100 
101         // The old broken implementation would have done:
102         // var plugin = getBuild().getDelegate().getPluginsAsMap().get(pluginKey);
103         // return plugin != null ? new Plugin(plugin) : null;
104         // This would create a disconnected Plugin that doesn't persist changes.
105 
106         // The new fixed implementation does:
107         // Find the plugin in the connected plugins list
108         Plugin connectedPlugin = project.getPlugin("org.apache.maven.plugins:maven-surefire-plugin");
109         assertNotNull(connectedPlugin, "Plugin should be found");
110 
111         // Test that modifications persist (this would fail with the old implementation)
112         connectedPlugin.setVersion("3.0.0-M7");
113 
114         Plugin pluginAfterChange = project.getPlugin("org.apache.maven.plugins:maven-surefire-plugin");
115         assertEquals(
116                 "3.0.0-M7",
117                 pluginAfterChange.getVersion(),
118                 "Plugin modifications should persist - this proves the fix is working");
119     }
120 }