| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| RegexpValidator |
|
| 1.4285714285714286;1.429 |
| 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 org.apache.regexp.RE; | |
| 25 | import org.apache.regexp.RESyntaxException; | |
| 26 | ||
| 27 | /** | |
| 28 | * Perform regular expression matches on the body of | |
| 29 | * the HTTP response. Setting the "condition" attribute | |
| 30 | * of the test indicates whether or not a match | |
| 31 | * is expected. If the server returns a <i>null</i> | |
| 32 | * (e.g. in the case of an empty HEAD response), the | |
| 33 | * validator will always fail, regardless of the setting | |
| 34 | * of the "condition" attribute. | |
| 35 | * | |
| 36 | * @author Morgan Delagrange | |
| 37 | * @author dIon Gillard | |
| 38 | * @version $Id: RegexpValidator.java 155424 2005-02-26 13:09:29Z dirkv $ | |
| 39 | */ | |
| 40 | public class RegexpValidator extends BaseConditionalValidator implements Validator { | |
| 41 | ||
| 42 | // --------------------------------------------------------------- Attributes | |
| 43 | ||
| 44 | 0 | protected String _pattern = null; |
| 45 | 0 | protected boolean _ignoreCase = false; |
| 46 | ||
| 47 | protected static final String BARE_EXCEPTION_MESSAGE = " TO MATCH PATTERN: "; | |
| 48 | ||
| 49 | // ------------------------------------------------------------- Constructors | |
| 50 | ||
| 51 | public RegexpValidator() { | |
| 52 | 0 | this(null,null,true,false); |
| 53 | 0 | } |
| 54 | ||
| 55 | public RegexpValidator(String label) { | |
| 56 | 0 | this(label,null,true,false); |
| 57 | 0 | } |
| 58 | ||
| 59 | public RegexpValidator(String label, String pattern, boolean cond, boolean ignoreCase) { | |
| 60 | 0 | super(label, cond); |
| 61 | 0 | _pattern = pattern; |
| 62 | 0 | _ignoreCase = ignoreCase; |
| 63 | 0 | } |
| 64 | ||
| 65 | // ------------------------------------------------------------------ Methods | |
| 66 | ||
| 67 | public void setPattern(String pattern) { | |
| 68 | 0 | _pattern = pattern; |
| 69 | 0 | } |
| 70 | ||
| 71 | public void setIgnoreCase(boolean ignoreCase) { | |
| 72 | 0 | _ignoreCase = ignoreCase; |
| 73 | 0 | } |
| 74 | ||
| 75 | public boolean assertTrue(Response response) | |
| 76 | throws ValidationException { | |
| 77 | ||
| 78 | 0 | String responseBody = response.getResource(); |
| 79 | 0 | if (responseBody == null) { |
| 80 | 0 | fail("HTTP RESPONSE BODY WAS NULL"); |
| 81 | } | |
| 82 | ||
| 83 | 0 | RE r = null; |
| 84 | try { | |
| 85 | 0 | r = new RE(_pattern); // Compile expression |
| 86 | 0 | } catch (RESyntaxException e) { |
| 87 | 0 | fail(e.toString()); |
| 88 | 0 | } |
| 89 | ||
| 90 | 0 | if (_ignoreCase == true) { |
| 91 | 0 | r.setMatchFlags(RE.MATCH_CASEINDEPENDENT); |
| 92 | } | |
| 93 | ||
| 94 | 0 | boolean matched = |
| 95 | r.match(response.getResource()); // Match against expression | |
| 96 | ||
| 97 | 0 | return matched; |
| 98 | ||
| 99 | } | |
| 100 | ||
| 101 | public String generateBareExceptionMessage() { | |
| 102 | 0 | return BARE_EXCEPTION_MESSAGE + _pattern; |
| 103 | } | |
| 104 | ||
| 105 | } |