001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.assertions;
020:
021: import java.io.Serializable;
022: import java.text.MessageFormat;
023:
024: import org.apache.jmeter.samplers.SampleResult;
025: import org.apache.jmeter.testelement.AbstractTestElement;
026: import org.apache.jmeter.testelement.property.IntegerProperty;
027: import org.apache.jmeter.testelement.property.LongProperty;
028: import org.apache.jmeter.util.JMeterUtils;
029:
030: //@see org.apache.jmeter.assertions.PackageTest for unit tests
031:
032: /**
033: * Checks if the results of a Sample matches a particular size.
034: *
035: * author <a href="mailto:wolfram.rittmeyer@web.de">Wolfram Rittmeyer</a>
036: */
037: public class SizeAssertion extends AbstractTestElement implements
038: Serializable, Assertion {
039:
040: private String comparatorErrorMessage = "ERROR!";
041:
042: // * Static int to signify the type of logical comparitor to assert
043: public final static int EQUAL = 1;
044:
045: public final static int NOTEQUAL = 2;
046:
047: public final static int GREATERTHAN = 3;
048:
049: public final static int LESSTHAN = 4;
050:
051: public final static int GREATERTHANEQUAL = 5;
052:
053: public final static int LESSTHANEQUAL = 6;
054:
055: /** Key for storing assertion-informations in the jmx-file. */
056: private static final String SIZE_KEY = "SizeAssertion.size"; // $NON-NLS-1$
057:
058: private static final String OPERATOR_KEY = "SizeAssertion.operator"; // $NON-NLS-1$
059:
060: byte[] resultData;
061:
062: /**
063: * Returns the result of the Assertion.
064: * Here it checks the Sample responseData length.
065: */
066: public AssertionResult getResult(SampleResult response) {
067: AssertionResult result = new AssertionResult(getName());
068: result.setFailure(false);
069: resultData = response.getResponseData();
070: long resultSize = resultData.length;
071: // is the Sample the correct size?
072: if (!(compareSize(resultSize))) {
073: result.setFailure(true);
074: Object[] arguments = { new Long(resultSize),
075: comparatorErrorMessage, new Long(getAllowedSize()) };
076: String message = MessageFormat.format(JMeterUtils
077: .getResString("size_assertion_failure"), arguments); //$NON-NLS-1$
078: result.setFailureMessage(message);
079: }
080: return result;
081: }
082:
083: /**
084: * Returns the size in bytes to be asserted.
085: */
086: public long getAllowedSize() {
087: return getPropertyAsLong(SIZE_KEY);
088: }
089:
090: /***************************************************************************
091: * set the Operator
092: **************************************************************************/
093: public void setCompOper(int operator) {
094: setProperty(new IntegerProperty(OPERATOR_KEY, operator));
095:
096: }
097:
098: /**
099: * Returns the operator to be asserted. EQUAL = 1, NOTEQUAL = 2 GREATERTHAN =
100: * 3,LESSTHAN = 4,GREATERTHANEQUAL = 5,LESSTHANEQUAL = 6
101: */
102:
103: public int getCompOper() {
104: return getPropertyAsInt(OPERATOR_KEY);
105: }
106:
107: /**
108: * Set the size that shall be asserted.
109: *
110: * @param size -
111: * a number of bytes. Is not allowed to be negative. Use
112: * Long.MAX_VALUE to indicate illegal or empty inputs. This will
113: * result in not checking the assertion.
114: *
115: * @throws IllegalArgumentException
116: * If <code>size</code> is negative.
117: */
118: public void setAllowedSize(long size)
119: throws IllegalArgumentException {
120: if (size < 0L) {
121: throw new IllegalArgumentException(JMeterUtils
122: .getResString("argument_must_not_be_negative")); //$NON-NLS-1$
123: }
124: if (size == Long.MAX_VALUE) {
125: setProperty(new LongProperty(SIZE_KEY, 0));
126: } else {
127: setProperty(new LongProperty(SIZE_KEY, size));
128: }
129: }
130:
131: /**
132: * Compares the the size of a return result to the set allowed size using a
133: * logical comparator set in setLogicalComparator().
134: *
135: * Possible values are: equal, not equal, greater than, less than, greater
136: * than eqaul, less than equal, .
137: *
138: */
139: private boolean compareSize(long resultSize) {
140: boolean result = false;
141: int comp = getCompOper();
142: switch (comp) {
143: case EQUAL:
144: result = (resultSize == getAllowedSize());
145: comparatorErrorMessage = JMeterUtils
146: .getResString("size_assertion_comparator_error_equal"); //$NON-NLS-1$
147: break;
148: case NOTEQUAL:
149: result = (resultSize != getAllowedSize());
150: comparatorErrorMessage = JMeterUtils
151: .getResString("size_assertion_comparator_error_notequal"); //$NON-NLS-1$
152: break;
153: case GREATERTHAN:
154: result = (resultSize > getAllowedSize());
155: comparatorErrorMessage = JMeterUtils
156: .getResString("size_assertion_comparator_error_greater"); //$NON-NLS-1$
157: break;
158: case LESSTHAN:
159: result = (resultSize < getAllowedSize());
160: comparatorErrorMessage = JMeterUtils
161: .getResString("size_assertion_comparator_error_less"); //$NON-NLS-1$
162: break;
163: case GREATERTHANEQUAL:
164: result = (resultSize >= getAllowedSize());
165: comparatorErrorMessage = JMeterUtils
166: .getResString("size_assertion_comparator_error_greaterequal"); //$NON-NLS-1$
167: break;
168: case LESSTHANEQUAL:
169: result = (resultSize <= getAllowedSize());
170: comparatorErrorMessage = JMeterUtils
171: .getResString("size_assertion_comparator_error_lessequal"); //$NON-NLS-1$
172: break;
173: }
174: return result;
175: }
176: }
|