📄 jmetertest.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.jmeter.junit;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.swing.JComponent;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.jmeter.config.gui.ObsoleteGui;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.functions.Function;
import org.apache.jmeter.gui.GuiPackage;
import org.apache.jmeter.gui.JMeterGUIComponent;
import org.apache.jmeter.gui.UnsharedComponent;
import org.apache.jmeter.gui.action.ActionRouter;
import org.apache.jmeter.gui.tree.JMeterTreeListener;
import org.apache.jmeter.gui.tree.JMeterTreeModel;
import org.apache.jmeter.gui.tree.JMeterTreeNode;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testbeans.TestBean;
import org.apache.jmeter.testbeans.gui.TestBeanGUI;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.property.JMeterProperty;
import org.apache.jmeter.testelement.property.PropertyIterator;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.jorphan.reflect.ClassFinder;
import org.apache.jorphan.util.JOrphanUtils;
import org.apache.log.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
/**
* @version $Revision: 541880 $ Last update $Date: 2007-05-26 10:51:07 +0100 (Sat, 26 May 2007) $
*/
public class JMeterTest extends JMeterTestCase {
private static Logger log = LoggingManager.getLoggerForClass();
private static Map guiTitles;
private static Map guiTags;
private static Map funcTitles;
private static Properties nameMap;
private static final Locale TEST_LOCALE = Locale.ENGLISH;
public JMeterTest(String name) {
super(name);
}
/*
* The suite() method creates separate test suites for each of the types of
* test. The suitexxx() methods create a list of items to be tested, and
* create a new test instance for each.
*
* Each test type has its own constructor, which saves the item to be tested
*
* Note that the suite() method must be static, and the methods to run the
* tests must be instance methods so that they can pick up the item value
* which was saved by the constructor.
*
*/
// Constructor for TestElement tests
private TestElement testItem;
public JMeterTest(String testName, TestElement te) {
super(testName);// Save the method name
testItem = te;
}
// Constructor for Serializable tests
private Serializable serObj;
public JMeterTest(String testName, Serializable ser) {
super(testName);// Save the method name
serObj = ser;
}
// Constructor for GUI tests
private JMeterGUIComponent guiItem;
public JMeterTest(String testName, JMeterGUIComponent gc) {
super(testName);// Save the method name
guiItem = gc;
}
// Constructor for Function tests
private Function funcItem;
private static boolean classPathShown = false;// Only show classpath once
public JMeterTest(String testName, Function fi) {
super(testName);// Save the method name
funcItem = fi;
}
/*
* Use a suite to allow the tests to be generated at run-time
*/
public static Test suite() throws Exception {
// ensure the GuiPackage is initialized.
JMeterTreeModel treeModel = new JMeterTreeModel();
JMeterTreeListener treeLis = new JMeterTreeListener(treeModel);
treeLis.setActionHandler(ActionRouter.getInstance());
GuiPackage.getInstance(treeLis, treeModel);
try {
// The GuiPackage needs a MainFrame to work:
org.apache.jmeter.gui.MainFrame main = new org.apache.jmeter.gui.MainFrame(ActionRouter.getInstance(),
treeModel, treeLis);
} catch (RuntimeException e) {
System.out.println("Cannot create MainFrame: " + e);
}
// The Locale used to instantiate the GUI objects
JMeterUtils.setLocale(TEST_LOCALE);
TestSuite suite = new TestSuite("JMeterTest");
suite.addTest(new JMeterTest("readAliases"));
suite.addTest(new JMeterTest("createTitleSet"));
suite.addTest(new JMeterTest("createTagSet"));
suite.addTest(suiteGUIComponents());
suite.addTest(suiteSerializableElements());
suite.addTest(suiteTestElements());
suite.addTest(suiteBeanComponents());
suite.addTest(new JMeterTest("createFunctionSet"));
suite.addTest(suiteFunctions());
suite.addTest(new JMeterTest("checkGuiSet"));
suite.addTest(new JMeterTest("checkFunctionSet"));
return suite;
}
/*
* Extract titles from component_reference.xml
*/
public void createTitleSet() throws Exception {
guiTitles = new HashMap(90);
String compref = "../xdocs/usermanual/component_reference.xml";
SAXBuilder bldr = new SAXBuilder();
Document doc;
doc = bldr.build(compref);
Element root = doc.getRootElement();
Element body = root.getChild("body");
List sections = body.getChildren("section");
for (int i = 0; i < sections.size(); i++) {
List components = ((Element) sections.get(i)).getChildren("component");
for (int j = 0; j < components.size(); j++) {
Element comp = (Element) components.get(j);
String nm=comp.getAttributeValue("name");
if (!nm.equals("SSL Manager")){// Not a true GUI component
guiTitles.put(nm.replace(' ','_'), Boolean.FALSE);
}
}
}
// Add titles that don't need to be documented
//guiTitles.put("Root", Boolean.FALSE);
guiTitles.put("Example Sampler", Boolean.FALSE);
}
/*
* Extract titles from component_reference.xml
*/
public void createTagSet() throws Exception {
guiTags = new HashMap(90);
String compref = "../xdocs/usermanual/component_reference.xml";
SAXBuilder bldr = new SAXBuilder();
Document doc;
doc = bldr.build(compref);
Element root = doc.getRootElement();
Element body = root.getChild("body");
List sections = body.getChildren("section");
for (int i = 0; i < sections.size(); i++) {
List components = ((Element) sections.get(i)).getChildren("component");
for (int j = 0; j < components.size(); j++) {
Element comp = (Element) components.get(j);
guiTags.put(comp.getAttributeValue("tag"), Boolean.FALSE);
}
}
}
/*
* Extract titles from functions.xml
*/
public void createFunctionSet() throws Exception {
funcTitles = new HashMap(20);
String compref = "../xdocs/usermanual/functions.xml";
SAXBuilder bldr = new SAXBuilder();
Document doc;
doc = bldr.build(compref);
Element root = doc.getRootElement();
Element body = root.getChild("body");
Element section = body.getChild("section");
List sections = section.getChildren("subsection");
for (int i = 0; i < sections.size(); i++) {
List components = ((Element) sections.get(i)).getChildren("component");
for (int j = 0; j < components.size(); j++) {
Element comp = (Element) components.get(j);
funcTitles.put(comp.getAttributeValue("name"), Boolean.FALSE);
}
}
}
private int scanprintMap(Map m, String t) {
Set s = m.keySet();
int unseen = 0;
if (s.size() == 0)
return 0;
Iterator i = s.iterator();
while (i.hasNext()) {
Object key = i.next();
if (!m.get(key).equals(Boolean.TRUE)) {
if (unseen == 0)// first time
{
System.out.println("\nNames remaining in " + t + " Map:");
}
unseen++;
System.out.println(key);
}
}
return unseen;
}
public void checkGuiSet() throws Exception {
guiTitles.remove("Example Sampler");// We don't mind if this is left over
guiTitles.remove("Sample_Result_Save_Configuration");// Ditto, not a sampler
assertEquals("Should not have any names left over", 0, scanprintMap(guiTitles, "GUI"));
}
public void checkFunctionSet() throws Exception {
assertEquals("Should not have any names left over", 0, scanprintMap(funcTitles, "Function"));
}
/*
* Test GUI elements - create the suite of tests
*/
private static Test suiteGUIComponents() throws Exception {
TestSuite suite = new TestSuite("GuiComponents");
Iterator iter = getObjects(JMeterGUIComponent.class).iterator();
while (iter.hasNext()) {
JMeterGUIComponent item = (JMeterGUIComponent) iter.next();
if (item instanceof JMeterTreeNode) {
System.out.println("INFO: JMeterGUIComponent: skipping all tests " + item.getClass().getName());
continue;
}
if (item instanceof ObsoleteGui){
continue;
}
TestSuite ts = new TestSuite(item.getClass().getName());
ts.addTest(new JMeterTest("GUIComponents1", item));
if (item instanceof TestBeanGUI) {
System.out.println("INFO: JMeterGUIComponent: skipping some tests " + item.getClass().getName());
} else {
ts.addTest(new JMeterTest("GUIComponents2", item));
ts.addTest(new JMeterTest("runGUITitle", item));
}
suite.addTest(ts);
}
return suite;
}
/*
* Test Functions - create the suite of tests
*/
private static Test suiteFunctions() throws Exception {
TestSuite suite = new TestSuite("Functions");
Iterator iter = getObjects(Function.class).iterator();
while (iter.hasNext()) {
Object item = iter.next();
if (item.getClass().equals(CompoundVariable.class)) {
continue;
}
TestSuite ts = new TestSuite(item.getClass().getName());
ts.addTest(new JMeterTest("runFunction", (Function) item));
ts.addTest(new JMeterTest("runFunction2", (Function) item));
suite.addTest(ts);
}
return suite;
}
/*
* Test GUI elements - create the suite of tests
*/
private static Test suiteBeanComponents() throws Exception {
TestSuite suite = new TestSuite("BeanComponents");
Iterator iter = getObjects(TestBean.class).iterator();
while (iter.hasNext()) {
Class c = iter.next().getClass();
try {
JMeterGUIComponent item = new TestBeanGUI(c);
// JMeterGUIComponent item = (JMeterGUIComponent) iter.next();
TestSuite ts = new TestSuite(item.getClass().getName());
ts.addTest(new JMeterTest("GUIComponents2", item));
ts.addTest(new JMeterTest("runGUITitle", item));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -