From 3b66f3fe900626c280a025d3b388e3db70061aeb Mon Sep 17 00:00:00 2001 From: Neil McErlean Date: Mon, 22 Nov 2010 12:11:27 +0000 Subject: [PATCH] Merge from V3.4 to HEAD r. xx (Part ONE) r. 23839 Merge from V3.3-BUG-FIX to V3.4 r.23837: Reimplementation of the build fix for InviteServiceTest. This one won't send emails. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@23841 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../web/scripts/invite/InviteServiceTest.java | 43 +++++++++++++++++++ .../web/scripts/site/SiteServiceTest.java | 6 --- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/source/java/org/alfresco/repo/web/scripts/invite/InviteServiceTest.java b/source/java/org/alfresco/repo/web/scripts/invite/InviteServiceTest.java index b824b776ae..b961430290 100644 --- a/source/java/org/alfresco/repo/web/scripts/invite/InviteServiceTest.java +++ b/source/java/org/alfresco/repo/web/scripts/invite/InviteServiceTest.java @@ -23,8 +23,10 @@ import java.util.List; import java.util.Set; import org.alfresco.model.ContentModel; +import org.alfresco.repo.action.executer.MailActionExecuter; import org.alfresco.repo.content.MimetypeMap; import org.alfresco.repo.invitation.WorkflowModelNominatedInvitation; +import org.alfresco.repo.management.subsystems.ChildApplicationContextFactory; import org.alfresco.repo.security.authentication.AuthenticationComponent; import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.authentication.MutableAuthenticationDao; @@ -52,6 +54,7 @@ import org.alfresco.util.GUID; import org.alfresco.util.PropertyMap; import org.apache.commons.lang.RandomStringUtils; import org.json.JSONObject; +import org.springframework.context.ApplicationContext; import org.springframework.core.io.ClassPathResource; import org.springframework.extensions.surf.util.URLEncoder; import org.springframework.extensions.webscripts.Status; @@ -131,6 +134,8 @@ public class InviteServiceTest extends BaseWebScriptTest this.transactionService = (TransactionService) getServer().getApplicationContext() .getBean("TransactionService"); + configureMailExecutorForTestMode(); + // We're using a MailActionExecuter defined in outboundSMTP-test-context.xml which // sets the testMode property to true via spring injection. This will prevent emails // from being sent from within this test case. @@ -223,6 +228,44 @@ public class InviteServiceTest extends BaseWebScriptTest this.authenticationComponent.setCurrentUser(USER_INVITER); } + /** + * This method turns off email-sending within the MailActionExecuter bean. + */ + private void configureMailExecutorForTestMode() + { + // This test class depends on a MailActionExecuter bean which sends out emails + // in a live system. We want to prevent these emails from being sent during + // test execution. + // To do that, we need to get at the outboundSMTP-context.xml and change its + // "mail" MailActionExecuter bean to test mode. setTestMode(true) on that object + // will turn off email sending. + // But that bean is defined within a subsystem i.e. a child application context. + + // There are a number of ways we could do this, none of them perfect. + // + // 1. Expose the setTestMode(boolean) method in the subsystem API. + // We could have the "mail" bean implement a "TestModeable" interface and + // expose that through the proxy. + // But that would mean that the setTestMode method would be available in the + // live system too, which is not ideal. + // 2. Replace the "mail" bean in outboundSMTP-context.xml with an alternative in a + // different subsystem context file as described in + // http://wiki.alfresco.com/wiki/Alfresco_Subsystems#Debugging_Alfresco_Subsystems + // But to do that, we'd have to reproduce all the spring config for that bean + // and add a testMode property. Again not ideal. + // 3. Hack into the "mail" bean by programmatically going through the known applicationContext + // and bean structure. This is not ideal either, but it has no effect on product code + // and isolates all the hacking into this test class. + // + // Therefore we've decided to do [3]. + + ChildApplicationContextFactory outboundSmptSubsystem + = (ChildApplicationContextFactory)getServer().getApplicationContext().getBean("OutboundSMTP"); + ApplicationContext childAppCtxt = outboundSmptSubsystem.getApplicationContext(); + MailActionExecuter mailActionExecutor = (MailActionExecuter)childAppCtxt.getBean("mail"); + mailActionExecutor.setTestMode(true); + } + @Override protected void tearDown() throws Exception { diff --git a/source/java/org/alfresco/repo/web/scripts/site/SiteServiceTest.java b/source/java/org/alfresco/repo/web/scripts/site/SiteServiceTest.java index 98e4f97f48..3a5bacd679 100644 --- a/source/java/org/alfresco/repo/web/scripts/site/SiteServiceTest.java +++ b/source/java/org/alfresco/repo/web/scripts/site/SiteServiceTest.java @@ -88,12 +88,6 @@ public class SiteServiceTest extends BaseWebScriptTest this.nodeService = (NodeService)getServer().getApplicationContext().getBean("NodeService"); this.authorityService = (AuthorityService)getServer().getApplicationContext().getBean("AuthorityService"); - // We're using a MailActionExecuter defined in outboundSMTP-test-context.xml which - // sets the testMode property to true via spring injection. This will prevent emails - // from being sent from within this test case. - // This MailExecutorAction bean is named "test-mail" but is in all other respects equivalent to the - // 'real' executer bean. It is automatically included during OutboundSMTP subsystem startup. - this.authenticationComponent.setSystemUserAsCurrentUser(); // Create users