Class JMeterUtils
-
- All Implemented Interfaces:
-
org.apache.jorphan.test.UnitTestManager
public class JMeterUtils implements UnitTestManager
This class contains the static utility methods used by JMeter.
-
-
Field Summary
Fields Modifier and Type Field Description public final static String
THREAD_GROUP_DISTRIBUTED_PREFIX_PROPERTY_NAME
public final static String
RES_KEY_PFX
public static volatile Locale
locale
public static String
localHostIP
public static String
localHostName
public static String
localHostFullName
-
Constructor Summary
Constructors Constructor Description JMeterUtils()
-
Method Summary
Modifier and Type Method Description static Locale
getLocale()
Gets the current locale. static void
setLocale(Locale loc)
Changes the current locale: re-reads resource strings and notifies listeners. static String
getLocalHostIP()
Returns the cached result from calling InetAddress.getLocalHost(). static String
getLocalHostName()
Returns the cached result from calling InetAddress.getLocalHost(). static String
getLocalHostFullName()
Returns the cached result from calling InetAddress.getLocalHost(). static Perl5Matcher
getMatcher()
Gets Perl5Matcher for this thread. static Properties
getProperties(String file)
This method is used by the init method to load the property file that may even reside in the user space, or in the classpath under org.apache.jmeter.jmeter.properties. static void
initLogging()
Initialise JMeter logging static void
initLocale()
Initialise the JMeter Locale static void
loadJMeterProperties(String file)
Load the JMeter properties file; if not found, then default to "org/apache/jmeter/jmeter. static Properties
loadProperties(String file)
This method loads a property file that may reside in the user space, or in the classpath static Properties
loadProperties(String file, Properties defaultProps)
This method loads a property file that may reside in the user space, or in the classpath static Pattern
compilePattern(String expression)
static Pattern
compilePattern(String expression, int flags)
static PatternCacheLRU
getPatternCache()
static Pattern
getPattern(String expression)
Get a compiled expression from the pattern cache (READ_ONLY). static Pattern
getPattern(String expression, int options)
Get a compiled expression from the pattern cache. void
initializeProperties(String file)
static <S> Collection<S>
loadServicesAndScanJars(Class<S> service, ServiceLoader<S> serviceLoader, ClassLoader classLoader, ServiceLoadExceptionHandler<in S> exceptionHandler)
Loads services implementing a given interface and scans JMeter search path for the implementations. static List<String>
findClassesThatExtend(Class<out Object> superClass)
Convenience method for findClassesThatExtend with the option to include inner classes in the search set to false and the path list is derived from JMeterUtils.getSearchPaths(). static Array<String>
getSearchPaths()
Generate a list of paths to search. static int
getRandomInt(int r)
Provide random numbers static void
addLocaleChangeListener(LocaleChangeListener listener)
static void
removeLocaleChangeListener(LocaleChangeListener listener)
static String
getResString(String key)
Gets the resource string for this key. static String
getResString(String key, Locale forcedLocale)
Gets the resource string for this key in Locale. static String
getResString(String key, String defaultValue)
Gets the resource string for this key. static String
getParsedLabel(String key)
To get I18N label from properties file static String
getLocaleString(String locale)
Get the locale name as a resource. static Properties
getJMeterProperties()
This gets the currently defined appProperties. static ImageIcon
getImage(String name)
This looks for the requested image in the classpath under org.apache.jmeter.images.<name> static ImageIcon
getImage(String name, String description)
This looks for the requested image in the classpath under org.apache.jmeter.images.<name>, and also sets the description of the image, which is useful if the icon is going to be placed on the clipboard. static String
getResourceFileAsText(String name)
static int
getPropDefault(String propName, int defaultVal)
Get a int value with default if not present. static boolean
getPropDefault(String propName, boolean defaultVal)
Get a boolean value with default if not present. static Array<String>
getArrayPropDefault(String propName, Array<String> defaultVal)
Get an array of String if present and not empty, defaultValue if not present. static long
getPropDefault(String propName, long defaultVal)
Get a long value with default if not present. static float
getPropDefault(String propName, float defaultVal)
Get a float value with default if not present. static double
getPropDefault(String propName, double defaultVal)
Get a double value with default if not present. static String
getPropDefault(String propName, String defaultVal)
Get a String value with default if not present. static String
getProperty(String propName)
Get the value of a JMeter property. static Object
setProperty(String propName, String propValue)
Set a String value static void
reportErrorToUser(String errorMsg)
Report an error through a dialog box. static void
reportErrorToUser(String errorMsg, String titleMsg)
Report an error through a dialog box in GUI mode or in logs and stdout in Non GUI mode static void
reportErrorToUser(String errorMsg, Exception exception)
Report an error through a dialog box. static void
reportErrorToUser(String errorMsg, String titleMsg, Exception exception)
Report an error through a dialog box in GUI mode or in logs and stdout in Non GUI mode static void
reportInfoToUser(String msg, String titleMsg)
Report an information through a dialog box in GUI mode static JLabel
labelFor(Component component, String resourceId)
Creates JLabel that is associated with a given Component instance. static JLabel
labelFor(Component component, String labelValue, String name)
Creates JLabel that is associated with a given Component instance. static String
unsplit(Array<Object> splittee, Object splitChar)
Takes an array of strings and a tokenizer character, and returns a string of all the strings concatenated with the tokenizer string in between each one. static String
unsplit(Array<Object> splittee, Object splitChar, String def)
Takes an array of strings and a tokenizer character, and returns a string of all the strings concatenated with the tokenizer string in between each one. static boolean
isTestRunning()
static String
getJMeterHome()
Get the JMeter home directory - does not include the trailing separator. static String
getJMeterBinDir()
Get the JMeter bin directory - does not include the trailing separator. static void
setJMeterHome(String home)
static String
getJMeterVersion()
Gets the JMeter Version. static String
getJMeterCopyright()
Gets the JMeter copyright. static boolean
isExpertMode()
Determine whether we are in 'expert' mode. static File
findFile(String fileName)
Find a file in the current directory or in the JMeter bin directory. static LinkedHashMap<String, String>
parseHeaders(String headers)
Split line into name/value pairs and remove colon ':' static void
runSafe(Runnable runnable)
Run the runnable in AWT Thread if current thread is not AWT thread otherwise runs call invokeAndWait static void
runSafe(boolean synchronous, Runnable runnable)
Run the runnable in AWT Thread if current thread is not AWT thread otherwise runs call invokeAndWait static void
helpGC()
Help GC by triggering GC and finalization static void
clearMatcherMemory(Perl5Matcher matcher, Pattern pattern)
Hack to make matcher clean the two internal buffers it keeps in memory which size is equivalent to the unzipped page size static boolean
getHiDPIMode()
Provide info, whether we run in HiDPI mode static double
getHiDPIScaleFactor()
Provide info about the HiDPI scale factor static void
applyHiDPI(JTable table)
Apply HiDPI mode management to JTable static String
getDelimiter(String delimiterValue)
Return delimiterValue handling the TAB case static void
applyHiDPIOnFonts()
Apply HiDPI scale factor on font if HiDPI mode is enabled static void
applyScaleOnFonts(float scale)
Apply HiDPI scale factor on fonts static void
refreshUI()
Refresh UI after LAF change or resizing static void
setupXStreamSecurityPolicy(XStream xstream)
Setup default security policy static String
formatJMeterExportedVariableName(String elementName)
final static XStream
createXStream()
-
-
Method Detail
-
setLocale
static void setLocale(Locale loc)
Changes the current locale: re-reads resource strings and notifies listeners.
- Parameters:
loc
- - new locale
-
getLocalHostIP
static String getLocalHostIP()
Returns the cached result from calling InetAddress.getLocalHost().getHostAddress()
- Returns:
String representation of local IP address
-
getLocalHostName
static String getLocalHostName()
Returns the cached result from calling InetAddress.getLocalHost().getHostName()
- Returns:
local host name
-
getLocalHostFullName
static String getLocalHostFullName()
Returns the cached result from calling InetAddress.getLocalHost().getCanonicalHostName()
- Returns:
local host name in canonical form
-
getMatcher
static Perl5Matcher getMatcher()
Gets Perl5Matcher for this thread.
- Returns:
the Perl5Matcher for this thread
-
getProperties
static Properties getProperties(String file)
This method is used by the init method to load the property file that may even reside in the user space, or in the classpath under org.apache.jmeter.jmeter.properties. The method also initialises logging and sets up the default Locale TODO - perhaps remove? [still used
- Parameters:
file
- the file to load- Returns:
the Properties from the file
-
initLogging
@Deprecated() static void initLogging()
Initialise JMeter logging
-
initLocale
static void initLocale()
Initialise the JMeter Locale
-
loadJMeterProperties
static void loadJMeterProperties(String file)
Load the JMeter properties file; if not found, then default to "org/apache/jmeter/jmeter.properties" from the classpath
c.f. loadProperties
- Parameters:
file
- Name of the file from which the JMeter properties should be loaded
-
loadProperties
static Properties loadProperties(String file)
This method loads a property file that may reside in the user space, or in the classpath
- Parameters:
file
- the file to load- Returns:
the Properties from the file, may be null (e.g. file not found)
-
loadProperties
static Properties loadProperties(String file, Properties defaultProps)
This method loads a property file that may reside in the user space, or in the classpath
- Parameters:
file
- the file to loaddefaultProps
- a set of default properties- Returns:
the Properties from the file; if it could not be processed, the defaultProps are returned.
-
compilePattern
static Pattern compilePattern(String expression)
-
compilePattern
static Pattern compilePattern(String expression, int flags)
-
getPatternCache
static PatternCacheLRU getPatternCache()
-
getPattern
static Pattern getPattern(String expression)
Get a compiled expression from the pattern cache (READ_ONLY).
- Parameters:
expression
- regular expression to be looked up- Returns:
compiled pattern
-
getPattern
static Pattern getPattern(String expression, int options)
Get a compiled expression from the pattern cache.
- Parameters:
expression
- REoptions
- e.g.- Returns:
compiled pattern
-
initializeProperties
void initializeProperties(String file)
-
loadServicesAndScanJars
@API(status = API.Status.DEPRECATED, since = "5.6") static <S> Collection<S> loadServicesAndScanJars(Class<S> service, ServiceLoader<S> serviceLoader, ClassLoader classLoader, ServiceLoadExceptionHandler<in S> exceptionHandler)
Loads services implementing a given interface and scans JMeter search path for the implementations. This is a transition replacement for ClassFinder, and JMeter would migrate to ServiceLoader-only lookup in the future.
Note: it is not always safe to cache the result as
search_paths
property might change over time- Parameters:
service
- interface that services should extend.serviceLoader
- ServiceLoader to fetch services.classLoader
- classLoader to use when searching for classes on the search path.exceptionHandler
- exception handler to use for services that fail to load.- Returns:
collection of services that load successfully
-
findClassesThatExtend
@API(status = API.Status.DEPRECATED, since = "5.6")@Deprecated() static List<String> findClassesThatExtend(Class<out Object> superClass)
Convenience method for findClassesThatExtend with the option to include inner classes in the search set to false and the path list is derived from JMeterUtils.getSearchPaths().
- Parameters:
superClass
- - single class to search for- Returns:
List of Strings containing discovered class names.
-
getSearchPaths
static Array<String> getSearchPaths()
Generate a list of paths to search. The output array always starts with JMETER_HOME/lib/ext and is followed by any paths obtained from the "search_paths" JMeter property.
- Returns:
array of path strings
-
getRandomInt
static int getRandomInt(int r)
Provide random numbers
- Parameters:
r
- - the upper bound (exclusive)- Returns:
a random
int
-
addLocaleChangeListener
static void addLocaleChangeListener(LocaleChangeListener listener)
-
removeLocaleChangeListener
static void removeLocaleChangeListener(LocaleChangeListener listener)
-
getResString
static String getResString(String key)
Gets the resource string for this key. If the resource is not found, a warning is logged
- Parameters:
key
- the key in the resource file- Returns:
the resource string if the key is found; otherwise, return "[res_key="+key+"]"
-
getResString
static String getResString(String key, Locale forcedLocale)
Gets the resource string for this key in Locale. If the resource is not found, a warning is logged
- Parameters:
key
- the key in the resource fileforcedLocale
- Force a particular locale- Returns:
the resource string if the key is found; otherwise, return "[res_key="+key+"]"
- Since:
2.7
-
getResString
@Deprecated() static String getResString(String key, String defaultValue)
Gets the resource string for this key. If the resource is not found, a warning is logged
- Parameters:
key
- the key in the resource filedefaultValue
- - the default value- Returns:
the resource string if the key is found; otherwise, return the default
-
getParsedLabel
static String getParsedLabel(String key)
To get I18N label from properties file
- Parameters:
key
- in messages.- Returns:
I18N label without (if exists) last colon ':' and spaces
-
getLocaleString
static String getLocaleString(String locale)
Get the locale name as a resource. Does not log an error if the resource does not exist. This is needed to support additional locales, as they won't be in existing messages files.
- Parameters:
locale
- name- Returns:
the locale display name as defined in the current Locale or the original string if not present
-
getJMeterProperties
static Properties getJMeterProperties()
This gets the currently defined appProperties. It can only be called after the getProperties or loadJMeterProperties method has been called.
- Returns:
The JMeterProperties value, may be null if loadJMeterProperties has not been called
-
getImage
static ImageIcon getImage(String name)
This looks for the requested image in the classpath under org.apache.jmeter.images.<name>
- Parameters:
name
- Description of Parameter- Returns:
The Image value
-
getImage
static ImageIcon getImage(String name, String description)
This looks for the requested image in the classpath under org.apache.jmeter.images.<name>, and also sets the description of the image, which is useful if the icon is going to be placed on the clipboard.
- Parameters:
name
- the name of the imagedescription
- the description of the image- Returns:
The Image value
-
getResourceFileAsText
static String getResourceFileAsText(String name)
-
getPropDefault
static int getPropDefault(String propName, int defaultVal)
Get a int value with default if not present.
- Parameters:
propName
- the name of the property.defaultVal
- the default value.- Returns:
The PropDefault value
-
getPropDefault
static boolean getPropDefault(String propName, boolean defaultVal)
Get a boolean value with default if not present.
- Parameters:
propName
- the name of the property.defaultVal
- the default value.- Returns:
The PropDefault value
-
getArrayPropDefault
static Array<String> getArrayPropDefault(String propName, Array<String> defaultVal)
Get an array of String if present and not empty, defaultValue if not present.
- Parameters:
propName
- the name of the property.defaultVal
- the default value.- Returns:
The PropDefault value
-
getPropDefault
static long getPropDefault(String propName, long defaultVal)
Get a long value with default if not present.
- Parameters:
propName
- the name of the property.defaultVal
- the default value.- Returns:
The PropDefault value
-
getPropDefault
static float getPropDefault(String propName, float defaultVal)
Get a float value with default if not present.
- Parameters:
propName
- the name of the property.defaultVal
- the default value.- Returns:
The PropDefault value
-
getPropDefault
static double getPropDefault(String propName, double defaultVal)
Get a double value with default if not present.
- Parameters:
propName
- the name of the property.defaultVal
- the default value.- Returns:
The PropDefault value
-
getPropDefault
static String getPropDefault(String propName, String defaultVal)
Get a String value with default if not present.
- Parameters:
propName
- the name of the property.defaultVal
- the default value.- Returns:
The PropDefault value applying a trim on it
-
getProperty
static String getProperty(String propName)
Get the value of a JMeter property.
- Parameters:
propName
- the name of the property.- Returns:
the value of the JMeter property, or
null
if not defined
-
setProperty
static Object setProperty(String propName, String propValue)
Set a String value
- Parameters:
propName
- the name of the property.propValue
- the value of the property- Returns:
the previous value of the property
-
reportErrorToUser
static void reportErrorToUser(String errorMsg)
Report an error through a dialog box. Title defaults to "error_title" resource string
- Parameters:
errorMsg
- - the error message.
-
reportErrorToUser
static void reportErrorToUser(String errorMsg, String titleMsg)
Report an error through a dialog box in GUI mode or in logs and stdout in Non GUI mode
- Parameters:
errorMsg
- - the error message.titleMsg
- - title string
-
reportErrorToUser
static void reportErrorToUser(String errorMsg, Exception exception)
Report an error through a dialog box. Title defaults to "error_title" resource string
- Parameters:
errorMsg
- - the error message.exception
- Exception
-
reportErrorToUser
static void reportErrorToUser(String errorMsg, String titleMsg, Exception exception)
Report an error through a dialog box in GUI mode or in logs and stdout in Non GUI mode
- Parameters:
errorMsg
- - the error message.titleMsg
- - title stringexception
- Exception
-
reportInfoToUser
static void reportInfoToUser(String msg, String titleMsg)
Report an information through a dialog box in GUI mode
- Parameters:
msg
- - the information message.titleMsg
- - title string
-
labelFor
static JLabel labelFor(Component component, String resourceId)
- Parameters:
component
- component for the labelresourceId
- resource ID to be used for retrieving label text- Returns:
JLabel instance
-
labelFor
static JLabel labelFor(Component component, String labelValue, String name)
- Parameters:
component
- component for the labellabelValue
- label textname
- JLabel name- Returns:
JLabel instance
-
unsplit
@Deprecated() static String unsplit(Array<Object> splittee, Object splitChar)
Takes an array of strings and a tokenizer character, and returns a string of all the strings concatenated with the tokenizer string in between each one.
- Parameters:
splittee
- Array of Objects to be concatenated.splitChar
- Object to unsplit the strings with.- Returns:
Array of all the tokens.
-
unsplit
static String unsplit(Array<Object> splittee, Object splitChar, String def)
Takes an array of strings and a tokenizer character, and returns a string of all the strings concatenated with the tokenizer string in between each one.
- Parameters:
splittee
- Array of Objects to be concatenated.splitChar
- Object to unsplit the strings with.def
- Default value to replace null values in array.- Returns:
Array of all the tokens.
-
isTestRunning
static boolean isTestRunning()
- Returns:
true if test is running
-
getJMeterHome
static String getJMeterHome()
Get the JMeter home directory - does not include the trailing separator.
- Returns:
the home directory
-
getJMeterBinDir
static String getJMeterBinDir()
Get the JMeter bin directory - does not include the trailing separator.
- Returns:
the bin directory
-
setJMeterHome
static void setJMeterHome(String home)
-
getJMeterVersion
static String getJMeterVersion()
Gets the JMeter Version.
- Returns:
the JMeter version string
-
getJMeterCopyright
static String getJMeterCopyright()
Gets the JMeter copyright.
- Returns:
the JMeter copyright string
-
isExpertMode
static boolean isExpertMode()
Determine whether we are in 'expert' mode. Certain features may be hidden from user's view unless in expert mode.
- Returns:
true if we're in expert mode
-
findFile
static File findFile(String fileName)
Find a file in the current directory or in the JMeter bin directory.
- Parameters:
fileName
- the name of the file to find- Returns:
File object
-
parseHeaders
static LinkedHashMap<String, String> parseHeaders(String headers)
Split line into name/value pairs and remove colon ':'
- Parameters:
headers
- multi-line string headers- Returns:
a map name/value for each header
-
runSafe
static void runSafe(Runnable runnable)
Run the runnable in AWT Thread if current thread is not AWT thread otherwise runs call invokeAndWait
- Parameters:
runnable
- Runnable
-
runSafe
static void runSafe(boolean synchronous, Runnable runnable)
Run the runnable in AWT Thread if current thread is not AWT thread otherwise runs call invokeAndWait
- Parameters:
synchronous
- flag, whether we will wait for the AWT Thread to finish its job.runnable
- Runnable
-
helpGC
static void helpGC()
Help GC by triggering GC and finalization
-
clearMatcherMemory
static void clearMatcherMemory(Perl5Matcher matcher, Pattern pattern)
Hack to make matcher clean the two internal buffers it keeps in memory which size is equivalent to the unzipped page size
- Parameters:
matcher
- Perl5Matcherpattern
- Pattern
-
getHiDPIMode
@API(since = "5.3", status = API.Status.DEPRECATED) static boolean getHiDPIMode()
Provide info, whether we run in HiDPI mode
- Returns:
true
if we run in HiDPI mode,false
otherwise
-
getHiDPIScaleFactor
@API(since = "5.3", status = API.Status.DEPRECATED) static double getHiDPIScaleFactor()
Provide info about the HiDPI scale factor
- Returns:
the factor by which we should scale elements for HiDPI mode
-
applyHiDPI
@API(since = "5.3", status = API.Status.DEPRECATED) static void applyHiDPI(JTable table)
Apply HiDPI mode management to JTable
- Parameters:
table
- the JTable which should be adapted for HiDPI mode
-
getDelimiter
static String getDelimiter(String delimiterValue)
Return delimiterValue handling the TAB case
- Parameters:
delimiterValue
- Delimited value- Returns:
String delimited modified to handle correctly tab
-
applyHiDPIOnFonts
@API(since = "5.3", status = API.Status.DEPRECATED) static void applyHiDPIOnFonts()
Apply HiDPI scale factor on font if HiDPI mode is enabled
-
applyScaleOnFonts
@API(since = "5.3", status = API.Status.DEPRECATED) static void applyScaleOnFonts(float scale)
Apply HiDPI scale factor on fonts
- Parameters:
scale
- float scale to apply
-
refreshUI
static void refreshUI()
Refresh UI after LAF change or resizing
-
setupXStreamSecurityPolicy
static void setupXStreamSecurityPolicy(XStream xstream)
Setup default security policy
- Parameters:
xstream
- XStream
-
formatJMeterExportedVariableName
static String formatJMeterExportedVariableName(String elementName)
- Parameters:
elementName
- String elementName- Returns:
variable name for index following JMeter convention
-
createXStream
final static XStream createXStream()
- Returns:
XStream XStream instance following JMeter security policy
-
-
-
-