You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MimeMessageAssert.java 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.test.html;
  21. import java.io.IOException;
  22. import javax.mail.MessagingException;
  23. import javax.mail.internet.MimeMessage;
  24. import javax.mail.internet.MimeMultipart;
  25. import org.assertj.core.api.AbstractAssert;
  26. import org.assertj.core.api.Assertions;
  27. public final class MimeMessageAssert extends AbstractAssert<MimeMessageAssert, MimeMessage> {
  28. public MimeMessageAssert(MimeMessage mimeMessage) {
  29. super(mimeMessage, MimeMessageAssert.class);
  30. }
  31. public static MimeMessageAssert assertThat(MimeMessage m) {
  32. return new MimeMessageAssert(m);
  33. }
  34. public MultipartMessageAssert isMultipart() {
  35. isNotNull();
  36. try {
  37. Object content = actual.getContent();
  38. Assertions.assertThat(content).isInstanceOf(MimeMultipart.class);
  39. MimeMultipart m = (MimeMultipart) content;
  40. return new MultipartMessageAssert(m);
  41. } catch (MessagingException | IOException e) {
  42. throw new IllegalStateException(e);
  43. }
  44. }
  45. /**
  46. * Convenience method for {@code isMultipart().isHtml()}.
  47. */
  48. public HtmlFragmentAssert isHtml() {
  49. return isMultipart()
  50. .isHtml();
  51. }
  52. public MimeMessageAssert hasRecipient(String userEmail) {
  53. isNotNull();
  54. try {
  55. Assertions
  56. .assertThat(actual.getHeader("To", null))
  57. .isEqualTo(String.format("<%s>", userEmail));
  58. } catch (MessagingException e) {
  59. throw new IllegalStateException(e);
  60. }
  61. return this;
  62. }
  63. public MimeMessageAssert subjectContains(String text) {
  64. isNotNull();
  65. try {
  66. Assertions.assertThat(actual.getSubject()).contains(text);
  67. } catch (MessagingException e) {
  68. throw new IllegalStateException(e);
  69. }
  70. return this;
  71. }
  72. }