Class Utils


  • public final class Utils
    extends Object
    Helper methods to parse annotations.
    • Method Detail

      • stringToArray

        @Deprecated
        public static String[] stringToArray​(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​(Class<?>[] classes)
      • writeUtf8File

        public static void writeUtf8File​(@Nullable
                                         String outputDir,
                                         String fileName,
                                         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
                                     String outputDir,
                                     String fileName,
                                     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
      • openWriter

        public static BufferedWriter openWriter​(@Nullable
                                                String outputDir,
                                                String fileNameParameter)
                                         throws 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:
        IOException - if anything goes wrong while creating files.
      • dumpMap

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

        public static void log​(String msg)
      • log

        public static void log​(String cls,
                               int level,
                               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​(String errorMessage)
      • calculateInvokedMethodCount

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

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

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

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

        public static boolean isStringBlank​(String s)
      • isStringEmpty

        public static boolean isStringEmpty​(String s)
      • isStringNotBlank

        public static boolean isStringNotBlank​(String s)
      • isStringNotEmpty

        public static boolean isStringNotEmpty​(String s)
      • longStackTrace

        public static String longStackTrace​(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 String shortStackTrace​(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.
      • escapeHtml

        public static String escapeHtml​(String s)
      • escapeUnicode

        public static String escapeUnicode​(String s)
      • detailedMethodName

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

        public static 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 String arrayToString​(String[] strings)
      • replaceSpecialCharacters

        public static String replaceSpecialCharacters​(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
      • copyFile

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

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

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

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

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