| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| RegexMatchers |
|
| 1.0;1 |
| 1 | /** | |
| 2 | * Copyright (c) 2011-2014, jcabi.com | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: 1) Redistributions of source code must retain the above | |
| 8 | * copyright notice, this list of conditions and the following | |
| 9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
| 10 | * copyright notice, this list of conditions and the following | |
| 11 | * disclaimer in the documentation and/or other materials provided | |
| 12 | * with the distribution. 3) Neither the name of the jcabi.com nor | |
| 13 | * the names of its contributors may be used to endorse or promote | |
| 14 | * products derived from this software without specific prior written | |
| 15 | * permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
| 19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
| 21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
| 26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
| 28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 | */ | |
| 30 | package com.jcabi.matchers; | |
| 31 | ||
| 32 | import lombok.EqualsAndHashCode; | |
| 33 | import lombok.ToString; | |
| 34 | import org.hamcrest.Matcher; | |
| 35 | ||
| 36 | /** | |
| 37 | * Convenient matchers for checking Strings against regular expressions. | |
| 38 | * | |
| 39 | * @author Carlos Miranda (miranda.cma@gmail.com) | |
| 40 | * @version $Id$ | |
| 41 | * @since 1.3 | |
| 42 | */ | |
| 43 | 0 | @ToString |
| 44 | 0 | @EqualsAndHashCode |
| 45 | public final class RegexMatchers { | |
| 46 | ||
| 47 | /** | |
| 48 | * Private ctor, it's a utility class. | |
| 49 | */ | |
| 50 | 0 | private RegexMatchers() { |
| 51 | // Utility class, shouldn't be instantiated. | |
| 52 | 0 | } |
| 53 | ||
| 54 | /** | |
| 55 | * Checks whether a String matches the given regular expression. Works in a | |
| 56 | * similar manner to {@link String#matches(String)}. For example: | |
| 57 | * | |
| 58 | * <pre> MatcherAssert.assert( | |
| 59 | * "abc123", | |
| 60 | * RegexMatchers.matchesPattern("[a-c]+\\d{3}") | |
| 61 | * );</pre> | |
| 62 | * | |
| 63 | * @param pattern The pattern to match against | |
| 64 | * @return Matcher suitable for JUnit/Hamcrest matching | |
| 65 | * @todo #10 Let's create a convenience method | |
| 66 | * matchesAnyPattern(String...patterns) that should pass if a string | |
| 67 | * matches any of the given patterns. | |
| 68 | */ | |
| 69 | public static Matcher<String> matchesPattern(final String pattern) { | |
| 70 | 3 | return new RegexMatchingPatternMatcher(pattern); |
| 71 | } | |
| 72 | ||
| 73 | /** | |
| 74 | * Checks whether a String contains a subsequence matching the given regular | |
| 75 | * expression. Works in a similar manner to | |
| 76 | * {@link java.util.regex.Matcher#find()}. For example: | |
| 77 | * | |
| 78 | * <pre> MatcherAssert.assert( | |
| 79 | * "fooBar123", | |
| 80 | * RegexMatchers.containsPattern("Bar12") | |
| 81 | * );</pre> | |
| 82 | * | |
| 83 | * @param pattern The pattern to match against | |
| 84 | * @return Matcher suitable for JUnit/Hamcrest matching | |
| 85 | * @todo #10 Let's create the following convenience methods: | |
| 86 | * 1) containsAnyPattern(String...patterns), which should pass if a string | |
| 87 | * contains ANY of the given patterns, and | |
| 88 | * 2) containsAllPatterns(String...patterns). which should pass if a string | |
| 89 | * contains ALL of the given patterns. | |
| 90 | */ | |
| 91 | public static Matcher<String> containsPattern(final String pattern) { | |
| 92 | 1 | return new RegexContainingPatternMatcher(pattern); |
| 93 | } | |
| 94 | ||
| 95 | } |