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.

ExceptionCauseMatcher.java 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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;
  21. import java.util.Objects;
  22. import javax.annotation.CheckForNull;
  23. import javax.annotation.Nullable;
  24. import javax.annotation.concurrent.Immutable;
  25. import org.hamcrest.Description;
  26. import org.hamcrest.Matcher;
  27. import org.hamcrest.TypeSafeMatcher;
  28. /**
  29. * Matchers designed to be used as an argument of {@link org.junit.rules.ExpectedException#expectCause(Matcher)} such as:
  30. *
  31. * <pre>
  32. * expectedException.expect(VisitException.class);
  33. * expectedException.expectCause(hasType(IllegalArgumentException.class).andMessage("file and otherFile Components can not be the same"));
  34. * </pre>
  35. *
  36. * Class strongly inspired from {@code CauseMatcher} class from {@code http://blog.codeleak.pl/2014/03/junit-expectedexception-rule-beyond.html}
  37. */
  38. @Immutable
  39. public class ExceptionCauseMatcher extends TypeSafeMatcher<Throwable> {
  40. private static final String EXPECT_NO_MESSAGE_CONSTANT = "RQXG8QTUCXOT7HZ3APPKBKUE5";
  41. private final Class<? extends Throwable> type;
  42. @CheckForNull
  43. private final String expectedMessage;
  44. private ExceptionCauseMatcher(Class<? extends Throwable> type, @Nullable String expectedMessage) {
  45. this.type = type;
  46. this.expectedMessage = expectedMessage;
  47. }
  48. public static ExceptionCauseMatcher hasType(Class<? extends Throwable> type) {
  49. return new ExceptionCauseMatcher(type, null);
  50. }
  51. public ExceptionCauseMatcher andMessage(String expectedMessage) {
  52. return new ExceptionCauseMatcher(type, Objects.requireNonNull(expectedMessage));
  53. }
  54. public ExceptionCauseMatcher andNoMessage() {
  55. return new ExceptionCauseMatcher(type, EXPECT_NO_MESSAGE_CONSTANT);
  56. }
  57. @Override
  58. protected boolean matchesSafely(Throwable item) {
  59. if (!type.isAssignableFrom(item.getClass())) {
  60. return false;
  61. }
  62. if (expectedMessage == null) {
  63. return true;
  64. }
  65. if (EXPECT_NO_MESSAGE_CONSTANT.equals(expectedMessage)) {
  66. return item.getMessage() == null;
  67. }
  68. return item.getMessage().contains(expectedMessage);
  69. }
  70. @Override
  71. public void describeTo(Description description) {
  72. description.appendText("of type ")
  73. .appendValue(type);
  74. if (EXPECT_NO_MESSAGE_CONSTANT.equals(expectedMessage)) {
  75. description.appendText(" and no message");
  76. } else if (expectedMessage != null) {
  77. description.appendText(" and message ")
  78. .appendValue(expectedMessage);
  79. }
  80. description.appendText(" but");
  81. }
  82. }