Package org.apache.jmeter.util
Class JMeterUtils
- java.lang.Object
-
- org.apache.jmeter.util.JMeterUtils
-
- All Implemented Interfaces:
UnitTestManager
public class JMeterUtils extends Object implements UnitTestManager
This class contains the static utility methods used by JMeter.
-
-
Field Summary
Fields Modifier and Type Field Description static StringRES_KEY_PFX
-
Constructor Summary
Constructors Constructor Description JMeterUtils()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static voidaddLocaleChangeListener(LocaleChangeListener listener)static voidclearMatcherMemory(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 sizestatic JButtoncreateButton(String name, ActionListener listener)Create a button with the netscape stylestatic JButtoncreateSimpleButton(String name, ActionListener listener)Create a button with the netscape stylestatic List<String>findClassesThatExtend(Class<?> superClass)Convenience method forClassFinder.findClassesThatExtend(String[], Class[], boolean)with the option to include inner classes in the search set to false and the path list is derived from JMeterUtils.getSearchPaths().static FilefindFile(String fileName)Find a file in the current directory or in the JMeter bin directory.static intfindInArray(String[] array, String value)Finds a string in an array of strings and returns thestatic Hashtable<String,String>getAlias(Properties properties)Creates the vector of alias strings.static Vector<Object>getControllers(Properties properties)Creates a vector of SampleController plugins.static Hashtable<String,String>getHashtable(Properties properties, String prefix)Creates a table of strings for all the properties that start with a common prefix.static ImageIcongetImage(String name)This looks for the requested image in the classpath under org.apache.jmeter.images.<name>static ImageIcongetImage(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 StringgetJMeterBinDir()Get the JMeter bin directory - does not include the trailing separator.static StringgetJMeterCopyright()Gets the JMeter copyright.static StringgetJMeterHome()Get the JMeter home directory - does not include the trailing separator.static PropertiesgetJMeterProperties()This gets the currently defined appProperties.static StringgetJMeterVersion()Gets the JMeter Version.static LocalegetLocale()Gets the current locale.static StringgetLocaleString(String locale)Get the locale name as a resource.static StringgetLocalHostFullName()Returns the cached result from calling InetAddress.getLocalHost().getCanonicalHostName()static StringgetLocalHostIP()Returns the cached result from calling InetAddress.getLocalHost().getHostAddress()static StringgetLocalHostName()Returns the cached result from calling InetAddress.getLocalHost().getHostName()static Perl5MatchergetMatcher()Gets Perl5Matcher for this thread.static StringgetParsedLabel(String key)To get I18N label from properties filestatic PatterngetPattern(String expression)Get a compiled expression from the pattern cache (READ_ONLY).static PatterngetPattern(String expression, int options)Get a compiled expression from the pattern cache.static PatternCacheLRUgetPatternCache()static booleangetPropDefault(String propName, boolean defaultVal)Get a boolean value with default if not present.static intgetPropDefault(String propName, int defaultVal)Get a int value with default if not present.static longgetPropDefault(String propName, long defaultVal)Get a long value with default if not present.static StringgetPropDefault(String propName, String defaultVal)Get a String value with default if not present.static PropertiesgetProperties(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 StringgetProperty(String propName)Get the value of a JMeter property.static intgetRandomInt(int r)Provide random numbersstatic StringgetResourceFileAsText(String name)static StringgetResString(String key)Gets the resource string for this key.static StringgetResString(String key, String defaultValue)Deprecated.Only intended for use in development; use getResString(String) normallystatic StringgetResString(String key, Locale forcedLocale)Gets the resource string for this key in Locale.static String[]getSearchPaths()Generate a list of paths to search.static String[]getTestSamples(Properties properties, String name)Create a string of class names for a particular SamplerControllerstatic Vector<Object>getTimers(Properties properties)Creates the vector of Timers plugins.static Vector<String>getVector(Properties properties, String name)Creates a vector of strings for all the properties that start with a common prefix.static Vector<Object>getVisualizers(Properties properties)Creates the vector of visualizer plugins.static XMLReadergetXMLParser()Create an instance of an org.xml.sax.Parser based on the default props.static voidhelpGC()Help GC by triggering GC and finalizationvoidinitializeProperties(String file)Your implementation will be handed the filename that was provided to AllTests as a configuration file.static voidinitLocale()Initialise the JMeter Localestatic voidinitLogging()Initialise JMeter loggingstatic Objectinstantiate(String className, String impls)Instatiate an object and guarantee its class.static Vector<Object>instantiate(Vector<String> v, String className)Instantiate a vector of classesstatic booleanisExpertMode()Determine whether we are in 'expert' mode.static voidloadJMeterProperties(String file)Load the JMeter properties file; if not found, then default to "org/apache/jmeter/jmeter.properties" from the classpathstatic PropertiesloadProperties(String file)This method loads a property file that may reside in the user space, or in the classpathstatic PropertiesloadProperties(String file, Properties defaultProps)This method loads a property file that may reside in the user space, or in the classpathstatic LinkedHashMap<String,String>parseHeaders(String headers)Split line into name/value pairs and remove colon ':'static voidremoveLocaleChangeListener(LocaleChangeListener listener)static voidreportErrorToUser(String errorMsg)Report an error through a dialog box.static voidreportErrorToUser(String errorMsg, String titleMsg)Report an error through a dialog box.static voidrunSafe(Runnable runnable)Run the runnable in AWT Thread if current thread is not AWT thread otherwise runs callSwingUtilities.invokeAndWait(Runnable)static voidselJComboBoxItem(Properties properties, JComboBox combo, Vector<?> namVec, String name)Sets the selection of the JComboBox to the Object 'name' from the list in namVec.static voidsetJMeterHome(String home)static voidsetLocale(Locale loc)Changes the current locale: re-reads resource strings and notifies listeners.static ObjectsetProperty(String propName, String propValue)Set a String valuestatic Stringunsplit(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 Stringunsplit(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.
-
-
-
Field Detail
-
RES_KEY_PFX
public static final String RES_KEY_PFX
- See Also:
- Constant Field Values
-
-
Method Detail
-
getMatcher
public static Perl5Matcher getMatcher()
Gets Perl5Matcher for this thread.- Returns:
- the
Perl5Matcherfor this thread
-
getProperties
public 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
- See Also:
getJMeterProperties(),loadJMeterProperties(String),initLogging(),initLocale()
-
initLogging
public static void initLogging()
Initialise JMeter logging
-
initLocale
public static void initLocale()
Initialise the JMeter Locale
-
loadJMeterProperties
public static void loadJMeterProperties(String file)
Load the JMeter properties file; if not found, then default to "org/apache/jmeter/jmeter.properties" from the classpathc.f. loadProperties
- Parameters:
file- Name of the file from which the JMeter properties should be loaded
-
loadProperties
public 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
public 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.
-
getPatternCache
public static PatternCacheLRU getPatternCache()
-
getPattern
public static Pattern getPattern(String expression) throws MalformedCachePatternException
Get a compiled expression from the pattern cache (READ_ONLY).- Parameters:
expression- regular expression to be looked up- Returns:
- compiled pattern
- Throws:
MalformedCachePatternException- (Runtime) This should be caught for expressions that may vary (e.g. user input)
-
getPattern
public static Pattern getPattern(String expression, int options) throws MalformedCachePatternException
Get a compiled expression from the pattern cache.- Parameters:
expression- REoptions- e.g.READ_ONLY_MASK- Returns:
- compiled pattern
- Throws:
MalformedCachePatternException- (Runtime) This should be caught for expressions that may vary (e.g. user input)
-
initializeProperties
public void initializeProperties(String file)
Description copied from interface:UnitTestManagerYour implementation will be handed the filename that was provided to AllTests as a configuration file. It can hold whatever properties you need to configure your system prior to the unit tests running.- Specified by:
initializePropertiesin interfaceUnitTestManager- Parameters:
file- path to the configuration file
-
findClassesThatExtend
public static List<String> findClassesThatExtend(Class<?> superClass) throws IOException
Convenience method forClassFinder.findClassesThatExtend(String[], Class[], boolean)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.
- Throws:
IOException- when the usedClassFinderthrows one while searching for the class
-
getSearchPaths
public static 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
public static int getRandomInt(int r)
Provide random numbers- Parameters:
r- - the upper bound (exclusive)- Returns:
- a random
int
-
setLocale
public static void setLocale(Locale loc)
Changes the current locale: re-reads resource strings and notifies listeners.- Parameters:
loc- - new locale
-
getLocale
public static Locale getLocale()
Gets the current locale.- Returns:
- current locale
-
addLocaleChangeListener
public static void addLocaleChangeListener(LocaleChangeListener listener)
-
removeLocaleChangeListener
public static void removeLocaleChangeListener(LocaleChangeListener listener)
-
getResString
public 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
public 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 public static String getResString(String key, String defaultValue)
Deprecated.Only intended for use in development; use getResString(String) normallyGets 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
public static String getParsedLabel(String key)
To get I18N label from properties file- Parameters:
key- in messages.properties- Returns:
- I18N label without (if exists) last colon ':' and spaces
-
getLocaleString
public 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
public static Properties getJMeterProperties()
This gets the currently defined appProperties. It can only be called after thegetProperties(String)orloadJMeterProperties(String)method has been called.- Returns:
- The JMeterProperties value,
may be null if
loadJMeterProperties(String)has not been called - See Also:
getProperties(String),loadJMeterProperties(String)
-
getImage
public 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
public 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
-
getTimers
public static Vector<Object> getTimers(Properties properties)
Creates the vector of Timers plugins.- Parameters:
properties- Description of Parameter- Returns:
- The Timers value
-
getVisualizers
public static Vector<Object> getVisualizers(Properties properties)
Creates the vector of visualizer plugins.- Parameters:
properties- Description of Parameter- Returns:
- The Visualizers value
-
getControllers
public static Vector<Object> getControllers(Properties properties)
Creates a vector of SampleController plugins.- Parameters:
properties- The properties with information about the samplers- Returns:
- The Controllers value
-
getTestSamples
public static String[] getTestSamples(Properties properties, String name)
Create a string of class names for a particular SamplerController- Parameters:
properties- The properties with info about the samples.name- The name of the sampler controller.- Returns:
- The TestSamples value
-
getXMLParser
public static XMLReader getXMLParser()
Create an instance of an org.xml.sax.Parser based on the default props.- Returns:
- The XMLParser value
-
getAlias
public static Hashtable<String,String> getAlias(Properties properties)
Creates the vector of alias strings.The properties will be filtered by all values starting with
alias.. The matching entries will be used for the newHashtablewhile the prefixalias.will be stripped of the keys.- Parameters:
properties- the input values- Returns:
- The Alias value
-
getVector
public static Vector<String> getVector(Properties properties, String name)
Creates a vector of strings for all the properties that start with a common prefix.- Parameters:
properties- Description of Parametername- Description of Parameter- Returns:
- The Vector value
-
getHashtable
public static Hashtable<String,String> getHashtable(Properties properties, String prefix)
Creates a table of strings for all the properties that start with a common prefix.So if you have
Propertiespropwith two entries, say- this.test
- that.something
prefixthis, the result would be a newHashtablewith one entry, which key would be test.- Parameters:
properties- input to searchprefix- to match against properties- Returns:
- a Hashtable where the keys are the original matching keys with the prefix removed
-
getPropDefault
public 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
public 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
-
getPropDefault
public 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
public 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
-
getProperty
public 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
public 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
-
selJComboBoxItem
public static void selJComboBoxItem(Properties properties, JComboBox combo, Vector<?> namVec, String name)
Sets the selection of the JComboBox to the Object 'name' from the list in namVec. NOTUSED?- Parameters:
properties- not used at the momentcombo-JComboBoxto work onnamVec- List of names, which are displayed incomboname- Name, that is to be selected. It has to be innamVec
-
instantiate
public static Object instantiate(String className, String impls)
Instatiate an object and guarantee its class.- Parameters:
className- The name of the class to instantiate.impls- The name of the class it must be an instance of- Returns:
- an instance of the class, or null if instantiation failed or the class did not implement/extend as required
-
instantiate
public static Vector<Object> instantiate(Vector<String> v, String className)
Instantiate a vector of classes- Parameters:
v- Description of ParameterclassName- Description of Parameter- Returns:
- Description of the Returned Value
-
createButton
public static JButton createButton(String name, ActionListener listener)
Create a button with the netscape style- Parameters:
name- Description of Parameterlistener- Description of Parameter- Returns:
- Description of the Returned Value
-
createSimpleButton
public static JButton createSimpleButton(String name, ActionListener listener)
Create a button with the netscape style- Parameters:
name- Description of Parameterlistener- Description of Parameter- Returns:
- Description of the Returned Value
-
reportErrorToUser
public 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
public static void reportErrorToUser(String errorMsg, String titleMsg)
Report an error through a dialog box.- Parameters:
errorMsg- - the error message.titleMsg- - title string
-
findInArray
public static int findInArray(String[] array, String value)
Finds a string in an array of strings and returns the- Parameters:
array- Array of strings.value- String to compare to array values.- Returns:
- Index of value in array, or -1 if not in array.
-
unsplit
public static String unsplit(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
public static String unsplit(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.
-
getJMeterHome
public static String getJMeterHome()
Get the JMeter home directory - does not include the trailing separator.- Returns:
- the home directory
-
getJMeterBinDir
public static String getJMeterBinDir()
Get the JMeter bin directory - does not include the trailing separator.- Returns:
- the bin directory
-
setJMeterHome
public static void setJMeterHome(String home)
-
getJMeterVersion
public static String getJMeterVersion()
Gets the JMeter Version.- Returns:
- the JMeter version string
-
getJMeterCopyright
public static String getJMeterCopyright()
Gets the JMeter copyright.- Returns:
- the JMeter copyright string
-
isExpertMode
public 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 iif we're in expert mode
-
findFile
public 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
-
getLocalHostIP
public static String getLocalHostIP()
Returns the cached result from calling InetAddress.getLocalHost().getHostAddress()- Returns:
- String representation of local IP address
-
getLocalHostName
public static String getLocalHostName()
Returns the cached result from calling InetAddress.getLocalHost().getHostName()- Returns:
- local host name
-
getLocalHostFullName
public static String getLocalHostFullName()
Returns the cached result from calling InetAddress.getLocalHost().getCanonicalHostName()- Returns:
- local host name in canonical form
-
parseHeaders
public 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
public static final void runSafe(Runnable runnable)
Run the runnable in AWT Thread if current thread is not AWT thread otherwise runs callSwingUtilities.invokeAndWait(Runnable)- Parameters:
runnable-Runnable
-
helpGC
public static final void helpGC()
Help GC by triggering GC and finalization
-
clearMatcherMemory
public static final 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
-
-