1 18 19 package org.apache.jmeter.assertions; 20 import java.io.Serializable ; 21 import java.util.ArrayList ; 22 23 import org.apache.jmeter.samplers.SampleResult; 24 import org.apache.jmeter.testelement.AbstractTestElement; 25 import org.apache.jmeter.testelement.property.CollectionProperty; 26 import org.apache.jmeter.testelement.property.IntegerProperty; 27 import org.apache.jmeter.testelement.property.JMeterProperty; 28 import org.apache.jmeter.testelement.property.NullProperty; 29 import org.apache.jmeter.testelement.property.PropertyIterator; 30 import org.apache.jmeter.testelement.property.StringProperty; 31 import org.apache.jorphan.util.JOrphanUtils; 32 import org.apache.oro.text.MalformedCachePatternException; 33 import org.apache.oro.text.PatternCacheLRU; 34 import org.apache.oro.text.regex.Pattern; 35 import org.apache.oro.text.regex.Perl5Compiler; 36 import org.apache.oro.text.regex.Perl5Matcher; 37 38 42 public class ResponseAssertion 43 extends AbstractTestElement 44 implements Serializable , Assertion 45 { 46 public final static String TEST_FIELD = "Assertion.test_field"; 47 public final static String SAMPLE_LABEL = "Assertion.sample_label"; 49 public final static String RESPONSE_DATA = "Assertion.response_data"; 50 public final static String RESPONSE_CODE = "Assertion.response_code"; 51 public final static String RESPONSE_MESSAGE = "Assertion.response_message"; 52 public final static String ASSUME_SUCCESS = "Assertion.assume_success"; 53 54 public final static String TEST_STRINGS = "Asserion.test_strings"; 55 56 public final static String TEST_TYPE = "Assertion.test_type"; 57 61 private final static int MATCH = 1 << 0; 62 private final static int CONTAINS = 1 << 1; 63 private final static int NOT = 1 << 2; 64 65 private static ThreadLocal matcher = new ThreadLocal () 66 { 67 protected Object initialValue() 68 { 69 return new Perl5Matcher(); 70 } 71 }; 72 private static PatternCacheLRU patternCache = 73 new PatternCacheLRU(1000, new Perl5Compiler()); 74 77 public ResponseAssertion() 78 { 79 setProperty(new CollectionProperty(TEST_STRINGS, new ArrayList ())); 80 } 81 88 public ResponseAssertion(String field, int type, String string) 89 { 90 this(); 91 setTestField(field); 92 setTestType(type); 93 getTestStrings().addProperty(new StringProperty(string,string)); 94 } 95 96 public void clear() 97 { 98 super.clear(); 99 setProperty(new CollectionProperty(TEST_STRINGS, new ArrayList ())); 100 } 101 102 107 public void setTestField(String testField) 108 { 109 setProperty(TEST_FIELD, testField); 110 } 111 116 public void setTestType(int testType) 117 { 118 setProperty(new IntegerProperty(TEST_TYPE, testType)); 119 } 120 121 126 public void addTestString(String testString) 127 { 128 getTestStrings().addProperty(new StringProperty(testString,testString)); 129 } 130 public void setTestString(String testString, int index) { 132 getTestStrings().set(index, testString); 133 } 134 public void removeTestString(String testString) { 136 getTestStrings().remove(testString); 137 } 138 public void removeTestString(int index) { 140 getTestStrings().remove(index); 141 } 142 public void clearTestStrings() 143 { 144 getTestStrings().clear(); 145 } 146 152 public AssertionResult getResult(SampleResult response) 153 { 154 AssertionResult result; 155 156 169 result = evaluateResponse(response); 170 return result; 171 } 172 177 public String getTestField() 178 { 179 return getPropertyAsString(TEST_FIELD); 180 } 181 186 public int getTestType() 187 { 188 JMeterProperty type = getProperty(TEST_TYPE); 189 if (type instanceof NullProperty) 190 { 191 return CONTAINS; 192 } 193 else 194 { 195 return type.getIntValue(); 196 } 197 } 198 203 public CollectionProperty getTestStrings() 204 { 205 return (CollectionProperty) getProperty(TEST_STRINGS); 206 } 207 public boolean isContainsType() 208 { 209 return (getTestType() & CONTAINS) > 0; 210 } 211 public boolean isMatchType() 212 { 213 return (getTestType() & MATCH) > 0; 214 } 215 public boolean isNotType() 216 { 217 return (getTestType() & NOT) > 0; 218 } 219 public void setToContainsType() 220 { 221 setTestType( 222 (getTestType() | CONTAINS) & (~ MATCH)); 223 } 224 public void setToMatchType() 225 { 226 setTestType( 227 (getTestType() | MATCH) & (~ CONTAINS)); 228 } 229 public void setToNotType() 230 { 231 setTestType((getTestType() | NOT)); 232 } 233 public void unsetNotType() 234 { 235 setTestType(getTestType() & ~ NOT); 236 } 237 public boolean getAssumeSuccess() 238 { 239 return getPropertyAsBoolean(ASSUME_SUCCESS,false); 240 } 241 public void setAssumeSuccess(boolean b) 242 { 243 setProperty(ASSUME_SUCCESS,JOrphanUtils.booleanToString(b)); 244 } 245 251 private AssertionResult evaluateResponse(SampleResult response) 252 { 253 boolean pass = true; 254 boolean not = (NOT & getTestType()) > 0; 255 AssertionResult result = new AssertionResult(); 256 String toCheck=""; 258 if (getAssumeSuccess()) 259 { 260 response.setSuccessful(true); } 262 263 if (ResponseAssertion.RESPONSE_DATA.equals(getTestField())) 265 { 266 toCheck = new StringBuffer (response.getResponseHeaders()).append(new String (response.responseDataAsBA())).toString(); 267 } 268 else if (ResponseAssertion.RESPONSE_CODE.equals(getTestField())) 269 { 270 toCheck=response.getResponseCode(); 271 } 272 else if (ResponseAssertion.RESPONSE_MESSAGE.equals(getTestField())) 273 { 274 toCheck=response.getResponseMessage(); 275 } 276 else 277 { toCheck=response.getSamplerData(); 279 if (toCheck == null) toCheck = ""; 280 } 281 282 if(toCheck.length()==0) 283 { 284 return setResultForNull(result); 285 } 286 287 try 288 { 289 Perl5Matcher localMatcher = (Perl5Matcher) matcher.get(); 291 PropertyIterator iter = getTestStrings().iterator(); 292 while (iter.hasNext()) 293 { 294 String stringPattern = iter.next().getStringValue(); 295 Pattern pattern = 296 patternCache.getPattern( 297 stringPattern, 298 Perl5Compiler.READ_ONLY_MASK); 299 boolean found; 300 if ((CONTAINS & getTestType()) > 0) 301 { 302 found = localMatcher.contains(toCheck, pattern); 303 } 304 else 305 { 306 found = localMatcher.matches(toCheck, pattern); 307 } 308 pass = not ? !found : found; 309 if (!pass) 310 { 311 result.setFailure(true); 312 result.setFailureMessage(getFailText(stringPattern)); 313 break; 314 } 315 } 316 if (pass) 317 { 318 result.setFailure(false); 319 } 320 result.setError(false); 321 } 322 catch (MalformedCachePatternException e) 323 { 324 result.setError(true); 325 result.setFailure(false); 326 result.setFailureMessage("Bad test configuration" + e); 327 } 328 return result; 329 } 330 331 338 private String getFailText(String stringPattern) { 339 String text; 340 String what; 341 if (ResponseAssertion.RESPONSE_DATA.equals(getTestField())) 342 { 343 what="text"; 344 } 345 else if (ResponseAssertion.RESPONSE_CODE.equals(getTestField())) 346 { 347 what="code"; 348 } 349 else if (ResponseAssertion.RESPONSE_MESSAGE.equals(getTestField())) 350 { 351 what="message"; 352 } 353 else { 355 what="URL"; 356 } 357 switch(getTestType()){ 358 case CONTAINS: 359 text = " expected to contain "; 360 break; 361 case NOT | CONTAINS: 362 text = " expected not to contain "; 363 break; 364 case MATCH: 365 text = " expected to match "; 366 break; 367 case NOT | MATCH: 368 text = " expected not to match "; 369 break; 370 default: text = " expected something using "; 372 } 373 374 return "Test failed, " + what + text + "/" + stringPattern + "/"; 375 } 376 protected AssertionResult setResultForNull(AssertionResult result) 377 { 378 result.setError(false); 379 result.setFailure(true); 380 result.setFailureMessage("Response (or URL) was null"); 381 return result; 382 } 383 public static class Test extends junit.framework.TestCase 384 { 385 int threadsRunning; 386 int failed; 387 public Test(String name) 388 { 389 super(name); 390 } 391 public void testThreadSafety() throws Exception 392 { 393 Thread [] threads = new Thread [100]; 394 for (int i = 0; i < threads.length; i++) 395 { 396 threads[i] = new TestThread(); 397 } 398 failed = 0; 399 for (int i = 0; i < threads.length; i++) 400 { 401 threads[i].start(); 402 threadsRunning++; 403 } 404 synchronized (this) 405 { 406 while (threadsRunning > 0) 407 { 408 wait(); 409 } 410 } 411 assertEquals(failed, 0); 412 } 413 class TestThread extends Thread 414 { 415 static final String TEST_STRING = "DAbale arroz a la zorra el abad."; 416 static final String TEST_PATTERN = ".*A.*\\."; 418 public void run() 419 { 420 ResponseAssertion assertion = 421 new ResponseAssertion(RESPONSE_DATA, CONTAINS, TEST_PATTERN); 422 SampleResult response = new SampleResult(); 423 response.setResponseData(TEST_STRING.getBytes()); 424 for (int i = 0; i < 100; i++) 425 { 426 AssertionResult result; 427 result = assertion.evaluateResponse(response); 428 if (result.isFailure() || result.isError()) 429 { 430 failed++; 431 } 432 } 433 synchronized (Test.this) 434 { 435 threadsRunning--; 436 Test.this.notifyAll(); 437 } 438 } 439 } 440 } 441 } 442 | Popular Tags |