Skip to content

Commit

Permalink
#165: Also consider test dependencies for classpath of static analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
maybeec committed Dec 13, 2015
1 parent d715ee6 commit 23f224f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cobigen-maven/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<artifactId>cobigen-maven-plugin</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>1.1.1</version>
<packaging>maven-plugin</packaging>
<name>CobiGen Maven Plugin</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;

import org.apache.maven.artifact.Artifact;
Expand Down Expand Up @@ -43,15 +44,16 @@
import com.capgemini.cobigen.textmerger.TextMergerPluginActivator;
import com.capgemini.cobigen.xmlplugin.XmlPluginActivator;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import freemarker.template.TemplateException;

/**
* CobiGen generation Mojo, which handles generation using a configuration folder/archive
* @author mbrunnli (08.02.2015)
*/
@Mojo(name = "generate", requiresDependencyResolution = ResolutionScope.RUNTIME, requiresProject = true,
defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyCollection = ResolutionScope.RUNTIME)
@Mojo(name = "generate", requiresDependencyResolution = ResolutionScope.TEST, requiresProject = true,
defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyCollection = ResolutionScope.TEST)
public class GenerateMojo extends AbstractMojo {

static {
Expand Down Expand Up @@ -333,13 +335,16 @@ private void generateFromTemplates(CobiGen cobiGen, List<Object> inputs) throws
* @author mbrunnli (11.02.2015)
*/
private ClassLoader getProjectClassLoader() throws MojoFailureException {
List<String> classpathElements = null;
Set<String> classpathElements = Sets.newHashSet();
try {
classpathElements = project.getCompileClasspathElements();
classpathElements.addAll(project.getCompileClasspathElements());
classpathElements.addAll(project.getTestClasspathElements());
List<URL> projectClasspathList = new ArrayList<>();
for (String element : classpathElements) {
try {
projectClasspathList.add(new File(element).toURI().toURL());
URL url = new File(element).toURI().toURL();
getLog().debug("Add Classpath-URL: " + url);
projectClasspathList.add(url);
} catch (MalformedURLException e) {
getLog().error(element + " is an invalid classpath element", e);
throw new MojoFailureException(element + " is an invalid classpath element");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import static org.junit.Assert.assertThat;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.project.DefaultMavenProjectBuilder;
import org.apache.maven.project.DefaultProjectBuilderConfiguration;
import org.apache.maven.project.MavenProject;
import org.junit.Ignore;
import org.junit.Test;

Expand Down Expand Up @@ -44,4 +48,32 @@ public void testMojoPackageInputRetrieval() throws Exception {

assertThat(inputObjects.size(), equalTo(1));
}

/**
*
* @throws Exception
* @author mbrunnli (11.12.2015)
*/
@Test
public void testResolveTestScopeClasspathResources() throws Exception {
File testPom = new File("src/test/resources/testdata/systemtest/GenerateMojoTest/pom.xml");
GenerateMojo mojo = (GenerateMojo) lookupMojo("generate", testPom);
assertThat(mojo, notNullValue());

Method method = mojo.getClass().getDeclaredMethod("getProjectClassLoader");
method.setAccessible(true);

Field field = mojo.getClass().getDeclaredField("project");
field.setAccessible(true);
MavenProject build =
new DefaultMavenProjectBuilder().build(testPom, new DefaultProjectBuilderConfiguration());
field.set(mojo, build);

@SuppressWarnings("unchecked")
ClassLoader classLoader = (ClassLoader) method.invoke(mojo);

// Assert: should not throw a ClassNotFoundException
classLoader.loadClass("org.custommonkey.xmlunit.XMLTestCase");

}
}

0 comments on commit 23f224f

Please sign in to comment.