Class Utils

java.lang.Object
org.testng.internal.Utils

public final class Utils extends Object
Helper methods to parse annotations.
  • Field Details

    • LINE_SEP

      private static final String LINE_SEP
    • SPECIAL_CHARACTERS

      private static final char[] SPECIAL_CHARACTERS
    • CHAR_REPLACEMENT

      public static final char CHAR_REPLACEMENT
      See Also:
    • UNICODE_REPLACEMENT

      public static final char UNICODE_REPLACEMENT
      See Also:
    • FORMAT

      private static final String FORMAT
    • LOG

      private static final Logger LOG
    • ESCAPES

      private static final Map<Character,String> ESCAPES
  • Constructor Details

    • Utils

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

    • writeUtf8File

      public static void writeUtf8File(@Nullable String outputDir, String fileName, XMLStringBuffer xsb, String prefix)
    • 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
    • writeFile

      private static void writeFile(@Nullable File outputFolder, String fileNameParameter, String sb, @Nullable 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(File outputFile, String sb, @Nullable String encoding)
    • 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.
    • openWriter

      private static BufferedWriter openWriter(File outputFile, @Nullable String encoding) throws IOException
      Throws:
      IOException
    • 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)
    • warn

      public static void warn(String warnMsg)
    • split

      public static String[] split(String string, String sep)
    • writeResourceToFile

      public static void writeResourceToFile(File file, String resourceName, Class<?> clasz) throws IOException
      Throws:
      IOException
    • 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.
    • buildStackTrace

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

      private static boolean isTooVerbose()
    • escapeHtml

      public static String escapeHtml(String s)
    • escapeUnicode

      public static String escapeUnicode(String s)
    • filterTrace

      static String filterTrace(String trace)
    • toString

      public static String toString(Object object, Class<?> objectClass)
    • 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
    • join

      public static <T> String join(List<T> objects, String separator)
    • checkInstanceOrStatic

      public static void checkInstanceOrStatic(Object instance, Method method)
    • checkReturnType

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

      private static String toString(Class<?>[] classes)
    • 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
    • extractParameterTypes

      public static Class<?>[] extractParameterTypes(Object[] objects)
    • stringifyTypes

      public static String stringifyTypes(Class<?>[] parameterTypes)