Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.engine.util » [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.engine.util;
   20   
   21   import java.util.Collection;
   22   import java.util.HashMap;
   23   import java.util.Iterator;
   24   import java.util.LinkedList;
   25   import java.util.List;
   26   import java.util.Map;
   27   
   28   import org.apache.jmeter.functions.InvalidVariableException;
   29   import org.apache.jmeter.testelement.TestElement;
   30   import org.apache.jmeter.testelement.TestPlan;
   31   import org.apache.jmeter.testelement.property.JMeterProperty;
   32   import org.apache.jmeter.testelement.property.MultiProperty;
   33   import org.apache.jmeter.testelement.property.PropertyIterator;
   34   import org.apache.jmeter.testelement.property.StringProperty;
   35   import org.apache.jorphan.logging.LoggingManager;
   36   import org.apache.log.Logger;
   37   
   38   /**
   39    * Perfom replacement of ${variable} references.
   40    */
   41   public class ValueReplacer {
   42       private static final Logger log = LoggingManager.getLoggerForClass();
   43   
   44       private final CompoundVariable masterFunction = new CompoundVariable();
   45   
   46       private Map variables = new HashMap();
   47   
   48       public ValueReplacer() {
   49       }
   50   
   51       public ValueReplacer(TestPlan tp) {
   52           setUserDefinedVariables(tp.getUserDefinedVariables());
   53       }
   54   
   55       boolean containsKey(String k){
   56           return variables.containsKey(k);
   57       }
   58       public void setUserDefinedVariables(Map variables) {
   59           this.variables = variables;
   60       }
   61   
   62       public void replaceValues(TestElement el) throws InvalidVariableException {
   63           Collection newProps = replaceValues(el.propertyIterator(), new ReplaceStringWithFunctions(masterFunction,
   64                   variables));
   65           setProperties(el, newProps);
   66       }
   67   
   68       private void setProperties(TestElement el, Collection newProps) {
   69           Iterator iter = newProps.iterator();
   70           el.clear();
   71           while (iter.hasNext()) {
   72               el.setProperty((JMeterProperty) iter.next());
   73           }
   74       }
   75   
   76       public void reverseReplace(TestElement el) throws InvalidVariableException {
   77           Collection newProps = replaceValues(el.propertyIterator(), new ReplaceFunctionsWithStrings(masterFunction,
   78                   variables));
   79           setProperties(el, newProps);
   80       }
   81   
   82       public void reverseReplace(TestElement el, boolean regexMatch) throws InvalidVariableException {
   83           Collection newProps = replaceValues(el.propertyIterator(), new ReplaceFunctionsWithStrings(masterFunction,
   84                   variables, regexMatch));
   85           setProperties(el, newProps);
   86       }
   87   
   88       public void undoReverseReplace(TestElement el) throws InvalidVariableException {
   89           Collection newProps = replaceValues(el.propertyIterator(), new UndoVariableReplacement(masterFunction,
   90                   variables));
   91           setProperties(el, newProps);
   92       }
   93   
   94       public void addVariable(String name, String value) {
   95           variables.put(name, value);
   96       }
   97   
   98       /**
   99        * Add all the given variables to this replacer's variables map.
  100        *
  101        * @param vars
  102        *            A map of variable name-value pairs (String-to-String).
  103        */
  104       public void addVariables(Map vars) {
  105           variables.putAll(vars);
  106       }
  107   
  108       private Collection replaceValues(PropertyIterator iter, ValueTransformer transform) throws InvalidVariableException {
  109           List props = new LinkedList();
  110           while (iter.hasNext()) {
  111               JMeterProperty val = iter.next();
  112               if (log.isDebugEnabled()) {
  113                   log.debug("About to replace in property of type: " + val.getClass() + ": " + val);
  114               }
  115               if (val instanceof StringProperty) {
  116                   // Must not convert TestElement.gui_class etc
  117                   if (!val.getName().equals(TestElement.GUI_CLASS) &&
  118                           !val.getName().equals(TestElement.TEST_CLASS)) {
  119                       val = transform.transformValue(val);
  120                       if (log.isDebugEnabled()) {
  121                           log.debug("Replacement result: " + val);
  122                       }
  123                   }
  124               } else if (val instanceof MultiProperty) {
  125                   MultiProperty multiVal = (MultiProperty) val;
  126                   Collection newValues = replaceValues(multiVal.iterator(), transform);
  127                   multiVal.clear();
  128                   Iterator propIter = newValues.iterator();
  129                   while (propIter.hasNext()) {
  130                       multiVal.addProperty((JMeterProperty) propIter.next());
  131                   }
  132                   if (log.isDebugEnabled()) {
  133                       log.debug("Replacement result: " + multiVal);
  134                   }
  135               } else {
  136                   if (log.isDebugEnabled()) {
  137                       log.debug("Won't replace " + val);
  138                   }
  139               }
  140               props.add(val);
  141           }
  142           return props;
  143       }
  144   }

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