| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| StatusCodeValidator |
|
| 1.1666666666666667;1.167 |
| 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.Validator; | |
| 20 | import org.apache.commons.latka.ValidationException; | |
| 21 | ||
| 22 | import org.apache.commons.latka.http.Response; | |
| 23 | ||
| 24 | import java.text.MessageFormat; | |
| 25 | ||
| 26 | /** | |
| 27 | * Validates a Response according to its HTTP status code | |
| 28 | * (200, 301, 302, 404, etc). Note: by default, Latka | |
| 29 | * will automatically follow redirects. If you want to | |
| 30 | * test for a 301 or 302 response, you will need to | |
| 31 | * set followRedirects="false" on the Latka request. | |
| 32 | * | |
| 33 | * @author Morgan Delagrange | |
| 34 | * @author dIon Gillard | |
| 35 | * @version $Id: StatusCodeValidator.java 155424 2005-02-26 13:09:29Z dirkv $ | |
| 36 | */ | |
| 37 | public class StatusCodeValidator extends BaseValidator implements Validator { | |
| 38 | ||
| 39 | // --------------------------------------------------------------- Attributes | |
| 40 | ||
| 41 | 0 | protected static final MessageFormat MESSAGE = new MessageFormat("Expected status code \"{0,number,000}\". Found \"{1,number,000}\"."); |
| 42 | 0 | protected int _statusCode = 200; |
| 43 | ||
| 44 | // ------------------------------------------------------------- Constructors | |
| 45 | ||
| 46 | public StatusCodeValidator() { | |
| 47 | 0 | this(null,200); |
| 48 | 0 | } |
| 49 | ||
| 50 | public StatusCodeValidator(int code) { | |
| 51 | 0 | this(null,code); |
| 52 | 0 | } |
| 53 | ||
| 54 | public StatusCodeValidator(String label) { | |
| 55 | 0 | this(label,200); |
| 56 | 0 | } |
| 57 | ||
| 58 | public StatusCodeValidator(String label, int code) { | |
| 59 | 0 | super(label); |
| 60 | 0 | _statusCode = code; |
| 61 | 0 | } |
| 62 | ||
| 63 | // ------------------------------------------------------------------ Methods | |
| 64 | ||
| 65 | public void setStatusCode(int statusCode) { | |
| 66 | 0 | _statusCode = statusCode; |
| 67 | 0 | } |
| 68 | ||
| 69 | public void validate(Response response) | |
| 70 | throws ValidationException { | |
| 71 | ||
| 72 | 0 | int responseStatusCode = response.getStatusCode(); |
| 73 | ||
| 74 | 0 | if (_statusCode != responseStatusCode) { |
| 75 | 0 | fail(MESSAGE.format(new Object[] { new Integer(_statusCode), new Integer(responseStatusCode) }).toString()); |
| 76 | } | |
| 77 | 0 | } |
| 78 | } |