| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 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 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
public class CookieValidator extends BaseConditionalValidator { |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | 0 | protected String _cookieValue = null; |
| 36 | 0 | 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 | |
|
| 43 | 0 | protected String _lastTestedCookieValue = null; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
public CookieValidator() { |
| 48 | 0 | this(null,null,null,true); |
| 49 | 0 | } |
| 50 | |
|
| 51 | |
public CookieValidator(String label) { |
| 52 | 0 | this(label,null,null,true); |
| 53 | 0 | } |
| 54 | |
|
| 55 | |
public CookieValidator(String name, String value) { |
| 56 | 0 | this(null,name,value,true); |
| 57 | 0 | } |
| 58 | |
|
| 59 | |
public CookieValidator(String label, String name, String value, boolean condition) { |
| 60 | 0 | super(label, condition); |
| 61 | 0 | _cookieName = name; |
| 62 | 0 | _cookieValue = value; |
| 63 | 0 | } |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
public void setCookieValue(String value) { |
| 73 | 0 | _cookieValue = value; |
| 74 | 0 | } |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
public void setCookieName(String name) { |
| 82 | 0 | _cookieName = name; |
| 83 | 0 | } |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
public boolean assertTrue(Response response) |
| 90 | |
throws ValidationException { |
| 91 | |
|
| 92 | 0 | if (_cookieName != null) { |
| 93 | 0 | Session session = response.getRequest().getSession(); |
| 94 | |
|
| 95 | 0 | _lastTestedCookieValue = session.getCookieValue(_cookieName); |
| 96 | |
|
| 97 | |
|
| 98 | 0 | if (_lastTestedCookieValue == null) { |
| 99 | 0 | return false; |
| 100 | |
} |
| 101 | |
|
| 102 | 0 | else if (_cookieValue != null) { |
| 103 | 0 | if (!_cookieValue.equals(_lastTestedCookieValue)) { |
| 104 | 0 | return false; |
| 105 | |
} |
| 106 | |
} |
| 107 | 0 | } |
| 108 | |
|
| 109 | |
else { |
| 110 | 0 | fail(MESSAGE_INVALID_TEST); |
| 111 | |
} |
| 112 | |
|
| 113 | 0 | return true; |
| 114 | |
} |
| 115 | |
|
| 116 | |
public String generateBareExceptionMessage() { |
| 117 | |
|
| 118 | 0 | if (_lastTestedCookieValue == null) { |
| 119 | 0 | return BARE_MESSAGE_EXISTENT_COOKIE; |
| 120 | |
} |
| 121 | |
|
| 122 | |
else { |
| 123 | 0 | StringBuffer buffer = new StringBuffer(BARE_MESSAGE_EQUAL_VALUES); |
| 124 | 0 | buffer.append(" EXPECTED: "); |
| 125 | 0 | buffer.append(_cookieValue); |
| 126 | 0 | buffer.append(" RECEIVED: "); |
| 127 | 0 | buffer.append(_lastTestedCookieValue); |
| 128 | 0 | return buffer.toString(); |
| 129 | |
} |
| 130 | |
} |
| 131 | |
|
| 132 | |
} |