PageRenderTime 24ms CodeModel.GetById 15ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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