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   import java.text.SimpleDateFormat;
   22   import java.util.Collection;
   23   import java.util.Date;
   24   import java.util.HashMap;
   25   import java.util.LinkedList;
   26   import java.util.List;
   27   import java.util.Map;
   28   
   29   import org.apache.jmeter.engine.util.CompoundVariable;
   30   import org.apache.jmeter.samplers.SampleResult;
   31   import org.apache.jmeter.samplers.Sampler;
   32   import org.apache.jmeter.threads.JMeterVariables;
   33   import org.apache.jmeter.util.JMeterUtils;
   34   
   35   // See org.apache.jmeter.functions.TestTimeFunction for unit tests
   36   
   37   /**
   38    * __time() function - returns the current time in milliseconds
   39    */
   40   public class TimeFunction extends AbstractFunction {
   41   
   42       private static final String KEY = "__time"; // $NON-NLS-1$
   43   
   44       private static final List desc = new LinkedList();
   45   
   46       private static final Map aliases = new HashMap();
   47   
   48       static {
   49           desc.add(JMeterUtils.getResString("time_format")); //$NON-NLS-1$
   50           desc.add(JMeterUtils.getResString("function_name_paropt")); //$NON-NLS-1$
   51           aliases.put("YMD", //$NON-NLS-1$
   52                   JMeterUtils.getPropDefault("time.YMD", //$NON-NLS-1$
   53                           "yyyyMMdd")); //$NON-NLS-1$
   54           aliases.put("HMS", //$NON-NLS-1$
   55                   JMeterUtils.getPropDefault("time.HMS", //$NON-NLS-1$
   56                           "HHmmss")); //$NON-NLS-1$
   57           aliases.put("YMDHMS", //$NON-NLS-1$
   58                   JMeterUtils.getPropDefault("time.YMDHMS", //$NON-NLS-1$
   59                           "yyyyMMdd-HHmmss")); //$NON-NLS-1$
   60           aliases.put("USER1", //$NON-NLS-1$
   61                   JMeterUtils.getPropDefault("time.USER1","")); //$NON-NLS-1$
   62           aliases.put("USER2", //$NON-NLS-1$
   63                   JMeterUtils.getPropDefault("time.USER2","")); //$NON-NLS-1$
   64       }
   65   
   66       // Ensure that these are set, even if no paramters are provided
   67       private String format   = ""; //$NON-NLS-1$
   68       private String variable = ""; //$NON-NLS-1$
   69   
   70       public TimeFunction(){
   71           super();
   72       }
   73   
   74       /*
   75        * (non-Javadoc)
   76        *
   77        * @see org.apache.jmeter.functions.Function#execute(SampleResult, Sampler)
   78        */
   79       public synchronized String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
   80           String datetime;
   81           if (format.length() == 0){// Default to milliseconds
   82               datetime = Long.toString(System.currentTimeMillis());
   83           } else {
   84               // Resolve any aliases
   85               String fmt = (String) aliases.get(format);
   86               if (fmt == null) {
   87                   fmt = format;// Not found
   88               }
   89               SimpleDateFormat df = new SimpleDateFormat(fmt);// Not synchronised, so can't be shared
   90               datetime = df.format(new Date());
   91           }
   92   
   93           if (variable.length() > 0) {
   94               JMeterVariables vars = getVariables();
   95               if (vars != null){// vars will be null on TestPlan
   96                   vars.put(variable, datetime);
   97               }
   98           }
   99           return datetime;
  100       }
  101   
  102       /*
  103        * (non-Javadoc)
  104        *
  105        * It appears that this is not called if no parameters are provided.
  106        *
  107        * @see org.apache.jmeter.functions.Function#setParameters(Collection)
  108        */
  109       public synchronized void setParameters(Collection parameters) throws InvalidVariableException {
  110   
  111           checkParameterCount(parameters, 0, 2);
  112   
  113           Object []values = parameters.toArray();
  114           int count = values.length;
  115   
  116           if (count > 0) {
  117               format = ((CompoundVariable) values[0]).execute();
  118           }
  119   
  120           if (count > 1) {
  121               variable = ((CompoundVariable)values[1]).execute().trim();
  122           }
  123   
  124       }
  125   
  126       /*
  127        * (non-Javadoc)
  128        *
  129        * @see org.apache.jmeter.functions.Function#getReferenceKey()
  130        */
  131       public String getReferenceKey() {
  132           return KEY;
  133       }
  134   
  135       /*
  136        * (non-Javadoc)
  137        *
  138        * @see org.apache.jmeter.functions.Function#getArgumentDesc()
  139        */
  140       public List getArgumentDesc() {
  141           return desc;
  142       }
  143   }

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