Coverage Report - org.apache.commons.latka.validators.CookieValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
CookieValidator
0%
0/36
0%
0/10
2
 
 1  
 /*
 2  
  * Copyright 1999-2002,2004 The Apache Software Foundation.
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 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  
  * CookieValidator validates cookie existence and/or value in an HTTP session.
 26  
  *
 27  
  * @author <a href="mailto:dsale@us.britannica.com">Doug Sale</a>
 28  
  * @author dIon Gillard
 29  
  * @version $Id: CookieValidator.java 155424 2005-02-26 13:09:29Z dirkv $
 30  
  */
 31  
 public class CookieValidator extends BaseConditionalValidator {
 32  
 
 33  
     // --------------------------------------------------------------- Attributes
 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  
     // needed to generate the correct exception
 43  0
     protected String _lastTestedCookieValue = null;
 44  
 
 45  
     // ------------------------------------------------------------- Constructors
 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  
     // ------------------------------------------------------------------ Methods
 66  
 
 67  
     /**
 68  
      * If cookie value is set, this <code>Validator</code> tests for cookie value equivalency, in addition to cookie existence
 69  
      *
 70  
      * @param value the value of the cookie
 71  
      */
 72  
     public void setCookieValue(String value) {
 73  0
         _cookieValue = value;
 74  0
     }
 75  
 
 76  
     /**
 77  
      * The cookie name must be set for this <code>Validator</code> to function properly.
 78  
      *
 79  
      * @param name the name of the cookie
 80  
      */
 81  
     public void setCookieName(String name) {
 82  0
         _cookieName = name;
 83  0
     }
 84  
 
 85  
     /**
 86  
      * @throws org.apache.commons.latka.ValidationException
 87  
      * if cookie name hasn't been set, the cookie doesn't exist in the session, or if cookie value has been set and the cookie values don't match
 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  
             // existence test
 98  0
             if (_lastTestedCookieValue == null) {
 99  0
                 return false;
 100  
             }
 101  
             // value test
 102  0
             else if (_cookieValue != null) {
 103  0
                 if (!_cookieValue.equals(_lastTestedCookieValue)) {
 104  0
                     return false;
 105  
                 }
 106  
             }
 107  0
         }
 108  
         // invalid test
 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  
         // value test
 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  
 }