Class TestUtil


  • public abstract class TestUtil
    extends java.lang.Object
    Static utilities for JUnit tests.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String JAVA_VERSION  
      private static java.lang.String LINE_BREAK  
      private static java.util.regex.Pattern LINE_BREAK_PATTERN  
      private static java.util.regex.Pattern TAB_PATTERN  
    • Constructor Summary

      Constructors 
      Constructor Description
      TestUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void assertEqualsVerbose​(java.lang.String expected, java.lang.String actual)  
      static java.lang.String escapeString​(java.lang.String s)
      Quotes a string for Java or JSON.
      static java.lang.StringBuilder escapeString​(java.lang.StringBuilder buf, java.lang.String s)
      Quotes a string for Java or JSON, into a builder.
      static java.lang.String fold​(java.lang.String... strings)
      Combines an array of strings, each representing a line, into a single string containing line separators.
      static int getJavaMajorVersion()
      Returns the Java major version: 7 for JDK 1.7, 8 for JDK 8, 10 for JDK 10, etc.
      static boolean hasMessage​(java.lang.Throwable t, java.lang.String substring)
      Checks if exceptions have give substring.
      (package private) static int majorVersionFromString​(java.lang.String version)
      Detects java major version given long format of full JDK version.
      static java.lang.String printStackTrace​(java.lang.Throwable t)
      Returns string representation of the given Throwable.
      static java.lang.String quoteForJava​(java.lang.String s)
      Converts a string (which may contain quotes and newlines) into a java literal.
      static java.lang.String quotePattern​(java.lang.String s)
      Quotes a pattern.
      static <E extends java.lang.Throwable>
      java.lang.RuntimeException
      rethrow​(java.lang.Throwable e)
      Rethrows given exception keeping stacktraces clean and compact.
      static <E extends java.lang.Throwable>
      java.lang.RuntimeException
      rethrow​(java.lang.Throwable e, java.lang.String message)
      Rethrows given exception keeping stacktraces clean and compact.
      static java.lang.String toJavaString​(java.lang.String s)
      Converts a string (which may contain quotes and newlines) into a java literal.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LINE_BREAK_PATTERN

        private static final java.util.regex.Pattern LINE_BREAK_PATTERN
      • TAB_PATTERN

        private static final java.util.regex.Pattern TAB_PATTERN
      • LINE_BREAK

        private static final java.lang.String LINE_BREAK
      • JAVA_VERSION

        private static final java.lang.String JAVA_VERSION
    • Constructor Detail

      • TestUtil

        public TestUtil()
    • Method Detail

      • assertEqualsVerbose

        public static void assertEqualsVerbose​(java.lang.String expected,
                                               java.lang.String actual)
      • quoteForJava

        public static java.lang.String quoteForJava​(java.lang.String s)
        Converts a string (which may contain quotes and newlines) into a java literal.

        For example,

        string with "quotes" split
         across lines

        becomes

        "string with \"quotes\" split" + NL +
          "across lines"
      • toJavaString

        public static java.lang.String toJavaString​(java.lang.String s)
        Converts a string (which may contain quotes and newlines) into a java literal.

        For example,

        string with "quotes" split
         across lines

        becomes

        TestUtil.fold(
          "string with \"quotes\" split\n",
          + "across lines")
      • fold

        public static java.lang.String fold​(java.lang.String... strings)
        Combines an array of strings, each representing a line, into a single string containing line separators.
      • escapeString

        public static java.lang.String escapeString​(java.lang.String s)
        Quotes a string for Java or JSON.
      • escapeString

        public static java.lang.StringBuilder escapeString​(java.lang.StringBuilder buf,
                                                           java.lang.String s)
        Quotes a string for Java or JSON, into a builder.
      • quotePattern

        public static java.lang.String quotePattern​(java.lang.String s)
        Quotes a pattern.
      • getJavaMajorVersion

        public static int getJavaMajorVersion()
        Returns the Java major version: 7 for JDK 1.7, 8 for JDK 8, 10 for JDK 10, etc. depending on current system property java.version.
      • majorVersionFromString

        static int majorVersionFromString​(java.lang.String version)
        Detects java major version given long format of full JDK version. See JEP 223: New Version-String Scheme.
        Parameters:
        version - current version as string usually from java.version property.
        Returns:
        major java version (8, 9, 10, 11 etc.)
      • hasMessage

        public static boolean hasMessage​(java.lang.Throwable t,
                                         java.lang.String substring)
        Checks if exceptions have give substring. That is handy to prevent logging SQL text twice
      • rethrow

        public static <E extends java.lang.Throwable> java.lang.RuntimeException rethrow​(java.lang.Throwable e)
                                                                                  throws E extends java.lang.Throwable
        Rethrows given exception keeping stacktraces clean and compact.
        Throws:
        E extends java.lang.Throwable
      • rethrow

        public static <E extends java.lang.Throwable> java.lang.RuntimeException rethrow​(java.lang.Throwable e,
                                                                                         java.lang.String message)
                                                                                  throws E extends java.lang.Throwable
        Rethrows given exception keeping stacktraces clean and compact.
        Throws:
        E extends java.lang.Throwable
      • printStackTrace

        public static java.lang.String printStackTrace​(java.lang.Throwable t)
        Returns string representation of the given Throwable.