/maven-amps-plugin/src/main/java/com/atlassian/maven/plugins/amps/TestJarMojo.java

https://bitbucket.org/mmeinhold/amps · Java · 151 lines · 115 code · 24 blank · 12 comment · 8 complexity · ad9c8ade40eaeb1663624393e129441a MD5 · raw file

  1. package com.atlassian.maven.plugins.amps;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.lang.reflect.Method;
  5. import java.net.MalformedURLException;
  6. import java.util.Collection;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. import com.atlassian.fugue.Option;
  10. import com.atlassian.maven.plugins.amps.util.ClassUtils;
  11. import com.atlassian.plugins.codegen.ClassId;
  12. import com.atlassian.plugins.codegen.ComponentDeclaration;
  13. import com.atlassian.plugins.codegen.PluginProjectChangeset;
  14. import com.atlassian.plugins.codegen.PluginXmlRewriter;
  15. import com.atlassian.plugins.codegen.util.ClassnameUtil;
  16. import org.apache.commons.io.FileUtils;
  17. import org.apache.commons.lang.StringUtils;
  18. import org.apache.maven.plugin.MojoExecutionException;
  19. import org.apache.maven.plugin.MojoFailureException;
  20. import org.apache.maven.plugins.annotations.Mojo;
  21. import org.apache.maven.plugins.annotations.Parameter;
  22. import org.apache.maven.plugins.annotations.ResolutionScope;
  23. import org.apache.maven.project.MavenProject;
  24. import org.dom4j.DocumentException;
  25. import org.junit.Test;
  26. import static com.atlassian.maven.plugins.amps.util.FileUtils.file;
  27. /**
  28. * Jars the tests into an OSGi bundle. Only builds the jar if the {@link #buildTestPlugin} flag is set or it detects
  29. * an atlassian-plugin.xml file in the target/test-classes directory.
  30. *
  31. * Note, this test jar will not have its resources filtered or a manifest generated for it. If no manifest is present,
  32. * a dummy manifest that does a dynamic package import on everything will be used.
  33. *
  34. * @since 3.3
  35. */
  36. @Mojo(name = "test-jar",requiresDependencyResolution = ResolutionScope.TEST)
  37. public class TestJarMojo extends AbstractAmpsMojo
  38. {
  39. /**
  40. * The final name for the test plugin, without the "-tests" suffix.
  41. */
  42. @Parameter(property = "project.build.finalName")
  43. private String finalName;
  44. public void execute() throws MojoExecutionException, MojoFailureException
  45. {
  46. MavenProject prj = getMavenContext().getProject();
  47. File testClassesDir = file(prj.getBuild().getTestOutputDirectory());
  48. if (shouldBuildTestPlugin())
  49. {
  50. File mf = file(testClassesDir, "META-INF", "MANIFEST.MF");
  51. if (!mf.exists())
  52. {
  53. try
  54. {
  55. String symbolicName = prj.getGroupId() + "." + prj.getArtifactId() + "-tests";
  56. FileUtils.writeStringToFile(mf,
  57. "Manifest-Version: 1.0\n" +
  58. "Bundle-SymbolicName: " + symbolicName + "\n" +
  59. "Bundle-Version: 1.0\n" +
  60. "Bundle-Name: " + finalName + "-tests\n" +
  61. "DynamicImport-Package: *\n" +
  62. "Atlassian-Plugin-Key: " + symbolicName + "\n"
  63. );
  64. }
  65. catch (IOException e)
  66. {
  67. throw new MojoFailureException("Unable to write manifest");
  68. }
  69. }
  70. File pluginXml = file(testClassesDir,"atlassian-plugin.xml");
  71. File itPackageDir = file(testClassesDir,"it");
  72. if(pluginXml.exists() && itPackageDir.exists())
  73. {
  74. PluginProjectChangeset changes = new PluginProjectChangeset();
  75. Collection<File> classFiles = FileUtils.listFiles(itPackageDir, new String[]{"class"}, true);
  76. try
  77. {
  78. for(File classFile : classFiles)
  79. {
  80. String className = ClassUtils.getClassnameFromFile(classFile, prj.getBuild().getTestOutputDirectory());
  81. if(ClassUtils.isWiredPluginTestClass(classFile))
  82. {
  83. getLog().info("found Test: " + className + ", adding to plugin.xml...");
  84. Map<String,String> serviceProps = new HashMap<String, String>();
  85. serviceProps.put("inProductTest","true");
  86. String simpleClassname = StringUtils.substringAfterLast(className,".");
  87. ComponentDeclaration component = ComponentDeclaration.builder(ClassId.fullyQualified(className),ClassnameUtil.camelCaseToDashed(simpleClassname).toLowerCase())
  88. .interfaceId(Option.some(ClassId.fullyQualified(className)))
  89. .visibility(ComponentDeclaration.Visibility.PUBLIC)
  90. .serviceProperties(serviceProps)
  91. .build();
  92. changes = changes.with(component);
  93. }
  94. }
  95. new PluginXmlRewriter(pluginXml).applyChanges(changes);
  96. }
  97. catch (MalformedURLException e)
  98. {
  99. throw new MojoExecutionException("unable to convert test classes folder to URL",e);
  100. }
  101. catch (DocumentException e)
  102. {
  103. throw new MojoExecutionException("unable to modify plugin.xml",e);
  104. }
  105. catch (IOException e)
  106. {
  107. throw new MojoExecutionException("unable to modify plugin.xml",e);
  108. }
  109. }
  110. getMavenGoals().jarTests(finalName);
  111. }
  112. }
  113. private boolean hasTestMethod(Class<?> testClass)
  114. {
  115. boolean hasTestAnno = false;
  116. Method[] methods = testClass.getMethods();
  117. for(Method m : methods)
  118. {
  119. if(m.isAnnotationPresent(Test.class))
  120. {
  121. hasTestAnno = true;
  122. break;
  123. }
  124. }
  125. return hasTestAnno;
  126. }
  127. }