1 54 55 package junitx.framework; 56 57 63 public class ThrowableAssert { 64 65 68 private ThrowableAssert() { 69 } 70 71 82 public static void assertEquals(String message, 83 Throwable expected, 84 Throwable actual) { 85 ObjectAssert.assertInstanceOf(message, expected.getClass(), actual); 86 if (message == null) { 87 Assert.assertEquals("[message]", expected.getMessage(), actual.getMessage()); 88 Assert.assertEquals("[localized message]", expected.getLocalizedMessage(), actual.getLocalizedMessage()); 89 } else { 90 Assert.assertEquals(message + " [message]", expected.getMessage(), actual.getMessage()); 91 Assert.assertEquals(message + " [localized message]", expected.getLocalizedMessage(), actual.getLocalizedMessage()); 92 } 93 } 94 95 101 public static void assertEquals(Throwable expected, 102 Throwable actual) { 103 assertEquals(null, expected, actual); 104 } 105 106 117 public static void assertSimilar(String message, 118 Throwable expected, 119 Throwable actual) { 120 ObjectAssert.assertInstanceOf(message, expected.getClass(), actual); 121 if (expected.getMessage() == null) { 122 throw new IllegalArgumentException ("String to search cannot be <null>"); 123 } 124 if (actual.getMessage() != null) { 125 if (actual.getMessage().indexOf(expected.getMessage()) >= 0) { 126 return; 127 } 128 } 129 if (actual.getLocalizedMessage() != null) { 130 if (actual.getLocalizedMessage().indexOf(expected.getMessage()) >= 0) { 131 return; 132 } 133 } 134 failContainsMessage(message, expected, actual); 135 } 136 137 143 public static void assertSimilar(Throwable expected, 144 Throwable actual) { 145 assertSimilar(null, expected, actual); 146 } 147 148 private static void failContainsMessage(String message, 149 Throwable expected, 150 Throwable actual) { 151 String formatted = ""; 152 if (message != null) { 153 formatted = message + " "; 154 } 155 if (actual.getMessage().equals(actual.getLocalizedMessage())) { 156 Assert.fail(formatted + "Expected containing <" + expected.getMessage() + "> in <" + actual.getMessage() + ">"); 157 } else { 158 Assert.fail(formatted + "Expected containing <" + expected.getMessage() + "> in <" + actual.getMessage() + "> or <" + actual.getLocalizedMessage() + "> (localized)"); 159 } 160 } 161 162 } 163 | Popular Tags |