Skip to content

Conversation

@gnodet
Copy link
Contributor

@gnodet gnodet commented Nov 10, 2025

This PR enhances the @MojoParameter annotation to support injection of complex data types when unit testing mojos, with comprehensive test coverage.

Key Features

1. Added xml attribute to @MojoParameter annotation

  • xml=true (default): Parse value as XML content (existing behavior)
  • xml=false: Treat value as plain text, escaping XML special characters
  • Enables comma-separated lists and values with special characters

2. Updated MojoExtension to handle the xml flag

  • When xml=true, value is parsed as XML elements
  • When xml=false, value is escaped and treated as plain text
  • Properly handles special characters like <, >, &, etc.

3. Comprehensive unit tests (15 new tests)

Covering:

  • List<String> with XML format
  • List<String> with comma-separated format using xml=false
  • String[] arrays with both XML and comma-separated formats
  • Map<String, String> with XML format
  • Properties with XML format
  • Custom bean objects with nested properties
  • Primitive types (int, boolean)
  • Special characters in values with xml=false

4. Fixed plugin.xml type declarations

  • Changed from java.lang.List&lt;java.lang.String&gt; (HTML-encoded)
  • To java.util.List (proper Maven plugin descriptor format)
  • Maven's plugin descriptor doesn't support parameterized types in <type>
  • Added proper type declarations for Map, Properties, arrays, and custom beans

Testing

All tests pass successfully (22 tests in maven-testing module).

Example Usage

// Comma-separated list
@Test
@InjectMojo(goal = "my-goal", pom = "pom.xml")
@MojoParameter(name = "items", value = "one,two,three", xml = false)
public void testCommaSeparatedList(MyMojo mojo) {
    assertEquals(List.of("one", "two", "three"), mojo.items);
}

// XML format (default)
@Test
@InjectMojo(goal = "my-goal", pom = "pom.xml")
@MojoParameter(name = "items", value = "<item>one</item><item>two</item>")
public void testXmlList(MyMojo mojo) {
    assertEquals(List.of("one", "two"), mojo.items);
}

// Complex bean
@Test
@InjectMojo(goal = "my-goal", pom = "pom.xml")
@MojoParameter(name = "config", value = "<host>localhost</host><port>8080</port>")
public void testBean(MyMojo mojo) {
    assertEquals("localhost", mojo.config.host);
    assertEquals(8080, mojo.config.port);
}

Pull Request opened by Augment Code with guidance from the PR author

@gnodet gnodet added the enhancement New feature or request label Nov 10, 2025
@gnodet gnodet added this to the 4.1.0 milestone Nov 10, 2025
…ting

Fixes apachegh-11427

This commit enhances the @MojoParameter annotation to support injection of
complex data types when unit testing mojos, with comprehensive test coverage.

Key features:

1. Added xml attribute to @MojoParameter annotation:
   - xml=true (default): Parse value as XML content (existing behavior)
   - xml=false: Treat value as plain text, escaping XML special characters
   - Enables comma-separated lists and values with special characters

2. Updated MojoExtension to handle the xml flag:
   - When xml=true, value is parsed as XML elements
   - When xml=false, value is escaped and treated as plain text
   - Properly handles special characters like <, >, &, etc.

3. Added comprehensive unit tests (15 new tests) covering:
   - List<String> with XML format
   - List<String> with comma-separated format using xml=false
   - String arrays with both XML and comma-separated formats
   - Map<String, String> with XML format
   - Properties with XML format
   - Custom bean objects with nested properties
   - Primitive types (int, boolean)
   - Special characters in values with xml=false

4. Fixed plugin.xml type declarations:
   - Changed from java.lang.List&lt;java.lang.String&gt; (HTML-encoded)
   - To java.util.List (proper Maven plugin descriptor format)
   - Maven's plugin descriptor doesn't support parameterized types in <type>
   - Added proper type declarations for Map, Properties, arrays, and custom beans

All tests pass successfully (22 tests in maven-testing module).
@gnodet gnodet force-pushed the feature/testing-complex-mojo-params branch from 41117ba to ce70464 Compare November 10, 2025 18:37
@gnodet gnodet requested a review from desruisseaux November 18, 2025 15:22
.replace("'", "&apos;");
s = "<" + mp.name() + ">" + escapedValue + "</" + mp.name() + ">";
}
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a small details (not really important), but the code could be factorized a little bit:

String value = mp.value();
if (!mp.xml()) {
    // Treat as plain text - escape XML special characters
    value = value
            .replace("&", "&amp;")
            .replace("<", "&lt;")
            .replace(">", "&gt;")
            .replace("\"", "&quot;")
            .replace("'", "&apos;");
}
String s = '<' + mp.name() + '>' + value + "</" + mp.name() + '>';

I also replaced strings of 1 character by single characters.

@gnodet gnodet merged commit 36c4d10 into apache:master Nov 26, 2025
41 of 42 checks passed
@gnodet gnodet deleted the feature/testing-complex-mojo-params branch November 26, 2025 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants