Created
December 10, 2014 15:15
-
-
Save tomasmalmsten/2193295def1ac413d030 to your computer and use it in GitHub Desktop.
A hamcrest matcher verifying that a string matches the pattern of a UUID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tomasmalmsten.matchers | |
import org.hamcrest.Description; | |
import org.hamcrest.Factory; | |
import org.hamcrest.Matcher; | |
import org.hamcrest.TypeSafeMatcher; | |
public class StringMatchesUUIDPattern extends TypeSafeMatcher<String> { | |
private static final String UUID_REGEX = "[0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}"; | |
@Override | |
protected boolean matchesSafely(String s) { | |
return s.matches(UUID_REGEX); | |
} | |
@Override | |
public void describeTo(Description description) { | |
description.appendText("a string matching the pattern of a UUID"); | |
} | |
@Factory | |
public static Matcher<String> matchesThePatternOfAUUID() { | |
return new StringMatchesUUIDPattern(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment