Package org.apache.jmeter.functions

# Class StringFromFile

java.lang.Object
org.apache.jmeter.functions.AbstractFunction
org.apache.jmeter.functions.StringFromFile

All Implemented Interfaces:
  `Function`, `TestStateListener`

@AutoService(Function.class) public class StringFromFile extends AbstractFunction implements TestStateListener

StringFromFile Function to read a String from a text file.

Parameters:

- file name
- variable name (optional - defaults to `StringFromFile_`)
- sequence start
- sequence end

Returns:

- the next line from the file
- or `**ERR**` if an error occurs
- value is also saved in the variable for later re-use.

Ensure that different variable names are used for each call to the function

Notes:

- JMeter instantiates a single copy of each function for every reference in the test plan
- Function instances are shared between threads.
- Each StringFromFile instance reads the file independently. The output variable can be used to save the value for later use in the same thread.
- The file name is resolved at file (re-)open time; the file is initially opened on first execution (which could be any thread)
- the output variable name is resolved every time the function is invoked

Because function instances are shared, it does not make sense to use the thread number as part of the file name.

Since:
  1.9

## Constructor Summary

Constructors
Constructor
Description
`StringFromFile()`

## Method Summary

Modifier and Type
Method
Description
`String`
`execute(SampleResult previousResult, Sampler currentSampler)`
N.B.
`List<String>`
`getArgumentDesc()`
Return a list of strings briefly describing each parameter your function takes.
`String`
`getReferenceKey()`
Return the name of your function.
`void`
`setParameters(Collection<CompoundVariable> parameters)`
Note: This is always called even if no parameters are provided (versions of JMeter after 2.3.1)
`void`
`testEnded()`
Called once for all threads after the end of a test.
`void`
`testEnded(String host)`
Called once for all threads after the end of a test.
`void`
`testStarted()`
Called just before the start of the test from the main engine thread.
`void`
`testStarted(String host)`
Called just before the start of the test from the main engine thread.

### Methods inherited from class org.apache.jmeter.functions.AbstractFunction

`addVariableValue, checkMinParameterCount, checkParameterCount, checkParameterCount, execute, getVariables`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### StringFromFile

public StringFromFile()

## Method Details

### execute

public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException

N.B. execute() should be synchronized if function is operating with non-thread-safe objects (e.g. operates with files).

JMeter ensures setParameters() happens-before execute(): setParameters is executed in main thread, and worker threads are started after that.

Specified by:
  `execute` in interface `Function`
Specified by:
  `execute` in class `AbstractFunction`
Parameters:
  `previousResult` - The previous `SampleResult`
  `currentSampler` - The current `Sampler`
Returns:
  The replacement value, which was generated by the function
Throws:
  `InvalidVariableException` - - when the variables for the function call can't be evaluated
See Also:
  - `Function.execute(SampleResult, Sampler)`

### setParameters

public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException
Note: This is always called even if no parameters are provided (versions of JMeter after 2.3.1)

Specified by:
  `setParameters` in interface `Function`
Specified by:
  `setParameters` in class `AbstractFunction`
Parameters:
  `parameters` - The parameters for the function call
Throws:
  `InvalidVariableException` - - when the variables for the function call can't be evaluated
See Also:
  - `Function.setParameters(Collection)`

### getReferenceKey

public String getReferenceKey()
Return the name of your function. Convention is to prepend "__" to the name (ie "__regexFunction")

Specified by:
  `getReferenceKey` in interface `Function`
Specified by:
  `getReferenceKey` in class `AbstractFunction`
Returns:
  The name of the function
See Also:
  - `Function.getReferenceKey()`

### getArgumentDesc

public List<String> getArgumentDesc()
Return a list of strings briefly describing each parameter your function takes. Please use JMeterUtils.getResString(resource_name) to grab a resource string. Otherwise, your help text will be difficult to internationalize. This list is not optional. If you don't wish to write help, you must at least return a List containing the correct number of blank strings, one for each argument.

Specified by:
  `getArgumentDesc` in interface `Function`
Returns:
  List with brief descriptions for each parameter the function takes

### testStarted

public void testStarted()

Called just before the start of the test from the main engine thread. This is before the test elements are cloned. Note that not all the test variables will have been set up at this point.

N.B. testStarted() and testEnded() are called from different threads.

Specified by:
  `testStarted` in interface `TestStateListener`
See Also:
  - `StandardJMeterEngine.run()`

### testStarted

public void testStarted(String host)

Called just before the start of the test from the main engine thread. This is before the test elements are cloned. Note that not all the test variables will have been set up at this point.

N.B. testStarted() and testEnded() are called from different threads.

Specified by:
  `testStarted` in interface `TestStateListener`
Parameters:
  `host` - name of host
See Also:
  - `StandardJMeterEngine.run()`

### testEnded

public void testEnded()

Called once for all threads after the end of a test. This will use the same element instances as at the start of the test.

N.B. testStarted() and testEnded() are called from different threads.

Specified by:
  `testEnded` in interface `TestStateListener`
See Also:
  - `StandardJMeterEngine.stopTest()`

### testEnded

public void testEnded(String host)

Called once for all threads after the end of a test. This will use the same element instances as at the start of the test.

N.B. testStarted() and testEnded() are called from different threads.

Specified by:
  `testEnded` in interface `TestStateListener`
Parameters:
  `host` - name of host
See Also:
  - `StandardJMeterEngine.stopTest()`