View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.geometry.core;
18  
19  import java.util.regex.Pattern;
20  
21  import org.junit.jupiter.api.Assertions;
22  import org.junit.jupiter.api.function.Executable;
23  
24  /** Class containing various geometry-related test utilities.
25   */
26  public final class GeometryTestUtils {
27  
28      /** Utility class; no instantiation. */
29      private GeometryTestUtils() {}
30  
31      /** Asserts that the given value is positive infinity.
32       * @param value
33       */
34      public static void assertPositiveInfinity(final double value) {
35          final String msg = "Expected value to be positive infinity but was " + value;
36          Assertions.assertTrue(Double.isInfinite(value), msg);
37          Assertions.assertTrue(value > 0, msg);
38      }
39  
40      /** Asserts that the given value is negative infinity..
41       * @param value
42       */
43      public static void assertNegativeInfinity(final double value) {
44          final String msg = "Expected value to be negative infinity but was " + value;
45          Assertions.assertTrue(Double.isInfinite(value), msg);
46          Assertions.assertTrue(value < 0, msg);
47      }
48  
49      /** Asserts that the Executable throws an exception matching the given type and message.
50       * @param executable the Executable instance
51       * @param exceptionType the expected exception type
52       * @param message the expected exception message; may be null
53       */
54      public static <T extends Throwable> void assertThrowsWithMessage(final Executable executable,
55              final Class<T> exceptionType, final String message) {
56          Assertions.assertEquals(message, Assertions.assertThrows(exceptionType, executable).getMessage());
57      }
58  
59      /** Asserts that the Executable throws an exception of the given type with a non-null message matching
60       * the specified regex pattern.
61       * @param executable the Executable instance
62       * @param exceptionType the expected exception type
63       * @param pattern regex pattern to match
64       */
65      public static <T extends Throwable> void assertThrowsWithMessage(final Executable executable,
66              final Class<T> exceptionType, final Pattern pattern) {
67          final String message = Assertions.assertThrows(exceptionType, executable).getMessage();
68          Assertions.assertTrue(pattern.matcher(message).matches(),
69                  "Expected exception message to match /" + pattern + "/ but was [" + message + "]");
70      }
71  
72      /** Assert that a string contains a given substring value.
73       * @param substr
74       * @param actual
75       */
76      public static void assertContains(final String substr, final String actual) {
77          final String msg = "Expected string to contain [" + substr + "] but was [" + actual + "]";
78          Assertions.assertTrue(actual.contains(substr), msg);
79      }
80  
81      /** Assert that the {@code equals} method of the argument meets the following requirements:
82       * <ol>
83       *  <li>{@code obj} is not equal to null</li>
84       *  <li>{@code obj} is not equal to an instance of a supertype ({@code java.lang.Object})</li>
85       *  <li>{@code obj} is equal to itself</li>
86       * </ol>
87       * @param obj object to test the {@code equals} method of
88       */
89      public static void assertSimpleEqualsCases(final Object obj) {
90          // Use the JUnit boolean assertions here to ensure that the equals methods are actually
91          // invoked and no assertion shortcuts are taken
92  
93          Assertions.assertFalse(obj.equals(null), "Object should not equal null");
94  
95          if (obj.getClass().getSuperclass() != null) {
96              Assertions.assertFalse(obj.equals(new Object()), "Object should not equal an instance of different type");
97          }
98  
99          Assertions.assertTrue(obj.equals(obj), "Object should equal itself");
100     }
101 }