KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > assertions > DurationAssertion


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/assertions/DurationAssertion.java,v 1.5 2004/02/13 01:27:26 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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.assertions;
20 import java.io.Serializable JavaDoc;
21 import java.text.MessageFormat JavaDoc;
22
23 import org.apache.jmeter.samplers.SampleResult;
24 import org.apache.jmeter.testelement.AbstractTestElement;
25 import org.apache.jmeter.testelement.property.LongProperty;
26 import org.apache.jmeter.util.JMeterUtils;
27 /**
28  * Checks if an Sample is sampled within a specified time-frame. If the
29  * duration is larger than the timeframe the Assertion is considered
30  * a failure.
31  *
32  * @author <a HREF="mailto:wolfram.rittmeyer@web.de">Wolfram Rittmeyer</a>
33  * @version $Revision: 1.5 $, $Date: 2004/02/13 01:27:26 $
34  */

35 public class DurationAssertion
36     extends AbstractTestElement
37     implements Serializable JavaDoc, Assertion
38 {
39    /** Key for storing assertion-informations in the jmx-file. */
40    private static final String JavaDoc DURATION_KEY = "DurationAssertion.duration";
41    /**
42     * Returns the result of the Assertion. Here it checks wether the
43     * Sample took to long to be considered successful. If so an AssertionResult
44     * containing a FailureMessage will be returned. Otherwise the returned
45     * AssertionResult will reflect the success of the Sample.
46     */

47    public AssertionResult getResult(SampleResult response)
48    {
49       AssertionResult result = new AssertionResult();
50       result.setFailure(false);
51       // has the Sample lasted to long?
52
if (((response.getTime() > getAllowedDuration())
53         && (getAllowedDuration() > 0)))
54       {
55          result.setFailure(true);
56         Object JavaDoc[] arguments =
57             { new Long JavaDoc(response.getTime()), new Long JavaDoc(getAllowedDuration())};
58         String JavaDoc message =
59             MessageFormat.format(
60                 JMeterUtils.getResString("duration_assertion_failure"),
61                 arguments);
62          result.setFailureMessage(message);
63       }
64       return result;
65    }
66    
67    /**
68     * Returns the duration to be asserted. A duration of 0 indicates this
69     * assertion is to be ignored.
70     */

71    public long getAllowedDuration()
72    {
73       return getPropertyAsLong(DURATION_KEY);
74    }
75    
76    /**
77     * Set the duration that shall be asserted.
78     *
79     * @param duration a period of time in milliseconds. Is not allowed to be
80     * negative. Use Double.MAX_VALUE to indicate illegal or
81     * empty inputs. This will result to not checking the
82     * assertion.
83     *
84     * @throws IllegalArgumentException if <code>duration</code> is negative.
85     */

86    public void setAllowedDuration(long duration) throws IllegalArgumentException JavaDoc
87    {
88       if (duration < 0L)
89       {
90         throw new IllegalArgumentException JavaDoc(
91             JMeterUtils.getResString("argument_must_not_be_negative"));
92       }
93       if (duration == Long.MAX_VALUE)
94       {
95          setProperty(new LongProperty(DURATION_KEY,0));
96       }
97       else
98       {
99          setProperty(new LongProperty(DURATION_KEY,duration));
100       }
101    }
102 }
Popular Tags