1 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.testelement.property.IntegerProperty; 27 import org.apache.jmeter.testelement.property.LongProperty; 28 import org.apache.jmeter.util.JMeterUtils; 29 30 36 public class SizeAssertion 37 extends AbstractTestElement 38 implements Serializable , Assertion 39 { 40 41 private String comparatorErrorMessage = "ERROR!"; 42 public final static int EQUAL = 1; 44 public final static int NOTEQUAL = 2; 45 public final static int GREATERTHAN = 3; 46 public final static int LESSTHAN = 4; 47 public final static int GREATERTHANEQUAL = 5; 48 public final static int LESSTHANEQUAL = 6; 49 50 private static final String SIZE_KEY = "SizeAssertion.size"; 51 private static final String OPERATOR_KEY = "SizeAssertion.operator"; 52 byte[] resultData; 53 54 60 public AssertionResult getResult(SampleResult response) 61 { 62 AssertionResult result = new AssertionResult(); 63 result.setFailure(false); 64 if (response.getResponseData() == null) 65 { 66 return setResultForNull(result); 67 } 68 resultData = response.getResponseData(); 70 long resultSize = resultData.length; 71 if ((!(compareSize(resultSize)) && (getAllowedSize() > 0))) 72 { 73 result.setFailure(true); 74 Object [] arguments = 75 { 76 new Long (resultSize), 77 comparatorErrorMessage, 78 new Long (getAllowedSize())}; 79 String message = 80 MessageFormat.format( 81 JMeterUtils.getResString("size_assertion_failure"), 82 arguments); 83 result.setFailureMessage(message); 84 } 85 return result; 86 } 87 88 92 public long getAllowedSize() 93 { 94 return getPropertyAsLong(SIZE_KEY); 95 } 96 97 100 public void setCompOper(int operator) 101 { 102 setProperty(new IntegerProperty(OPERATOR_KEY, operator)); 103 104 } 105 106 protected AssertionResult setResultForNull(AssertionResult result) 107 { 108 result.setError(false); 109 result.setFailure(true); 110 result.setFailureMessage("Response was null"); 111 return result; 112 } 113 114 118 119 public int getCompOper() 120 { 121 return getPropertyAsInt(OPERATOR_KEY); 122 } 123 124 133 public void setAllowedSize(long size) throws IllegalArgumentException 134 { 135 if (size < 0L) 136 { 137 throw new IllegalArgumentException ( 138 JMeterUtils.getResString("argument_must_not_be_negative")); 139 } 140 if (size == Long.MAX_VALUE) 141 { 142 setProperty(new LongProperty(SIZE_KEY, 0)); 143 } 144 else 145 { 146 setProperty(new LongProperty(SIZE_KEY, size)); 147 } 148 } 149 150 160 private boolean compareSize(long resultSize) 161 { 162 boolean result = false; 163 int comp = getCompOper(); 164 switch (comp) 165 { 166 case EQUAL : 167 result = (resultSize == getAllowedSize()); 168 comparatorErrorMessage = 169 JMeterUtils.getResString( 170 "size_assertion_comparator_error_equal"); 171 break; 172 case NOTEQUAL : 173 result = (resultSize != getAllowedSize()); 174 comparatorErrorMessage = 175 JMeterUtils.getResString( 176 "size_assertion_comparator_error_notequal"); 177 break; 178 case GREATERTHAN : 179 result = (resultSize > getAllowedSize()); 180 comparatorErrorMessage = 181 JMeterUtils.getResString( 182 "size_assertion_comparator_error_greater"); 183 break; 184 case LESSTHAN : 185 result = (resultSize < getAllowedSize()); 186 comparatorErrorMessage = 187 JMeterUtils.getResString( 188 "size_assertion_comparator_error_less"); 189 break; 190 case GREATERTHANEQUAL : 191 result = (resultSize >= getAllowedSize()); 192 comparatorErrorMessage = 193 JMeterUtils.getResString( 194 "size_assertion_comparator_error_greaterequal"); 195 break; 196 case LESSTHANEQUAL : 197 result = (resultSize <= getAllowedSize()); 198 comparatorErrorMessage = 199 JMeterUtils.getResString( 200 "size_assertion_comparator_error_lessequal"); 201 break; 202 } 203 return result; 204 } 205 } 206 | Popular Tags |