1 16 17 package org.apache.commons.latka.validators; 18 19 import org.apache.commons.latka.ValidationException; 20 21 import org.apache.commons.latka.http.Session; 22 import org.apache.commons.latka.http.Response; 23 24 31 public class CookieValidator extends BaseConditionalValidator { 32 33 35 protected String _cookieValue = null; 36 protected String _cookieName = null; 37 38 protected static final String MESSAGE_INVALID_TEST = "INVALID TEST: COOKIE NAME NOT SET"; 39 protected static final String BARE_MESSAGE_EXISTENT_COOKIE = " TO FIND COOKIE IN SESSION"; 40 protected static final String BARE_MESSAGE_EQUAL_VALUES = " THAT COOKIE VALUES EQUAL:"; 41 42 protected String _lastTestedCookieValue = null; 44 45 47 public CookieValidator() { 48 this(null,null,null,true); 49 } 50 51 public CookieValidator(String label) { 52 this(label,null,null,true); 53 } 54 55 public CookieValidator(String name, String value) { 56 this(null,name,value,true); 57 } 58 59 public CookieValidator(String label, String name, String value, boolean condition) { 60 super(label, condition); 61 _cookieName = name; 62 _cookieValue = value; 63 } 64 65 67 72 public void setCookieValue(String value) { 73 _cookieValue = value; 74 } 75 76 81 public void setCookieName(String name) { 82 _cookieName = name; 83 } 84 85 89 public boolean assertTrue(Response response) 90 throws ValidationException { 91 92 if (_cookieName != null) { 93 Session session = response.getRequest().getSession(); 94 95 _lastTestedCookieValue = session.getCookieValue(_cookieName); 96 97 if (_lastTestedCookieValue == null) { 99 return false; 100 } 101 else if (_cookieValue != null) { 103 if (!_cookieValue.equals(_lastTestedCookieValue)) { 104 return false; 105 } 106 } 107 } 108 else { 110 fail(MESSAGE_INVALID_TEST); 111 } 112 113 return true; 114 } 115 116 public String generateBareExceptionMessage() { 117 118 if (_lastTestedCookieValue == null) { 119 return BARE_MESSAGE_EXISTENT_COOKIE; 120 } 121 else { 123 StringBuffer buffer = new StringBuffer (BARE_MESSAGE_EQUAL_VALUES); 124 buffer.append(" EXPECTED: "); 125 buffer.append(_cookieValue); 126 buffer.append(" RECEIVED: "); 127 buffer.append(_lastTestedCookieValue); 128 return buffer.toString(); 129 } 130 } 131 132 } 133 | Popular Tags |