📄 oldsaveservice.java
字号:
if (saveConfig.saveSamplerData(result)) {
config.addChild(createConfigForString("samplerData", result.getSamplerData())); // $NON-NLS-1$
}
if (saveConfig.saveAssertions() && assResults != null) {
for (int i = 0; i < assResults.length; i++) {
config.addChild(getConfiguration(assResults[i]));
}
}
if (saveConfig.saveResponseData(result)) {
config.addChild(getConfiguration(result.getResponseData()));
}
return config;
}
private static Configuration getConfigForTestElement(String named, TestElement item) {
TestElementSaver saver = new TestElementSaver(named);
item.traverse(saver);
Configuration config = saver.getConfiguration();
/*
* DefaultConfiguration config = new DefaultConfiguration("testelement",
* "testelement");
*
* if (named != null) { config.setAttribute("name", named); } if
* (item.getProperty(TestElement.TEST_CLASS) != null) {
* config.setAttribute("class", (String)
* item.getProperty(TestElement.TEST_CLASS)); } else {
* config.setAttribute("class", item.getClass().getName()); } Iterator
* iter = item.getPropertyNames().iterator();
*
* while (iter.hasNext()) { String name = (String) iter.next(); Object
* value = item.getProperty(name);
*
* if (value instanceof TestElement) {
* config.addChild(getConfigForTestElement(name, (TestElement) value)); }
* else if (value instanceof Collection) {
* config.addChild(createConfigForCollection(name, (Collection) value)); }
* else if (value != null) { config.addChild(createConfigForString(name,
* value.toString())); } }
*/
return config;
}
private static Configuration createConfigForString(String name, String value) {
if (value == null) {
value = "";
}
DefaultConfiguration config = new DefaultConfiguration("property", "property");
config.setAttribute("name", name);
config.setValue(value);
config.setAttribute(XML_SPACE, PRESERVE);
return config;
}
public synchronized static HashTree loadSubTree(InputStream in) throws IOException {
try {
Configuration config = builder.build(in);
HashTree loadedTree = generateNode(config);
return loadedTree;
} catch (ConfigurationException e) {
String message = "Problem loading using Avalon Configuration tools";
log.error(message, e);
throw new IOException(message);
} catch (SAXException e) {
String message = "Problem with SAX implementation";
log.error(message, e);
throw new IOException(message);
}
}
private static TestElement createTestElement(Configuration config) throws ConfigurationException,
ClassNotFoundException, IllegalAccessException, InstantiationException {
TestElement element = null;
String testClass = config.getAttribute("class"); // $NON-NLS-1$
String gui_class=""; // $NON-NLS-1$
Configuration[] children = config.getChildren();
for (int i = 0; i < children.length; i++) {
if (children[i].getName().equals("property")) { // $NON-NLS-1$
if (children[i].getAttribute("name").equals(TestElement.GUI_CLASS)){ // $NON-NLS-1$
gui_class=children[i].getValue();
}
}
}
String newClass = NameUpdater.getCurrentTestName(testClass,gui_class);
element = (TestElement) Class.forName(newClass).newInstance();
for (int i = 0; i < children.length; i++) {
if (children[i].getName().equals("property")) { // $NON-NLS-1$
try {
JMeterProperty prop = createProperty(children[i], newClass);
if (prop!=null) element.setProperty(prop);
} catch (Exception ex) {
log.error("Problem loading property", ex);
element.setProperty(children[i].getAttribute("name"), ""); // $NON-NLS-1$ // $NON-NLS-2$
}
} else if (children[i].getName().equals("testelement")) { // $NON-NLS-1$
element.setProperty(new TestElementProperty(children[i].getAttribute("name", ""), // $NON-NLS-1$ // $NON-NLS-2$
createTestElement(children[i])));
} else if (children[i].getName().equals("collection")) { // $NON-NLS-1$
element.setProperty(new CollectionProperty(children[i].getAttribute("name", ""), // $NON-NLS-1$ // $NON-NLS-2$
createCollection(children[i], newClass)));
} else if (children[i].getName().equals("map")) { // $NON-NLS-1$
element.setProperty(new MapProperty(children[i].getAttribute("name", ""), // $NON-NLS-1$ // $NON-NLS-2$
createMap(children[i],newClass)));
}
}
return element;
}
private static Collection createCollection(Configuration config, String testClass) throws ConfigurationException,
ClassNotFoundException, IllegalAccessException, InstantiationException {
Collection coll = (Collection) Class.forName(config.getAttribute("class")).newInstance(); // $NON-NLS-1$
Configuration[] items = config.getChildren();
for (int i = 0; i < items.length; i++) {
if (items[i].getName().equals("property")) { // $NON-NLS-1$
JMeterProperty prop = createProperty(items[i], testClass);
if (prop!=null) coll.add(prop);
} else if (items[i].getName().equals("testelement")) { // $NON-NLS-1$
coll.add(new TestElementProperty(items[i].getAttribute("name", ""), createTestElement(items[i]))); // $NON-NLS-1$ // $NON-NLS-2$
} else if (items[i].getName().equals("collection")) { // $NON-NLS-1$
coll.add(new CollectionProperty(items[i].getAttribute("name", ""), // $NON-NLS-1$ // $NON-NLS-2$
createCollection(items[i], testClass)));
} else if (items[i].getName().equals("string")) { // $NON-NLS-1$
JMeterProperty prop = createProperty(items[i], testClass);
if (prop!=null) coll.add(prop);
} else if (items[i].getName().equals("map")) { // $NON-NLS-1$
coll.add(new MapProperty(items[i].getAttribute("name", ""), createMap(items[i], testClass))); // $NON-NLS-1$ // $NON-NLS-2$
}
}
return coll;
}
private static JMeterProperty createProperty(Configuration config, String testClass) throws IllegalAccessException,
ClassNotFoundException, InstantiationException {
String value = config.getValue(""); // $NON-NLS-1$
String name = config.getAttribute("name", value); // $NON-NLS-1$
String oname = name;
String type = config.getAttribute("propType", StringProperty.class.getName()); // $NON-NLS-1$
// Do upgrade translation:
name = NameUpdater.getCurrentName(name, testClass);
if (TestElement.GUI_CLASS.equals(name)) {
value = NameUpdater.getCurrentName(value);
} else if (TestElement.TEST_CLASS.equals(name)) {
value=testClass; // must always agree
} else {
value = NameUpdater.getCurrentName(value, name, testClass);
}
// Delete any properties whose name converts to the empty string
if (oname.length() != 0 && name.length()==0) {
return null;
}
// Create the property:
JMeterProperty prop = (JMeterProperty) Class.forName(type).newInstance();
prop.setName(name);
prop.setObjectValue(value);
return prop;
}
private static Map createMap(Configuration config, String testClass) throws ConfigurationException,
ClassNotFoundException, IllegalAccessException, InstantiationException {
Map map = (Map) Class.forName(config.getAttribute("class")).newInstance();
Configuration[] items = config.getChildren();
for (int i = 0; i < items.length; i++) {
if (items[i].getName().equals("property")) { // $NON-NLS-1$
JMeterProperty prop = createProperty(items[i], testClass);
if (prop!=null) map.put(prop.getName(), prop);
} else if (items[i].getName().equals("testelement")) { // $NON-NLS-1$
map.put(items[i].getAttribute("name", ""), new TestElementProperty(items[i].getAttribute("name", ""), // $NON-NLS-1$ // $NON-NLS-2$
createTestElement(items[i])));
} else if (items[i].getName().equals("collection")) { // $NON-NLS-1$
map.put(items[i].getAttribute("name"), // $NON-NLS-1$
new CollectionProperty(items[i].getAttribute("name", ""), // $NON-NLS-1$ // $NON-NLS-2$
createCollection(items[i], testClass)));
} else if (items[i].getName().equals("map")) { // $NON-NLS-1$
map.put(items[i].getAttribute("name", ""), // $NON-NLS-1$ // $NON-NLS-2$
new MapProperty(items[i].getAttribute("name", ""), // $NON-NLS-1$ // $NON-NLS-2$
createMap(items[i], testClass)));
}
}
return map;
}
private static HashTree generateNode(Configuration config) {
TestElement element = null;
try {
element = createTestElement(config.getChild("testelement")); // $NON-NLS-1$
} catch (Exception e) {
log.error("Problem loading part of file", e);
return null;
}
HashTree subTree = new ListedHashTree(element);
Configuration[] subNodes = config.getChildren("node"); // $NON-NLS-1$
for (int i = 0; i < subNodes.length; i++) {
HashTree t = generateNode(subNodes[i]);
if (t != null) {
subTree.add(element, t);
}
}
return subTree;
}
// Called by ResultCollector#loadExistingFile()
public static void processSamples(String filename, Visualizer visualizer, ResultCollector rc)
throws SAXException, IOException, ConfigurationException
{
DefaultConfigurationBuilder cfgbuilder = new DefaultConfigurationBuilder();
Configuration savedSamples = cfgbuilder.buildFromFile(filename);
Configuration[] samples = savedSamples.getChildren();
for (int i = 0; i < samples.length; i++) {
SampleResult result = OldSaveService.getSampleResult(samples[i]);
if (rc.isSampleWanted(result.isSuccessful())) {
visualizer.add(result);
}
}
}
// Called by ResultCollector#recordResult()
public static String getSerializedSampleResult(
SampleResult result, DefaultConfigurationSerializer slzr, SampleSaveConfiguration cfg)
throws SAXException, IOException,
ConfigurationException {
ByteArrayOutputStream tempOut = new ByteArrayOutputStream();
slzr.serialize(tempOut, OldSaveService.getConfiguration(result, cfg));
String serVer = tempOut.toString();
String lineSep=System.getProperty("line.separator"); // $NON-NLS-1$
/*
* Remove the <?xml ... ?> prefix.
* When using the x-jars (xakan etc) or Java 1.4, the serialised output has a
* newline after the prefix. However, when using Java 1.5 without the x-jars, the output
* has no newline at all.
*/
int index = serVer.indexOf(lineSep); // Is there a new-line?
if (index > -1) {// Yes, assume it follows the prefix
return serVer.substring(index);
}
if (serVer.startsWith("<?xml")){ // $NON-NLS-1$
index=serVer.indexOf("?>");// must exist // $NON-NLS-1$
return lineSep + serVer.substring(index+2);// +2 for ?>
}
return serVer;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -