Skip to content

Commit

Permalink
[MNG-7587] Add an IT for jsr330 components (apache#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet authored Jun 2, 2023
1 parent 5e0613e commit 425b92d
Show file tree
Hide file tree
Showing 10 changed files with 389 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.apache.maven.it;

/*
* 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.shared.verifier.Verifier;
import org.apache.maven.shared.verifier.util.ResourceExtractor;
import org.codehaus.plexus.util.Os;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnJre;
import org.junit.jupiter.api.condition.JRE;

import java.io.File;

/**
* This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-7587">MNG-7587</a>.
* Simply verifies that plexus component using JSR330 and compiled with JDK 17 bytecode can
* work on maven.
*/
class MavenITmng7587Jsr330
extends AbstractMavenIntegrationTestCase
{

public MavenITmng7587Jsr330()
{
// affected Maven versions: 3.9.2 and 4.0.0-alpha-5
super( "(3.9.2,3.999),(4.0.0-alpha-5,)" );
}

/**
* Verify components can be written using JSR330 on JDK 17.
*
* @throws Exception in case of failure
*/
@Test
@EnabledOnJre(JRE.JAVA_17)
void testJdk17() throws Exception
{
File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-7587-jsr330").getAbsoluteFile();

final Verifier pluginVerifier = newVerifier( new File( testDir, "plugin" ).getPath() );
pluginVerifier.addCliArgument( "clean" );
pluginVerifier.addCliArgument( "install" );
pluginVerifier.addCliArgument( "-V" );
pluginVerifier.execute();
pluginVerifier.verifyErrorFreeLog();

final Verifier consumerVerifier = newVerifier( new File( testDir, "consumer" ).getPath() );
consumerVerifier.addCliArgument( "clean" );
consumerVerifier.addCliArgument( "verify" );
consumerVerifier.addCliArgument( "-V" );
consumerVerifier.execute();
consumerVerifier.verifyErrorFreeLog();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public TestSuiteOrdering()
* the tests are to finishing. Newer tests are also more likely to fail, so this is
* a fail fast technique as well.
*/
suite.addTestSuite( MavenITmng7587Jsr330.class );
suite.addTestSuite( MavenITmng7038RootdirTest.class );
suite.addTestSuite( MavenITmng7697PomWithEmojiTest.class );
suite.addTestSuite( MavenITmng7737ProfileActivationTest.class );
Expand Down
2 changes: 2 additions & 0 deletions its/core-it-suite/src/test/resources-filtered/bootstrap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ org.apache.maven.plugins:maven-plugin-plugin:${stubPluginVersion}
org.apache.maven.plugins:maven-plugin-plugin:3.2
org.apache.maven.plugins:maven-plugin-plugin:3.3
org.apache.maven.plugins:maven-plugin-plugin:3.6.0
org.apache.maven.plugins:maven-plugin-plugin:3.9.0
org.apache.maven.plugins:maven-rar-plugin:${stubPluginVersion}
org.apache.maven.plugins:maven-release-plugin:3.0.0-M5
org.apache.maven.plugins:maven-resources-plugin:${stubPluginVersion}
Expand Down Expand Up @@ -135,6 +136,7 @@ org.apache.maven:maven-resolver-provider:3.8.6
org.apache.maven:maven-script-ant:2.1.0
org.apache.maven:maven-settings-builder:3.1.1
org.apache.maven:maven-settings:3.1.1
org.apache.maven.plugin-testing:maven-plugin-testing-harness:3.3.0
org.codehaus.gmavenplus:gmavenplus-plugin:1.11.0
org.codehaus.mojo:build-helper-maven-plugin:3.2.0
org.codehaus.mojo:flatten-maven-plugin:1.0.0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.its.mng7587</groupId>
<artifactId>consumer</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.its.mng7587</groupId>
<artifactId>plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<id>touch</id>
<goals>
<goal>touch</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.its.mng7587</groupId>
<artifactId>plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<name>hello-maven-plugin Maven Plugin</name>

<prerequisites>
<maven>${maven.version}</maven>
</prerequisites>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.version>3.8.6</maven.version>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>${maven.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${maven.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.0</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
</plugin>
<plugin>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.9.0</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>17</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<executions>
<execution>
<id>mojo-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package sample.plugin;

import javax.inject.Named;

@Named
public class MyComponent implements MyService {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package sample.plugin;


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

/**
* Goal which touches a timestamp file.
*/
@Mojo( name = "touch", defaultPhase = LifecyclePhase.PROCESS_SOURCES )
public class MyMojo
extends AbstractMojo
{
/**
* Location of the file.
*/
@Parameter( defaultValue = "${project.build.directory}", property = "outputDir", required = true )
private File outputDirectory;

@Component
private MyService myService;

@Override
public void execute()
throws MojoExecutionException
{
File f = outputDirectory;

if ( !f.exists() )
{
f.mkdirs();
}

File touch = new File( f, "touch.txt" );

FileWriter w = null;
try
{
w = new FileWriter( touch );

w.write( "touch.txt" );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Error creating file " + touch, e );
}
finally
{
if ( w != null )
{
try
{
w.close();
}
catch ( IOException e )
{
// ignore
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package sample.plugin;

public interface MyService {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package sample.plugin;


import org.apache.maven.plugin.testing.MojoRule;
import org.apache.maven.plugin.testing.WithoutMojo;

import org.junit.Rule;
import static org.junit.Assert.*;
import org.junit.Test;
import java.io.File;

public class MyMojoTest
{
@Rule
public MojoRule rule = new MojoRule()
{
@Override
protected void before() throws Throwable
{
}

@Override
protected void after()
{
}
};

/**
* @throws Exception if any
*/
@Test
public void testSomething()
throws Exception
{
File pom = new File( "target/test-classes/project-to-test/" );
assertNotNull( pom );
assertTrue( pom.exists() );

MyMojo myMojo = ( MyMojo ) rule.lookupConfiguredMojo( pom, "touch" );
assertNotNull( myMojo );
myMojo.execute();

File outputDirectory = ( File ) rule.getVariableValueFromObject( myMojo, "outputDirectory" );
assertNotNull( outputDirectory );
assertTrue( outputDirectory.exists() );

File touch = new File( outputDirectory, "touch.txt" );
assertTrue( touch.exists() );

}

/** Do not need the MojoRule. */
@WithoutMojo
@Test
public void testSomethingWhichDoesNotNeedTheMojoAndProbablyShouldBeExtractedIntoANewClassOfItsOwn()
{
assertTrue( true );
}

}

Loading

0 comments on commit 425b92d

Please sign in to comment.