Skip to content

Commit

Permalink
[FIXED JENKINS-5044] catch FileNotFoundException if mojos are execute…
Browse files Browse the repository at this point in the history
…d from a classes directory instead from a jar

Originally-Committed-As: 68f5a0d23ab4a5631df15b521b8eb71a2a74cc49
  • Loading branch information
kutzi committed May 22, 2011
1 parent 5b90051 commit 10b68f6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
7 changes: 7 additions & 0 deletions pom.xml
Expand Up @@ -308,6 +308,13 @@ THE SOFTWARE.
<version>1.1</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/hudson/maven/ExecutedMojo.java
Expand Up @@ -30,6 +30,7 @@
import hudson.util.ReflectionUtils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -94,12 +95,15 @@ public ExecutedMojo(MojoInfo mojo, long duration) throws IOException, Interrupte
MojoDescriptor md = mojo.mojoExecution.getMojoDescriptor();
PluginDescriptor pd = md.getPluginDescriptor();
try {
Class clazz = getMojoClass( md, pd );// pd.getClassRealm().loadClass(md.getImplementation());
Class<?> clazz = getMojoClass( md, pd );
digest = Util.getDigestOf(new FileInputStream(Which.jarFile(clazz)));
} catch (IllegalArgumentException e) {
LOGGER.log(Level.WARNING, "Failed to locate jar for "+md.getImplementation(),e);
} catch (ClassNotFoundException e) {
// perhaps the plugin has failed to load.
} catch (FileNotFoundException e) {
// Maybe mojo was loaded from a classes dir instead of from a jar (JENKINS-5044)
LOGGER.log(Level.WARNING, "Failed to caculate digest for "+md.getImplementation(),e);
}
this.digest = digest;
}
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/hudson/maven/ExecutedMojoTest.java
@@ -0,0 +1,62 @@
package hudson.maven;

import java.io.IOException;

import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;

/**
* Unit test for {@link ExecutedMojo}.
*
* @author kutzi
*/
public class ExecutedMojoTest {

private MojoDescriptor mojoDescriptor;

@Before
public void before() {
PluginDescriptor pluginDescriptor = new PluginDescriptor();
pluginDescriptor.setGroupId("com.test");
pluginDescriptor.setArtifactId("testPlugin");
pluginDescriptor.setVersion("1.0");

ClassRealm classRealm = new ClassRealm(null, "test", getClass().getClassLoader());
pluginDescriptor.setClassRealm(classRealm);

MojoDescriptor descriptor = new MojoDescriptor();
descriptor.setPluginDescriptor(pluginDescriptor);
this.mojoDescriptor = descriptor;
}

@Test
public void testMojoFromJarFile() throws IOException, InterruptedException {
// Faking JUnit's Assert to be the plugin class
this.mojoDescriptor.setImplementation(Assert.class.getName());
MojoExecution execution = new MojoExecution(this.mojoDescriptor);
MojoInfo info = new MojoInfo(execution, null, null, null);

ExecutedMojo executedMojo = new ExecutedMojo(info, 1L);

Assert.assertNotNull(executedMojo.digest);
}

@Test
@Bug(5044)
public void testMojoFromClassesDirectory() throws IOException, InterruptedException {
// Faking this class as the mojo impl:
this.mojoDescriptor.setImplementation(getClass().getName());
MojoExecution execution = new MojoExecution(this.mojoDescriptor);
MojoInfo info = new MojoInfo(execution, null, null, null);

ExecutedMojo executedMojo = new ExecutedMojo(info, 1L);

Assert.assertEquals("com.test", executedMojo.groupId);
}
}

0 comments on commit 10b68f6

Please sign in to comment.