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.io.ByteArrayInputStream;
    20  import java.io.ByteArrayOutputStream;
    21  import java.io.ObjectInputStream;
    22  import java.io.ObjectOutputStream;
    23  import java.util.regex.Pattern;
    24
    25  import org.junit.Assert;
    26
    27  /** Class containing various geometry-related test utilities.
    28   */
    29  public final class GeometryTestUtils {
    30
    31      private GeometryTestUtils() {}
    32
    33      /** Asserts that the given value is positive infinity.
    34       * @param value
    35       */
    36      public static void assertPositiveInfinity(final double value) {
    37          final String msg = "Expected value to be positive infinity but was " + value;
    38          Assert.assertTrue(msg, Double.isInfinite(value));
    39          Assert.assertTrue(msg, value > 0);
    40      }
    41
    42      /** Asserts that the given value is negative infinity..
    43       * @param value
    44       */
    45      public static void assertNegativeInfinity(final double value) {
    46          final String msg = "Expected value to be negative infinity but was " + value;
    47          Assert.assertTrue(msg, Double.isInfinite(value));
    48          Assert.assertTrue(msg, value < 0);
    49      }
    50
    51      /** Asserts that the given Runnable throws an exception of the given type.
    52       * @param r the Runnable instance
    53       * @param exceptionType the expected exception type
    54       */
    55      public static void assertThrows(final Runnable r, final Class<?> exceptionType) {
    56          assertThrows(r, exceptionType, (String) null);
    57      }
    58
    59      /** Asserts that the given Runnable throws an exception of the given type. If
    60       * {@code message} is not null, the exception message is asserted to equal the
    61       * given value.
    62       * @param r the Runnable instance
    63       * @param exceptionType the expected exception type
    64       * @param message the expected exception message; ignored if null
    65       */
    66      public static void assertThrows(final Runnable r, final Class<?> exceptionType, final String message) {
    67          try {
    68              r.run();
    69              Assert.fail("Operation should have thrown an exception");
    70          } catch (final Exception exc) {
    71              final Class<?> actualType = exc.getClass();
    72
    73              Assert.assertTrue("Expected exception of type " + exceptionType.getName() + " but was " + actualType.getName(),
    74                      exceptionType.isAssignableFrom(actualType));
    75
    76              if (message != null) {
    77                  Assert.assertEquals(message, exc.getMessage());
    78              }
    79          }
    80      }
    81
    82      /** Asserts that the given Runnable throws an exception of the given type. If
    83       * {@code pattern} is not null, the exception message is asserted to match the
    84       * given regex.
    85       * @param r the Runnable instance
    86       * @param exceptionType the expected exception type
    87       * @param pattern regex pattern to match; ignored if null
    88       */
    89      public static void assertThrows(final Runnable r, final Class<?> exceptionType, final Pattern pattern) {
    90          try {
    91              r.run();
    92              Assert.fail("Operation should have thrown an exception");
    93          } catch (final Exception exc) {
    94              final Class<?> actualType = exc.getClass();
    95
    96              Assert.assertTrue("Expected exception of type " + exceptionType.getName() + " but was " + actualType.getName(),
    97                      exceptionType.isAssignableFrom(actualType));
    98
    99              if (pattern != null) {
    100                 final String message = exc.getMessage();
    101
    102                 final String err = "Expected exception message to match /" + pattern + "/ but was [" + message + "]";
    103                 Assert.assertTrue(err, pattern.matcher(message).matches());
    104             }
    105         }
    106     }
    107
    108     /** Assert that a string contains a given substring value.
    109      * @param substr
    110      * @param actual
    111      */
    112     public static void assertContains(final String substr, final String actual) {
    113         final String msg = "Expected string to contain [" + substr + "] but was [" + actual + "]";
    114         Assert.assertTrue(msg, actual.contains(substr));
    115     }
    116
    117     /**
    118      * Serializes and then recovers an object from a byte array. Returns the deserialized object.
    119      *
    120      * @param obj  object to serialize and recover
    121      * @return  the recovered, deserialized object
    122      */
    123     public static Object serializeAndRecover(final Object obj) {
    124         try {
    125             // serialize the Object
    126             final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    127             final ObjectOutputStream so = new ObjectOutputStream(bos);
    128             so.writeObject(obj);
    129
    130             // deserialize the Object
    131             final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    132             final ObjectInputStream si = new ObjectInputStream(bis);
    133             return si.readObject();
    134         } catch (final Exception e) {
    135             throw new RuntimeException(e);
    136         }
    137     }
    138 }