DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Using Barcodes in iText 7
  • A Disappointing Story on Java Memory Optimization
  • Optional... What Else?
  • Parallelism in ConcurrentHashMap

Trending

  • The Systemic Process of Debugging
  • Implementing Stronger RBAC and Multitenancy in Kubernetes Using Istio
  • Performance Optimization Strategies in Highly Scalable Systems
  • Understanding Europe's Cyber Resilience Act and What It Means for You
  1. DZone
  2. Coding
  3. Java
  4. Introducing the Regex-tester Library for Java

Introducing the Regex-tester Library for Java

Nick Watts user avatar by
Nick Watts
·
Feb. 12, 14 · Interview
Like (0)
Save
Tweet
Share
3.90K Views

Join the DZone community and get the full member experience.

Join For Free

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’sREADME 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.

Library Testing Strings Java (programming language)

Published at DZone with permission of Nick Watts, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Using Barcodes in iText 7
  • A Disappointing Story on Java Memory Optimization
  • Optional... What Else?
  • Parallelism in ConcurrentHashMap

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: