Skip to content

Commit

Permalink
Fixed JENKINS-9160 for the legacy UserCause and fixed several bugs in…
Browse files Browse the repository at this point in the history
… the previous commit
  • Loading branch information
kutzi committed Nov 19, 2011
1 parent c0c690c commit b6304d8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 17 deletions.
23 changes: 16 additions & 7 deletions src/main/java/hudson/plugins/emailext/ExtendedEmailPublisher.java
Expand Up @@ -39,6 +39,7 @@


import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -388,7 +389,7 @@ private void addUserTriggeringTheBuild(AbstractBuild<?, ?> build, Set<InternetAd
EnvVars env, BuildListener listener) {
User user = getByUserIdCause(build);
if (user == null) {
getByLegacyUseCause(build);
user = getByLegacyUseCause(build);
}

if (user != null) {
Expand All @@ -404,7 +405,8 @@ private void addUserTriggeringTheBuild(AbstractBuild<?, ?> build, Set<InternetAd
@SuppressWarnings("unchecked")
private User getByUserIdCause(AbstractBuild<?, ?> build) {
try {
Class<? extends Cause> userIdCause = (Class<? extends Cause>) Class.forName("hudson.model.Cause.UserIdCause");
Class<? extends Cause> userIdCause = (Class<? extends Cause>)
ExtendedEmailPublisher.class.getClassLoader().loadClass("hudson.model.Cause$UserIdCause");
Method getUserId = userIdCause.getMethod("getUserId", new Class[0]);

Cause cause = build.getCause(userIdCause);
Expand All @@ -420,11 +422,18 @@ private User getByUserIdCause(AbstractBuild<?, ?> build) {
}

private User getByLegacyUseCause(AbstractBuild<?, ?> build) {
UserCause userCause = build.getCause(Cause.UserCause.class);
if (userCause != null) {
// this may fail, as userCause.getUserName() *may* return the displayname instead of the unique name,
// but still the best we can do here
return User.get(userCause.getUserName(), false);
try {
UserCause userCause = build.getCause(Cause.UserCause.class);
// userCause.getUserName() returns displayName which may be different from authentication name
// Therefore use reflection to access the real authenticationName
if (userCause != null) {
Field authenticationName = UserCause.class.getDeclaredField("authenticationName");
authenticationName.setAccessible(true);
String name = (String) authenticationName.get(userCause);
return User.get(name, false);
}
} catch(Exception e) {
LOGGER.info(e.getMessage());
}
return null;
}
Expand Down
@@ -1,8 +1,13 @@
package hudson.plugins.emailext;

import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.matchers.JUnitMatchers.containsString;
import static org.junit.matchers.JUnitMatchers.hasItems;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.model.Cause.UserCause;
import hudson.model.FreeStyleProject;
import hudson.model.User;
import hudson.plugins.emailext.plugins.EmailTrigger;
import hudson.plugins.emailext.plugins.trigger.AbortedTrigger;
Expand All @@ -13,13 +18,8 @@
import hudson.plugins.emailext.plugins.trigger.StillFailingTrigger;
import hudson.plugins.emailext.plugins.trigger.SuccessTrigger;
import hudson.tasks.Mailer;
import net.sf.json.JSONObject;
import org.jvnet.hudson.test.FailureBuilder;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.MockBuilder;
import org.jvnet.mock_javamail.Mailbox;
import org.kohsuke.stapler.Stapler;

import java.lang.reflect.Field;
import java.util.List;
import java.util.concurrent.Callable;

Expand All @@ -28,9 +28,13 @@
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.junit.matchers.JUnitMatchers.*;
import net.sf.json.JSONObject;

import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.FailureBuilder;
import org.jvnet.hudson.test.MockBuilder;
import org.jvnet.mock_javamail.Mailbox;
import org.kohsuke.stapler.Stapler;

public class ExtendedEmailPublisherTest
extends HudsonTestCase
Expand Down Expand Up @@ -372,6 +376,35 @@ public void testShouldSendEmailUsingUtf8ByDefault()
// return userId;
// }
// }

public void testSendToRequesterLegacy() throws Exception {
SuccessTrigger successTrigger = new SuccessTrigger();
successTrigger.setEmail(new EmailType(){{
setSendToRequester(true);
}});
publisher.getConfiguredTriggers().add( successTrigger );

User u = User.get("kutzi");
u.setFullName("Christoph Kutzinski");
Mailer.UserProperty prop = new Mailer.UserProperty("kutzi@xxx.com");
u.addProperty(prop);

UserCause cause = new MockUserCause("kutzi");

FreeStyleBuild build = project.scheduleBuild2( 0, cause ).get();
assertBuildStatusSuccess( build );

assertEquals( 1, Mailbox.get( "kutzi@xxx.com" ).size() );
}

private static class MockUserCause extends UserCause {
public MockUserCause(String userName) throws Exception {
super();
Field f = UserCause.class.getDeclaredField("authenticationName");
f.setAccessible(true);
f.set(this, userName);
}
}

public void testNewInstance_shouldGetBasicInformation()
throws Exception
Expand Down

0 comments on commit b6304d8

Please sign in to comment.