⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oldsaveservice.java

📁 测试工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * 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.save;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.configuration.DefaultConfiguration;
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer;
import org.apache.jmeter.assertions.AssertionResult;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.SampleSaveConfiguration;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.property.CollectionProperty;
import org.apache.jmeter.testelement.property.JMeterProperty;
import org.apache.jmeter.testelement.property.MapProperty;
import org.apache.jmeter.testelement.property.StringProperty;
import org.apache.jmeter.testelement.property.TestElementProperty;
import org.apache.jmeter.util.NameUpdater;
import org.apache.jmeter.visualizers.Visualizer;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.ListedHashTree;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
import org.xml.sax.SAXException;

/**
 * This class saves/restores the original Avalon XML format (not used by default).
 * 
 * This may be removed in a future release.
 */
public final class OldSaveService {
	private static final Logger log = LoggingManager.getLoggerForClass();

    // ---------------------------------------------------------------------
    // XML RESULT FILE CONSTANTS AND FIELD NAME CONSTANTS
    // ---------------------------------------------------------------------

    // Shared with TestElementSaver
    final static String PRESERVE = "preserve"; // $NON-NLS-1$
    final static String XML_SPACE = "xml:space"; // $NON-NLS-1$

    private static final String ASSERTION_RESULT_TAG_NAME = "assertionResult"; // $NON-NLS-1$
    private static final String BINARY = "binary"; // $NON-NLS-1$
    private static final String DATA_TYPE = "dataType"; // $NON-NLS-1$
    private static final String ERROR = "error"; // $NON-NLS-1$
    private static final String FAILURE = "failure"; // $NON-NLS-1$
    private static final String FAILURE_MESSAGE = "failureMessage"; // $NON-NLS-1$
    private static final String LABEL = "label"; // $NON-NLS-1$
    private static final String RESPONSE_CODE = "responseCode"; // $NON-NLS-1$
    private static final String RESPONSE_MESSAGE = "responseMessage"; // $NON-NLS-1$
    private static final String SAMPLE_RESULT_TAG_NAME = "sampleResult"; // $NON-NLS-1$
    private static final String SUCCESSFUL = "success"; // $NON-NLS-1$
    private static final String THREAD_NAME = "threadName"; // $NON-NLS-1$
    private static final String TIME = "time"; // $NON-NLS-1$
    private static final String TIME_STAMP = "timeStamp"; // $NON-NLS-1$

	private static DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();

	/**
	 * Private constructor to prevent instantiation.
	 */
	private OldSaveService() {
	}

    
    public static void saveSubTree(HashTree subTree, OutputStream writer) throws IOException {
		Configuration config = (Configuration) getConfigsFromTree(subTree).get(0);
		DefaultConfigurationSerializer saver = new DefaultConfigurationSerializer();

		saver.setIndent(true);
		try {
			saver.serialize(writer, config);
		} catch (SAXException e) {
			throw new IOException("SAX implementation problem");
		} catch (ConfigurationException e) {
			throw new IOException("Problem using Avalon Configuration tools");
		}
	}
  
    /**
     * Read sampleResult from Avalon XML file.
     * 
     * @param config Avalon configuration
     * @return sample result
     */
    // Probably no point in converting this to return a SampleEvent
    public static SampleResult getSampleResult(Configuration config) {
		SampleResult result = new SampleResult(config.getAttributeAsLong(TIME_STAMP, 0L), config.getAttributeAsLong(
				TIME, 0L));

		result.setThreadName(config.getAttribute(THREAD_NAME, "")); // $NON-NLS-1$
		result.setDataType(config.getAttribute(DATA_TYPE, ""));
		result.setResponseCode(config.getAttribute(RESPONSE_CODE, "")); // $NON-NLS-1$
		result.setResponseMessage(config.getAttribute(RESPONSE_MESSAGE, "")); // $NON-NLS-1$
		result.setSuccessful(config.getAttributeAsBoolean(SUCCESSFUL, false));
		result.setSampleLabel(config.getAttribute(LABEL, "")); // $NON-NLS-1$
		result.setResponseData(getBinaryData(config.getChild(BINARY)));
		Configuration[] subResults = config.getChildren(SAMPLE_RESULT_TAG_NAME);

		for (int i = 0; i < subResults.length; i++) {
			result.storeSubResult(getSampleResult(subResults[i]));
		}
		Configuration[] assResults = config.getChildren(ASSERTION_RESULT_TAG_NAME);

		for (int i = 0; i < assResults.length; i++) {
			result.addAssertionResult(getAssertionResult(assResults[i]));
		}

		Configuration[] samplerData = config.getChildren("property"); // $NON-NLS-1$
		for (int i = 0; i < samplerData.length; i++) {
			result.setSamplerData(samplerData[i].getValue("")); // $NON-NLS-1$
		}
		return result;
	}

