Class MethodCallContext


  • public class MethodCallContext
    extends Object
    Contains information and logic needed by MethodCallBuilder instances to create MethodCalls for a given JMeter test plan subtree.

    One MethodCallContext is created for each JMeter test element in a test plan tree, and they are linked in tree structure (through getParent() method to provide entire structure to MethodCallBuilder instances when building a MethodCall

    Since:
    0.45
    • Constructor Detail

      • MethodCallContext

        public MethodCallContext​(org.apache.jmeter.testelement.TestElement testElement,
                                 org.apache.jorphan.collections.HashTree childrenTree,
                                 MethodCallContext parent,
                                 MethodCallBuilderRegistry builderRegistry)
    • Method Detail

      • getTestElement

        public org.apache.jmeter.testelement.TestElement getTestElement()
        Gets the JMeter test element associated to this context.
        Returns:
        the test element.
      • getParent

        public MethodCallContext getParent()
        Gets the parent context.

        This is useful in some scenarios to register end listeners on parent node, or access root context for globally shared entries (check setEntry(Object, Object)).

        Returns:
        the parent context. Null is returned if the current context is the root context.
      • getRoot

        public MethodCallContext getRoot()
        Gets the root context associated to the test plan.

        This is useful when some data has to only be processed once and at root of the test plan build context.

        Returns:
        the parent context. Null is returned if the current context is the root context.
      • getChildrenTree

        public org.apache.jorphan.collections.HashTree getChildrenTree()
        Gets the JMeter test plan subtree of children elements of current context test element.

        This is useful when some alteration or inspection is required in the tree before other builder methods try to convert contained test elements.

        Eg: DslHttpSampler.CodeBuilder uses this method to remove children HTTP Headers which are directly included as chained methods of httpSampler method invocation.

        Returns:
        the JMeter test plan children subtree.
      • getEntry

        public Object getEntry​(Object key)
        Gets a value associated to a given key in the context.

        The context allows you to store (through setEntry(Object, Object)) any sort of information on it that may be required later on be used by the builder in some other test element context (for example: check if a test element already was processed by this builder).

        Parameters:
        key - is an object identifying an entry in the context. A simple way of sharing info for a MethodCallBuilder is just use the MethodCallBuilder class as key, storing some custom class instance with structured context info for the particular MethodCallBuilder.
        Returns:
        the value associated to the key. Null is returned if no entry is associated to the key.
      • setEntry

        public void setEntry​(Object key,
                             Object value)
        Allows to store a value associated to a given key in the context.
        Parameters:
        key - identifies the entry in context to later on be able to retrieve it.
        value - the value to store in the context, associated to the given key.
        See Also:
        for more details
      • computeEntryIfAbsent

        public <V> V computeEntryIfAbsent​(Object key,
                                          Supplier<V> computation)
        Gets existing entry or creates a new one using provided computation function.
        Parameters:
        key - identifies the entry in context to later on be able to retrieve it.
        computation - function used to build the new entry for the given key, if none exists.
        Since:
        1.3
        See Also:
        for more details
      • addEndListener

        public void addEndListener​(MethodCallContext.MethodCallContextEndListener listener)
        Allows registering logic that needs to be executed at the end of MethodCall build for this context.

        This allows to do some advance stuff, like registering some action/logic to be executed on a parent context after processing current context and only under some specific condition (eg: when no other sibling test element is included in parent context).

        Parameters:
        listener - specifies the listener containing the logic to be executed at the end of MethodCall build.
      • buildMethodCall

        public MethodCall buildMethodCall()
        Builds a MethodCall for the current context.

        This might be useful in some MethodCallBuilders to trigger a build of children context (after removal for example).

        Returns:
        the MethodCall instance.
      • child

        public MethodCallContext child​(org.apache.jmeter.testelement.TestElement element,
                                       org.apache.jorphan.collections.HashTree childrenTree)
        Allows creating a child context for the given test element and tree.
        Parameters:
        element - the test element associated to the child context.
        childrenTree - the test element subtree.
        Returns:
        the created child method context.
        Since:
        1.3
      • prependChild

        public MethodCallContext prependChild​(org.apache.jmeter.testelement.TestElement testElement,
                                              org.apache.jorphan.collections.HashTree childrenTree)
        Adds a child method call context, as first child call, built using provided test element and tree.

        This is useful when it is needed to modify existing test plan, for example, to optimize it and use default config elements that avoid code duplication in children elements.

        Parameters:
        testElement - specifies the test element from which to create the new method call.
        childrenTree - specifies children elements of the test element, which are also going to be built and attached as children method calls of the method call created for the test element.
        Returns:
        the created child method call context.
        Since:
        1.8
      • removeChild

        public MethodCallContext removeChild​(Predicate<org.apache.jmeter.testelement.TestElement> filter)
        Allows removing an instance of the given test element class from the children tree.

        If multiple instances exists, then only the first one is removed.

        Parameters:
        filter - specifies condition to be matched by test element to be removed.
        Returns:
        the context associated to the removed test element, or null if no test element is found.
      • findBuilder

        public <T extends MethodCallBuilder> T findBuilder​(Class<T> builderClass)
        Finds the builder associated to the given JMeter test element class.
        Type Parameters:
        T - is the type of the builder to find. This provides proper type safety when using the method.
        Parameters:
        builderClass - is the class of the builder to find.
        Returns:
        the builder associated to the given JMeter test element class, or null if none is found.
      • getMethodCall

        public MethodCall getMethodCall()
        The method call created for this context.

        This is useful mainly when method calls need to be modified after their creation, for example in an end listener (addEndListener(MethodCallContextEndListener)).

        Returns:
        the created method call, if it has been already created, null otherwise.
        Since:
        1.8
      • replaceMethodCall

        public void replaceMethodCall​(org.apache.jmeter.testelement.TestElement element,
                                      UnaryOperator<MethodCall> operator)
        Allows replacing or transforming the method call associated to a given test element.

        This is particularly helpful in scenarios like module controller, where basic conversion of a controller has to be replaced by a call to a test fragment containing the target controller pointed by the module controller

        Parameters:
        element - is the test element associated to the method call to be replaced/transformed.
        operator - provides the logic to be applied to create a new method call from the original test element method call.
        Since:
        1.3
      • getBuilderOption

        public Object getBuilderOption​(String optionName)