Java Code Examples for org.apache.jorphan.util.JOrphanUtils#split()

The following examples show how to use org.apache.jorphan.util.JOrphanUtils#split() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedisDataSet.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Override
public void iterationStart(LoopIterationEvent event) {
    Jedis connection = null;
    try {
        connection = pool.getResource();

        // Get data from list's head
        String line = getDataFromConnection(connection, redisKey);

        if(line == null) { // i.e. no more data (nil)
            throw new JMeterStopThreadException("End of redis data detected");
        }

        if (getRecycleDataOnUse()) {
            addDataToConnection(connection, redisKey, line);
        }

        final String names = variableNames;
        if (vars == null) {
            vars = JOrphanUtils.split(names, ","); 
        }
        
        final JMeterContext context = getThreadContext();
        JMeterVariables threadVars = context.getVariables();
        String[] values = JOrphanUtils.split(line, delimiter, false);
        for (int a = 0; a < vars.length && a < values.length; a++) {
            threadVars.put(vars[a], values[a]);
        }
        
    } finally {
        pool.returnResource(connection);
    }
}
 
Example 2
Source File: RandomCSVDataSetConfig.java    From jmeter-bzm-plugins with Apache License 2.0 4 votes vote down vote up
public String[] getDestinationVariableKeys() {
    String vars = getVariableNames();
    return hasVariablesNames() ?
            JOrphanUtils.split(vars, ",") :
            getReader().getHeader();
}
 
Example 3
Source File: VariableFromCsvFileReader.java    From jmeter-plugins with Apache License 2.0 4 votes vote down vote up
/**
    * Parses (name, value) pairs from the input and returns the result as a Map, with the option to skip the first line.
    * The name is taken from the first column and value from the second column. If an input line contains only one
    * column its value is defaulted to an empty string. Any extra columns are ignored.
    *
    * If the input contains headers, call with skipLines equal to the number of lines of headers. A negative value for
    * skipLines yields the same result as 0.
    *
    * @param prefix a prefix to apply to the mapped variable names
    * @param separator the field delimiter
    * @param skipLines the number of lines at the beginning of the input to skip
    * @return a map of (name, value) pairs
    */
   public Map<String, String> getDataAsMap(String prefix, String separator, int skipLines) {
if (separator.isEmpty()) {
    throw new IllegalArgumentException("CSV separator cannot be empty");
}

Map<String, String> variables = new HashMap<>();
if (input != null) {
    try {
	String line;
	int lineNum = 0;
	StringBuilder multiLineValue = new StringBuilder();
	String multiLineVariable = null;
	while ((line = input.readLine()) != null) {
	    if (++lineNum > skipLines) {
		if (line.startsWith("#")) {
		    continue;
		}
		String[] lineValues = JOrphanUtils.split(line, separator, false);

		if (lineValues.length == 1) {
		    if (multiLineValue.length() > 0 && lineValues[0].endsWith("\"")) {
			multiLineValue.append(lineValues[0].substring(0, lineValues[0].length() - 1));
			variables.put(prefix + multiLineVariable, multiLineValue.toString());
			// reset memory
			multiLineValue.setLength(0);
			multiLineVariable = null;
		    } else if (multiLineValue.length() > 0) {
			multiLineValue.append(lineValues[0]).append('\n');
		    } else {
			log.warn("Less than 2 columns at line: " + line);
			variables.put(prefix + lineValues[0], "");
		    }
		} else if (lineValues.length >= 2) {
		    if (lineValues[1].startsWith("\"")) {
			// multi line value
			multiLineVariable = lineValues[0];
			multiLineValue.append(lineValues[1].substring(1)).append('\n');
		    } else {
			variables.put(prefix + lineValues[0], lineValues[1]);
		    }
		}
	    }
	}
    } catch (IOException ex) {
	log.error("Error while reading: " + ex.getMessage());
    }
}
return variables;
   }