jmeterutils.java
来自「测试工具」· Java 代码 · 共 1,073 行 · 第 1/3 页
JAVA
1,073 行
/**
* Gets the resource string for this key.
*
* If the resource is not found, a warning is logged
*
* @param key
* the key in the resource file
* @param defaultValue -
* the default value
*
* @return the resource string if the key is found; otherwise, return the
* default
* @deprecated Only intended for use in development; use
* getResString(String) normally
*/
public static String getResString(String key, String defaultValue) {
return getResStringDefault(key, defaultValue);
}
/*
* Helper method to do the actual work of fetching resources; allows
* getResString(S,S) to be deprecated without affecting getResString(S);
*/
private static String getResStringDefault(String key, String defaultValue) {
if (key == null) {
return null;
}
key = key.replace(' ', '_'); // TODO - why does it do this? // $NON-NLS-1$ // $NON-NLS-2$
key = key.toLowerCase(); // (it's been here since v1.1)
String resString = null;
try {
resString = resources.getString(key);
} catch (MissingResourceException mre) {
log.warn("ERROR! Resource string not found: [" + key + "]", mre);
resString = defaultValue;
}
return resString;
}
/**
* This gets the currently defined appProperties. It can only be called
* after the {@link #getProperties(String)} method is called.
*
* @return The JMeterProperties value
*/
public static Properties getJMeterProperties() {
return appProperties;
}
/**
* This looks for the requested image in the classpath under
* org.apache.jmeter.images. <name>
*
* @param name
* Description of Parameter
* @return The Image value
*/
public static ImageIcon getImage(String name) {
try {
return new ImageIcon(JMeterUtils.class.getClassLoader().getResource(
"org/apache/jmeter/images/" + name.trim())); // $NON-NLS-1$
} catch (NullPointerException e) {
log.warn("no icon for " + name);
return null;
} catch (NoClassDefFoundError e) {// Can be returned by headless hosts
log.info("no icon for " + name + " " + e.getMessage());
return null;
} catch (InternalError e) {// Can be returned by headless hosts
log.info("no icon for " + name + " " + e.getMessage());
return null;
}
}
/**
* 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.
*
* @param name
* the name of the image
* @param description
* the description of the image
* @return The Image value
*/
public static ImageIcon getImage(String name, String description) {
ImageIcon icon = getImage(name);
if(icon != null) {
icon.setDescription(description);
}
return icon;
}
public static String getResourceFileAsText(String name) {
BufferedReader fileReader = null;
try {
String lineEnd = System.getProperty("line.separator"); // $NON-NLS-1$
fileReader = new BufferedReader(new InputStreamReader(JMeterUtils.class.getClassLoader()
.getResourceAsStream(name)));
StringBuffer text = new StringBuffer();
String line = "NOTNULL"; // $NON-NLS-1$
while (line != null) {
line = fileReader.readLine();
if (line != null) {
text.append(line);
text.append(lineEnd);
}
}
// Done by finally block: fileReader.close();
return text.toString();
} catch (NullPointerException e) // Cannot find file
{
return ""; // $NON-NLS-1$
} catch (IOException e) {
return ""; // $NON-NLS-1$
} finally {
if (fileReader != null)
try {
fileReader.close();
} catch (IOException e1) {
}
}
}
/**
* Creates the vector of Timers plugins.
*
* @param properties
* Description of Parameter
* @return The Timers value
*/
public static Vector getTimers(Properties properties) {
return instantiate(getVector(properties, "timer."), // $NON-NLS-1$
"org.apache.jmeter.timers.Timer"); // $NON-NLS-1$
}
/**
* Creates the vector of visualizer plugins.
*
* @param properties
* Description of Parameter
* @return The Visualizers value
*/
public static Vector getVisualizers(Properties properties) {
return instantiate(getVector(properties, "visualizer."), // $NON-NLS-1$
"org.apache.jmeter.visualizers.Visualizer"); // $NON-NLS-1$
}
/**
* Creates a vector of SampleController plugins.
*
* @param properties
* The properties with information about the samplers
* @return The Controllers value
*/
// TODO - does not appear to be called directly
public static Vector getControllers(Properties properties) {
String name = "controller."; // $NON-NLS-1$
Vector v = new Vector();
Enumeration names = properties.keys();
while (names.hasMoreElements()) {
String prop = (String) names.nextElement();
if (prop.startsWith(name)) {
Object o = instantiate(properties.getProperty(prop),
"org.apache.jmeter.control.SamplerController"); // $NON-NLS-1$
v.addElement(o);
}
}
return v;
}
/**
* Create a string of class names for a particular SamplerController
*
* @param properties
* The properties with info about the samples.
* @param name
* The name of the sampler controller.
* @return The TestSamples value
*/
public static String[] getTestSamples(Properties properties, String name) {
return (String[]) getVector(properties, name + ".testsample").toArray(new String[0]); // $NON-NLS-1$
}
/**
* Create an instance of an org.xml.sax.Parser based on the default props.
*
* @return The XMLParser value
*/
public static XMLReader getXMLParser() {
XMLReader reader = null;
try {
reader = (XMLReader) instantiate(getPropDefault("xml.parser", // $NON-NLS-1$
"org.apache.xerces.parsers.SAXParser"), // $NON-NLS-1$
"org.xml.sax.XMLReader"); // $NON-NLS-1$
// reader = xmlFactory.newSAXParser().getXMLReader();
} catch (Exception e) {
reader = (XMLReader) instantiate(getPropDefault("xml.parser", // $NON-NLS-1$
"org.apache.xerces.parsers.SAXParser"), // $NON-NLS-1$
"org.xml.sax.XMLReader"); // $NON-NLS-1$
}
return reader;
}
/**
* Creates the vector of alias strings.
*
* @param properties
* Description of Parameter
* @return The Alias value
*/
public static Hashtable getAlias(Properties properties) {
return getHashtable(properties, "alias."); // $NON-NLS-1$
}
/**
* Creates a vector of strings for all the properties that start with a
* common prefix.
*
* @param properties
* Description of Parameter
* @param name
* Description of Parameter
* @return The Vector value
*/
public static Vector getVector(Properties properties, String name) {
Vector v = new Vector();
Enumeration names = properties.keys();
while (names.hasMoreElements()) {
String prop = (String) names.nextElement();
if (prop.startsWith(name)) {
v.addElement(properties.getProperty(prop));
}
}
return v;
}
/**
* Creates a table of strings for all the properties that start with a
* common prefix.
*
* @param properties
* Description of Parameter
* @param name
* Description of Parameter
* @return The Hashtable value
*/
public static Hashtable getHashtable(Properties properties, String name) {
Hashtable t = new Hashtable();
Enumeration names = properties.keys();
while (names.hasMoreElements()) {
String prop = (String) names.nextElement();
if (prop.startsWith(name)) {
t.put(prop.substring(name.length()), properties.getProperty(prop));
}
}
return t;
}
/**
* Get a int value with default if not present.
*
* @param propName
* the name of the property.
* @param defaultVal
* the default value.
* @return The PropDefault value
*/
public static int getPropDefault(String propName, int defaultVal) {
int ans;
try {
ans = (Integer.valueOf(appProperties.getProperty(propName, Integer.toString(defaultVal)).trim()))
.intValue();
} catch (Exception e) {
ans = defaultVal;
}
return ans;
}
/**
* Get a boolean value with default if not present.
*
* @param propName
* the name of the property.
* @param defaultVal
* the default value.
* @return The PropDefault value
*/
public static boolean getPropDefault(String propName, boolean defaultVal) {
boolean ans;
try {
String strVal = appProperties.getProperty(propName, Boolean.toString(defaultVal)).trim();
if (strVal.equalsIgnoreCase("true") || strVal.equalsIgnoreCase("t")) { // $NON-NLS-1$ // $NON-NLS-2$
ans = true;
} else if (strVal.equalsIgnoreCase("false") || strVal.equalsIgnoreCase("f")) { // $NON-NLS-1$ // $NON-NLS-2$
ans = false;
} else {
ans = ((Integer.valueOf(strVal)).intValue() == 1);
}
} catch (Exception e) {
ans = defaultVal;
}
return ans;
}
/**
* Get a long value with default if not present.
*
* @param propName
* the name of the property.
* @param defaultVal
* the default value.
* @return The PropDefault value
*/
public static long getPropDefault(String propName, long defaultVal) {
long ans;
try {
ans = (Long.valueOf(appProperties.getProperty(propName, Long.toString(defaultVal)).trim())).longValue();
} catch (Exception e) {
ans = defaultVal;
}
return ans;
}
/**
* Get a String value with default if not present.
*
* @param propName
* the name of the property.
* @param defaultVal
* the default value.
* @return The PropDefault value
*/
public static String getPropDefault(String propName, String defaultVal) {
String ans;
try {
ans = appProperties.getProperty(propName, defaultVal).trim();
} catch (Exception e) {
ans = defaultVal;
}
return ans;
}
/**
* Get the value of a JMeter property.
*
* @param propName
* the name of the property.
* @return the value of the JMeter property, or null if not defined
*/
public static String getProperty(String propName) {
String ans = null;
try {
ans = appProperties.getProperty(propName);
} catch (Exception e) {
ans = null;
}
return ans;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?