Merged DEV to HEAD

48848: ALF-18520: Mails subsystem fails with Nullpointer during initialization
          If option is set to send test message on mail subsystem init then do it on behalf of System user.
          During getFrom field calculation if there is no current user then send mail from default address
          like it is System user. Case to unit test was added. 
   Reformatted MailActionExecuterTest


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@48866 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2013-04-05 10:57:33 +00:00
parent d542afaa5c
commit 0bc61d6d35
2 changed files with 204 additions and 175 deletions

View File

@@ -38,6 +38,8 @@ import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel; import org.alfresco.model.ContentModel;
import org.alfresco.repo.action.ParameterDefinitionImpl; import org.alfresco.repo.action.ParameterDefinitionImpl;
import org.alfresco.repo.admin.SysAdminParams; import org.alfresco.repo.admin.SysAdminParams;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.repo.template.DateCompareMethod; import org.alfresco.repo.template.DateCompareMethod;
import org.alfresco.repo.template.HasAspectMethod; import org.alfresco.repo.template.HasAspectMethod;
import org.alfresco.repo.template.I18NMessageMethod; import org.alfresco.repo.template.I18NMessageMethod;
@@ -389,13 +391,20 @@ public class MailActionExecuter extends ActionExecuterAbstractBase
super.init(); super.init();
if (sendTestMessage && testMessageTo != null) if (sendTestMessage && testMessageTo != null)
{ {
Map<String, Serializable> params = new HashMap<String, Serializable>(); AuthenticationUtil.runAs(new RunAsWork<Object>()
params.put(PARAM_TO, testMessageTo); {
params.put(PARAM_SUBJECT, testMessageSubject); public Object doWork() throws Exception
params.put(PARAM_TEXT, testMessageText); {
Map<String, Serializable> params = new HashMap<String, Serializable>();
Action ruleAction = serviceRegistry.getActionService().createAction(NAME, params); params.put(PARAM_TO, testMessageTo);
executeImpl(ruleAction, null); params.put(PARAM_SUBJECT, testMessageSubject);
params.put(PARAM_TEXT, testMessageText);
Action ruleAction = serviceRegistry.getActionService().createAction(NAME, params);
executeImpl(ruleAction, null);
return null;
}
}, AuthenticationUtil.getSystemUserName());
} }
} }
@@ -926,7 +935,7 @@ public class MailActionExecuter extends ActionExecuterAbstractBase
if (! authService.isCurrentUserTheSystemUser()) if (! authService.isCurrentUserTheSystemUser())
{ {
String currentUserName = authService.getCurrentUserName(); String currentUserName = authService.getCurrentUserName();
if (personExists(currentUserName)) if (currentUserName != null && personExists(currentUserName))
{ {
fromPersonName = currentUserName; fromPersonName = currentUserName;
locale = getLocaleForUser(fromPersonName); locale = getLocaleForUser(fromPersonName);

View File

@@ -1,167 +1,187 @@
/* /*
* Copyright (C) 2005-2013 Alfresco Software Limited. * Copyright (C) 2005-2013 Alfresco Software Limited.
* *
* This file is part of Alfresco * This file is part of Alfresco
* *
* Alfresco is free software: you can redistribute it and/or modify * Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by * it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* Alfresco is distributed in the hope that it will be useful, * Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details. * GNU Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.alfresco.repo.action.executer; package org.alfresco.repo.action.executer;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.mail.MessagingException; import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import junit.framework.Assert; import org.alfresco.repo.management.subsystems.ApplicationContextFactory;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.management.subsystems.ApplicationContextFactory; import org.alfresco.service.cmr.action.Action;
import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.service.cmr.action.ActionService;
import org.alfresco.service.cmr.action.Action; import org.alfresco.service.cmr.preference.PreferenceService;
import org.alfresco.service.cmr.action.ActionService; import org.alfresco.util.test.junitrules.AlfrescoPerson;
import org.alfresco.service.cmr.preference.PreferenceService; import org.alfresco.util.test.junitrules.ApplicationContextInit;
import org.alfresco.util.test.junitrules.AlfrescoPerson; import org.junit.AfterClass;
import org.alfresco.util.test.junitrules.ApplicationContextInit; import org.junit.Assert;
import org.junit.AfterClass; import org.junit.BeforeClass;
import org.junit.BeforeClass; import org.junit.ClassRule;
import org.junit.ClassRule; import org.junit.Test;
import org.junit.Test; import org.junit.rules.RuleChain;
import org.junit.rules.RuleChain; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContext;
public class MailActionExecuterTest {
public class MailActionExecuterTest {
// Rule to initialise the default Alfresco spring configuration
// Rule to initialise the default Alfresco spring configuration public static ApplicationContextInit APP_CONTEXT_INIT = new ApplicationContextInit();
public static ApplicationContextInit APP_CONTEXT_INIT = new ApplicationContextInit();
// Rules to create 2 test users.
// Rules to create 2 test users. public static AlfrescoPerson AUSTRALIAN_USER = new AlfrescoPerson(APP_CONTEXT_INIT, "AustralianUser@test.com");
public static AlfrescoPerson AUSTRALIAN_USER = new AlfrescoPerson(APP_CONTEXT_INIT, "AustralianUser@test.com"); public static AlfrescoPerson BRITISH_USER = new AlfrescoPerson(APP_CONTEXT_INIT, "EnglishUser@test.com");
public static AlfrescoPerson BRITISH_USER = new AlfrescoPerson(APP_CONTEXT_INIT, "EnglishUser@test.com"); public static AlfrescoPerson FRENCH_USER = new AlfrescoPerson(APP_CONTEXT_INIT, "FrenchUser@test.com");
public static AlfrescoPerson FRENCH_USER = new AlfrescoPerson(APP_CONTEXT_INIT, "FrenchUser@test.com"); public static AlfrescoPerson UNKNOWN_USER1 = new AlfrescoPerson(APP_CONTEXT_INIT, "UnknownUser1@test.com");
public static AlfrescoPerson UNKNOWN_USER1 = new AlfrescoPerson(APP_CONTEXT_INIT, "UnknownUser1@test.com"); public static AlfrescoPerson UNKNOWN_USER2 = new AlfrescoPerson(APP_CONTEXT_INIT, "UnknowUser2@test.com");
public static AlfrescoPerson UNKNOWN_USER2 = new AlfrescoPerson(APP_CONTEXT_INIT, "UnknowUser2@test.com");
// Tie them together in a static Rule Chain
// Tie them together in a static Rule Chain @ClassRule public static RuleChain ruleChain = RuleChain.outerRule(APP_CONTEXT_INIT)
@ClassRule public static RuleChain ruleChain = RuleChain.outerRule(APP_CONTEXT_INIT) .around(AUSTRALIAN_USER)
.around(AUSTRALIAN_USER) .around(BRITISH_USER)
.around(BRITISH_USER) .around(FRENCH_USER)
.around(FRENCH_USER) .around(UNKNOWN_USER1)
.around(UNKNOWN_USER1) .around(UNKNOWN_USER2);
.around(UNKNOWN_USER2);
private static ActionService ACTION_SERVICE;
private static ActionService ACTION_SERVICE; private static MailActionExecuter ACTION_EXECUTER;
private static MailActionExecuter ACTION_EXECUTER; private static PreferenceService PREFERENCE_SERVICE;
private static PreferenceService PREFERENCE_SERVICE;
private static boolean WAS_IN_TEST_MODE;
private static boolean WAS_IN_TEST_MODE;
@BeforeClass
@BeforeClass public static void setup()
public static void setup() {
{ ApplicationContext appCtx = APP_CONTEXT_INIT.getApplicationContext();
ApplicationContext appCtx = APP_CONTEXT_INIT.getApplicationContext(); ACTION_SERVICE = appCtx.getBean("ActionService", ActionService.class);
ACTION_SERVICE = appCtx.getBean("ActionService", ActionService.class); ACTION_EXECUTER = appCtx.getBean("OutboundSMTP", ApplicationContextFactory.class).getApplicationContext().getBean("mail", MailActionExecuter.class);
ACTION_EXECUTER = appCtx.getBean("OutboundSMTP", ApplicationContextFactory.class).getApplicationContext().getBean("mail", MailActionExecuter.class); PREFERENCE_SERVICE = appCtx.getBean("PreferenceService", PreferenceService.class);
PREFERENCE_SERVICE = appCtx.getBean("PreferenceService", PreferenceService.class);
WAS_IN_TEST_MODE = ACTION_EXECUTER.isTestMode();
WAS_IN_TEST_MODE = ACTION_EXECUTER.isTestMode(); ACTION_EXECUTER.setTestMode(true);
ACTION_EXECUTER.setTestMode(true);
AuthenticationUtil.setRunAsUserSystem();
AuthenticationUtil.setRunAsUserSystem();
Map<String, Serializable> preferences = new HashMap<String, Serializable>();
Map<String, Serializable> preferences = new HashMap<String, Serializable>();
preferences.put("locale", "fr");
preferences.put("locale", "fr"); PREFERENCE_SERVICE.setPreferences(FRENCH_USER.getUsername(), preferences);
PREFERENCE_SERVICE.setPreferences(FRENCH_USER.getUsername(), preferences);
preferences.clear();
preferences.clear(); preferences.put("locale", "en_GB");
preferences.put("locale", "en_GB"); PREFERENCE_SERVICE.setPreferences(BRITISH_USER.getUsername(), preferences);
PREFERENCE_SERVICE.setPreferences(BRITISH_USER.getUsername(), preferences);
preferences.clear();
preferences.clear(); preferences.put("locale", "en_AU");
preferences.put("locale", "en_AU"); PREFERENCE_SERVICE.setPreferences(AUSTRALIAN_USER.getUsername(), preferences);
PREFERENCE_SERVICE.setPreferences(AUSTRALIAN_USER.getUsername(), preferences);
}
}
@AfterClass
@AfterClass public static void tearDown()
public static void tearDown() {
{ ACTION_EXECUTER.setTestMode(WAS_IN_TEST_MODE);
ACTION_EXECUTER.setTestMode(WAS_IN_TEST_MODE); }
}
@Test public void testUnknownRecipientUnknownSender() throws IOException, MessagingException
@Test public void testUnknownRecipientUnknownSender() throws IOException, MessagingException {
{ Action mailAction = ACTION_SERVICE.createAction(MailActionExecuter.NAME);
Action mailAction = ACTION_SERVICE.createAction(MailActionExecuter.NAME); mailAction.setParameterValue(MailActionExecuter.PARAM_FROM, "some.body@example.com");
mailAction.setParameterValue(MailActionExecuter.PARAM_FROM, "some.body@example.com"); mailAction.setParameterValue(MailActionExecuter.PARAM_TO, "some.bodyelse@example.com");
mailAction.setParameterValue(MailActionExecuter.PARAM_TO, "some.bodyelse@example.com");
mailAction.setParameterValue(MailActionExecuter.PARAM_SUBJECT, "Testing");
mailAction.setParameterValue(MailActionExecuter.PARAM_SUBJECT, "Testing"); mailAction.setParameterValue(MailActionExecuter.PARAM_TEMPLATE, "alfresco/templates/mail/test.txt.ftl");
mailAction.setParameterValue(MailActionExecuter.PARAM_TEMPLATE, "alfresco/templates/mail/test.txt.ftl");
mailAction.setParameterValue(MailActionExecuter.PARAM_TEMPLATE_MODEL, (Serializable)getModel());
mailAction.setParameterValue(MailActionExecuter.PARAM_TEMPLATE_MODEL, (Serializable)getModel());
ACTION_SERVICE.executeAction(mailAction, null);
ACTION_SERVICE.executeAction(mailAction, null);
MimeMessage message = ACTION_EXECUTER.retrieveLastTestMessage();
MimeMessage message = ACTION_EXECUTER.retrieveLastTestMessage(); Assert.assertNotNull(message);
Assert.assertNotNull(message); Assert.assertEquals("Hello Jan 1, 1970", (String)message.getContent());
Assert.assertEquals("Hello Jan 1, 1970", (String)message.getContent()); }
}
private Serializable getModel()
private Serializable getModel() {
{ Map<String, Object> model = new HashMap<String, Object>();
Map<String, Object> model = new HashMap<String, Object>();
model.put("epoch", new Date(0));
model.put("epoch", new Date(0)); return (Serializable)model;
return (Serializable)model; }
}
@Test public void testFrenchRecipient() throws IOException, MessagingException
@Test public void testFrenchRecipient() throws IOException, MessagingException {
{ Action mailAction = ACTION_SERVICE.createAction(MailActionExecuter.NAME);
Action mailAction = ACTION_SERVICE.createAction(MailActionExecuter.NAME); mailAction.setParameterValue(MailActionExecuter.PARAM_FROM, "some.body@example.com");
mailAction.setParameterValue(MailActionExecuter.PARAM_FROM, "some.body@example.com"); mailAction.setParameterValue(MailActionExecuter.PARAM_TO_MANY, (Serializable)Arrays.asList(FRENCH_USER.getUsername()));
mailAction.setParameterValue(MailActionExecuter.PARAM_TO_MANY, (Serializable)Arrays.asList(FRENCH_USER.getUsername()));
mailAction.setParameterValue(MailActionExecuter.PARAM_SUBJECT, "");
mailAction.setParameterValue(MailActionExecuter.PARAM_SUBJECT, ""); mailAction.setParameterValue(MailActionExecuter.PARAM_TEMPLATE, "alfresco/templates/mail/test.txt.ftl");
mailAction.setParameterValue(MailActionExecuter.PARAM_TEMPLATE, "alfresco/templates/mail/test.txt.ftl"); mailAction.setParameterValue(MailActionExecuter.PARAM_TEMPLATE_MODEL, getModel());
mailAction.setParameterValue(MailActionExecuter.PARAM_TEMPLATE_MODEL, getModel());
ACTION_SERVICE.executeAction(mailAction, null);
ACTION_SERVICE.executeAction(mailAction, null);
MimeMessage message = ACTION_EXECUTER.retrieveLastTestMessage();
MimeMessage message = ACTION_EXECUTER.retrieveLastTestMessage(); Assert.assertNotNull(message);
Assert.assertNotNull(message); Assert.assertEquals("Bonjour 1 janv. 1970", (String)message.getContent());
Assert.assertEquals("Bonjour 1 janv. 1970", (String)message.getContent()); }
}
@Test public void testUnknowRecipientAustralianSender() throws IOException, MessagingException
@Test public void testUnknowRecipientAustralianSender() throws IOException, MessagingException {
{ Action mailAction = ACTION_SERVICE.createAction(MailActionExecuter.NAME);
Action mailAction = ACTION_SERVICE.createAction(MailActionExecuter.NAME); mailAction.setParameterValue(MailActionExecuter.PARAM_FROM, AUSTRALIAN_USER.getUsername());
mailAction.setParameterValue(MailActionExecuter.PARAM_FROM, AUSTRALIAN_USER.getUsername()); mailAction.setParameterValue(MailActionExecuter.PARAM_TO, "some.body@eaxmple.com");
mailAction.setParameterValue(MailActionExecuter.PARAM_TO, "some.body@eaxmple.com");
mailAction.setParameterValue(MailActionExecuter.PARAM_SUBJECT, "Testing");
mailAction.setParameterValue(MailActionExecuter.PARAM_SUBJECT, "Testing"); mailAction.setParameterValue(MailActionExecuter.PARAM_TEMPLATE, "alfresco/templates/mail/test.txt.ftl");
mailAction.setParameterValue(MailActionExecuter.PARAM_TEMPLATE, "alfresco/templates/mail/test.txt.ftl"); mailAction.setParameterValue(MailActionExecuter.PARAM_TEMPLATE_MODEL, getModel());
mailAction.setParameterValue(MailActionExecuter.PARAM_TEMPLATE_MODEL, getModel());
ACTION_SERVICE.executeAction(mailAction, null);
ACTION_SERVICE.executeAction(mailAction, null);
MimeMessage message = ACTION_EXECUTER.retrieveLastTestMessage();
MimeMessage message = ACTION_EXECUTER.retrieveLastTestMessage(); Assert.assertNotNull(message);
Assert.assertNotNull(message); Assert.assertEquals("G'Day 01/01/1970", (String)message.getContent());
Assert.assertEquals("G'Day 01/01/1970", (String)message.getContent()); }
}
@Test public void testSendingTestMessageWithNoCurrentUser()
} {
try
{
// run with no current user
AuthenticationUtil.clearCurrentSecurityContext();
Action mailAction = ACTION_SERVICE.createAction(MailActionExecuter.NAME);
mailAction.setParameterValue(MailActionExecuter.PARAM_TO, "some.body@eaxmple.com");
mailAction.setParameterValue(MailActionExecuter.PARAM_SUBJECT, "Testing");
mailAction.setParameterValue(MailActionExecuter.PARAM_TEXT, "This is a test message.");
ACTION_EXECUTER.executeImpl(mailAction, null);
}
finally
{
// restore system user as current user
AuthenticationUtil.setRunAsUserSystem();
}
}
}