diff --git a/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng8525MavenDIPlugin.java b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng8525MavenDIPlugin.java new file mode 100644 index 000000000000..f3b4e7379032 --- /dev/null +++ b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng8525MavenDIPlugin.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.it; + +import java.io.File; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * This is a test set for MNG-8525. + * + */ +public class MavenITmng8525MavenDIPlugin extends AbstractMavenIntegrationTestCase { + + private File testDir; + + public MavenITmng8525MavenDIPlugin() { + super("[4.0.0-rc-2,)"); + } + + @BeforeEach + public void setUp() throws Exception { + testDir = extractResources("/mng-8525-maven-di-plugin"); + } + + @Test + public void testMavenDIPlugin() throws Exception { + // + // Build a plugin that uses a Maven DI plugin + // + Verifier v0 = newVerifier(testDir.getAbsolutePath()); + v0.setAutoclean(false); + v0.deleteDirectory("target"); + v0.deleteArtifacts("org.apache.maven.plugins"); + v0.addCliArgument("install"); + v0.execute(); + v0.verifyErrorFreeLog(); + + // + // Execute the Maven DI plugin + // + Verifier v1 = newVerifier(testDir.getAbsolutePath()); + v1.setAutoclean(false); + v1.addCliArgument("org.apache.maven.plugins:mavendi-maven-plugin:0.0.1-SNAPSHOT:hello"); + v1.execute(); + v1.verifyErrorFreeLog(); + v1.verifyTextInLog("Hello! I am a component that is being used via field injection!"); + } +} diff --git a/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/pom.xml b/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/pom.xml new file mode 100644 index 000000000000..b9895d993892 --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/pom.xml @@ -0,0 +1,177 @@ + + + + 4.1.0 + + org.apache.maven.its + mavendi-maven-plugin + 0.0.1-SNAPSHOT + maven-plugin + + mavendi-maven-plugin Maven Plugin + http://maven.apache.org + + + UTF-8 + 17 + 6.0.0 + 4.0.0-beta-5 + 4.0.0-beta-1 + 4.0.0-beta-2 + 2.0.2 + + + + + org.apache.maven + maven-api-core + ${mavenVersion} + provided + + + org.apache.maven + maven-api-di + ${mavenVersion} + provided + + + org.apache.maven + maven-api-meta + ${mavenVersion} + provided + + + + org.apache.maven + maven-core + ${mavenVersion} + test + + + org.apache.maven.resolver + maven-resolver-api + ${mavenResolverVersion} + test + + + org.apache.maven + maven-api-impl + ${mavenVersion} + test + + + com.google.inject + guice + ${guiceVersion} + test + + + org.apache.maven.plugin-testing + maven-plugin-testing-harness + ${mavenPluginTestingVersion} + test + + + org.junit.jupiter + junit-jupiter-api + test + + + org.mockito + mockito-core + test + + + org.mockito + mockito-junit-jupiter + test + + + org.assertj + assertj-core + 3.27.2 + test + + + + + + + org.junit + junit-bom + 5.11.4 + pom + import + + + org.mockito + mockito-bom + 5.15.2 + pom + import + + + + + + + + org.apache.maven.plugins + maven-plugin-plugin + ${mavenPluginPluginVersion} + + + generate-helpmojo + + helpmojo + + + + + + + + + + run-its + + + + org.apache.maven.plugins + maven-invoker-plugin + 3.6.1 + + true + ${project.build.directory}/it + true + + */pom.xml + + setup + verify + ${project.build.directory}/local-repo + src/it/settings.xml + + ${project.build.directory}/remote-repo + + + ${project.build.directory}/remote-repo + + + + + integration-test + + install + integration-test + verify + + + + + + + + + diff --git a/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/src/it/hello-maven-plugin/invoker.properties b/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/src/it/hello-maven-plugin/invoker.properties new file mode 100644 index 000000000000..1c0f90042fd8 --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/src/it/hello-maven-plugin/invoker.properties @@ -0,0 +1,17 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +invoker.goals=initialize diff --git a/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/src/it/hello-maven-plugin/pom.xml b/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/src/it/hello-maven-plugin/pom.xml new file mode 100644 index 000000000000..c04601eb0ca7 --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/src/it/hello-maven-plugin/pom.xml @@ -0,0 +1,51 @@ + + + + 4.1.0 + + org.apache.maven.plugins.mvn4.its + mvn4-plugin-demo + 1.0 + + Test a mvn4 plugin + + + + + org.apache.maven.its + mavendi-maven-plugin + @project.version@ + + World + + + + initialize + + hello + + + + + + + + diff --git a/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/src/it/hello-maven-plugin/verify.groovy b/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/src/it/hello-maven-plugin/verify.groovy new file mode 100644 index 000000000000..593d72f8cbbd --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/src/it/hello-maven-plugin/verify.groovy @@ -0,0 +1,22 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +File buildLog = new File(basedir, 'build.log') +assert buildLog.exists() +assert buildLog.text.contains("[INFO] Hello World! I am a component that is being used via field injection!") diff --git a/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/src/it/settings.xml b/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/src/it/settings.xml new file mode 100644 index 000000000000..7fe15992d84a --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/src/it/settings.xml @@ -0,0 +1,69 @@ + + + + + + + + it-repo + + true + + + + local.central + @localRepositoryUrl@ + + true + ignore + + + true + ignore + + + + + + local.central + @localRepositoryUrl@ + + true + ignore + + + true + ignore + + + + + false + + + true + + apache.snapshots + https://repository.apache.org/snapshots/ + + + + + diff --git a/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/src/main/java/org/apache/maven/plugins/HelloMojo.java b/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/src/main/java/org/apache/maven/plugins/HelloMojo.java new file mode 100644 index 000000000000..a8a4e5476fc9 --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/src/main/java/org/apache/maven/plugins/HelloMojo.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.plugins; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.maven.api.Lifecycle.Phase; +import org.apache.maven.api.di.Inject; +import org.apache.maven.api.plugin.Log; +import org.apache.maven.api.plugin.annotations.Mojo; +import org.apache.maven.api.plugin.annotations.Parameter; + +@Mojo(name = "hello", defaultPhase = Phase.VALIDATE, projectRequired = false) +public class HelloMojo implements org.apache.maven.api.plugin.Mojo { + + @Inject + private MavenDIComponent component; + + @Inject + private Log logger; + + @Parameter(property = "name") + private String name; + + public void execute() { + // + // Say hello to the world, my little constructor injected component! + // + String message = component.hello(name); + logger.info(message); + } + +} diff --git a/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/src/main/java/org/apache/maven/plugins/MavenDIComponent.java b/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/src/main/java/org/apache/maven/plugins/MavenDIComponent.java new file mode 100644 index 000000000000..7dbb5acbe5c4 --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/src/main/java/org/apache/maven/plugins/MavenDIComponent.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.plugins; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.maven.api.di.Named; +import org.apache.maven.api.di.Singleton; + +@Named +@Singleton +public class MavenDIComponent { + + public String hello(String name) { + return "Hello " + name + "! I am a component that is being used via field injection!"; + } +} diff --git a/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/src/test/java/org/apache/maven/plugins/HelloMojoTest.java b/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/src/test/java/org/apache/maven/plugins/HelloMojoTest.java new file mode 100644 index 000000000000..4c1b3a280dc5 --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-8525-maven-di-plugin/src/test/java/org/apache/maven/plugins/HelloMojoTest.java @@ -0,0 +1,41 @@ +package org.apache.maven.plugins; + + +import org.apache.maven.api.di.Inject; +import org.apache.maven.api.di.Provides; +import org.apache.maven.api.di.Singleton; +import org.apache.maven.api.plugin.testing.InjectMojo; +import org.apache.maven.api.plugin.testing.MojoParameter; +import org.apache.maven.api.plugin.testing.MojoTest; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +@MojoTest +public class HelloMojoTest { + + @Inject + private MavenDIComponent componentMock; + + @Test + @InjectMojo(goal="hello") + @MojoParameter(name="name", value = "World") + public void testHello(HelloMojo mojoUnderTest) { + + mojoUnderTest.execute(); + + ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + verify(componentMock).hello(captor.capture()); + assertThat(captor.getValue()).isEqualTo("World"); + } + + @Singleton + @Provides + private MavenDIComponent createMavenDIComponent() { + return mock(MavenDIComponent.class); + } + +}