Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.chrisbaur.test.mailaction;
- import static org.junit.Assert.assertTrue;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Date;
- import org.junit.Rule;
- import org.junit.Test;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import com.chrisbaur.mailaction.BasicSendAndReceive;
- import com.chrisbaur.mailbean.MailBean;
- import com.chrisbaur.properties.MailConfigBean;
- import com.chrisbaur.test.MethodLogger;
- import jodd.mail.EmailAttachment;
- /**
- * A basic test method that determines if a simple message sent is the same
- * message received
- *
- * @author Ken Fogel
- *
- */
- public class MailActionTest {
- // A Rule is implemented as a class with methods that are associated
- // with the lifecycle of a unit test. These methods run when required.
- // Avoids the need to cut and paste code into every test method.
- @Rule
- public MethodLogger methodLogger = new MethodLogger();
- // Real programmers use logging, not System.out.println
- private final Logger log = LoggerFactory.getLogger(getClass().getName());
- //Email accounts and passwords
- private final String receiver = "christo.receive@gmail.com";
- private final String sender = "christo.send@gmail.com";
- private final String rPass = "Receivemail!";
- private final String sPass = "Sendmail!";
- private final String email1 = "chriskbaur@gmail.com";
- private final String email2 = "christobaur@hotmail.fr";
- private final String email3 = "c-baur@hotmail.com";
- private final String email4 = "christoffer.baur@dawsoncollege.qc.ca";
- private final String html = "<html><META http-equiv=Content-Type content=\"text/html; charset=utf-8\">" +
- "<body><h1>Hey!</h1><img src='cid:cat.png'><h2>Hay!</h2></body></html>";
- private final EmailAttachment e1 = EmailAttachment.attachment().bytes(new File("cat.jpg")).setInline("cat.jpg").create();
- private final EmailAttachment e2 = EmailAttachment.attachment().bytes(new File("cat.jpg")).setInline("cat.jpg").create();
- private final EmailAttachment a1 = EmailAttachment.attachment().file("cat.jpg").create();
- private final EmailAttachment a2 = EmailAttachment.attachment().file("cat.jpg").create();
- private final ArrayList<String> twoReceiver = new ArrayList<String>();
- private final ArrayList<String> oneReceiver = new ArrayList<String>();
- private final ArrayList<String> twoMemberCc = new ArrayList<String>();
- private final ArrayList<String> twoMemberBcc = new ArrayList<String>();
- private final ArrayList<String> oneMemberCc = new ArrayList<String>();
- private final ArrayList<String> oneMemberBcc = new ArrayList<String>();
- // private final ArrayList<String> noMemberCc = new ArrayList<String>();
- // private final ArrayList<String> noMemberBcc = new ArrayList<String>();
- private final ArrayList<EmailAttachment> oneEmbedded = new ArrayList<EmailAttachment>();
- private final ArrayList<EmailAttachment> twoEmbedded = new ArrayList<EmailAttachment>();
- private final ArrayList<EmailAttachment> oneAttach = new ArrayList<EmailAttachment>();
- private final ArrayList<EmailAttachment> twoAttach = new ArrayList<EmailAttachment>();
- // private final ArrayList<EmailAttachment> noEmbedded = new ArrayList<EmailAttachment>();
- // private final ArrayList<EmailAttachment> noAttach = new ArrayList<EmailAttachment>();
- private final ArrayList<EmailAttachment> bothAttach = new ArrayList<EmailAttachment>();
- /**
- *
- * @param receivers
- * @param subjectField
- * @param textMessage
- * @param htmlMessage
- * @param folder
- * @param cc
- * @param bcc
- * @param emailAttachments
- * @param sendDate
- * @param receiveDate
- *
- * This method takes care of initializing the different mailBeans for testing purposes to ensure that
- * all cases work
- * Validation of required fields for the Jodd Email class will be validated in GUI when getting fields
- * required values: receivers, subjectField, textMessage (From field is set by the ConfigBean)
- */
- private void sendEmail(ArrayList<String> receivers, String subjectField, String textMessage, String htmlMessage,
- String folder, ArrayList<String> cc, ArrayList<String> bcc, ArrayList<EmailAttachment> emailAttachments,
- Date sendDate, Date receiveDate){
- MailConfigBean sendConfigBean = new MailConfigBean("smtp.gmail.com", sender, sPass);
- MailConfigBean receiveConfigBean = new MailConfigBean("imap.gmail.com", receiver, rPass);
- BasicSendAndReceive basicSendAndReceive = new BasicSendAndReceive();
- MailBean mailBeanSend = new MailBean(receivers, sendConfigBean.getUserEmailAddress(), subjectField, textMessage, htmlMessage, folder, 0, cc,
- bcc, emailAttachments, sendDate, receiveDate);
- //for(String s : receivers)
- // mailBeanSend.getToField().add(receiver);
- // mailBeanSend.setFromField(sendConfigBean.getUserEmailAddress());
- // mailBeanSend.setSubjectField(subjectField);
- // mailBeanSend.setTextMessageField(textMessage);
- // mailBeanSend.setHtmlMessageField(htmlMessage);
- // mailBeanSend.setFolder(folder);
- // // no need for status in
- // mailBeanSend.setMailStatus(0);
- // mailBeanSend.setCcField(cc);
- // mailBeanSend.setBccField(bcc);
- // mailBeanSend.setAttachments(emailAttachments);
- String messageId = basicSendAndReceive.sendEmail(mailBeanSend, sendConfigBean);
- log.info("MessageId is " + messageId);
- // Add a five second pause to allow the Gmail server to receive what has
- // been sent
- try {
- Thread.sleep(5000);
- } catch (InterruptedException e) {
- log.error("Threaded sleep failed", e);
- }
- ArrayList<MailBean> mailBeanReceive = basicSendAndReceive.receiveEmail(receiveConfigBean);
- boolean test = false;
- // An optional block of log statements so that you can see the message
- log.info(" To: " + mailBeanSend.getToField().get(0) + " : " + mailBeanReceive.get(0).getToField().get(0));
- log.info(" From: " + mailBeanSend.getFromField() + " : " + mailBeanReceive.get(0).getFromField());
- log.info("Subject: " + mailBeanSend.getSubjectField() + " : " + mailBeanReceive.get(0).getSubjectField());
- log.info(" Text: " + mailBeanSend.getTextMessageField() + "=" + mailBeanSend.getTextMessageField().length()
- + " : " + mailBeanReceive.get(0).getTextMessageField() + "="
- + mailBeanReceive.get(0).getTextMessageField().length());
- log.info(" Html: " + mailBeanSend.getHtmlMessageField() + " : " + mailBeanReceive.get(0).getHtmlMessageField());
- log.info(" folder: " + mailBeanSend.getFolder() + " : " + mailBeanReceive.get(0).getFolder());
- log.info(" cc: " + mailBeanSend.getCcField() + " : " + mailBeanReceive.get(0).getCcField());
- log.info(" bcc: " + mailBeanSend.getBccField() + " : " + mailBeanReceive.get(0).getBccField());
- for(int i = 0; i < mailBeanSend.getAttachments().size(); i++){
- log.info("attachId: " + mailBeanSend.getAttachments().get(i).getContentId() + " : " + mailBeanReceive.get(0).getAttachments().get(i).getContentId());
- }
- log.info("Sent @: " + mailBeanSend.getSendDate() + " : " + mailBeanReceive.get(0).getSendDate());
- log.info("Rcvd @: " + mailBeanSend.getReceiveDate() + " : " + mailBeanReceive.get(0).getReceiveDate());
- //TODO: Test for equality between the beans
- // A better test approach is to have a custom equals and hashCode method
- // in the MailBean. This way you could just write:
- // assertEquals(mailSendbean, mailReceiveBean);
- // I leave this for you to implement. Here is the crude implementation.
- // if (mailBeanSend.getToField().get(0).trim().equals(mailBeansReceive.get(0).getToField().get(0).trim())) {
- // if (mailBeanSend.getFromField().trim().equals(mailBeansReceive.get(0).getFromField().trim())) {
- // if (mailBeanSend.getSubjectField().trim().equals(mailBeansReceive.get(0).getSubjectField().trim())) {
- // if (mailBeanSend.getTextMessageField().trim()
- // .equals(mailBeansReceive.get(0).getTextMessageField().trim())) {
- // test = true;
- // }
- // }
- // }
- // }
- // Testing by using hashCode, and equals if necessary
- //if(mailBeanSend.hashCode() == mailBeanReceive.hashCode()))
- // It would be better if we were informed which fields did not match.
- // Another feature left to you.
- // assertTrue("Messages are not the same", test);
- }
- // Extra code for when i may need it
- // EmailMessage htmlMessage = new EmailMessage(
- // "<html><META http-equiv=Content-Type content=\"text/html; charset=utf-8\">" +
- // "<body><h1>Hey!</h1><img src='cid:c.png'><h2>Hay!</h2></body></html>",
- // MimeTypes.MIME_TEXT_HTML);
- // email.addMessage(htmlMessage);
- // if (attachment.getContentId() != null)
- // mailBean.getEmbedded().add(attachment);
- // else
- // mailBean.getAttachments().add(attachment);
- /**
- * Basic Email with only required fields and one receiver
- */
- //@Test
- public void test1(){
- oneReceiver.add(receiver);
- sendEmail(oneReceiver, "First Test", "Basic Email with only required fields and one receiver", null, "Sent", null, null, null, null, null);
- }
- /**
- * Basic Email with only required fields and more than one receiver
- */
- //@Test
- public void test2(){
- twoReceiver.add(receiver);
- twoReceiver.add(email1);
- sendEmail(twoReceiver, "Second Test", "Basic Email with only required fields and more than one receiver", null, "Sent", null, null, null, null, null);
- }
- /**
- * Basic Email with Required fields, one receiver and one cc
- */
- //@Test
- public void test3(){
- oneReceiver.add(receiver);
- oneMemberCc.add(email1);
- sendEmail(oneReceiver, "Third Test", "Basic Email with Required fields, one receiver and one cc", null, "Sent", oneMemberCc, null, null, null, null);
- }
- /**
- * Basic Email with Required fields, one receiver and two cc
- */
- //@Test
- public void test4(){
- oneReceiver.add(receiver);
- twoMemberCc.add(email1);
- twoMemberCc.add(email2);
- sendEmail(oneReceiver, "Fourth Test", "Basic Email with Required fields, one receiver and two cc", null, "Sent", twoMemberCc, null, null, null, null);
- }
- /**
- * Basic Email with Required fields, one receiver and one bcc
- */
- //@Test
- public void test5(){
- oneReceiver.add(receiver);
- oneMemberBcc.add(email3);
- sendEmail(oneReceiver, "Fifth Test", "Basic Email with Required fields, one receiver and one bcc", null, "Sent", null, oneMemberBcc, null, null, null);
- }
- /**
- * Basic Email with Required fields, one receiver and two bcc
- */
- //@Test
- public void test6(){
- oneReceiver.add(receiver);
- twoMemberBcc.add(email3);
- twoMemberBcc.add(email4);
- sendEmail(oneReceiver, "Sixth Test", "Basic Email with Required fields, one receiver and two bcc", null, "Sent", null, twoMemberBcc, null, null, null);
- }
- /**
- * Basic Email with Required fields, one receiver, and one attachment Embedded
- */
- //@Test
- public void test7(){
- oneReceiver.add(receiver);
- oneEmbedded.add(e1);
- sendEmail(oneReceiver, "Seventh Test", "Basic Email with Required fields, one receiver and one attachment Embedded", null, "Sent", null, null, oneEmbedded, null, null);
- }
- /**
- * Basic Email with Required fields, one receiver and two attachments Embedded
- */
- //@Test
- public void test8(){
- oneReceiver.add(receiver);
- twoEmbedded.add(e1);
- twoEmbedded.add(e2);
- sendEmail(oneReceiver, "Eigth Test", "Basic Email with Required fields, one receiver and two attachments Embedded", null, "Sent", null, null, twoEmbedded, null, null);
- }
- /**
- * Basic Email with Required fields, one receiver and one attachment
- */
- //@Test
- public void test9(){
- oneReceiver.add(receiver);
- oneAttach.add(a1);
- sendEmail(oneReceiver, "Ninth Test", "Basic Email with Required fields, one receiver and one attachment", null, "Sent", null, null, oneAttach, null, null);
- }
- /**
- * Basic Email with Required fields, one receiver and two attachments
- */
- //@Test
- public void test10(){
- oneReceiver.add(receiver);
- twoAttach.add(a1);
- twoAttach.add(a2);
- log.info(" Getting the names of the attachments " + twoAttach.get(0).getName());
- log.info(twoAttach.get(1).getName());
- sendEmail(oneReceiver, "Tenth Test", "Basic Email with Required fields, one receiver and two attachments", null, "Sent", null, null, twoAttach, null, null);
- }
- /**
- * Basic Email with Required fields, one receiver and both attachments, Embedded and not
- */
- //@Test
- public void test11(){
- oneReceiver.add(receiver);
- bothAttach.add(a1);
- bothAttach.add(e1);
- sendEmail(oneReceiver, "Eleleventh Test", "Basic Email with Required fields, one receiver and both attachments, Embedded and not", null, "Sent", null, null, bothAttach, null, null);
- }
- /**
- * Basic Email with Required fields, one receiver and HTML text added
- */
- @Test
- public void test12(){
- oneReceiver.add(receiver);
- sendEmail(oneReceiver, "Twelvth Test", "Basic Email with Required fields, one receiver and HTML text added", html, "Sent", null, null, null, null, null);
- }
- /**
- * Basic Email with Required fields, one receiver, one embedded attachment and HTML text added
- */
- //@Test
- public void test13(){
- oneReceiver.add(receiver);
- oneEmbedded.add(e1);
- sendEmail(oneReceiver, "Thirteenth Test", "Basic Email with Required fields, one receiver, one embedded attachment and HTML text added", html, "Sent", null, null, null, null, null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement