jmeterutils.java

来自「测试工具」· Java 代码 · 共 1,073 行 · 第 1/3 页

JAVA
1,073
字号
	}

	/**
	 * Set a String value
	 * 
	 * @param propName
	 *            the name of the property.
	 * @param propValue
	 *            the value of the property
	 * @return the previous value of the property
	 */
	public static Object setProperty(String propName, String propValue) {
		return appProperties.setProperty(propName, propValue);
	}

	/**
	 * Sets the selection of the JComboBox to the Object 'name' from the list in
	 * namVec.
	 */
	public static void selJComboBoxItem(Properties properties, JComboBox combo, Vector namVec, String name) {
		int idx = namVec.indexOf(name);
		combo.setSelectedIndex(idx);
		// Redisplay.
		combo.updateUI();
		return;
	}

	/**
	 * Instatiate an object and guarantee its class.
	 * 
	 * @param className
	 *            The name of the class to instantiate.
	 * @param impls
	 *            The name of the class it subclases.
	 * @return Description of the Returned Value
	 */
	public static Object instantiate(String className, String impls) {
		if (className != null) {
			className = className.trim();
		}

		if (impls != null) {
			impls = impls.trim();
		}

		try {
			Class c = Class.forName(impls);
			try {
				Class o = Class.forName(className);
				Object res = o.newInstance();
				if (c.isInstance(res)) {
					return res;
				}
				throw new IllegalArgumentException(className + " is not an instance of " + impls);
			} catch (ClassNotFoundException e) {
				log.error("Error loading class " + className + ": class is not found");
			} catch (IllegalAccessException e) {
				log.error("Error loading class " + className + ": does not have access");
			} catch (InstantiationException e) {
				log.error("Error loading class " + className + ": could not instantiate");
			} catch (NoClassDefFoundError e) {
				log.error("Error loading class " + className + ": couldn't find class " + e.getMessage());
			}
		} catch (ClassNotFoundException e) {
			log.error("Error loading class " + impls + ": was not found.");
		}
		return null;
	}

	/**
	 * Instantiate a vector of classes
	 * 
	 * @param v
	 *            Description of Parameter
	 * @param className
	 *            Description of Parameter
	 * @return Description of the Returned Value
	 */
	public static Vector instantiate(Vector v, String className) {
		Vector i = new Vector();
		try {
			Class c = Class.forName(className);
			Enumeration elements = v.elements();
			while (elements.hasMoreElements()) {
				String name = (String) elements.nextElement();
				try {
					Object o = Class.forName(name).newInstance();
					if (c.isInstance(o)) {
						i.addElement(o);
					}
				} catch (ClassNotFoundException e) {
					log.error("Error loading class " + name + ": class is not found");
				} catch (IllegalAccessException e) {
					log.error("Error loading class " + name + ": does not have access");
				} catch (InstantiationException e) {
					log.error("Error loading class " + name + ": could not instantiate");
				} catch (NoClassDefFoundError e) {
					log.error("Error loading class " + name + ": couldn't find class " + e.getMessage());
				}
			}
		} catch (ClassNotFoundException e) {
			log.error("Error loading class " + className + ": class is not found");
		}
		return i;
	}

	/**
	 * Tokenize a string into a vector of tokens
	 * 
	 * @param string
	 *            Description of Parameter
	 * @param separator
	 *            Description of Parameter
	 * @return Description of the Returned Value
	 */
    //TODO move to JOrphanUtils ?
	public static Vector tokenize(String string, String separator) {
		Vector v = new Vector();
		StringTokenizer s = new StringTokenizer(string, separator);
		while (s.hasMoreTokens()) {
			v.addElement(s.nextToken());
		}
		return v;
	}

	/**
	 * Create a button with the netscape style
	 * 
	 * @param name
	 *            Description of Parameter
	 * @param listener
	 *            Description of Parameter
	 * @return Description of the Returned Value
	 */
	public static JButton createButton(String name, ActionListener listener) {
		JButton button = new JButton(getImage(name + ".on.gif")); // $NON-NLS-1$
		button.setDisabledIcon(getImage(name + ".off.gif")); // $NON-NLS-1$
		button.setRolloverIcon(getImage(name + ".over.gif")); // $NON-NLS-1$
		button.setPressedIcon(getImage(name + ".down.gif")); // $NON-NLS-1$
		button.setActionCommand(name);
		button.addActionListener(listener);
		button.setRolloverEnabled(true);
		button.setFocusPainted(false);
		button.setBorderPainted(false);
		button.setOpaque(false);
		button.setPreferredSize(new Dimension(24, 24));
		return button;
	}

	/**
	 * Create a button with the netscape style
	 * 
	 * @param name
	 *            Description of Parameter
	 * @param listener
	 *            Description of Parameter
	 * @return Description of the Returned Value
	 */
	public static JButton createSimpleButton(String name, ActionListener listener) {
		JButton button = new JButton(getImage(name + ".gif")); // $NON-NLS-1$
		button.setActionCommand(name);
		button.addActionListener(listener);
		button.setFocusPainted(false);
		button.setBorderPainted(false);
		button.setOpaque(false);
		button.setPreferredSize(new Dimension(25, 25));
		return button;
	}


	/**
	 * Report an error through a dialog box.
	 * Title defaults to "error_title" resource string
	 * @param errorMsg - the error message.
	 */
	public static void reportErrorToUser(String errorMsg) {
		reportErrorToUser(errorMsg, JMeterUtils.getResString("error_title")); // $NON-NLS-1$
	}

	/**
	 * Report an error through a dialog box.
	 * 
	 * @param errorMsg - the error message.
	 * @param titleMsg - title string
	 */
	public static void reportErrorToUser(String errorMsg, String titleMsg) {
		if (errorMsg == null) {
			errorMsg = "Unknown error - see log file";
			log.warn("Unknown error", new Throwable("errorMsg == null"));
		}
		GuiPackage instance = GuiPackage.getInstance();
		if (instance == null) {
			System.out.println(errorMsg);
			return; // Done
		}
		try {
			JOptionPane.showMessageDialog(instance.getMainFrame(),
					errorMsg, 
					titleMsg,
					JOptionPane.ERROR_MESSAGE);
		} catch (HeadlessException e) {
				log.warn("reportErrorToUser(\"" + errorMsg + "\") caused", e);
		}
	}

	/**
	 * Finds a string in an array of strings and returns the
	 * 
	 * @param array
	 *            Array of strings.
	 * @param value
	 *            String to compare to array values.
	 * @return Index of value in array, or -1 if not in array.
	 */
    //TODO - move to JOrphanUtils?
	public static int findInArray(String[] array, String value) {
		int count = -1;
		int index = -1;
		if (array != null && value != null) {
			while (++count < array.length) {
				if (array[count] != null && array[count].equals(value)) {
					index = count;
					break;
				}
			}
		}
		return index;
	}

	/**
	 * 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.
	 * 
	 * @param splittee
	 *            Array of Objects to be concatenated.
	 * @param splitChar
	 *            Object to unsplit the strings with.
	 * @return Array of all the tokens.
	 */
    //TODO - move to JOrphanUtils?
	public static String unsplit(Object[] splittee, Object splitChar) {
		StringBuffer retVal = new StringBuffer();
		int count = -1;
		while (++count < splittee.length) {
			if (splittee[count] != null) {
				retVal.append(splittee[count]);
			}
			if (count + 1 < splittee.length && splittee[count + 1] != null) {
				retVal.append(splitChar);
			}
		}
		return retVal.toString();
	}

	// End Method

	/**
	 * 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.
	 * 
	 * @param splittee
	 *            Array of Objects to be concatenated.
	 * @param splitChar
	 *            Object to unsplit the strings with.
	 * @param def
	 *            Default value to replace null values in array.
	 * @return Array of all the tokens.
	 */
    //TODO - move to JOrphanUtils?
	public static String unsplit(Object[] splittee, Object splitChar, String def) {
		StringBuffer retVal = new StringBuffer();
		int count = -1;
		while (++count < splittee.length) {
			if (splittee[count] != null) {
				retVal.append(splittee[count]);
			} else {
				retVal.append(def);
			}
			if (count + 1 < splittee.length) {
				retVal.append(splitChar);
			}
		}
		return retVal.toString();
	}

	/**
	 * Get the JMeter home directory - does not include the trailing separator.
	 * 
	 * @return the home directory
	 */
	public static String getJMeterHome() {
		return jmDir;
	}

	/**
	 * Get the JMeter bin directory - does not include the trailing separator.
	 * 
	 * @return the bin directory
	 */
	public static String getJMeterBinDir() {
		return jmBin;
	}

	public static void setJMeterHome(String home) {
		jmDir = home;
		jmBin = jmDir + File.separator + "bin"; // $NON-NLS-1$
	}

	private static String jmDir; // JMeter Home directory (excludes trailing separator)
	private static String jmBin; // JMeter bin directory (excludes trailing separator)


	/**
	 * Gets the JMeter Version.
	 * 
	 * @return the JMeter version string
	 */
	public static String getJMeterVersion() {
		return JMeterVersion.getVERSION();
	}

	/**
	 * Gets the JMeter copyright.
	 * 
	 * @return the JMeter copyright string
	 */
	public static String getJMeterCopyright() {
		return JMeterVersion.COPYRIGHT;
	}

	/**
	 * Determine whether we are in 'expert' mode. Certain features may be hidden
	 * from user's view unless in expert mode.
	 * 
	 * @return true iif we're in expert mode
	 */
	public static boolean isExpertMode() {
		return JMeterUtils.getPropDefault(EXPERT_MODE_PROPERTY, false);
	}
	
	/**
	 * Find a file in the current directory or in the JMeter bin directory.
	 * 
	 * @param fileName
	 * @return File object
	 */
	public static File findFile(String fileName){
		File f =new File(fileName);
		if (!f.exists()){
			f=new File(getJMeterBinDir(),fileName);
		}
		return f;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?