Skip to content

Commit

Permalink
Port tests to JUnit 5
Browse files Browse the repository at this point in the history
Exception: DomainValidatorStartupTest
  • Loading branch information
garydgregory committed Dec 1, 2023
1 parent 05cd89b commit 66a1c50
Show file tree
Hide file tree
Showing 84 changed files with 5,997 additions and 6,776 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<!-- For testing DomainValidatorStartup -->
Expand Down
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="Gary Gregory">
Fix SpotBugs [ERROR] Medium: Inconsistent synchronization of org.apache.commons.validator.ValidatorAction.jsFunction; locked 62% of time [org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.ValidatorAction] Unsynchronized access at ValidatorAction.java:[line 340]Unsynchronized access at ValidatorAction.java:[line 358]Synchronized access at ValidatorAction.java:[line 393]Synchronized access at ValidatorAction.java:[line 394]Synchronized access at ValidatorAction.java:[line 459]Synchronized access at ValidatorAction.java:[line 461]Synchronized access at ValidatorAction.java:[line 462] IS2_INCONSISTENT_SYNC.
</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">
Port tests to JUnit 5. Exception: DomainValidatorStartupTest.
</action>
<!-- ADD -->
<action type="add" dev="ggregory" due-to="Dependabot, Gary Gregory">
Add github/codeql-action.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,21 @@
import java.io.IOException;
import java.io.InputStream;

import junit.framework.TestCase;

import org.xml.sax.SAXException;

