01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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:
21: import java.io.Serializable;
22: import java.text.MessageFormat;
23:
24: import org.apache.jmeter.samplers.SampleResult;
25: import org.apache.jmeter.testelement.AbstractTestElement;
26: import org.apache.jmeter.util.JMeterUtils;
27:
28: /**
29: * Checks if an Sample is sampled within a specified time-frame. If the duration
30: * is larger than the timeframe the Assertion is considered a failure.
31: *
32: * author <a href="mailto:wolfram.rittmeyer@web.de">Wolfram Rittmeyer</a>
33: * @version $Revision: 519376 $, $Date: 2007-03-17 17:36:09 +0000 (Sat, 17 Mar 2007) $
34: */
35: public class DurationAssertion extends AbstractTestElement implements
36: Serializable, Assertion {
37: /** Key for storing assertion-informations in the jmx-file. */
38: public static final String DURATION_KEY = "DurationAssertion.duration"; // $NON-NLS-1$
39:
40: /**
41: * Returns the result of the Assertion. Here it checks wether the Sample
42: * took to long to be considered successful. If so an AssertionResult
43: * containing a FailureMessage will be returned. Otherwise the returned
44: * AssertionResult will reflect the success of the Sample.
45: */
46: public AssertionResult getResult(SampleResult response) {
47: AssertionResult result = new AssertionResult(getName());
48: result.setFailure(false);
49: long duration = getAllowedDuration();
50: if (duration > 0) {
51: long responseTime = response.getTime();
52: // has the Sample lasted too long?
53: if (responseTime > duration) {
54: result.setFailure(true);
55: Object[] arguments = { new Long(responseTime),
56: new Long(duration) };
57: String message = MessageFormat.format(JMeterUtils
58: .getResString("duration_assertion_failure") // $NON-NLS-1$
59: , arguments);
60: result.setFailureMessage(message);
61: }
62: }
63: return result;
64: }
65:
66: /**
67: * Returns the duration to be asserted. A duration of 0 indicates this
68: * assertion is to be ignored.
69: */
70: private long getAllowedDuration() {
71: return getPropertyAsLong(DURATION_KEY);
72: }
73:
74: }
|