Here it is.

/*
 * Copyright 2001-2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
*/

package org.apache.jmeter.control.gui;

import java.io.Serializable;
import java.text.MessageFormat;

import org.apache.jmeter.junit.JMeterTestCase;
import org.apache.jmeter.junit.stubs.TestSampler;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.testelement.property.LongProperty;
import org.apache.jmeter.testelement.property.StringProperty;
import org.apache.jmeter.testelement.property.IntegerProperty;
import org.apache.jmeter.testelement.property.BooleanProperty;
import org.apache.jmeter.control.GenericController;
import org.apache.jmeter.control.NextIsNullException;
import org.apache.jmeter.assertions.Assertion;
import org.apache.jmeter.assertions.AssertionResult;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

/**
 * @version   $Revision: 1.4 $
 */
public class ProcessController extends GenericController implements 
Serializable, Assertion
{
    private static final Logger log = LoggingManager.getLoggerForClass();
    private final static String SECONDS = "ProcessController.seconds";
    private final static String COUNT = "ProcessController.count";
    private final static String SUM = "ProcessController.sum";
    private final static String FLAG = "ProcessController.flag";
    private volatile long startTime = 0;


        private int loopCount = 0; // for getIterCount
 
    public ProcessController()
    {
    }

    public void setRuntime(long seconds)
    {
        setProperty(new LongProperty(SECONDS, seconds));
    }

    public void setCount(int count)
    {
        setProperty(new IntegerProperty(COUNT, count));
    }
    public void setSum(int sum)
    {
        setProperty(new IntegerProperty(SUM, sum));
    }
    public void setFlag(boolean flag)
    {
        setProperty(new BooleanProperty(FLAG, flag));
    }
    public void setRuntime(String seconds)
    {
        setProperty(new StringProperty(SECONDS, seconds));
    }
    public void setCount(String count)
    {
        setProperty(new StringProperty(COUNT, count));
    }
    public void setSum(String sum)
    {
        setProperty(new StringProperty(SUM, sum));
    }
    public void setFlag(String flag)
    {
        setProperty(new StringProperty(FLAG, flag));
    }

    public long getRuntime()
    {
        try
        {
            return Long.parseLong(getPropertyAsString(SECONDS));

        }
        catch (NumberFormatException e)
        {
            return 0L;
        }
    }

    public int getCount()
    {
        try
        {
            return Integer.parseInt(getPropertyAsString(COUNT));

        }
        catch (NumberFormatException e)
        {
            return (int) 0L;
        }
    }

    public int getSum()
    {
        try
        {
            return Integer.parseInt(getPropertyAsString(SUM));

        }
        catch (NumberFormatException e)
        {
            return (int) 0L;
        }
    }

    public boolean getFlag()
    {
        try
        {
            return Boolean.parseBoolean(getPropertyAsString(FLAG));

        }
        catch (NumberFormatException e)
        {
            return true;
        }
    }

    public String getRuntimeString()
    {
        return getPropertyAsString(SECONDS);
    }

    public String getCountString()
    {
        return getPropertyAsString(COUNT);
    }

    public String getSumString()
    {
        return getPropertyAsString(SUM);
    }

    public String getFlagString()
    {
        return getPropertyAsString(FLAG);
    }

    /* (non-Javadoc)
     * @see org.apache.jmeter.control.Controller#isDone()
     */
    public boolean isDone()
    {

        if (getSubControllers().size() > 0)
        {
            this.removeCurrentElement();
            return super.isDone();
        }
        else
        {
             return true; // Runtime is zero - no point staying around
        }
    }

    private boolean endOfLoop()
    {
        if(getCount()==getSum())
        {
            return true;
        }
 
if(getFlag()==false&&(startTime!=0)&&(System.currentTimeMillis()-startTime 
>= 1000*getRuntime()))
        {
             return true;
        }
 
        return false;
    }

        public Sampler next()
        {

        if (startTime == 0)
        {
            if(getFlag()==false)startTime=System.currentTimeMillis();
        }
        else if(getFlag()==true)startTime=0;

        if (endOfLoop()){
            reInitialize();// ??
            resetLoopCount();
            return null;
        }
                return super.next();
        }
    /* (non-Javadoc)
     * @see org.apache.jmeter.control.GenericController#nextIsNull()
     */
    /*protected Sampler nextIsNull() throws NextIsNullException
    {
        log.info("nextIsNull()");
        reInitialize();
        if (endOfLoop())
        {
            log.info("Powinien skonczyc");
            resetLoopCount();
            return null;
        }
        else
        {
            return next();
        }
    }
      */
        protected void incrementLoopCount()
        {
                loopCount++;
        }
        protected void resetLoopCount()
        {
                loopCount=0;
                startTime=0;
        }
        /*
         * This is needed for OnceOnly to work like other Loop Controllers
         */
        protected int getIterCount()
        {
                return loopCount + 1;
        }
        protected void reInitialize()
        {

        setFirst(true);
        setSum("0");
                resetCurrent();
                incrementLoopCount();
        recoverRunningVersion();

        }


//////////////////////////////Start of Test Code 
///////////////////////////
   public AssertionResult getResult(SampleResult response)
   {
      AssertionResult result = new AssertionResult();
 
if(getFlag()==false&&(startTime!=0)&&(System.currentTimeMillis()-startTime 
>= 1000*getRuntime()))
        {
        result.setFailure(true);
        Object[] arguments =
            { new Long(getRuntime())};
        String message =
            MessageFormat.format(
                JMeterUtils.getResString("duration_assertion_failure"),
                arguments);
         result.setFailureMessage(message);

        }
        else
        {
            result.setFailure(false);
        }
      return result;
   }
}

Reply via email to