Skip to content

Commit

Permalink
Make test more readable and maintainable, and less verbose
Browse files Browse the repository at this point in the history
- Use JUnit 5 APIs
- Be consistent throughout tests
  • Loading branch information
garydgregory committed Oct 1, 2024
1 parent 1e50f15 commit f563431
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -548,23 +545,23 @@ 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
public void testValidator473Part2() { // reject null DomainValidator with mismatched allowLocal
final List<DomainValidator.Item> 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
public void testValidator473Part3() { // reject null DomainValidator with mismatched allowLocal
final List<DomainValidator.Item> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -615,23 +612,23 @@ 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
public void testValidator473Part2() { // reject null DomainValidator with mismatched allowLocal
final List<DomainValidator.Item> 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
public void testValidator473Part3() { // reject null DomainValidator with mismatched allowLocal
final List<DomainValidator.Item> 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());
}

}

0 comments on commit f563431

Please sign in to comment.