Skip to content

Introducing the regex-tester library for Java

February 8, 2014

regex-tester version 0.1 is an open source project that removes the boiler-plate code needed to test regular expressions with JUnit.

Regular expressions often contain business logic that is important to an application yet are rarely put through rigorous, automated testing. That’s unfortunate because it’s generally so easy to test a regular expression (regex, from here on). In many cases, you just want to know that a given string produces a match when the regex  is applied to it. That’s easy even without regex-tester but tested so infrequently in real software.

Running the regex against a handful of strings in a JUnit test is superior coverage to none at all. That’s easy to do with regex-tester:

@RunWith(value=RegexTestSuite.class)
@Regex(value="^com.*")
public class BasicRegexTest {

    @RegexTestStrings
    public static List<RegexTestStringInfo> getTestParameters() {
        return Arrays.asList(new RegexTestStringInfo[] {
                new RegexTestStringInfo(true, "com"),
                new RegexTestStringInfo(true, "com.thewonggei"),
                new RegexTestStringInfo(true, "com.thewonggei.regexTester"),
                new RegexTestStringInfo(false, ".com.thewonggei"),
                new RegexTestStringInfo(false, "www.google.com")
        });
    }

    @Test
    public void test() {}
}

Running the test suite above will automatically create and execute a test case for each test string specified. Even that is a lot of typing for when you want better coverage of the regex. If you want to test many more strings, you can put them in a simple properties file like this:

#A couple of out-of-range examples first
1899-12-31=false
2100-01-01=false
#These are just the wrong format
05/30/1979=false
05.30.1979=false

#...
#However many test strings you desire
#...

#A whole bunch of matching strings
1900-01-01=true
1900-01-02=true
1900-01-03=true
1900-01-04=true

Then the JUnit test gets even simpler:

@RunWith(value=RegexTestSuite.class)
@Regex(value="^(19|20)\\d\\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$")
@RegexTestStringsFile(propsFile="src/test/resources/date-format-test-strings-01.properties")
public class DateFormatRegexTest {

    @Test
    public void test() {}

}

In both cases, the crucial lines of code declare the important pieces under test: the specialized JUnit test suite to use (@RegexTestSuite), the regex to test with (@Regex) and the strings to execute against the regex (either the @RegexTestStrings method or @RegexTestStringsFile). For each string, a boolean value is supplied that indicates whether or not the regex should produce a match.

Therefore, the meaning of

RegexTestStringInfo(true, "com")

is “the given regex will produce a match against the string ‘com'”. If using the properties file

www.google.com=false

means “the given regex will not produce a match against the string ‘www.google.com'”.

These two examples show both the library’s current capabilities and all that is required of you to start automating the testing of your regular expressions. There is much yet to do (e.g. I know you already wondered why you can’t test for the number and content of matches in regular expressions with groups defined) that will come out in later versions. For now, I’m anxious to know about any usage of the library and, especially, what you think needs added or fixed. Feel free to open up an issue on GitHub for any usage problems, bugs or feature requests.

Get the Code

The code is under the MIT License and is hosted on GitHub here. See the project’s README file for more details.

Get the JARs

Maven coordinates:

<dependency>
  <groupId>com.thewonggei</groupId>
  <artifactId>regex-tester</artifactId>
  <version>0.1</version>
</dependency>

Or, download the JARs directly from Maven Central here.

Advertisements
One Comment
  1. nyancode permalink

    Thank you for sharing. You can take a look at the regex tester here too. I think the diagram explainer using Javascript is quite amazing!

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: