From: Pieter v. Z. <pv...@us...> - 2005-11-23 14:28:52
|
Update of /cvsroot/dithaka/base/src-test/org/dithaka/messaging In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21768/src-test/org/dithaka/messaging Added Files: MailUtil.java MailForumTest.java Log Message: Added src-test dir with Testcase: MailForumTest. This is to test the case sensitivity of e-mail addresses and forum names. Uses lower function in the select staments of the user e-mail addresses and forum names to make the queries case insensitive --- NEW FILE: MailUtil.java --- package org.dithaka.messaging; import java.util.Properties; import javax.mail.*; import javax.mail.Message; import javax.mail.internet.*; /** * This is a utility class that allows the system to send email notifications * */ public class MailUtil { //~ Static fields/initializers ============================================= private static Session session = null; static { Properties props = new Properties(); props.put("mail.smtp.host", "146.64.28.42"); props.put("mail.from", "ad...@co..."); props.put("mail.transport.protocol", "smtp"); session = Session.getDefaultInstance(props, null); } //~ Methods ================================================================ public static boolean sendEmail(String message, String subject, String recipient, String sender) { if (session == null) { return false; } boolean retVal = true; try { MimeMessage msg = new MimeMessage(session); msg.setContent(message, "text/html"); Address recp = new InternetAddress(recipient); if (sender != null) { Address snd = new InternetAddress(sender); msg.setFrom(snd); } msg.addRecipient(Message.RecipientType.TO, recp); msg.setSubject(subject); Transport.send(msg); } catch (MessagingException e) { e.printStackTrace(); retVal = false; } return retVal; } } --- NEW FILE: MailForumTest.java --- package org.dithaka.messaging; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Date; import java.util.Properties; import net.sf.hibernate.SessionFactory; import org.dithaka.messaging.AliasUser; import org.dithaka.messaging.Forum; import org.dithaka.messaging.MailGenerator; import org.dithaka.messaging.Message; import org.dithaka.persistence.PersistenceException; import org.dithaka.persistence.PersistenceService; import org.dithaka.messaging.MailUtil; import junit.framework.TestCase; /** * Nov 14, 2005 * * @author pieter20 * * This test case check to see if incoming mail is correctly handled when * information in the e-mail is changed. The change that we are interested in * here is case sensitivity of the addresses * * Note: the test db needs to be clear with each run otherwise the msg will be * duplicated. * * Note: These tests cases create a new conversation in the forum as each * message has a new mail id. * */ public class MailForumTest extends TestCase { private static String testingPropertiesFile = "/misc/test.properties"; private static Properties properties = new Properties(); private AliasUser user = new AliasUser(); private Forum forum = new Forum(); static { InputStream is = null; try { is = MailForumTest.class.getResourceAsStream(testingPropertiesFile); if (is != null) { properties.load(is); } else { throw new FileNotFoundException(); } } catch (FileNotFoundException e) { e.printStackTrace(); System.err.println("File not found: " + testingPropertiesFile); } catch (IOException io) { io.printStackTrace(); } } /** * Nov 14, 2005 pieter20 Not used at the moment * * @return */ public Message createMessage() { Message message = new Message(); message.setSubject("test subject"); message.setBody("test body"); message.setDateCreated(new Date(System.currentTimeMillis())); try { message.setAuthor(user); } catch (Exception e) { e.printStackTrace(); System.err.println("Could not set the author of the message"); } return message; } /** * Nov 14, 2005 pieter20 Not used at the moment */ private void createMail() { MailGenerator.setMailServer(properties.getProperty("mailSmptHost")); // pvz: old way: // String sender = ctx.getCurrentUser().getEmail(); // pvz: the alias way: String sender = user.getAliasEmailAddress(); String forumAddress = forum.getFullMailAddress(); Message message = createMessage(); System.out.println("Sending message............."); MailGenerator.createMail(sender, message, forumAddress); System.out.println("Sending message.............done"); } /** * Nov 14, 2005 pieter20 This tests to see when an e-mail is send from an * outside client back to the forum, what would happen if the real e-mail * address case is changed. For example if the real e-mail address is: * pv...@ab... it could be changed to PV...@ab..., Pv...@ab..., or * pv...@AB..., etc. For all these cases the e-mail address should be seen * the same as pv...@ab.... Test from outside into the forum. */ public void testUserRealMailAddressCaseSensitivity() { String msg = "case sensitive: user real e-mail address"; boolean success = false; // forum // .setFullMailAddress("tes...@pv..."); // forum.setFullName("list name"); // forum.setMailAddressSnippet("list"); user.setAliasEmailAddress(properties .getProperty("testUserRealMailAddressCaseSensitivity_alias")); user.setRealEmailAddress(properties.getProperty("real")); user.setUserName(properties.getProperty("username")); // createMail(); success = MailUtil.sendEmail(msg, "test", properties .getProperty("list_forum_name"), user.getRealEmailAddress()); System.out.println("Sending message. real sender:" + user.getRealEmailAddress() + " to " + properties.getProperty("list_forum_name") + ".............done"); assertTrue(success); assertTrue(checkMsgExistence(msg)); } /** * Nov 14, 2005 pieter20 Test from outside This tests to see when an e-mail * is send from an outside client back to the forum, what would happen if * the FORUM e-mail address case is changed. For example if the FORUM e-mail * address is: lis...@ab... it could be changed to LIS...@ab..., * Lis...@ab..., or lis...@AB..., etc. For all these cases the * e-mail address should be seen the same as lis...@ab.... Test from * outside into the forum */ public void testForumMailAddressCaseSensitivity() { String msg = "case sensitive: forum name"; boolean success = false; user.setAliasEmailAddress(properties.getProperty("alias")); user.setRealEmailAddress(properties.getProperty("real")); user.setUserName(properties.getProperty("username")); success = MailUtil.sendEmail(msg, "test", properties .getProperty("testForumMailAddressCaseSensitivity_forumName"), user.getRealEmailAddress()); System.out .println("Sending message. real sender: " + user.getRealEmailAddress() + " to " + properties .getProperty("testForumMailAddressCaseSensitivity_forumName") + ".............done"); assertTrue(success); assertTrue(checkMsgExistence(msg)); } /** * Nov 14, 2005 pieter20 Test from outside */ public void testWrongRealMailAddress() { String msg = "alias user address: should not work!"; boolean success = false; user.setAliasEmailAddress(properties.getProperty("alias")); user.setRealEmailAddress(properties.getProperty("real")); user.setUserName(properties.getProperty("username")); success = MailUtil.sendEmail(msg, "test", properties .getProperty("list_forum_name"), user.getAliasEmailAddress()); System.out .println("Sending message. alias sender: " + user.getAliasEmailAddress() + "to tes...@pv................done"); // this fails as we are setting the alias as the real and there is no // real with this address. assertTrue(success); assertFalse(checkMsgExistence(msg)); } private boolean checkMsgExistence(String msgBody) { String sqlQuery = "FROM Message WHERE body LIKE '" + msgBody + "'"; try { Message msg = (Message) PersistenceService.findOne(sqlQuery); if (msg == null) { return false; } else { return true; } } catch (PersistenceException ex) { ex.printStackTrace(); return false; } } /** * Nov 14, 2005 pieter20 * * @param args */ public static void main(String[] args) { } } |