Class Utils


  • public final class Utils
    extends java.lang.Object
    Helper methods to parse annotations.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  Utils.StackTraceType  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Utils()
      Hide constructor for utility class.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static java.lang.String annotationFormFor​(ITestNGMethod method)
      Given a TestNG method, returns the corresponding annotation based on the method type
      static java.lang.String arrayToString​(java.lang.String[] strings)  
      private static java.lang.String buildStackTrace​(java.lang.Throwable t, boolean toHtml, Utils.StackTraceType type)  
      static int calculateInvokedMethodCount​(java.util.List<ITestNGMethod> methods)  
      static int calculateInvokedMethodCount​(ITestNGMethod[] methods)  
      static void checkInstanceOrStatic​(java.lang.Object instance, java.lang.reflect.Method method)
      Make sure that either we have an instance or if not, that the method is static
      static void checkReturnType​(java.lang.reflect.Method method, java.lang.Class<?>... returnTypes)  
      static XmlClass[] classesToXmlClasses​(java.lang.Class<?>[] classes)  
      static void copyFile​(java.io.File from, java.io.File to)
      Deprecated.
      Unused
      static java.io.File createTempFile​(java.lang.String content)  
      static java.lang.String defaultIfStringEmpty​(java.lang.String s, java.lang.String defaultValue)  
      static java.lang.String[] dependentGroupsForThisMethodForConfiguration​(java.lang.reflect.Method m, IAnnotationFinder finder)
      Deprecated.
      Unused
      static java.lang.String[] dependentGroupsForThisMethodForTest​(java.lang.reflect.Method m, IAnnotationFinder finder)
      Deprecated.
      Unused
      static java.lang.String detailedMethodName​(ITestNGMethod method, boolean fqn)  
      static void dumpMap​(java.util.Map<?,​?> result)
      Deprecated.
      Unused
      static void dumpMethods​(java.util.List<ITestNGMethod> allMethods)
      Deprecated.
      Unused
      static void error​(java.lang.String errorMessage)  
      static java.lang.String escapeHtml​(java.lang.String s)  
      static java.lang.String escapeUnicode​(java.lang.String s)  
      (package private) static java.lang.String filterTrace​(java.lang.String trace)  
      static java.lang.String[] groupsForThisMethodForConfiguration​(java.lang.reflect.Method m, IAnnotationFinder finder)
      Deprecated.
      Unused
      static java.lang.String[] groupsForThisMethodForTest​(java.lang.reflect.Method m, IAnnotationFinder finder)
      Deprecated.
      Unused
      static boolean isStringBlank​(java.lang.String s)  
      static boolean isStringEmpty​(java.lang.String s)  
      static boolean isStringNotBlank​(java.lang.String s)  
      static boolean isStringNotEmpty​(java.lang.String s)  
      private static boolean isTooVerbose()  
      static <T> java.lang.String join​(java.util.List<T> objects, java.lang.String separator)  
      static void log​(java.lang.String msg)  
      static void log​(java.lang.String cls, int level, java.lang.String msg)
      Logs the the message to System.out if level is greater than or equal to TestRunner.getVerbose().
      static void logInvocation​(java.lang.String reason, java.lang.reflect.Method thisMethod, java.lang.Object[] parameters)
      Deprecated.
      Unused
      static java.lang.String longStackTrace​(java.lang.Throwable t, boolean toHtml)
      Helper that returns a short stack trace.
      private static java.io.BufferedWriter openWriter​(java.io.File outputFile, java.lang.String encoding)  
      static java.io.BufferedWriter openWriter​(java.lang.String outputDir, java.lang.String fileNameParameter)
      Open a BufferedWriter for the specified file.
      static java.lang.String[] parseMultiLine​(java.lang.String line)
      Deprecated.
      Unused
      private static void ppp​(java.lang.String s)  
      static java.lang.String replaceSpecialCharacters​(java.lang.String fileNameParameter)
      If the file name contains special characters like *,/,\ and so on, exception will be thrown and report file will not be created.
      Special characters are platform specific and they are not same for example on Windows and Macintosh.
      static java.lang.String shortStackTrace​(java.lang.Throwable t, boolean toHtml)
      Helper that returns a long stack trace.
      static java.lang.String[] split​(java.lang.String string, java.lang.String sep)
      Tokenize the string using the separator.
      static java.lang.String[] stackTrace​(java.lang.Throwable t, boolean toHtml)
      Deprecated.
      - Please consider using : longStackTrace(Throwable, boolean) - for getting full stack trace shortStackTrace(Throwable, boolean) - for getting short stack trace
      static java.lang.String[] stringToArray​(java.lang.String s)
      Deprecated.
      Unused
      private static java.lang.String toString​(java.lang.Class<?>[] classes)  
      static java.lang.String toString​(java.lang.Object obj)
      Returns the string representation of the specified object, transparently handling null references and arrays.
      static java.lang.String toString​(java.lang.Object object, java.lang.Class<?> objectClass)  
      private static void writeFile​(java.io.File outputFile, java.lang.String sb, java.lang.String encoding)  
      private static void writeFile​(java.io.File outputFolder, java.lang.String fileNameParameter, java.lang.String sb, java.lang.String encoding)
      Writes the content of the sb string to the file named filename in outDir.
      static void writeFile​(java.lang.String outputDir, java.lang.String fileName, java.lang.String sb)
      Writes the content of the sb string to the file named filename in outDir.
      static void writeResourceToFile​(java.io.File file, java.lang.String resourceName, java.lang.Class<?> clasz)  
      static void writeUtf8File​(java.lang.String outputDir, java.lang.String fileName, java.lang.String sb)
      Writes the content of the sb string to the file named filename in outDir encoding the output as UTF-8.
      static void writeUtf8File​(java.lang.String outputDir, java.lang.String fileName, XMLStringBuffer xsb, java.lang.String prefix)  
      • Methods inherited from class java.lang.Object

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

      • LINE_SEP

        private static final java.lang.String LINE_SEP
      • SPECIAL_CHARACTERS

        private static final char[] SPECIAL_CHARACTERS
      • FORMAT

        private static final java.lang.String FORMAT
      • LOG

        private static final Logger LOG
      • ESCAPES

        private static final java.util.Map<java.lang.Character,​java.lang.String> ESCAPES
    • Constructor Detail

      • Utils

        private Utils()
        Hide constructor for utility class.
    • Method Detail

      • stringToArray

        @Deprecated
        public static java.lang.String[] stringToArray​(java.lang.String s)
        Deprecated.
        Unused
        Splits the given String s into tokens where the separator is either the space character or the comma character. For example, if s is "a,b, c" this method returns {"a", "b", "c"}
        Parameters:
        s - the string to split
        Returns:
        the split token
      • classesToXmlClasses

        public static XmlClass[] classesToXmlClasses​(java.lang.Class<?>[] classes)
      • parseMultiLine

        @Deprecated
        public static java.lang.String[] parseMultiLine​(java.lang.String line)
        Deprecated.
        Unused
      • writeUtf8File

        public static void writeUtf8File​(@Nullable
                                         java.lang.String outputDir,
                                         java.lang.String fileName,
                                         XMLStringBuffer xsb,
                                         java.lang.String prefix)
      • writeUtf8File

        public static void writeUtf8File​(@Nullable
                                         java.lang.String outputDir,
                                         java.lang.String fileName,
                                         java.lang.String sb)
        Writes the content of the sb string to the file named filename in outDir encoding the output as UTF-8. If outDir does not exist, it is created.
        Parameters:
        outputDir - the output directory (may not exist). If null then current directory is used.
        fileName - the filename
        sb - the file content
      • writeFile

        public static void writeFile​(@Nullable
                                     java.lang.String outputDir,
                                     java.lang.String fileName,
                                     java.lang.String sb)
        Writes the content of the sb string to the file named filename in outDir. If outDir does not exist, it is created.
        Parameters:
        outputDir - the output directory (may not exist). If null then current directory is used.
        fileName - the filename
        sb - the file content
      • writeFile

        private static void writeFile​(@Nullable
                                      java.io.File outputFolder,
                                      java.lang.String fileNameParameter,
                                      java.lang.String sb,
                                      @Nullable
                                      java.lang.String encoding)
        Writes the content of the sb string to the file named filename in outDir. If outDir does not exist, it is created.
        Parameters:
        outputFolder - the output directory (may not exist). If null then current directory is used.
        fileNameParameter - the filename
        sb - the file content
      • writeFile

        private static void writeFile​(java.io.File outputFile,
                                      java.lang.String sb,
                                      @Nullable
                                      java.lang.String encoding)
      • openWriter

        public static java.io.BufferedWriter openWriter​(@Nullable
                                                        java.lang.String outputDir,
                                                        java.lang.String fileNameParameter)
                                                 throws java.io.IOException
        Open a BufferedWriter for the specified file. If output directory doesn't exist, it is created. If the output file exists, it is deleted. The output file is created in any case.
        Parameters:
        outputDir - output directory. If null, then current directory is used
        fileNameParameter - file name
        Throws:
        java.io.IOException - if anything goes wrong while creating files.
      • openWriter

        private static java.io.BufferedWriter openWriter​(java.io.File outputFile,
                                                         @Nullable
                                                         java.lang.String encoding)
                                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • ppp

        private static void ppp​(java.lang.String s)
      • dumpMap

        @Deprecated
        public static void dumpMap​(java.util.Map<?,​?> result)
        Deprecated.
        Unused
      • dumpMethods

        @Deprecated
        public static void dumpMethods​(java.util.List<ITestNGMethod> allMethods)
        Deprecated.
        Unused
      • dependentGroupsForThisMethodForTest

        @Deprecated
        public static java.lang.String[] dependentGroupsForThisMethodForTest​(java.lang.reflect.Method m,
                                                                             IAnnotationFinder finder)
        Deprecated.
        Unused
      • groupsForThisMethodForTest

        @Deprecated
        public static java.lang.String[] groupsForThisMethodForTest​(java.lang.reflect.Method m,
                                                                    IAnnotationFinder finder)
        Deprecated.
        Unused
      • groupsForThisMethodForConfiguration

        @Deprecated
        public static java.lang.String[] groupsForThisMethodForConfiguration​(java.lang.reflect.Method m,
                                                                             IAnnotationFinder finder)
        Deprecated.
        Unused
      • dependentGroupsForThisMethodForConfiguration

        @Deprecated
        public static java.lang.String[] dependentGroupsForThisMethodForConfiguration​(java.lang.reflect.Method m,
                                                                                      IAnnotationFinder finder)
        Deprecated.
        Unused
      • log

        public static void log​(java.lang.String msg)
      • log

        public static void log​(java.lang.String cls,
                               int level,
                               java.lang.String msg)
        Logs the the message to System.out if level is greater than or equal to TestRunner.getVerbose(). The message is logged as:
             "[cls] msg"
         
        Parameters:
        cls - the class name to prefix the log message.
        level - the logging level of the message.
        msg - the message to log to System.out.
      • error

        public static void error​(java.lang.String errorMessage)
      • calculateInvokedMethodCount

        public static int calculateInvokedMethodCount​(ITestNGMethod[] methods)
      • calculateInvokedMethodCount

        public static int calculateInvokedMethodCount​(java.util.List<ITestNGMethod> methods)
      • split

        public static java.lang.String[] split​(java.lang.String string,
                                               java.lang.String sep)
        Tokenize the string using the separator.
      • logInvocation

        @Deprecated
        public static void logInvocation​(java.lang.String reason,
                                         java.lang.reflect.Method thisMethod,
                                         java.lang.Object[] parameters)
        Deprecated.
        Unused
      • writeResourceToFile

        public static void writeResourceToFile​(java.io.File file,
                                               java.lang.String resourceName,
                                               java.lang.Class<?> clasz)
                                        throws java.io.IOException
        Throws:
        java.io.IOException
      • defaultIfStringEmpty

        public static java.lang.String defaultIfStringEmpty​(java.lang.String s,
                                                            java.lang.String defaultValue)
      • isStringBlank

        public static boolean isStringBlank​(java.lang.String s)
      • isStringEmpty

        public static boolean isStringEmpty​(java.lang.String s)
      • isStringNotBlank

        public static boolean isStringNotBlank​(java.lang.String s)
      • isStringNotEmpty

        public static boolean isStringNotEmpty​(java.lang.String s)
      • stackTrace

        @Deprecated
        public static java.lang.String[] stackTrace​(java.lang.Throwable t,
                                                    boolean toHtml)
        Deprecated.
        - Please consider using :
        Returns:
        an array of two strings: the short stack trace and the long stack trace.
      • longStackTrace

        public static java.lang.String longStackTrace​(java.lang.Throwable t,
                                                      boolean toHtml)
        Helper that returns a short stack trace.
        Parameters:
        t - - The Throwable exception
        toHtml - - true if the stacktrace should be translated to html as well
        Returns:
        - A string that represents the short stack trace.
      • shortStackTrace

        public static java.lang.String shortStackTrace​(java.lang.Throwable t,
                                                       boolean toHtml)
        Helper that returns a long stack trace.
        Parameters:
        t - - The Throwable exception
        toHtml - - true if the stacktrace should be translated to html as well
        Returns:
        - A string that represents the full stack trace.
      • buildStackTrace

        private static java.lang.String buildStackTrace​(java.lang.Throwable t,
                                                        boolean toHtml,
                                                        Utils.StackTraceType type)
      • isTooVerbose

        private static boolean isTooVerbose()
      • escapeHtml

        public static java.lang.String escapeHtml​(java.lang.String s)
      • escapeUnicode

        public static java.lang.String escapeUnicode​(java.lang.String s)
      • filterTrace

        static java.lang.String filterTrace​(java.lang.String trace)
      • toString

        public static java.lang.String toString​(java.lang.Object object,
                                                java.lang.Class<?> objectClass)
      • detailedMethodName

        public static java.lang.String detailedMethodName​(ITestNGMethod method,
                                                          boolean fqn)
      • annotationFormFor

        public static java.lang.String annotationFormFor​(ITestNGMethod method)
        Given a TestNG method, returns the corresponding annotation based on the method type
        Parameters:
        method - - An ITestNGMethod object.
        Returns:
        - A String representation of the corresponding annotation.
      • arrayToString

        public static java.lang.String arrayToString​(java.lang.String[] strings)
      • replaceSpecialCharacters

        public static java.lang.String replaceSpecialCharacters​(java.lang.String fileNameParameter)
        If the file name contains special characters like *,/,\ and so on, exception will be thrown and report file will not be created.
        Special characters are platform specific and they are not same for example on Windows and Macintosh. * is not allowed on Windows, but it is on Macintosh.
        In order to have the same behavior of testng on the all platforms, characters like * will be replaced on all platforms whether they are causing the problem or not.
        Parameters:
        fileNameParameter - file name that could contain special characters.
        Returns:
        fileName with special characters replaced
      • join

        public static <T> java.lang.String join​(java.util.List<T> objects,
                                                java.lang.String separator)
      • copyFile

        @Deprecated
        public static void copyFile​(java.io.File from,
                                    java.io.File to)
        Deprecated.
        Unused
      • createTempFile

        public static java.io.File createTempFile​(java.lang.String content)
        Returns:
        a temporary file with the given content.
      • checkInstanceOrStatic

        public static void checkInstanceOrStatic​(java.lang.Object instance,
                                                 java.lang.reflect.Method method)
        Make sure that either we have an instance or if not, that the method is static
      • checkReturnType

        public static void checkReturnType​(java.lang.reflect.Method method,
                                           java.lang.Class<?>... returnTypes)
      • toString

        private static java.lang.String toString​(java.lang.Class<?>[] classes)
      • toString

        public static java.lang.String toString​(java.lang.Object obj)
        Returns the string representation of the specified object, transparently handling null references and arrays.
        Parameters:
        obj - the object
        Returns:
        the string representation