Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.functions » [javadoc | source]
    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   
   19   package org.apache.jmeter.functions;
   20   
   21   
   22   import java.util.Collection;
   23   import java.util.LinkedList;
   24   import java.util.List;
   25   
   26   import org.apache.jmeter.engine.util.CompoundVariable;
   27   import org.apache.jmeter.samplers.SampleResult;
   28   import org.apache.jmeter.samplers.Sampler;
   29   import org.apache.jmeter.util.JMeterUtils;
   30   import org.apache.jorphan.logging.LoggingManager;
   31   import org.apache.log.Logger;
   32   
   33   // @see org.apache.jmeter.functions.PackageTest for unit tests
   34   
   35   /**
   36    * The function represented by this class allows data to be read from XML files.
   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    *
   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    *
   44    * NOTE: A single instance of each different file/expression combination
   45    * is opened and used for all threads.
   46    *
   47    */
   48   public class XPath extends AbstractFunction {
   49       private static final Logger log = LoggingManager.getLoggerForClass();
   50   
   51       // static {
   52       // LoggingManager.setPriority("DEBUG","jmeter");
   53       // LoggingManager.setTarget(new java.io.PrintWriter(System.out));
   54       // }
   55       private static final String KEY = "__XPath"; // Function name //$NON-NLS-1$
   56   
   57       private static final List desc = new LinkedList();
   58   
   59       private Object[] values; // Parameter list
   60   
   61       static {
   62           desc.add(JMeterUtils.getResString("xpath_file_file_name")); //$NON-NLS-1$
   63           desc.add(JMeterUtils.getResString("xpath_expression")); //$NON-NLS-1$
   64       }
   65   
   66       public XPath() {
   67       }
   68   
   69       /**
   70        * @see org.apache.jmeter.functions.Function#execute(SampleResult, Sampler)
   71        */
   72       public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
   73               throws InvalidVariableException {
   74           String myValue = ""; //$NON-NLS-1$
   75   
   76           String fileName = ((org.apache.jmeter.engine.util.CompoundVariable) values[0]).execute();
   77           String xpathString = ((org.apache.jmeter.engine.util.CompoundVariable) values[1]).execute();
   78   
   79           if (log.isDebugEnabled()){
   80               log.debug("execute (" + fileName + " " + xpathString + ")   ");
   81           }
   82   
   83           myValue = XPathWrapper.getXPathString(fileName, xpathString);
   84   
   85           if (log.isDebugEnabled()){
   86               log.debug("execute value: " + myValue);
   87           }
   88   
   89           return myValue;
   90       }
   91   
   92       /**
   93        * @see org.apache.jmeter.functions.Function#getArgumentDesc()
   94        */
   95       public List getArgumentDesc() {
   96           return desc;
   97       }
   98   
   99       /**
  100        * @see org.apache.jmeter.functions.Function#getReferenceKey()
  101        */
  102       public String getReferenceKey() {
  103           return KEY;
  104       }
  105   
  106       /**
  107        * @see org.apache.jmeter.functions.Function#setParameters(Collection)
  108        */
  109       public synchronized void setParameters(Collection parameters) throws InvalidVariableException {
  110           log.debug("setParameter - Collection.size=" + parameters.size());
  111   
  112           values = parameters.toArray();
  113   
  114           if (log.isDebugEnabled()) {
  115               for (int i = 0; i < parameters.size(); i++) {
  116                   log.debug("i:" + ((CompoundVariable) values[i]).execute());
  117               }
  118           }
  119   
  120           checkParameterCount(parameters, 2);
  121   
  122           /*
  123            * Need to reset the containers for repeated runs; about the only way
  124            * for functions to detect that a run is starting seems to be the
  125            * setParameters() call.
  126            */
  127           XPathWrapper.clearAll();// TODO only clear the relevant entry - if possible...
  128   
  129       }
  130   }

Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.functions » [javadoc | source]