Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDK-358 - Add integration test framework for helper classes #308

Closed
wants to merge 7 commits into from

Conversation

mseaton
Copy link
Member

@mseaton mseaton commented Nov 18, 2024

@mseaton mseaton changed the title Sdk 358 1 SDK-358 - Explore whether integration tests can be created for helper classes Nov 18, 2024
@mseaton mseaton changed the title SDK-358 - Explore whether integration tests can be created for helper classes SDK-358 - Add integration test framework for helper classes Nov 18, 2024
</goals>
</execution>
</executions>
</plugin>
Copy link
Member Author

Choose a reason for hiding this comment

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

This adds all of the test classes to their own library, so this library can be added to the classpath of the pom used by the Maven verifier during the integration tests, which runs outside of this project

mavenEnvironment.setArtifactFactory(artifactFactory);
mavenEnvironment.setPluginManager(pluginManager);
mavenEnvironment.setWizard(wizard);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

See the new MavenEnvironment class below - basically this just allows us to encapsulate everything provided by the Maven process during plugin execution in a single object that can be passed around and depended upon.

}
else {
testFunction.executeTest();
}
Copy link
Member Author

Choose a reason for hiding this comment

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

So the general idea here, is that a particular integration test will wrap it's test functionality in this executeTest method. When this is run, the mavenEnvironment will be null, and this will trigger the new "InvokeMethod" plugin to run it, by creating a new instance of this same test class, and executing this same method, but this time with the mavenEnvironment populated from the InvokeMethod plugin.

File expectedFile = new File(getMavenTestDirectory(), "idgen-omod-4.14.0.jar");
assertTrue(expectedFile.exists());
});
}
Copy link
Member Author

Choose a reason for hiding this comment

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

And so this is an example of how we would run such a test. By wrapping the code in the executeTest block, it will cause the code in that block to be executed by a bare-bones plugin that exists only to execute the provided block.

Any new test libraries that are referenced by tests that are use this pom (eg. those that extend AbstractMavenIT)
need to be added as dependencies must be added here so the plugin can load and execute the tests successfully
See: https://maven.apache.org/guides/mini/guide-maven-classloading.html
-->
Copy link
Member Author

Choose a reason for hiding this comment

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

This took me the longest time to figure out, but finally was able to do so. See comment above.

/**
* The purpose of this class is to handle all interactions with Maven that require retrieving artifacts from the Maven repository
*/
public class ArtifactHelper {
Copy link
Member Author

Choose a reason for hiding this comment

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

One of my goals that led me to doing this in the first place was to start identifying functionality that we might refactor into common libraries. This is the start of that effort, and the integration test that corresponds with it is necessary to make sure it is operating as expected.

/**
* Component that allows access to the Maven components set within the current execution environment
*/
@Data
Copy link
Member Author

Choose a reason for hiding this comment

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

I love lombok, we should use it everywhere.

catch (Exception e) {
throw new MojoExecutionException("Error executing method: " + methodName, e);
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This code is admittedly ugly. I probably should have just found a library to do this all more elegantly. In any event, this plugin will instantiate the given class, set the mavenEnvironment property on it if available, and the call the given method. This is what allows us to test functionality that requires a Maven Environment outside of individual plugins that consume that functionality (aside from this one, which is necessary).

Base automatically changed from SDK-357 to master November 18, 2024 17:03
@mseaton mseaton closed this Nov 19, 2024
@mseaton mseaton deleted the SDK-358-1 branch November 20, 2024 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant