From f56343157f81f578ee117a408b1fbed2b79fd1a8 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Tue, 1 Oct 2024 13:33:53 -0400 Subject: [PATCH] Make test more readable and maintainable, and less verbose - Use JUnit 5 APIs - Be consistent throughout tests --- .../routines/DomainValidatorStartupTest.java | 12 +++++------- .../validator/routines/EmailValidatorTest.java | 9 +++------ .../validator/routines/IBANValidatorTest.java | 15 +++++++-------- .../validator/routines/UrlValidatorTest.java | 9 +++------ 4 files changed, 18 insertions(+), 27 deletions(-) diff --git a/src/test/java/org/apache/commons/validator/routines/DomainValidatorStartupTest.java b/src/test/java/org/apache/commons/validator/routines/DomainValidatorStartupTest.java index d92c412ee..16a93edd6 100644 --- a/src/test/java/org/apache/commons/validator/routines/DomainValidatorStartupTest.java +++ b/src/test/java/org/apache/commons/validator/routines/DomainValidatorStartupTest.java @@ -16,9 +16,7 @@ */ package org.apache.commons.validator.routines; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThrows; @@ -82,28 +80,28 @@ public void testInstanceOverride() { // Show that the instance picks up static v public void testUpdateBaseArrayCC() { final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> DomainValidator.updateTLDOverride(ArrayType.COUNTRY_CODE_RO, "com")); - assertThat(thrown.getMessage(), is(equalTo("Cannot update the table: COUNTRY_CODE_RO"))); + assertEquals("Cannot update the table: COUNTRY_CODE_RO", thrown.getMessage()); } @Test public void testUpdateBaseArrayGeneric() { final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> DomainValidator.updateTLDOverride(ArrayType.GENERIC_RO, "com")); - assertThat(thrown.getMessage(), is(equalTo("Cannot update the table: GENERIC_RO"))); + assertEquals("Cannot update the table: GENERIC_RO", thrown.getMessage()); } @Test public void testUpdateBaseArrayInfra() { final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> DomainValidator.updateTLDOverride(ArrayType.INFRASTRUCTURE_RO, "com")); - assertThat(thrown.getMessage(), is(equalTo("Cannot update the table: INFRASTRUCTURE_RO"))); + assertEquals("Cannot update the table: INFRASTRUCTURE_RO", thrown.getMessage()); } @Test public void testUpdateBaseArrayLocal() { final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> DomainValidator.updateTLDOverride(ArrayType.LOCAL_RO, "com")); - assertThat(thrown.getMessage(), is(equalTo("Cannot update the table: LOCAL_RO"))); + assertEquals("Cannot update the table: LOCAL_RO", thrown.getMessage()); } @Test diff --git a/src/test/java/org/apache/commons/validator/routines/EmailValidatorTest.java b/src/test/java/org/apache/commons/validator/routines/EmailValidatorTest.java index 7dc2ee6d3..8bee311d4 100644 --- a/src/test/java/org/apache/commons/validator/routines/EmailValidatorTest.java +++ b/src/test/java/org/apache/commons/validator/routines/EmailValidatorTest.java @@ -16,9 +16,6 @@ */ package org.apache.commons.validator.routines; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -548,7 +545,7 @@ public void testValidator374() { @Test public void testValidator473Part1() { // reject null DomainValidator final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> new EmailValidator(false, false, null)); - assertThat(thrown.getMessage(), is(equalTo("DomainValidator cannot be null"))); + assertEquals("DomainValidator cannot be null", thrown.getMessage()); } @Test @@ -556,7 +553,7 @@ public void testValidator473Part2() { // reject null DomainValidator with mismat final List items = new ArrayList<>(); final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> new EmailValidator(false, false, DomainValidator.getInstance(true, items))); - assertThat(thrown.getMessage(), is(equalTo("DomainValidator must agree with allowLocal setting"))); + assertEquals("DomainValidator must agree with allowLocal setting", thrown.getMessage()); } @Test @@ -564,7 +561,7 @@ public void testValidator473Part3() { // reject null DomainValidator with mismat final List items = new ArrayList<>(); final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> new EmailValidator(true, false, DomainValidator.getInstance(false, items))); - assertThat(thrown.getMessage(), is(equalTo("DomainValidator must agree with allowLocal setting"))); + assertEquals("DomainValidator must agree with allowLocal setting", thrown.getMessage()); } @Test diff --git a/src/test/java/org/apache/commons/validator/routines/IBANValidatorTest.java b/src/test/java/org/apache/commons/validator/routines/IBANValidatorTest.java index c20983559..79124cbab 100644 --- a/src/test/java/org/apache/commons/validator/routines/IBANValidatorTest.java +++ b/src/test/java/org/apache/commons/validator/routines/IBANValidatorTest.java @@ -16,9 +16,7 @@ */ package org.apache.commons.validator.routines; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; @@ -359,20 +357,21 @@ public void testNull() { @Test public void testSetDefaultValidator1() { final IllegalStateException thrown = assertThrows(IllegalStateException.class, () -> VALIDATOR.setValidator("GB", 15, "GB")); - assertThat(thrown.getMessage(), is(equalTo("The singleton validator cannot be modified"))); + assertEquals("The singleton validator cannot be modified", thrown.getMessage()); + } @Test public void testSetDefaultValidator2() { final IllegalStateException thrown = assertThrows(IllegalStateException.class, () -> VALIDATOR.setValidator("GB", -1, "GB")); - assertThat(thrown.getMessage(), is(equalTo("The singleton validator cannot be modified"))); + assertEquals("The singleton validator cannot be modified", thrown.getMessage()); } @Test public void testSetValidatorLC() { final IBANValidator validator = new IBANValidator(); final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> validator.setValidator("gb", 15, "GB")); - assertThat(thrown.getMessage(), is(equalTo("Invalid country Code; must be exactly 2 upper-case characters"))); + assertEquals("Invalid country Code; must be exactly 2 upper-case characters", thrown.getMessage()); } @Test @@ -386,14 +385,14 @@ public void testSetValidatorLen1() { public void testSetValidatorLen35() { final IBANValidator validator = new IBANValidator(); final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> validator.setValidator("GB", 35, "GB")); - assertThat(thrown.getMessage(), is(equalTo("Invalid length parameter, must be in range 8 to 34 inclusive: 35"))); + assertEquals("Invalid length parameter, must be in range 8 to 34 inclusive: 35", thrown.getMessage()); } @Test public void testSetValidatorLen7() { final IBANValidator validator = new IBANValidator(); final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> validator.setValidator("GB", 7, "GB")); - assertThat(thrown.getMessage(), is(equalTo("Invalid length parameter, must be in range 8 to 34 inclusive: 7"))); + assertEquals("Invalid length parameter, must be in range 8 to 34 inclusive: 7", thrown.getMessage()); } @Test diff --git a/src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java b/src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java index 98953311c..b3f543294 100644 --- a/src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java +++ b/src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java @@ -16,9 +16,6 @@ */ package org.apache.commons.validator.routines; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -615,7 +612,7 @@ public void testValidator467() { @Test public void testValidator473Part1() { // reject null DomainValidator final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> new UrlValidator(new String[] {}, null, 0L, null)); - assertThat(thrown.getMessage(), is(equalTo("DomainValidator must not be null"))); + assertEquals("DomainValidator must not be null", thrown.getMessage()); } @Test @@ -623,7 +620,7 @@ public void testValidator473Part2() { // reject null DomainValidator with mismat final List items = new ArrayList<>(); final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> new UrlValidator(new String[] {}, null, 0L, DomainValidator.getInstance(true, items))); - assertThat(thrown.getMessage(), is(equalTo("DomainValidator disagrees with ALLOW_LOCAL_URLS setting"))); + assertEquals("DomainValidator disagrees with ALLOW_LOCAL_URLS setting", thrown.getMessage()); } @Test @@ -631,7 +628,7 @@ public void testValidator473Part3() { // reject null DomainValidator with mismat final List items = new ArrayList<>(); final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> new UrlValidator(new String[] {}, null, UrlValidator.ALLOW_LOCAL_URLS, DomainValidator.getInstance(false, items))); - assertThat(thrown.getMessage(), is(equalTo("DomainValidator disagrees with ALLOW_LOCAL_URLS setting"))); + assertEquals("DomainValidator disagrees with ALLOW_LOCAL_URLS setting", thrown.getMessage()); } }