KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > latka > validators > StatusCodeValidator


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 JavaDoc;
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   protected static final MessageFormat JavaDoc MESSAGE = new MessageFormat JavaDoc("Expected status code \"{0,number,000}\". Found \"{1,number,000}\".");
42   protected int _statusCode = 200;
43
44   // ------------------------------------------------------------- Constructors
45

46   public StatusCodeValidator() {
47     this(null,200);
48   }
49
50   public StatusCodeValidator(int code) {
51     this(null,code);
52   }
53
54   public StatusCodeValidator(String JavaDoc label) {
55     this(label,200);
56   }
57
58   public StatusCodeValidator(String JavaDoc label, int code) {
59     super(label);
60     _statusCode = code;
61   }
62
63   // ------------------------------------------------------------------ Methods
64

65   public void setStatusCode(int statusCode) {
66     _statusCode = statusCode;
67   }
68
69   public void validate(Response response)
70     throws ValidationException {
71
72     int responseStatusCode = response.getStatusCode();
73
74     if (_statusCode != responseStatusCode) {
75        fail(MESSAGE.format(new Object JavaDoc[] { new Integer JavaDoc(_statusCode), new Integer JavaDoc(responseStatusCode) }).toString());
76     }
77   }
78 }
79
Popular Tags