Mercurial > stress-tester
changeset 845:80b23882a893
PortValidator bugfix
author | Devel 2 |
---|---|
date | Tue, 23 Jan 2018 12:50:05 +0100 |
parents | 421e04bfc24b |
children | c6bd0e721da3 |
files | stress-tester/src/main/java/com/passus/st/validation/PortValidator.java stress-tester/src/test/java/com/passus/st/validation/PortValidatorTest.java |
diffstat | 2 files changed, 59 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/validation/PortValidator.java Tue Jan 23 12:12:00 2018 +0100 +++ b/stress-tester/src/main/java/com/passus/st/validation/PortValidator.java Tue Jan 23 12:50:05 2018 +0100 @@ -11,13 +11,24 @@ @Override public boolean supports(Class<?> clazz) { - return Integer.class.isAssignableFrom(clazz); + return Number.class.isAssignableFrom(clazz) + || String.class.isAssignableFrom(clazz); } @Override public void validate(Object target, Errors errors) { try { - int targetInt = Type.INTEGER.convert(((Integer) target).intValue()); + if (target == null) { + return; + } + + int targetInt; + if (target instanceof Number) { + targetInt = Type.INTEGER.convert(((Number) target).intValue()); + } else { + targetInt = Integer.parseInt(target.toString()); + } + if (!PortRange.RANGE_FULL.inRange(targetInt)) { errors.reject("Invalid port. " + "Value should be in range " + PortRange.MIN_VALUE + "-" + PortRange.MAX_VALUE + ".");
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/test/java/com/passus/st/validation/PortValidatorTest.java Tue Jan 23 12:50:05 2018 +0100 @@ -0,0 +1,46 @@ +package com.passus.st.validation; + +import com.passus.config.validation.Errors; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.fail; + +public class PortValidatorTest { + + private final PortValidator validator = new PortValidator(); + + private final Errors errors = new Errors(); + + @DataProvider(name = "portsDefs") + public Object[][] portsDefs() { + return new Object[][]{ + {null, null}, + {1, null}, + {"1", null}, + {0, null}, + {-1, "Invalid port. Value should be in range 0-65535."}, + {"700000", "Invalid port. Value should be in range 0-65535."}, + {"a", "Invalid port."} + }; + } + + @AfterMethod + public void afterMethod() { + errors.clear(); + } + + @Test(dataProvider = "portsDefs") + public void testValidate(Object value, String errorMsg) throws Exception { + validator.validate(value, errors); + if (errorMsg != null) { + if (!errors.hasError()) { + fail("Error message required for '" + value + "'."); + } else { + assertEquals(errorMsg, errors.getAllErrors().get(0).getMessage()); + } + } + } +} \ No newline at end of file