<commit_before>/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed 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.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
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 java.util.StringTokenizer;
import java.util.Vector;

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.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.threads.JMeterContextService;
import org.apache.jmeter.util.NameUpdater;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.ListedHashTree;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.jorphan.util.JOrphanUtils;
import org.apache.log.Logger;
import org.xml.sax.SAXException;

/**
 * This class provides a means for saving test results. Test results are
 * typically saved in an XML file, but other storage mechanisms may also be
 * used, for instance, CSV files or databases.
 * 
 * @version $Revision$ $Date$
 */
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$

    // ---------------------------------------------------------------------
    // ADDITIONAL CSV RESULT FILE CONSTANTS AND FIELD NAME CONSTANTS
    // ---------------------------------------------------------------------

    private static final String CSV_TIME = "elapsed"; // $NON-NLS-1$
    private static final String CSV_BYTES= "bytes"; // $NON-NLS-1$
    private static final String CSV_THREAD_COUNT1 = "grpThreads"; // $NON-NLS-1$
    private static final String CSV_THREAD_COUNT2 = "allThreads"; // $NON-NLS-1$
    private static final String CSV_URL = "URL"; // $NON-NLS-1$
    private static final String CSV_FILENAME = "Filename"; // $NON-NLS-1$
    
    // Initial config from properties
	static private final SampleSaveConfiguration _saveConfig = SampleSaveConfiguration.staticConfig();

	private static DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();

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

    //////////////////////////////////////////////////////////////////////////////
    //                  Start of CSV methods
    
    // TODO - move to separate file? If so, remember that some of the
    
	/**
	 * Make a SampleResult given a delimited string.
	 * 
	 * @param delim
	 * @return SampleResult
	 */
	public static SampleResult makeResultFromDelimitedString(String delim) {
		SampleResult result = null;
		long timeStamp = 0;
		long elapsed = 0;
		StringTokenizer splitter = new StringTokenizer(delim, _saveConfig.getDelimiter());
		String text = null;

		try {
			if (_saveConfig.printMilliseconds()) {
				text = splitter.nextToken();
				timeStamp = Long.parseLong(text);
			} else if (_saveConfig.formatter() != null) {
				text = splitter.nextToken();
				Date stamp = _saveConfig.formatter().parse(text);
				timeStamp = stamp.getTime();
			}

			if (_saveConfig.saveTime()) {
				text = splitter.nextToken();
				elapsed = Long.parseLong(text);
			}

			result = new SampleResult(timeStamp, elapsed);

			if (_saveConfig.saveLabel()) {
				text = splitter.nextToken();
				result.setSampleLabel(text);
			}
			if (_saveConfig.saveCode()) {
				text = splitter.nextToken();
				result.setResponseCode(text);
			}

			if (_saveConfig.saveMessage()) {
				text = splitter.nextToken();
				result.setResponseMessage(text);
			}

			if (_saveConfig.saveThreadName()) {
				text = splitter.nextToken();
				result.setThreadName(text);
			}

			if (_saveConfig.saveDataType()) {
				text = splitter.nextToken();
				result.setDataType(text);
			}

			if (_saveConfig.saveSuccess()) {
				text = splitter.nextToken();
				result.setSuccessful(Boolean.valueOf(text).booleanValue());
			}

			if (_saveConfig.saveAssertionResultsFailureMessage()) {
				text = splitter.nextToken();
			}
		} catch (NumberFormatException e) {<commit_msg>Allow for CSV header, and additional new fields<commit_after>/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed 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.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
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 java.util.StringTokenizer;
import java.util.Vector;

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.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.threads.JMeterContextService;
import org.apache.jmeter.util.NameUpdater;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.ListedHashTree;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.jorphan.util.JOrphanUtils;
import org.apache.log.Logger;
import org.xml.sax.SAXException;

/**
 * This class provides a means for saving test results. Test results are
 * typically saved in an XML file, but other storage mechanisms may also be
 * used, for instance, CSV files or databases.
 * 
 * @version $Revision$ $Date$
 */
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$

    // ---------------------------------------------------------------------
    // ADDITIONAL CSV RESULT FILE CONSTANTS AND FIELD NAME CONSTANTS
    // ---------------------------------------------------------------------

    private static final String CSV_TIME = "elapsed"; // $NON-NLS-1$
    private static final String CSV_BYTES= "bytes"; // $NON-NLS-1$
    private static final String CSV_THREAD_COUNT1 = "grpThreads"; // $NON-NLS-1$
    private static final String CSV_THREAD_COUNT2 = "allThreads"; // $NON-NLS-1$
    private static final String CSV_URL = "URL"; // $NON-NLS-1$
    private static final String CSV_FILENAME = "Filename"; // $NON-NLS-1$
    
    // Initial config from properties
	static private final SampleSaveConfiguration _saveConfig = SampleSaveConfiguration.staticConfig();

	private static DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();

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

    //////////////////////////////////////////////////////////////////////////////
    //                  Start of CSV methods
    
    // TODO - move to separate file? If so, remember that some of the
    
	/**
	 * Make a SampleResult given a delimited string.
     * Note that this assumes that the fields are present as per jmeter.properties.
	 * 
	 * @param inputLine
	 * @return SampleResult
	 */
	public static SampleResult makeResultFromDelimitedString(String inputLine) {
        return makeResultFromDelimitedString(inputLine, _saveConfig);
    }
    
    /**
     * Make a SampleResult given a delimited string.
     * 
     * @param inputLine - line from CSV file
     * @param config - configuration
     * @return SampleResult or null if header line detected
     * 
     * @throws NumberFormatException, ParseException
     */
    public static SampleResult makeResultFromDelimitedString(String inputLine, SampleSaveConfiguration saveConfig) {
        // Check for a header line
        if (inputLine.startsWith(TIME_STAMP) || inputLine.startsWith(CSV_TIME)){
            return null;
        }
		SampleResult result = null;
		long timeStamp = 0;
		long elapsed = 0;
		StringTokenizer splitter = new StringTokenizer(inputLine, _saveConfig.getDelimiter());
		String text = null;

		try {
			if (saveConfig.printMilliseconds()) {
				text = splitter.nextToken();
				timeStamp = Long.parseLong(text);
			} else if (saveConfig.formatter() != null) {
				text = splitter.nextToken();
				Date stamp = saveConfig.formatter().parse(text);
				timeStamp = stamp.getTime();
			}

			if (saveConfig.saveTime()) {
				text = splitter.nextToken();
				elapsed = Long.parseLong(text);
			}

			result = new SampleResult(timeStamp, elapsed);

			if (saveConfig.saveLabel()) {
				text = splitter.nextToken();
				result.setSampleLabel(text);
			}
			if (saveConfig.saveCode()) {
				text = splitter.nextToken();
				result.setResponseCode(text);
			}

			if (saveConfig.saveMessage()) {
				text = splitter.nextToken();
				result.setResponseMessage(text);
			}

			if (saveConfig.saveThreadName()) {
				text = splitter.nextToken();
				result.setThreadName(text);
			}

			if (saveConfig.saveDataType()) {
				text = splitter.nextToken();
				result.setDataType(text);
			}

			if (saveConfig.saveSuccess()) {
				text = splitter.nextToken();
				result.setSuccessful(Boolean.valueOf(text).booleanValue());
			}

			if (saveConfig.saveAssertionResultsFailureMessage()) {
				text = splitter.nextToken();
                // TODO - should this be restored?
			}
            
            if (saveConfig.saveBytes()) {
                text = splitter.nextToken();
                result.setBytes(Integer.parseInt(text));
            }
        
            if (saveConfig.saveThreadCounts()) {
                text = splitter.nextToken();
                // Not saved, as not part of a result
            }

            if (saveConfig.saveUrl()) {
                text = splitter.nextToken();
                // TODO: should this be restored?
            }
        
            if (saveConfig.saveFileName()) {
                text = splitter.nextToken();
                result.setResultFileName(text);
            }            
            
		} catch (NumberFormatException e) {
{'fraction_non_alphanumeric': 0.14743297018722681, 'fraction_numerical': 0.004957922891251875, 'mean_word_length': 2.8770864946889225, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7053864', 'n_tokens_mistral': 4559, 'n_tokens_neox': 4186, 'n_words': 1128}