-
Notifications
You must be signed in to change notification settings - Fork 85
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
Changes from 5 commits
3a3475c
f6182a8
9d20c9a
d4a4754
18c6a9e
43bdb48
350517f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package org.openmrs.maven.plugins; | ||
|
||
import lombok.Getter; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.apache.maven.artifact.factory.ArtifactFactory; | ||
import org.apache.maven.artifact.metadata.ArtifactMetadataSource; | ||
|
@@ -12,14 +13,15 @@ | |
import org.apache.maven.plugins.annotations.Parameter; | ||
import org.apache.maven.project.MavenProject; | ||
import org.apache.maven.settings.Settings; | ||
import org.openmrs.maven.plugins.git.DefaultGitHelper; | ||
import org.openmrs.maven.plugins.git.GitHelper; | ||
import org.openmrs.maven.plugins.model.Server; | ||
import org.openmrs.maven.plugins.utility.ConfigurationInstaller; | ||
import org.openmrs.maven.plugins.utility.DefaultJira; | ||
import org.openmrs.maven.plugins.utility.DistroHelper; | ||
import org.openmrs.maven.plugins.git.DefaultGitHelper; | ||
import org.openmrs.maven.plugins.git.GitHelper; | ||
import org.openmrs.maven.plugins.utility.DockerHelper; | ||
import org.openmrs.maven.plugins.utility.Jira; | ||
import org.openmrs.maven.plugins.utility.MavenEnvironment; | ||
import org.openmrs.maven.plugins.utility.ModuleInstaller; | ||
import org.openmrs.maven.plugins.utility.NodeHelper; | ||
import org.openmrs.maven.plugins.utility.OwaHelper; | ||
|
@@ -140,10 +142,17 @@ public abstract class AbstractTask extends AbstractMojo { | |
*/ | ||
DockerHelper dockerHelper; | ||
|
||
/** | ||
* provides access to the current Maven environment | ||
*/ | ||
@Getter | ||
private MavenEnvironment mavenEnvironment; | ||
|
||
public AbstractTask() { | ||
} | ||
|
||
public AbstractTask(AbstractTask other) { | ||
this.mavenEnvironment = other.mavenEnvironment; | ||
this.mavenProject = other.mavenProject; | ||
this.mavenSession = other.mavenSession; | ||
this.wizard = other.wizard; | ||
|
@@ -155,6 +164,7 @@ public AbstractTask(AbstractTask other) { | |
this.distroHelper = other.distroHelper; | ||
this.owaHelper = other.owaHelper; | ||
this.spaInstaller = other.spaInstaller; | ||
this.configurationInstaller = other.configurationInstaller; | ||
this.gitHelper = other.gitHelper; | ||
this.dockerHelper = other.dockerHelper; | ||
this.settings = other.settings; | ||
|
@@ -166,6 +176,16 @@ public AbstractTask(AbstractTask other) { | |
} | ||
|
||
public void initTask() { | ||
if (mavenEnvironment == null) { | ||
mavenEnvironment = new MavenEnvironment(); | ||
mavenEnvironment.setMavenProject(mavenProject); | ||
mavenEnvironment.setMavenSession(mavenSession); | ||
mavenEnvironment.setSettings(settings); | ||
mavenEnvironment.setArtifactMetadataSource(artifactMetadataSource); | ||
mavenEnvironment.setArtifactFactory(artifactFactory); | ||
mavenEnvironment.setPluginManager(pluginManager); | ||
mavenEnvironment.setWizard(wizard); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the new |
||
if (jira == null) { | ||
jira = new DefaultJira(); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package org.openmrs.maven.plugins; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.plugins.annotations.Mojo; | ||
import org.apache.maven.plugins.annotations.Parameter; | ||
import org.openmrs.maven.plugins.utility.MavenEnvironment; | ||
import org.openmrs.maven.plugins.utility.Wizard; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* The purpose is this Mojo is to support testing of helper utilities that are used by the various goals. This will: | ||
* instantiate a new object of type className | ||
* if this class has a setter property for a MavenExecution, this will be set | ||
* it will then invoke the given testMethod on the testClass | ||
*/ | ||
@Mojo(name = InvokeMethod.NAME) | ||
@Getter @Setter | ||
public class InvokeMethod extends AbstractTask { | ||
|
||
public static final String NAME = "invoke-method"; | ||
|
||
@Parameter(property = "className") | ||
String className; | ||
|
||
@Parameter(property = "methodName") | ||
String methodName; | ||
|
||
public void executeTask() throws MojoExecutionException { | ||
|
||
if (StringUtils.isBlank(className) || StringUtils.isBlank(methodName)) { | ||
throw new MojoExecutionException("You must supply both a className and methodName parameter"); | ||
} | ||
|
||
Wizard wizard = getMavenEnvironment().getWizard(); | ||
|
||
wizard.showMessage("Invoking: " + className + ":" + methodName); | ||
|
||
Class<?> clazz; | ||
try { | ||
clazz = getClass().getClassLoader().loadClass(className); | ||
} | ||
catch (ClassNotFoundException e) { | ||
throw new MojoExecutionException("Unable to load class", e); | ||
} | ||
wizard.showMessage("Class " + clazz.getName() + " loaded successfully"); | ||
|
||
Object instance; | ||
try { | ||
instance = clazz.getConstructor().newInstance(); | ||
} catch (Exception e) { | ||
throw new MojoExecutionException("Unable to instantiate class with " + getClass(), e); | ||
} | ||
wizard.showMessage("New instance of " + clazz.getName() + " instantiated"); | ||
|
||
Method setterMethod = null; | ||
try { | ||
setterMethod = clazz.getMethod("set" + MavenEnvironment.class.getSimpleName(), MavenEnvironment.class); | ||
} | ||
catch (NoSuchMethodException ignored) { | ||
} | ||
|
||
if (setterMethod != null) { | ||
try { | ||
setterMethod.invoke(instance, getMavenEnvironment()); | ||
wizard.showMessage("Instance populated with maven environment"); | ||
} | ||
catch (Exception e) { | ||
throw new MojoExecutionException("Error executing method: " + methodName, e); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
Method method; | ||
try { | ||
method = clazz.getMethod(methodName); | ||
} | ||
catch (NoSuchMethodException e) { | ||
throw new MojoExecutionException("Unable to find method: " + methodName, e); | ||
} | ||
wizard.showMessage("Got test method: " + method.getName()); | ||
|
||
try { | ||
method.invoke(instance); | ||
} | ||
catch (Exception e) { | ||
throw new MojoExecutionException("Error executing method: " + methodName, e); | ||
} | ||
wizard.showMessage("Method: " + method.getName() + " invoked successfully"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.openmrs.maven.plugins; | ||
|
||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.junit.Before; | ||
import org.openmrs.maven.plugins.utility.MavenEnvironment; | ||
|
||
import java.io.File; | ||
|
||
@Getter @Setter | ||
public abstract class AbstractMavenIT extends AbstractSdkIT { | ||
|
||
MavenEnvironment mavenEnvironment = null; | ||
|
||
@Override | ||
@Before | ||
public void setup() throws Exception { | ||
super.setup(); | ||
mavenEnvironment = null; | ||
} | ||
|
||
@Override | ||
void addTestResources() throws Exception { | ||
includePomFile("invokeIT", "pom.xml"); | ||
} | ||
|
||
protected void executeTest(MavenTestFunction testFunction) throws Exception { | ||
StackTraceElement invoker = Thread.currentThread().getStackTrace()[2]; | ||
String className = invoker.getClassName(); | ||
String testMethod = invoker.getMethodName(); | ||
if (mavenEnvironment == null) { | ||
addTaskParam("className", className); | ||
addTaskParam("methodName", testMethod); | ||
addTaskParam(BATCH_ANSWERS, getAnswers()); | ||
addTaskParam("testMode", "true"); | ||
String plugin = resolveSdkArtifact(); | ||
verifier.executeGoal(plugin + ":" + InvokeMethod.NAME); | ||
} | ||
else { | ||
testFunction.executeTest(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
|
||
protected File getMavenTestDirectory() { | ||
return new File(mavenEnvironment.getMavenProject().getBuild().getDirectory()); | ||
} | ||
|
||
/** | ||
* Simple interface that encapsulates a test that should be evaluated by tests that use this Mojo | ||
*/ | ||
public interface MavenTestFunction { | ||
void executeTest() throws Exception; | ||
} | ||
} |
There was a problem hiding this comment.
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