Re: maven-plugin-test-harness

2025-12-28 Thread Slawomir Jaranowski
On Sat, 27 Dec 2025 at 00:11, Claude Warren  wrote:

> Sławomir,
>
> I seem to be a bit lost.  I am able to generate the test poms in testing
> directories.
>
> I am able to generate POMs and associated test files during the
> generate-test-sources phase.  My test code looks like:
>
>
> /**
>  * Generated test for config/withDefaults
>  */
> @Test
> @InjectMojo(goal = "check")
>
> @Basedir("target/test-classes/org/apache/rat/maven/stubs/config/withDefaults")
> public void configWithDefaultsTest(RatCheckMojo mojo) throws
> MojoExecutionException {
> TestData testData = testDataMap.get("config/withDefaults");
>
>
> testData.setupFiles(Paths.get("target/test-classes/org/apache/rat/maven/stubs/config/withDefaults"));
> if (testData.getExpectedException() != null) {
>
>
> assertThatThrownBy(mojo::execute).hasMessageContaining(testData.getExpectedException().getMessage());
> } else {
> mojo.execute();
> ValidatorData validatorData = new ValidatorData(mojo.getOutput(),
> "src/test/resources/org/apache/rat/maven/config/noDefaults");
> testData.getValidator().accept(validatorData);
> }
> }
>
>
>
> The RatCheckMojo has a parameter :
>
>
> /** The xml output file. */
> @Parameter(defaultValue = "${project.build.directory}/.rat.xml", readonly =
> true)
> protected File xmlOutputFile;
>
>
> When the mojo executes, the "xmlOutputFile" is set to
>
> target/test-classes/org/apache/rat/maven/stubs/config/withDefaults/${project.build.directory}/.rat.xml
> So my question is how can I get the ${project.*} properties populated
> inside the test mojo under test?
>

MavenProject created for testing is a mock, which does not have mocked all
methods 
https://github.com/apache/maven-plugin-testing/issues/245

You need add mocking for Maven project like:

@Inject
 MavenProject project;



 when(project.getBuild()).thenReturn(mock());
 
when(project.getBuild().getDirectory()).thenReturn("target/test-classes/projects/run/target");


>
> I am trying to maintain a maven implementation that will support Maven 3.x
>
> The pom in the
> target/test-classes/org/apache/rat/maven/stubs/config/withDefaults
> directory
> would not work unless I added a "" element.
> But this seems like it may be a bastardization halfway between the JUnit3
> and JUnit5 implementations.
>
> I am using
> 3.9.12
> 3.4.0
> 3.15.2
>
> The code is in [1].  The tools submodule generates the test code as well as
> a base abstract class for the mojos.
>
> [1]
>
> https://github.com/apache/creadur-rat/tree/feature/restructure/apache-rat-plugin-parent
>
> Any assistance would be appreciated.
>
> Claude
>


-- 
Sławomir Jaranowski


Re: maven-plugin-test-harness

2025-12-26 Thread Claude Warren
Sławomir,

I seem to be a bit lost.  I am able to generate the test poms in testing
directories.

I am able to generate POMs and associated test files during the
generate-test-sources phase.  My test code looks like:


/**
 * Generated test for config/withDefaults
 */
@Test
@InjectMojo(goal = "check")
@Basedir("target/test-classes/org/apache/rat/maven/stubs/config/withDefaults")
public void configWithDefaultsTest(RatCheckMojo mojo) throws
MojoExecutionException {
TestData testData = testDataMap.get("config/withDefaults");

testData.setupFiles(Paths.get("target/test-classes/org/apache/rat/maven/stubs/config/withDefaults"));
if (testData.getExpectedException() != null) {

assertThatThrownBy(mojo::execute).hasMessageContaining(testData.getExpectedException().getMessage());
} else {
mojo.execute();
ValidatorData validatorData = new ValidatorData(mojo.getOutput(),
"src/test/resources/org/apache/rat/maven/config/noDefaults");
testData.getValidator().accept(validatorData);
}
}



The RatCheckMojo has a parameter :


/** The xml output file. */
@Parameter(defaultValue = "${project.build.directory}/.rat.xml", readonly =
true)
protected File xmlOutputFile;


When the mojo executes, the "xmlOutputFile" is set to
target/test-classes/org/apache/rat/maven/stubs/config/withDefaults/${project.build.directory}/.rat.xml
So my question is how can I get the ${project.*} properties populated
inside the test mojo under test?

I am trying to maintain a maven implementation that will support Maven 3.x

The pom in the
target/test-classes/org/apache/rat/maven/stubs/config/withDefaults directory
would not work unless I added a "" element.
But this seems like it may be a bastardization halfway between the JUnit3
and JUnit5 implementations.

I am using
3.9.12
3.4.0
3.15.2

The code is in [1].  The tools submodule generates the test code as well as
a base abstract class for the mojos.

[1]
https://github.com/apache/creadur-rat/tree/feature/restructure/apache-rat-plugin-parent

Any assistance would be appreciated.

Claude


Re: maven-plugin-test-harness

2025-11-24 Thread Slawomir Jaranowski
On Mon, 24 Nov 2025 at 20:19, Claude Warren  wrote:

> Greetings,
>
> I am working on the Apache RAT Maven plugin.  We are attempting to migrate
> to Maven 3.4.0.
>
> I have a comment and then a question.
>
> Comment:
>
> https://maven.apache.org/plugin-testing/maven-plugin-testing-harness/getting-started/index.html
> states that the AbstractMojoTestCase is deprecated and that the "MojoTest"
> annotation should be used, but the example code at the bottom of the page
> still shows an AbstractMojoTestCase example.  I feel that this example
> should be replaced with how to make the MojoTest case work or at least more
> strongly point to the MojoTest case recipe.
>

You are right, the documentation is to fix, update.
More examples you can found in code tests:
https://github.com/apache/maven-plugin-testing/blob/maven-plugin-testing-3.x/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/ParametersMojoTest.java


> Question:
>
> The @MojoTest annotation allows use of the @InjectMojo annotation.  which
> will accept a POM as an argument to the annotation.  My problem with this,
> and my biggest issue with annotations, is that I need to specify the POM at
> runtime, not at compile time.
>
> In the RAT ecosystem, everything is first defined in the CLI.  The Maven
> implementation converts the CLI options into Maven options.  So when we add
> new CLI options the Maven implementation automatically picks them up.  We
> have the ability to generate the POMs to test the various options.  What I
> don't have is a way to specify the POM and get a constructed Mojo within a
> JUnit test environment.  I have not looked but I suspect the same is true
> for an integration test as well.
>
> So the question is: Is there a simple way to generate the Mojo from a POM
> within the setup of a JUnit 5 test?
>
> Any pointers would be appreciated, and thank you for all your hard work in
> building the Maven tooling environment.
>

You can try generate a pom in @BeforeEach method
When you set Basedir for a test it will be available in setup method

Please look at example in test BaseDirInBeforeEach



>
> Claude
>


-- 
Sławomir Jaranowski


maven-plugin-test-harness

2025-11-24 Thread Claude Warren
Greetings,

I am working on the Apache RAT Maven plugin.  We are attempting to migrate
to Maven 3.4.0.

I have a comment and then a question.

Comment:
https://maven.apache.org/plugin-testing/maven-plugin-testing-harness/getting-started/index.html
states that the AbstractMojoTestCase is deprecated and that the "MojoTest"
annotation should be used, but the example code at the bottom of the page
still shows an AbstractMojoTestCase example.  I feel that this example
should be replaced with how to make the MojoTest case work or at least more
strongly point to the MojoTest case recipe.

Question:

The @MojoTest annotation allows use of the @InjectMojo annotation.  which
will accept a POM as an argument to the annotation.  My problem with this,
and my biggest issue with annotations, is that I need to specify the POM at
runtime, not at compile time.

In the RAT ecosystem, everything is first defined in the CLI.  The Maven
implementation converts the CLI options into Maven options.  So when we add
new CLI options the Maven implementation automatically picks them up.  We
have the ability to generate the POMs to test the various options.  What I
don't have is a way to specify the POM and get a constructed Mojo within a
JUnit test environment.  I have not looked but I suspect the same is true
for an integration test as well.

So the question is: Is there a simple way to generate the Mojo from a POM
within the setup of a JUnit 5 test?

Any pointers would be appreciated, and thank you for all your hard work in
building the Maven tooling environment.

Claude