Author: sebb Date: Fri Aug 14 10:05:41 2009 New Revision: 804152 URL: http://svn.apache.org/viewvc?rev=804152&view=rev Log: Bug 47565 - [Function] FileToString
Added: jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/FileToString.java (with props) Modified: jakarta/jmeter/trunk/xdocs/changes.xml jakarta/jmeter/trunk/xdocs/usermanual/functions.xml Added: jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/FileToString.java URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/FileToString.java?rev=804152&view=auto ============================================================================== --- jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/FileToString.java (added) +++ jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/FileToString.java Fri Aug 14 10:05:41 2009 @@ -0,0 +1,127 @@ +/* + * 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.functions; + +import java.io.File; +import java.io.IOException; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; + +import org.apache.commons.io.FileUtils; +import org.apache.jmeter.engine.util.CompoundVariable; +import org.apache.jmeter.samplers.SampleResult; +import org.apache.jmeter.samplers.Sampler; +import org.apache.jmeter.threads.JMeterVariables; +import org.apache.jmeter.util.JMeterUtils; +import org.apache.jorphan.logging.LoggingManager; +import org.apache.jorphan.util.JMeterStopThreadException; +import org.apache.log.Logger; + +/** + * FileToString Function to read a complete file into a String. + * + * Parameters: + * - file name + * - variable name (optional) + * + * Returns: + * - the whole text from a file + * - or **ERR** if an error occurs + * - value is also optionally saved in the variable for later re-use. + * + */ +public class FileToString extends AbstractFunction { + private static final Logger log = LoggingManager.getLoggerForClass(); + + private static final List desc = new LinkedList(); + + private static final String KEY = "__FileToString";//$NON-NLS-1$ + + static final String ERR_IND = "**ERR**";//$NON-NLS-1$ + + static { + desc.add(JMeterUtils.getResString("string_from_file_file_name"));//$NON-NLS-1$ + desc.add(JMeterUtils.getResString("function_name_paropt"));//$NON-NLS-1$ + } + + private static final int MIN_PARAM_COUNT = 1; + + private static final int MAX_PARAM_COUNT = 2; + + private static final int PARAM_NAME = 2; + + private Object[] values; + + public FileToString() { + } + + /** {...@inheritdoc} */ + public synchronized String execute(SampleResult previousResult, Sampler currentSampler) + throws InvalidVariableException { + + String fileName = ((CompoundVariable) values[0]).execute(); + + String myName = "";//$NON-NLS-1$ + if (values.length >= PARAM_NAME) { + myName = ((CompoundVariable) values[PARAM_NAME - 1]).execute().trim(); + } + + String myValue = ERR_IND; + + try { + myValue = FileUtils.readFileToString(new File(fileName), null); + } catch (IOException e) { + log.warn("Could not read file: "+fileName+" "+e.getMessage()); + throw new JMeterStopThreadException("End of sequence"); + } + + if (myName.length() > 0) { + JMeterVariables vars = getVariables(); + if (vars != null) {// Can be null if called from Config item testEnded() method + vars.put(myName, myValue); + } + } + + if (log.isDebugEnabled()) { + String tn = Thread.currentThread().getName(); + log.debug(tn + " name:" //$NON-NLS-1$ + + myName + " value:" + myValue);//$NON-NLS-1$ + } + + return myValue; + } + + + /** {...@inheritdoc} */ + public synchronized void setParameters(Collection parameters) throws InvalidVariableException { + checkParameterCount(parameters, MIN_PARAM_COUNT, MAX_PARAM_COUNT); + values = parameters.toArray(); + } + + /** {...@inheritdoc} */ + public String getReferenceKey() { + return KEY; + } + + /** {...@inheritdoc} */ + public List getArgumentDesc() { + return desc; + } +} \ No newline at end of file Propchange: jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/FileToString.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/FileToString.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Modified: jakarta/jmeter/trunk/xdocs/changes.xml URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/xdocs/changes.xml?rev=804152&r1=804151&r2=804152&view=diff ============================================================================== --- jakarta/jmeter/trunk/xdocs/changes.xml (original) +++ jakarta/jmeter/trunk/xdocs/changes.xml Fri Aug 14 10:05:41 2009 @@ -131,6 +131,7 @@ <h3>Functions</h3> <ul> +<li>Bug 47565 - [Function] FileToString</li> </ul> <h3>I18N</h3> Modified: jakarta/jmeter/trunk/xdocs/usermanual/functions.xml URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/xdocs/usermanual/functions.xml?rev=804152&r1=804151&r2=804152&view=diff ============================================================================== --- jakarta/jmeter/trunk/xdocs/usermanual/functions.xml (original) +++ jakarta/jmeter/trunk/xdocs/usermanual/functions.xml Fri Aug 14 10:05:41 2009 @@ -93,6 +93,7 @@ <tr><td>Information</td><td> <a href="#__log">log</a></td><td>log (or display) a message (and return the value)</td></tr> <tr><td>Information</td><td> <a href="#__logn">logn</a></td><td>log (or display) a message (empty return value)</td></tr> <tr><td>Input</td><td> <a href="#__StringFromFile">StringFromFile</a></td><td>read a line from a file</td></tr> + <tr><td>Input</td><td> <a href="#__FileToString">FileToString</a></td><td>read an entire file</td></tr> <tr><td>Input</td><td> <a href="#__CSVRead">CSVRead</a></td><td>read from CSV delimited file</td></tr> <tr><td>Input</td><td> <a href="#__XPath">XPath</a></td><td>Use an XPath expression to read from a file</td></tr> <tr><td>Calculation</td><td> <a href="#__counter">counter</a></td><td>generate an incrementing number</td></tr> @@ -1092,6 +1093,28 @@ </subsection> +<component index="§-num;.5.26" name="__FileToString"> + +<description> + <p> + The FileToString function can be used to read an entire file. + Each time it is called it reads the entire file. + </p> + <p>If an error occurs opening or reading the file, then the function returns the string "**ERR**"</p> +</description> + +<properties> + <property name="File Name" required="Yes">Path to the file name. + (The path can be relative to the JMeter launch directory) + </property> + <property name="Variable Name" required="No"> +A reference name - refName - for reusing the value created by this function. Stored values are of the form ${refName}. + </property> +</properties> +<p>The file name parameter and reference name parameter (if supplied) are resolved every time the function is executed.</p> +</component> + + <subsection name="§-num;.6 Pre-defined Variables" anchor="predefinedvars"> <p> Most variables are set by calling functions or by test elements such as User Defined Variables; --------------------------------------------------------------------- To unsubscribe, e-mail: jmeter-dev-unsubscr...@jakarta.apache.org For additional commands, e-mail: jmeter-dev-h...@jakarta.apache.org