Top Description Inners Fields Constructors Methods
org.testng.internal

public final Class Utils

extends Object
Class Inheritance
Imports
java.io.BufferedReader, .BufferedWriter, .File, .FileOutputStream, .IOException, .InputStream, .OutputStreamWriter, .PrintWriter, .StringReader, .StringWriter, java.lang.reflect.Method, .Modifier, java.nio.charset.StandardCharsets, java.util.Arrays, .HashMap, .List, .Map, java.util.stream.Collectors, javax.annotation.Nullable, org.testng.ITestNGMethod, .TestNGException, org.testng.collections.Lists, org.testng.log4testng.Logger, org.testng.reporters.XMLStringBuffer

Helper methods to parse annotations.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static enum

Field Summary

Modifier and TypeField and Description
public static final char
private static final Map<Character, String>
private static final String
private static final String
private static final Logger
private static int
private static final char[]
public static final char

Constructor Summary

AccessConstructor and Description
private
Utils()

Hide constructor for utility class.

Method Summary

Modifier and TypeMethod and Description
public static String

Returns:

- A String representation of the corresponding annotation.
annotationFormFor
(ITestNGMethod
- An ITestNGMethod object.
method
)

Given a TestNG method, returns the corresponding annotation based on the method type

public static String
arrayToString(String[] strings)

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

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

public static String
public static String
detailedMethodName(ITestNGMethod method, boolean fqn)

public static String
public static void
error(String errorMessage)

public static String
public static String
pack-priv static String
public static int
public static boolean
public static boolean
public static boolean
public static boolean
private static boolean
public static <T> String
join(List<T> objects, String separator)

public static void
log(String msg)

public static void
log(String
the class name to prefix the log message.
cls
,
int
the logging level of the message.
level
,
String
the message to log to System.out.
msg
)

Logs the the message to System.out if level is greater than or equal to getVerbose().

public static String

Returns:

- A string that represents the short stack trace.
longStackTrace
(Throwable
- The Throwable exception
t
,
boolean
- true if the stacktrace should be translated to html as well
toHtml
)

Helper that returns a short stack trace.

public static BufferedWriter
openWriter(String
output directory. If null, then current directory is used
outputDir
,
String
file name
fileNameParameter
)

Open a BufferedWriter for the specified file.

private static BufferedWriter
openWriter(File outputFile, String encoding)

public static String

Returns:

fileName with special characters replaced
replaceSpecialCharacters
(String
file name that could contain special characters.
fileNameParameter
)

If the file name contains special characters like *,/,\ and so on, exception will be thrown and report file will not be created.

public static void
setVerbose(int n)

public static String

Returns:

- A string that represents the full stack trace.
shortStackTrace
(Throwable
- The Throwable exception
t
,
boolean
- true if the stacktrace should be translated to html as well
toHtml
)

Helper that returns a long stack trace.

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

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

public static String
toString(Object object, Class<?> objectClass)

private static String
toString(Class<?>[] classes)

public static String

Returns:

the string representation
toString
(Object
the object
obj
)

Returns the string representation of the specified object, transparently handling null references and arrays.

public static void
warn(String warnMsg)

public static void
writeFile(String
the output directory (may not exist). If null then current directory is used.
outputDir
,
String
the filename
fileName
,
String
the file content
sb
)

Writes the content of the sb string to the file named filename in outDir.

private static void
writeFile(File
the output directory (may not exist). If null then current directory is used.
outputFolder
,
String
the filename
fileNameParameter
,
String
the file content
sb
,
String encoding)

Writes the content of the sb string to the file named filename in outDir.

private static void
writeFile(File outputFile, String sb, String encoding)

public static void
writeResourceToFile(File file, String resourceName, Class<?> clasz)

public static void
writeUtf8File(String outputDir, String fileName, XMLStringBuffer xsb, String prefix)

public static void
writeUtf8File(String
the output directory (may not exist). If null then current directory is used.
outputDir
,
String
the filename
fileName
,
String
the file content
sb
)

Writes the content of the sb string to the file named filename in outDir encoding the output as UTF-8.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CHAR_REPLACEMENTback to summary
public static final char CHAR_REPLACEMENT
ESCAPESback to summary
private static final Map<Character, String> ESCAPES
FORMATback to summary
private static final String FORMAT
LINE_SEPback to summary
private static final String LINE_SEP
LOGback to summary
private static final Logger LOG
m_verboseback to summary
private static int m_verbose
SPECIAL_CHARACTERSback to summary
private static final char[] SPECIAL_CHARACTERS
UNICODE_REPLACEMENTback to summary
public static final char UNICODE_REPLACEMENT

Constructor Detail

Utilsback to summary
private Utils()

Hide constructor for utility class.

Method Detail

annotationFormForback to summary
public static String annotationFormFor(ITestNGMethod method)

Given a TestNG method, returns the corresponding annotation based on the method type