	private static List getConfigsFromTree(HashTree subTree) {
		Iterator iter = subTree.list().iterator();
		List configs = new LinkedList();

		while (iter.hasNext()) {
			TestElement item = (TestElement) iter.next();
			DefaultConfiguration config = new DefaultConfiguration("node", "node"); // $NON-NLS-1$ // $NON-NLS-2$

			config.addChild(getConfigForTestElement(null, item));
			List configList = getConfigsFromTree(subTree.getTree(item));
			Iterator iter2 = configList.iterator();

			while (iter2.hasNext()) {
				config.addChild((Configuration) iter2.next());
			}
			configs.add(config);
		}
		return configs;
	}

	private static Configuration getConfiguration(byte[] bin) {
		DefaultConfiguration config = new DefaultConfiguration(BINARY, "JMeter Save Service"); // $NON-NLS-1$

		try {
			config.setValue(new String(bin, "UTF-8")); // $NON-NLS-1$
		} catch (UnsupportedEncodingException e) {
			log.error("", e); // $NON-NLS-1$
		}
		return config;
	}

	private static byte[] getBinaryData(Configuration config) {
		if (config == null) {
			return new byte[0];
		}
		try {
			return config.getValue("").getBytes("UTF-8"); // $NON-NLS-1$
		} catch (UnsupportedEncodingException e) {
			return new byte[0];
		}
	}

	private static AssertionResult getAssertionResult(Configuration config) {
		AssertionResult result = new AssertionResult(""); //TODO provide proper name?
		result.setError(config.getAttributeAsBoolean(ERROR, false));
		result.setFailure(config.getAttributeAsBoolean(FAILURE, false));
		result.setFailureMessage(config.getAttribute(FAILURE_MESSAGE, ""));
		return result;
	}

	private static Configuration getConfiguration(AssertionResult assResult) {
		DefaultConfiguration config = new DefaultConfiguration(ASSERTION_RESULT_TAG_NAME, "JMeter Save Service");

		config.setAttribute(FAILURE_MESSAGE, assResult.getFailureMessage());
		config.setAttribute(ERROR, "" + assResult.isError());
		config.setAttribute(FAILURE, "" + assResult.isFailure());
		return config;
	}

	/**
	 * This method determines the content of the result data that will be
	 * stored for the Avalon XML format.
	 * 
	 * @param result
	 *            the object containing all of the data that has been collected.
	 * @param saveConfig
	 *            the configuration giving the data items to be saved.
	 * N.B. It is rather out of date, as many fields are not saved.
	 * However it is probably not worth updating, as no-one should be using the format.
	 */
	public static Configuration getConfiguration(SampleResult result, SampleSaveConfiguration saveConfig) {
		DefaultConfiguration config = new DefaultConfiguration(SAMPLE_RESULT_TAG_NAME, "JMeter Save Service"); // $NON-NLS-1$

		if (saveConfig.saveTime()) {
			config.setAttribute(TIME, String.valueOf(result.getTime()));
		}
		if (saveConfig.saveLabel()) {
			config.setAttribute(LABEL, result.getSampleLabel());
		}
		if (saveConfig.saveCode()) {
			config.setAttribute(RESPONSE_CODE, result.getResponseCode());
		}
		if (saveConfig.saveMessage()) {
			config.setAttribute(RESPONSE_MESSAGE, result.getResponseMessage());
		}
		if (saveConfig.saveThreadName()) {
			config.setAttribute(THREAD_NAME, result.getThreadName());
		}
		if (saveConfig.saveDataType()) {
			config.setAttribute(DATA_TYPE, result.getDataType());
		}

		if (saveConfig.printMilliseconds()) {
			config.setAttribute(TIME_STAMP, String.valueOf(result.getTimeStamp()));
		} else if (saveConfig.formatter() != null) {
			String stamp = saveConfig.formatter().format(new Date(result.getTimeStamp()));

			config.setAttribute(TIME_STAMP, stamp);
		}

		if (saveConfig.saveSuccess()) {
			config.setAttribute(SUCCESSFUL, Boolean.toString(result.isSuccessful()));
		}

		SampleResult[] subResults = result.getSubResults();

		if (subResults != null) {
			for (int i = 0; i < subResults.length; i++) {
				config.addChild(getConfiguration(subResults[i], saveConfig));
			}
		}

		AssertionResult[] assResults = result.getAssertionResults();

⌨️ 快捷键说明

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