mnaufaldillah

XPath Jmeter

Oct 23rd, 2021
605
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to you under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  * http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17.  
  18. package org.apache.jmeter.functions;
  19.  
  20.  
  21. import java.util.ArrayList;
  22. import java.util.Collection;
  23. import java.util.List;
  24.  
  25. import org.apache.jmeter.engine.util.CompoundVariable;
  26. import org.apache.jmeter.samplers.SampleResult;
  27. import org.apache.jmeter.samplers.Sampler;
  28. import org.apache.jmeter.util.JMeterUtils;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31.  
  32. // @see org.apache.jmeter.functions.PackageTest for unit tests
  33.  
  34. /**
  35.  * The function represented by this class allows data to be read from XML files.
  36.  * <p>
  37.  * Syntax is similar to the CVSRead function. The function allows the test to
  38.  * line-thru the nodes in the XML file - one node per each test, e.g. inserting
  39.  * the following in the test scripts:
  40.  * <pre><code>
  41.  * ${_XPath(c:/BOF/abcd.xml,/xpath/)} // match the (first) node
  42.  * ${_XPath(c:/BOF/abcd.xml,/xpath/)} // Go to next match of '/xpath/' expression
  43.  * </code></pre>
  44.  * NOTE: A single instance of each different file/expression combination
  45.  * is opened and used for all threads.
  46.  * @since 2.0.3
  47.  */
  48. public class XPath extends AbstractFunction {
  49.     private static final Logger log = LoggerFactory.getLogger(XPath.class);
  50.  
  51.     private static final String KEY = "__XPath"; // Function name //$NON-NLS-1$
  52.  
  53.     private static final List<String> desc = new ArrayList<>();
  54.  
  55.     private Object[] values; // Parameter list
  56.  
  57.     static {
  58.         desc.add(JMeterUtils.getResString("xpath_file_file_name")); //$NON-NLS-1$
  59.         desc.add(JMeterUtils.getResString("xpath_expression")); //$NON-NLS-1$
  60.     }
  61.  
  62.     public XPath() {
  63.     }
  64.  
  65.     /** {@inheritDoc} */
  66.     @Override
  67.     public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
  68.             throws InvalidVariableException {
  69.         String fileName = ((CompoundVariable) values[0]).execute();
  70.         String xpathString = ((CompoundVariable) values[1]).execute();
  71.  
  72.         log.debug("execute ({} {})", fileName, xpathString);
  73.  
  74.         String myValue = XPathWrapper.getXPathString(fileName, xpathString);
  75.  
  76.         log.debug("execute value: {}", myValue);
  77.  
  78.         return myValue;
  79.     }
  80.  
  81.     /** {@inheritDoc} */
  82.     @Override
  83.     public List<String> getArgumentDesc() {
  84.         return desc;
  85.     }
  86.  
  87.     /** {@inheritDoc} */
  88.     @Override
  89.     public String getReferenceKey() {
  90.         return KEY;
  91.     }
  92.  
  93.     /** {@inheritDoc} */
  94.     @Override
  95.     public synchronized void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
  96.         if (log.isDebugEnabled()) {
  97.             log.debug("setParameter - Collection.size={}", Integer.toString(parameters.size()));
  98.         }
  99.  
  100.         values = parameters.toArray();
  101.  
  102.         if (log.isDebugEnabled()) {
  103.             for (int i = 0; i < parameters.size(); i++) {
  104.                 log.debug("i:{}", ((CompoundVariable) values[i]).execute());
  105.             }
  106.         }
  107.  
  108.         checkParameterCount(parameters, 2);
  109.  
  110.         /*
  111.          * Need to reset the containers for repeated runs; about the only way
  112.          * for functions to detect that a run is starting seems to be the
  113.          * setParameters() call.
  114.          */
  115.         XPathWrapper.clearAll();// TODO only clear the relevant entry - if possible...
  116.  
  117.     }
  118. }
RAW Paste Data