View Javadoc
1   package org.djunits.value;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertTrue;
5   
6   import org.junit.jupiter.api.Test;
7   
8   /**
9    * Test the constructors for ValueException.
10   * <p>
11   * Copyright (c) 2013-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
12   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
13   * </p>
14   * @version $Revision: 954 $, $LastChangedDate: 2022-01-10 03:42:57 +0100 (Mon, 10 Jan 2022) $, by $Author: averbraeck $,
15   *          initial version 27 sep. 2015 <br>
16   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
17   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
18   */
19  public class ValueExceptionTest
20  {
21      /**
22       * Test all constructors for ValueException.
23       */
24      @Test
25      public final void valueExceptionTest()
26      {
27          String message = "MessageString";
28          Exception e = new ValueRuntimeException(message);
29          assertTrue(null != e, "Exception should not be null");
30          assertEquals(message, e.getMessage(), "message should be our message");
31          assertEquals(null, e.getCause(), "cause should be null");
32          e = new ValueRuntimeException();
33          assertTrue(null != e, "Exception should not be null");
34          assertEquals(null, e.getCause(), "cause should be null");
35          String causeString = "CauseString";
36          Throwable cause = new Throwable(causeString);
37          e = new ValueRuntimeException(cause);
38          assertTrue(null != e, "Exception should not be null");
39          assertEquals(cause, e.getCause(), "cause should not be our cause");
40          assertEquals(causeString, e.getCause().getMessage(), "cause description should be our cause string");
41          e = new ValueRuntimeException(message, cause);
42          assertTrue(null != e, "Exception should not be null");
43          assertEquals(message, e.getMessage(), "message should be our message");
44          assertEquals(cause, e.getCause(), "cause should not be our cause");
45          assertEquals(causeString, e.getCause().getMessage(), "cause description should be our cause string");
46          for (boolean enableSuppression : new boolean[] {true, false})
47          {
48              for (boolean writableStackTrace : new boolean[] {true, false})
49              {
50                  e = new ValueRuntimeException(message, cause, enableSuppression, writableStackTrace);
51                  assertTrue(null != e, "Exception should not be null");
52                  assertEquals(message, e.getMessage(), "message should be our message");
53                  assertEquals(cause, e.getCause(), "cause should not be our cause");
54                  assertEquals(causeString, e.getCause().getMessage(), "cause description should be our cause string");
55                  // Don't know how to check if suppression is enabled/disabled
56                  StackTraceElement[] stackTrace = new StackTraceElement[1];
57                  stackTrace[0] = new StackTraceElement("a", "b", "c", 1234);
58                  try
59                  {
60                      e.setStackTrace(stackTrace);
61                  }
62                  catch (Exception e1)
63                  {
64                      assertTrue(writableStackTrace, "Stack trace should be writable");
65                      continue;
66                  }
67                  // You wouldn't believe it, but a call to setStackTrace if non-writable is silently ignored
68                  StackTraceElement[] retrievedStackTrace = e.getStackTrace();
69                  if (retrievedStackTrace.length > 0)
70                  {
71                      assertTrue(writableStackTrace, "stack trace should be writable");
72                  }
73              }
74          }
75      }
76  }