Search in sources :

Example 6 with TestStateListener

use of org.apache.jmeter.testelement.TestStateListener in project jmeter by apache.

the class FunctionParser method makeFunction.

/**
     * Compile a string into a function or SimpleVariable.
     *
     * Called by {@link #compileString(String)} when that has detected "${".
     *
     * Calls {@link CompoundVariable#getNamedFunction(String)} if it detects:
     * '(' - start of parameter list
     * '}' - end of function call
     *
     * @param reader points to input after the "${"
     * @return the function or variable object (or a String)
     * @throws InvalidVariableException when evaluation of variables fail
     */
Object makeFunction(StringReader reader) throws InvalidVariableException {
    char[] current = new char[1];
    // TODO - why use space?
    char previous = ' ';
    StringBuilder buffer = new StringBuilder();
    Object function;
    try {
        while (reader.read(current) == 1) {
            if (current[0] == '\\') {
                if (reader.read(current) == 0) {
                    break;
                }
                previous = ' ';
                buffer.append(current[0]);
            } else if (current[0] == '(' && previous != ' ') {
                String funcName = buffer.toString();
                function = CompoundVariable.getNamedFunction(funcName);
                if (function instanceof Function) {
                    ((Function) function).setParameters(parseParams(reader));
                    if (reader.read(current) == 0 || current[0] != '}') {
                        // set to start of string
                        reader.reset();
                        char[] cb = new char[100];
                        int nbRead = reader.read(cb);
                        throw new InvalidVariableException("Expected } after " + funcName + " function call in " + new String(cb, 0, nbRead));
                    }
                    if (function instanceof TestStateListener) {
                        StandardJMeterEngine.register((TestStateListener) function);
                    }
                    return function;
                } else {
                    // Function does not exist, so treat as per missing variable
                    buffer.append(current[0]);
                }
            } else if (current[0] == '}') {
                // variable, or function with no parameter list
                function = CompoundVariable.getNamedFunction(buffer.toString());
                if (function instanceof Function) {
                    // ensure that setParameters() is called.
                    ((Function) function).setParameters(new LinkedList<CompoundVariable>());
                }
                buffer.setLength(0);
                return function;
            } else {
                buffer.append(current[0]);
                previous = current[0];
            }
        }
    } catch (IOException e) {
        log.error("Error parsing function: {}", buffer, e);
        return null;
    }
    log.warn("Probably an invalid function string: {}", buffer);
    return buffer.toString();
}
Also used : Function(org.apache.jmeter.functions.Function) InvalidVariableException(org.apache.jmeter.functions.InvalidVariableException) TestStateListener(org.apache.jmeter.testelement.TestStateListener) IOException(java.io.IOException)

Aggregations

TestStateListener (org.apache.jmeter.testelement.TestStateListener)6 ConfigTestElement (org.apache.jmeter.config.ConfigTestElement)2 JMeterTreeModel (org.apache.jmeter.gui.tree.JMeterTreeModel)2 JMeterTreeNode (org.apache.jmeter.gui.tree.JMeterTreeNode)2 TestElement (org.apache.jmeter.testelement.TestElement)2 AbstractThreadGroup (org.apache.jmeter.threads.AbstractThreadGroup)2 IOException (java.io.IOException)1 RemoteException (java.rmi.RemoteException)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 Function (org.apache.jmeter.functions.Function)1 InvalidVariableException (org.apache.jmeter.functions.InvalidVariableException)1 RemoteListenerWrapper (org.apache.jmeter.samplers.RemoteListenerWrapper)1 RemoteSampleListener (org.apache.jmeter.samplers.RemoteSampleListener)1 RemoteSampleListenerImpl (org.apache.jmeter.samplers.RemoteSampleListenerImpl)1 RemoteSampleListenerWrapper (org.apache.jmeter.samplers.RemoteSampleListenerWrapper)1 RemoteTestListenerWrapper (org.apache.jmeter.samplers.RemoteTestListenerWrapper)1 Remoteable (org.apache.jmeter.samplers.Remoteable)1 SampleListener (org.apache.jmeter.samplers.SampleListener)1 ThreadListener (org.apache.jmeter.testelement.ThreadListener)1