/**
* Consolidates reading in XML config file into parent class.
*/
abstract public class AbstractCommonTest extends TestCase {
abstract public class AbstractCommonTest {

/**
* Resources used for validation tests.
*/
protected ValidatorResources resources;

public AbstractCommonTest(final String string) {
super(string);
}
protected String name;

/**
* Load <code>ValidatorResources</code> from
* validator-numeric.xml.
* Load <code>ValidatorResources</code> from validator-numeric.xml.
*/
protected void loadResources(final String file) throws IOException, SAXException {
// Load resources
Expand Down
36 changes: 18 additions & 18 deletions src/test/java/org/apache/commons/validator/AbstractNumberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@
*/
package org.apache.commons.validator;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;

import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.xml.sax.SAXException;

/**
* Abstracts number unit tests methods.
*/
abstract public class AbstractNumberTest extends AbstractCommonTest {
public abstract class AbstractNumberTest extends AbstractCommonTest {

/**
* The key used to retrieve the set of validation
* rules from the xml file.
* The key used to retrieve the set of validation rules from the xml file.
*/
protected String FORM_KEY;

Expand All @@ -37,21 +41,16 @@ abstract public class AbstractNumberTest extends AbstractCommonTest {
*/
protected String ACTION;

public AbstractNumberTest(final String name) {
super(name);
}

/**
* Load <code>ValidatorResources</code> from
* validator-numeric.xml.
* Load <code>ValidatorResources</code> from validator-numeric.xml.
*/
@Override
@BeforeEach
protected void setUp() throws IOException, SAXException {
// Load resources
loadResources("TestNumber-config.xml");
}

@Override
@AfterEach
protected void tearDown() {
}

Expand Down Expand Up @@ -79,8 +78,8 @@ public void testNumberFailure() throws ValidatorException {
/**
* Utility class to run a test on a value.
*
* @param info Value to run test on.
* @param passed Whether or not the test is expected to pass.
* @param info Value to run test on.
* @param passed Whether or not the test is expected to pass.
*/
protected void valueTest(final Object info, final boolean passed) throws ValidatorException {
// Construct validator based on the loaded resources
Expand All @@ -97,13 +96,14 @@ protected void valueTest(final Object info, final boolean passed) throws Validat
// throw this
final ValidatorResults results = validator.validate();

assertNotNull("Results are null.", results);
assertNotNull(results, "Results are null.");

final ValidatorResult result = results.getValidatorResult("value");

assertNotNull(ACTION + " value ValidatorResult should not be null.", result);
assertTrue(ACTION + " value ValidatorResult should contain the '" + ACTION + "' action.", result.containsAction(ACTION));
assertTrue(ACTION + " value ValidatorResult for the '" + ACTION + "' action should have " + (passed ? "passed" : "failed") + ".", passed ? result.isValid(ACTION) : !result.isValid(ACTION));
assertNotNull(result, () -> ACTION + " value ValidatorResult should not be null.");
assertTrue(result.containsAction(ACTION), () -> ACTION + " value ValidatorResult should contain the '" + ACTION + "' action.");
assertTrue(passed ? result.isValid(ACTION) : !result.isValid(ACTION),
() -> ACTION + " value ValidatorResult for the '" + ACTION + "' action should have " + (passed ? "passed" : "failed") + ".");
}

}
5 changes: 2 additions & 3 deletions src/test/java/org/apache/commons/validator/ByteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
*/
package org.apache.commons.validator;

import org.junit.Test;
import org.junit.jupiter.api.Test;

/**
* Performs Validation Test for <code>byte</code> validations.
*/
public class ByteTest extends AbstractNumberTest {

public ByteTest(final String name) {
super(name);
public ByteTest() {
ACTION = "byte";
FORM_KEY = "byteForm";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,32 @@
*/
package org.apache.commons.validator;

import junit.framework.TestCase;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.Test;
import org.junit.jupiter.api.Test;

/**
* Test the CreditCardValidator class.
*
* @deprecated this test can be removed when the deprecated class is removed
*/
@Deprecated
public class CreditCardValidatorTest extends TestCase {
public class CreditCardValidatorTest {

/**
* Test a custom implementation of CreditCardType.
*/
private static class DinersClub implements CreditCardValidator.CreditCardType {
private static final String PREFIX = "300,301,302,303,304,305,";

@Override
public boolean matches(final String card) {
final String prefix = card.substring(0, 3) + ",";
return PREFIX.contains(prefix) && card.length() == 14;
}
}

private static final String VALID_VISA = "4417123456789113";
private static final String VALID_SHORT_VISA = "4222222222222";
private static final String VALID_AMEX = "378282246310005";
Expand All @@ -47,13 +50,6 @@ public boolean matches(final String card) {

private static final String VALID_DINERS = "30569309025904";

/**
* Constructor for CreditCardValidatorTest.
*/
public CreditCardValidatorTest(final String name) {
super(name);
}

@Test
public void testAddAllowedCardType() {
final CreditCardValidator ccv = new CreditCardValidator(CreditCardValidator.NONE);
Expand All @@ -74,8 +70,8 @@ public void testIsValid() {

assertFalse(ccv.isValid(null));
assertFalse(ccv.isValid(""));
assertFalse(ccv.isValid("123456789012")); // too short
assertFalse(ccv.isValid("12345678901234567890")); // too long
assertFalse(ccv.isValid("123456789012")); // too short
assertFalse(ccv.isValid("12345678901234567890")); // too long
assertFalse(ccv.isValid("4417123456789112"));
assertFalse(ccv.isValid("4417q23456w89113"));
assertTrue(ccv.isValid(VALID_VISA));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,30 @@
*/
package org.apache.commons.validator;

import java.io.InputStream;
import static org.junit.jupiter.api.Assertions.fail;

import junit.framework.TestCase;
import java.io.InputStream;

import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Test custom ValidatorResources.
*/
public class CustomValidatorResourcesTest extends TestCase {

/**
* Constructs a test case with the specified name.
* @param name Name of the test
*/
public CustomValidatorResourcesTest(final String name) {
super(name);
}
public class CustomValidatorResourcesTest {

/**
* Sets up.
*/
@Override
@BeforeEach
protected void setUp() {
}

/**
* Tear Down
*/
@Override
@AfterEach
protected void tearDown() {
}

Expand Down
31 changes: 15 additions & 16 deletions src/test/java/org/apache/commons/validator/DateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
*/
package org.apache.commons.validator;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.util.Locale;

import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.xml.sax.SAXException;

/**
Expand All @@ -28,8 +32,7 @@
public class DateTest extends AbstractCommonTest {

/**
* The key used to retrieve the set of validation
* rules from the xml file.
* The key used to retrieve the set of validation rules from the xml file.
*/
protected String FORM_KEY = "dateForm";

Expand All @@ -38,15 +41,10 @@ public class DateTest extends AbstractCommonTest {
*/
protected String ACTION = "date";

public DateTest(final String name) {
super(name);
}

/**
* Load <code>ValidatorResources</code> from
* validator-numeric.xml.
* Load <code>ValidatorResources</code> from validator-numeric.xml.
*/
@Override
@BeforeEach
protected void setUp() throws IOException, SAXException {
// Load resources
loadResources("DateTest-config.xml");
Expand Down Expand Up @@ -77,8 +75,8 @@ public void testValidDate() throws ValidatorException {
/**
* Utlity class to run a test on a value.
*
* @param info Value to run test on.
* @param passed Whether or not the test is expected to pass.
* @param info Value to run test on.
* @param passed Whether or not the test is expected to pass.
*/
protected void valueTest(final Object info, final boolean passed) throws ValidatorException {
// Construct validator based on the loaded resources
Expand All @@ -96,13 +94,14 @@ protected void valueTest(final Object info, final boolean passed) throws Validat
// throw this
final ValidatorResults results = validator.validate();

assertNotNull("Results are null.", results);
assertNotNull(results, "Results are null.");

final ValidatorResult result = results.getValidatorResult("value");

assertNotNull(ACTION + " value ValidatorResult should not be null.", result);
assertTrue(ACTION + " value ValidatorResult should contain the '" + ACTION + "' action.", result.containsAction(ACTION));
assertTrue(ACTION + " value ValidatorResult for the '" + ACTION + "' action should have " + (passed ? "passed" : "failed") + ".", passed ? result.isValid(ACTION) : !result.isValid(ACTION));
assertNotNull(result, () -> ACTION + " value ValidatorResult should not be null.");
assertTrue(result.containsAction(ACTION), () -> ACTION + " value ValidatorResult should contain the '" + ACTION + "' action.");
assertTrue(passed ? result.isValid(ACTION) : !result.isValid(ACTION),
() -> ACTION + " value ValidatorResult for the '" + ACTION + "' action should have " + (passed ? "passed" : "failed") + ".");
}

}
5 changes: 2 additions & 3 deletions src/test/java/org/apache/commons/validator/DoubleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
*/
package org.apache.commons.validator;

import org.junit.Test;
import org.junit.jupiter.api.Test;

/**
* Performs Validation Test for <code>double</code> validations.
*/
public class DoubleTest extends AbstractNumberTest {

public DoubleTest(final String name) {
super(name);
public DoubleTest() {
ACTION = "double";
FORM_KEY = "doubleForm";
}
Expand Down
Loading

0 comments on commit 66a1c50

Please sign in to comment.