Parameters
method:ITestNGMethod

- An ITestNGMethod object.

Returns:String

- A String representation of the corresponding annotation.

arrayToStringback to summary
public static String arrayToString(String[] strings)
buildStackTraceback to summary
private static String buildStackTrace(Throwable t, boolean toHtml, Utils.StackTraceType type)
checkInstanceOrStaticback to summary
public static void checkInstanceOrStatic(Object instance, Method method)
checkReturnTypeback to summary
public static void checkReturnType(Method method, Class<?>... returnTypes)
defaultIfStringEmptyback to summary
public static String defaultIfStringEmpty(String s, String defaultValue)
detailedMethodNameback to summary
public static String detailedMethodName(ITestNGMethod method, boolean fqn)
detailedMethodNameback to summary
public static String detailedMethodName(ITestNGMethod method)
errorback to summary
public static void error(String errorMessage)
escapeHtmlback to summary
public static String escapeHtml(String s)
escapeUnicodeback to summary
public static String escapeUnicode(String s)
filterTraceback to summary
pack-priv static String filterTrace(String trace)
getVerboseback to summary
public static int getVerbose()
isStringBlankback to summary
public static boolean isStringBlank(String s)
isStringEmptyback to summary
public static boolean isStringEmpty(String s)
isStringNotBlankback to summary
public static boolean isStringNotBlank(String s)
isStringNotEmptyback to summary
public static boolean isStringNotEmpty(String s)
isTooVerboseback to summary
private static boolean isTooVerbose()
joinback to summary
public static <T> String join(List<T> objects, String separator)
logback to summary
public static void log(String msg)
logback to summary
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 getVerbose(). The message is logged as:

    "[cls] msg"
Parameters
cls:String

the class name to prefix the log message.

level:int

the logging level of the message.

msg:String

the message to log to System.out.

longStackTraceback to summary
public static String longStackTrace(Throwable t, boolean toHtml)

Helper that returns a short stack trace.

Parameters
t:Throwable

- The Throwable exception

toHtml:boolean

- true if the stacktrace should be translated to html as well

Returns:String

- A string that represents the short stack trace.

openWriterback to summary
public static BufferedWriter openWriter(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:String

output directory. If null, then current directory is used

fileNameParameter:String

file name

Exceptions
IOException:
if anything goes wrong while creating files.
openWriterback to summary
private static BufferedWriter openWriter(File outputFile, String encoding) throws IOException
replaceSpecialCharactersback to summary
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:String

file name that could contain special characters.

Returns:String

fileName with special characters replaced

setVerboseback to summary
public static void setVerbose(int n)
shortStackTraceback to summary
public static String shortStackTrace(Throwable t, boolean toHtml)

Helper that returns a long stack trace.

Parameters
t:Throwable

- The Throwable exception

toHtml:boolean

- true if the stacktrace should be translated to html as well

Returns:String

- A string that represents the full stack trace.

splitback to summary
public static String[] split(String string, String sep)
stringifyTypesback to summary
public static String stringifyTypes(Class<?>[] parameterTypes)
toStringback to summary
public static String toString(Object object, Class<?> objectClass)
toStringback to summary
private static String toString(Class<?>[] classes)
toStringback to summary
public static String toString(Object obj)

Returns the string representation of the specified object, transparently handling null references and arrays.

Parameters
obj:Object

the object

Returns:String

the string representation

warnback to summary
public static void warn(String warnMsg)
writeFileback to summary
public static void writeFile(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:String

the output directory (may not exist). If null then current directory is used.

fileName:String

the filename

sb:String

the file content

writeFileback to summary
private static void writeFile(File outputFolder, String fileNameParameter, String sb, 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:File

the output directory (may not exist). If null then current directory is used.

fileNameParameter:String

the filename

sb:String

the file content

writeFileback to summary
private static void writeFile(File outputFile, String sb, String encoding)
writeResourceToFileback to summary
public static void writeResourceToFile(File file, String resourceName, Class<?> clasz) throws IOException
writeUtf8Fileback to summary
public static void writeUtf8File(String outputDir, String fileName, XMLStringBuffer xsb, String prefix)
writeUtf8Fileback to summary
public static void writeUtf8File(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:String

the output directory (may not exist). If null then current directory is used.

fileName:String

the filename

sb:String

the file content

org.testng.internal back to summary

private final Enum Utils.StackTraceType

extends Enum<Utils.StackTraceType>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final Utils.StackTraceType
public static final Utils.StackTraceType

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static Utils.StackTraceType
public static Utils.StackTraceType[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

FULLback to summary
public static final Utils.StackTraceType FULL
SHORTback to summary
public static final Utils.StackTraceType SHORT

Constructor Detail

StackTraceTypeback to summary
private StackTraceType()

Method Detail

valueOfback to summary
public static Utils.StackTraceType valueOf(String name)
valuesback to summary
public static Utils.StackTraceType[] values()