This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-lang.git
commit a4f38e95eee8bfa6fd79b11bea289ca7c8c2a325 Author: Gary Gregory <[email protected]> AuthorDate: Thu Jul 25 08:46:19 2024 -0400 Add DefaultExceptionContextTest.testFormattedExceptionMessageExceptionHandling() --- ...st.java => ObjectToStringRuntimeException.java} | 30 +++++++++++----------- .../exception/DefaultExceptionContextTest.java | 19 +++++++++++++- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/test/java/org/apache/commons/lang3/exception/DefaultExceptionContextTest.java b/src/test/java/org/apache/commons/lang3/ObjectToStringRuntimeException.java similarity index 58% copy from src/test/java/org/apache/commons/lang3/exception/DefaultExceptionContextTest.java copy to src/test/java/org/apache/commons/lang3/ObjectToStringRuntimeException.java index 00aa50ba8..1e37b7580 100644 --- a/src/test/java/org/apache/commons/lang3/exception/DefaultExceptionContextTest.java +++ b/src/test/java/org/apache/commons/lang3/ObjectToStringRuntimeException.java @@ -14,27 +14,27 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.commons.lang3.exception; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +package org.apache.commons.lang3; /** - * JUnit tests for DefaultExceptionContext. + * An object that throws a runtime exception when you call {@link #toString()}. */ -public class DefaultExceptionContextTest extends AbstractExceptionContextTest<DefaultExceptionContext> { +public class ObjectToStringRuntimeException { - @Override - @BeforeEach - public void setUp() throws Exception { - exceptionContext = new DefaultExceptionContext(); - super.setUp(); - } + private final String message; - @Test - public void testFormattedExceptionMessageNull() { - exceptionContext = new DefaultExceptionContext(); - exceptionContext.getFormattedExceptionMessage(null); + public ObjectToStringRuntimeException(final String message) { + this.message = message; } + /** + * Throws {@link IllegalStateException}. + * + * @throws IllegalStateException always thrown. + */ + @Override + public String toString() { + throw new IllegalStateException(new NullPointerException(message)); + } } diff --git a/src/test/java/org/apache/commons/lang3/exception/DefaultExceptionContextTest.java b/src/test/java/org/apache/commons/lang3/exception/DefaultExceptionContextTest.java index 00aa50ba8..29100f266 100644 --- a/src/test/java/org/apache/commons/lang3/exception/DefaultExceptionContextTest.java +++ b/src/test/java/org/apache/commons/lang3/exception/DefaultExceptionContextTest.java @@ -16,6 +16,10 @@ */ package org.apache.commons.lang3.exception; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.apache.commons.lang3.ObjectToStringRuntimeException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -31,10 +35,23 @@ public class DefaultExceptionContextTest extends AbstractExceptionContextTest<De super.setUp(); } + @Test + public void testFormattedExceptionMessageExceptionHandling() { + exceptionContext = new DefaultExceptionContext(); + final String label1 = "throws 1"; + final String label2 = "throws 2"; + exceptionContext.addContextValue(label1, new ObjectToStringRuntimeException(label1)); + exceptionContext.addContextValue(label2, new ObjectToStringRuntimeException(label2)); + final String message = exceptionContext.getFormattedExceptionMessage(TEST_MESSAGE); + assertTrue(message.startsWith(TEST_MESSAGE)); + assertTrue(message.contains(label1)); + assertTrue(message.contains(label2)); + } + @Test public void testFormattedExceptionMessageNull() { exceptionContext = new DefaultExceptionContext(); - exceptionContext.getFormattedExceptionMessage(null); + assertEquals("", exceptionContext.getFormattedExceptionMessage(null)); } }
