Advertisement
Guest User

code

a guest
Sep 15th, 2015
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.18 KB | None | 0 0
  1. package com.chrisbaur.test.mailaction;
  2.  
  3. import static org.junit.Assert.assertTrue;
  4.  
  5. import java.io.File;
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import java.util.Date;
  9.  
  10. import org.junit.Rule;
  11. import org.junit.Test;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14.  
  15. import com.chrisbaur.mailaction.BasicSendAndReceive;
  16. import com.chrisbaur.mailbean.MailBean;
  17. import com.chrisbaur.properties.MailConfigBean;
  18. import com.chrisbaur.test.MethodLogger;
  19.  
  20. import jodd.mail.EmailAttachment;
  21.  
  22. /**
  23. * A basic test method that determines if a simple message sent is the same
  24. * message received
  25. *
  26. * @author Ken Fogel
  27. *
  28. */
  29. public class MailActionTest {
  30.  
  31. // A Rule is implemented as a class with methods that are associated
  32. // with the lifecycle of a unit test. These methods run when required.
  33. // Avoids the need to cut and paste code into every test method.
  34. @Rule
  35. public MethodLogger methodLogger = new MethodLogger();
  36.  
  37. // Real programmers use logging, not System.out.println
  38. private final Logger log = LoggerFactory.getLogger(getClass().getName());
  39.  
  40. //Email accounts and passwords
  41. private final String receiver = "christo.receive@gmail.com";
  42. private final String sender = "christo.send@gmail.com";
  43. private final String rPass = "Receivemail!";
  44. private final String sPass = "Sendmail!";
  45. private final String email1 = "chriskbaur@gmail.com";
  46. private final String email2 = "christobaur@hotmail.fr";
  47. private final String email3 = "c-baur@hotmail.com";
  48. private final String email4 = "christoffer.baur@dawsoncollege.qc.ca";
  49. private final String html = "<html><META http-equiv=Content-Type content=\"text/html; charset=utf-8\">" +
  50. "<body><h1>Hey!</h1><img src='cid:cat.png'><h2>Hay!</h2></body></html>";
  51. private final EmailAttachment e1 = EmailAttachment.attachment().bytes(new File("cat.jpg")).setInline("cat.jpg").create();
  52. private final EmailAttachment e2 = EmailAttachment.attachment().bytes(new File("cat.jpg")).setInline("cat.jpg").create();
  53. private final EmailAttachment a1 = EmailAttachment.attachment().file("cat.jpg").create();
  54. private final EmailAttachment a2 = EmailAttachment.attachment().file("cat.jpg").create();
  55.  
  56. private final ArrayList<String> twoReceiver = new ArrayList<String>();
  57. private final ArrayList<String> oneReceiver = new ArrayList<String>();
  58. private final ArrayList<String> twoMemberCc = new ArrayList<String>();
  59. private final ArrayList<String> twoMemberBcc = new ArrayList<String>();
  60. private final ArrayList<String> oneMemberCc = new ArrayList<String>();
  61. private final ArrayList<String> oneMemberBcc = new ArrayList<String>();
  62. // private final ArrayList<String> noMemberCc = new ArrayList<String>();
  63. // private final ArrayList<String> noMemberBcc = new ArrayList<String>();
  64. private final ArrayList<EmailAttachment> oneEmbedded = new ArrayList<EmailAttachment>();
  65. private final ArrayList<EmailAttachment> twoEmbedded = new ArrayList<EmailAttachment>();
  66. private final ArrayList<EmailAttachment> oneAttach = new ArrayList<EmailAttachment>();
  67. private final ArrayList<EmailAttachment> twoAttach = new ArrayList<EmailAttachment>();
  68. // private final ArrayList<EmailAttachment> noEmbedded = new ArrayList<EmailAttachment>();
  69. // private final ArrayList<EmailAttachment> noAttach = new ArrayList<EmailAttachment>();
  70. private final ArrayList<EmailAttachment> bothAttach = new ArrayList<EmailAttachment>();
  71.  
  72. /**
  73. *
  74. * @param receivers
  75. * @param subjectField
  76. * @param textMessage
  77. * @param htmlMessage
  78. * @param folder
  79. * @param cc
  80. * @param bcc
  81. * @param emailAttachments
  82. * @param sendDate
  83. * @param receiveDate
  84. *
  85. * This method takes care of initializing the different mailBeans for testing purposes to ensure that
  86. * all cases work
  87. * Validation of required fields for the Jodd Email class will be validated in GUI when getting fields
  88. * required values: receivers, subjectField, textMessage (From field is set by the ConfigBean)
  89. */
  90. private void sendEmail(ArrayList<String> receivers, String subjectField, String textMessage, String htmlMessage,
  91. String folder, ArrayList<String> cc, ArrayList<String> bcc, ArrayList<EmailAttachment> emailAttachments,
  92. Date sendDate, Date receiveDate){
  93.  
  94. MailConfigBean sendConfigBean = new MailConfigBean("smtp.gmail.com", sender, sPass);
  95. MailConfigBean receiveConfigBean = new MailConfigBean("imap.gmail.com", receiver, rPass);
  96. BasicSendAndReceive basicSendAndReceive = new BasicSendAndReceive();
  97.  
  98. MailBean mailBeanSend = new MailBean(receivers, sendConfigBean.getUserEmailAddress(), subjectField, textMessage, htmlMessage, folder, 0, cc,
  99. bcc, emailAttachments, sendDate, receiveDate);
  100. //for(String s : receivers)
  101. // mailBeanSend.getToField().add(receiver);
  102. // mailBeanSend.setFromField(sendConfigBean.getUserEmailAddress());
  103. // mailBeanSend.setSubjectField(subjectField);
  104. // mailBeanSend.setTextMessageField(textMessage);
  105. // mailBeanSend.setHtmlMessageField(htmlMessage);
  106. // mailBeanSend.setFolder(folder);
  107. // // no need for status in
  108. // mailBeanSend.setMailStatus(0);
  109. // mailBeanSend.setCcField(cc);
  110. // mailBeanSend.setBccField(bcc);
  111. // mailBeanSend.setAttachments(emailAttachments);
  112. String messageId = basicSendAndReceive.sendEmail(mailBeanSend, sendConfigBean);
  113. log.info("MessageId is " + messageId);
  114.  
  115. // Add a five second pause to allow the Gmail server to receive what has
  116. // been sent
  117. try {
  118. Thread.sleep(5000);
  119. } catch (InterruptedException e) {
  120. log.error("Threaded sleep failed", e);
  121. }
  122.  
  123. ArrayList<MailBean> mailBeanReceive = basicSendAndReceive.receiveEmail(receiveConfigBean);
  124.  
  125. boolean test = false;
  126.  
  127. // An optional block of log statements so that you can see the message
  128. log.info(" To: " + mailBeanSend.getToField().get(0) + " : " + mailBeanReceive.get(0).getToField().get(0));
  129. log.info(" From: " + mailBeanSend.getFromField() + " : " + mailBeanReceive.get(0).getFromField());
  130. log.info("Subject: " + mailBeanSend.getSubjectField() + " : " + mailBeanReceive.get(0).getSubjectField());
  131. log.info(" Text: " + mailBeanSend.getTextMessageField() + "=" + mailBeanSend.getTextMessageField().length()
  132. + " : " + mailBeanReceive.get(0).getTextMessageField() + "="
  133. + mailBeanReceive.get(0).getTextMessageField().length());
  134. log.info(" Html: " + mailBeanSend.getHtmlMessageField() + " : " + mailBeanReceive.get(0).getHtmlMessageField());
  135. log.info(" folder: " + mailBeanSend.getFolder() + " : " + mailBeanReceive.get(0).getFolder());
  136. log.info(" cc: " + mailBeanSend.getCcField() + " : " + mailBeanReceive.get(0).getCcField());
  137. log.info(" bcc: " + mailBeanSend.getBccField() + " : " + mailBeanReceive.get(0).getBccField());
  138. for(int i = 0; i < mailBeanSend.getAttachments().size(); i++){
  139. log.info("attachId: " + mailBeanSend.getAttachments().get(i).getContentId() + " : " + mailBeanReceive.get(0).getAttachments().get(i).getContentId());
  140. }
  141. log.info("Sent @: " + mailBeanSend.getSendDate() + " : " + mailBeanReceive.get(0).getSendDate());
  142. log.info("Rcvd @: " + mailBeanSend.getReceiveDate() + " : " + mailBeanReceive.get(0).getReceiveDate());
  143.  
  144. //TODO: Test for equality between the beans
  145.  
  146. // A better test approach is to have a custom equals and hashCode method
  147. // in the MailBean. This way you could just write:
  148. // assertEquals(mailSendbean, mailReceiveBean);
  149. // I leave this for you to implement. Here is the crude implementation.
  150. // if (mailBeanSend.getToField().get(0).trim().equals(mailBeansReceive.get(0).getToField().get(0).trim())) {
  151. // if (mailBeanSend.getFromField().trim().equals(mailBeansReceive.get(0).getFromField().trim())) {
  152. // if (mailBeanSend.getSubjectField().trim().equals(mailBeansReceive.get(0).getSubjectField().trim())) {
  153. // if (mailBeanSend.getTextMessageField().trim()
  154. // .equals(mailBeansReceive.get(0).getTextMessageField().trim())) {
  155. // test = true;
  156. // }
  157. // }
  158. // }
  159. // }
  160.  
  161. // Testing by using hashCode, and equals if necessary
  162. //if(mailBeanSend.hashCode() == mailBeanReceive.hashCode()))
  163.  
  164. // It would be better if we were informed which fields did not match.
  165. // Another feature left to you.
  166.  
  167. // assertTrue("Messages are not the same", test);
  168. }
  169.  
  170. // Extra code for when i may need it
  171. // EmailMessage htmlMessage = new EmailMessage(
  172. // "<html><META http-equiv=Content-Type content=\"text/html; charset=utf-8\">" +
  173. // "<body><h1>Hey!</h1><img src='cid:c.png'><h2>Hay!</h2></body></html>",
  174. // MimeTypes.MIME_TEXT_HTML);
  175. // email.addMessage(htmlMessage);
  176. // if (attachment.getContentId() != null)
  177. // mailBean.getEmbedded().add(attachment);
  178. // else
  179. // mailBean.getAttachments().add(attachment);
  180. /**
  181. * Basic Email with only required fields and one receiver
  182. */
  183. //@Test
  184. public void test1(){
  185. oneReceiver.add(receiver);
  186. sendEmail(oneReceiver, "First Test", "Basic Email with only required fields and one receiver", null, "Sent", null, null, null, null, null);
  187. }
  188. /**
  189. * Basic Email with only required fields and more than one receiver
  190. */
  191. //@Test
  192. public void test2(){
  193. twoReceiver.add(receiver);
  194. twoReceiver.add(email1);
  195. sendEmail(twoReceiver, "Second Test", "Basic Email with only required fields and more than one receiver", null, "Sent", null, null, null, null, null);
  196. }
  197. /**
  198. * Basic Email with Required fields, one receiver and one cc
  199. */
  200. //@Test
  201. public void test3(){
  202. oneReceiver.add(receiver);
  203. oneMemberCc.add(email1);
  204. sendEmail(oneReceiver, "Third Test", "Basic Email with Required fields, one receiver and one cc", null, "Sent", oneMemberCc, null, null, null, null);
  205. }
  206. /**
  207. * Basic Email with Required fields, one receiver and two cc
  208. */
  209. //@Test
  210. public void test4(){
  211. oneReceiver.add(receiver);
  212. twoMemberCc.add(email1);
  213. twoMemberCc.add(email2);
  214. sendEmail(oneReceiver, "Fourth Test", "Basic Email with Required fields, one receiver and two cc", null, "Sent", twoMemberCc, null, null, null, null);
  215. }
  216. /**
  217. * Basic Email with Required fields, one receiver and one bcc
  218. */
  219. //@Test
  220. public void test5(){
  221. oneReceiver.add(receiver);
  222. oneMemberBcc.add(email3);
  223. sendEmail(oneReceiver, "Fifth Test", "Basic Email with Required fields, one receiver and one bcc", null, "Sent", null, oneMemberBcc, null, null, null);
  224. }
  225. /**
  226. * Basic Email with Required fields, one receiver and two bcc
  227. */
  228. //@Test
  229. public void test6(){
  230. oneReceiver.add(receiver);
  231. twoMemberBcc.add(email3);
  232. twoMemberBcc.add(email4);
  233. sendEmail(oneReceiver, "Sixth Test", "Basic Email with Required fields, one receiver and two bcc", null, "Sent", null, twoMemberBcc, null, null, null);
  234. }
  235. /**
  236. * Basic Email with Required fields, one receiver, and one attachment Embedded
  237. */
  238. //@Test
  239. public void test7(){
  240. oneReceiver.add(receiver);
  241. oneEmbedded.add(e1);
  242. sendEmail(oneReceiver, "Seventh Test", "Basic Email with Required fields, one receiver and one attachment Embedded", null, "Sent", null, null, oneEmbedded, null, null);
  243. }
  244. /**
  245. * Basic Email with Required fields, one receiver and two attachments Embedded
  246. */
  247. //@Test
  248. public void test8(){
  249. oneReceiver.add(receiver);
  250. twoEmbedded.add(e1);
  251. twoEmbedded.add(e2);
  252. sendEmail(oneReceiver, "Eigth Test", "Basic Email with Required fields, one receiver and two attachments Embedded", null, "Sent", null, null, twoEmbedded, null, null);
  253. }
  254. /**
  255. * Basic Email with Required fields, one receiver and one attachment
  256. */
  257. //@Test
  258. public void test9(){
  259. oneReceiver.add(receiver);
  260. oneAttach.add(a1);
  261. sendEmail(oneReceiver, "Ninth Test", "Basic Email with Required fields, one receiver and one attachment", null, "Sent", null, null, oneAttach, null, null);
  262. }
  263. /**
  264. * Basic Email with Required fields, one receiver and two attachments
  265. */
  266. //@Test
  267. public void test10(){
  268. oneReceiver.add(receiver);
  269. twoAttach.add(a1);
  270. twoAttach.add(a2);
  271. log.info(" Getting the names of the attachments " + twoAttach.get(0).getName());
  272. log.info(twoAttach.get(1).getName());
  273. sendEmail(oneReceiver, "Tenth Test", "Basic Email with Required fields, one receiver and two attachments", null, "Sent", null, null, twoAttach, null, null);
  274. }
  275. /**
  276. * Basic Email with Required fields, one receiver and both attachments, Embedded and not
  277. */
  278. //@Test
  279. public void test11(){
  280. oneReceiver.add(receiver);
  281. bothAttach.add(a1);
  282. bothAttach.add(e1);
  283. sendEmail(oneReceiver, "Eleleventh Test", "Basic Email with Required fields, one receiver and both attachments, Embedded and not", null, "Sent", null, null, bothAttach, null, null);
  284. }
  285. /**
  286. * Basic Email with Required fields, one receiver and HTML text added
  287. */
  288. @Test
  289. public void test12(){
  290. oneReceiver.add(receiver);
  291. sendEmail(oneReceiver, "Twelvth Test", "Basic Email with Required fields, one receiver and HTML text added", html, "Sent", null, null, null, null, null);
  292. }
  293. /**
  294. * Basic Email with Required fields, one receiver, one embedded attachment and HTML text added
  295. */
  296. //@Test
  297. public void test13(){
  298. oneReceiver.add(receiver);
  299. oneEmbedded.add(e1);
  300. 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);
  301. }
  302.  
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement