package de.vanitasvitae.imi.codes; import static junit.framework.TestCase.assertEquals; import java.util.ArrayList; import java.util.List; import java.util.Random; import org.junit.Test; public class InputValidatorTest { private static final Random RAND = new Random(); /** * Test, whether {@link InputValidator#validateStudyNumber(String)} accepts 3 digit inputs from the allowed alphabet. * * @throws InvalidOptionException NOT expected. */ @Test public void testValidateStudyNumber() throws InvalidOptionException { String in = generateTestStringOfLength(3); assertEquals("validateStudyNumber must accept valid three digit inputs from the alphabet [a-zA-Z0-9].", new StudyNumber(in), InputValidator.validateStudyNumber(in)); } /** * Test, whether {@link InputValidator#validateStudyNumber(String)} fails for inputs of correct length, but from * illegal characters. * * @throws InvalidOptionException expected */ @Test(expected = InvalidOptionException.class) public void testValidateStudyNumberFailsForInvalidAlphabet() throws InvalidOptionException { String in = "7*A"; // -> InvalidOptionException InputValidator.validateStudyNumber(in); } /** * Test, whether {@link InputValidator#validateStudyNumber(String)} fails for Strings of length 0, 1 or two from * the allowed alphabet. * * @throws InvalidOptionException expected. */ @Test(expected = InvalidOptionException.class) public void testValidateStudyNumberFailsForInputOfInsufficientLength() throws InvalidOptionException { String in = generateTestStringOfLength(RAND.nextInt(3)); // -> InvalidOptionException InputValidator.validateStudyNumber(in); } /** * Test, whether {@link InputValidator#validateStudyNumber(String)} fails for input Strings which are too long. * * @throws InvalidOptionException expected. */ @Test(expected = InvalidOptionException.class) public void testValidateStudyNumberFailsForLongerInput() throws InvalidOptionException { String in = generateTestStringOfLength(RAND.nextInt(50) + 4); // -> InvalidOptionException InputValidator.validateStudyNumber(in); } /* Generates a String of length len which consists of letters from the alphabet [a-zA-Z0-9]. */ private static String generateTestStringOfLength(int len) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i++) { sb.append(InputValidator.ALPHABET.charAt( RAND.nextInt(InputValidator.ALPHABET.length()))); } return sb.toString(); } /** * Test, whether {@link InputValidator#validateSampleType(String)} returns the expected {@link SampleType} for * given input strings. * * @throws InvalidOptionException NOT expected */ @Test public void testValidateSampleType() throws InvalidOptionException { List sampleSampleTypes = new ArrayList<>(); sampleSampleTypes.add(new SampleType("u")); sampleSampleTypes.add(new SampleType("b")); for (SampleType t : sampleSampleTypes) { assertEquals("validateSampleType didn't return expected value for input \"" + t.toString() + "\"", t, InputValidator.validateSampleType(t.toString())); } } /** * Test, whether {@link InputValidator#validateNumberOfCodes(String)} returns the expected {@link Integer}. * In this method, posit * * @throws InvalidOptionException NOT expected */ @Test public void testValidateNumberOfCodes() throws InvalidOptionException { // nextInt(bounds) returns a number between 0 (inclusive) and bound (exclusive), so +1 should be safe. int number = RAND.nextInt(Integer.MAX_VALUE) + 1; String string = Integer.toString(number); assertEquals("validateNumberOfCodes failed to validate input \"" + string + "\"", number, InputValidator.validateNumberOfCodes(string)); } /** * Test, whether {@link InputValidator#validateNumberOfCodes(String)} throws a {@link InvalidOptionException} * for negative numbers and 0. * * @throws InvalidOptionException expected */ @Test(expected = InvalidOptionException.class) public void testValidateNumberOfCodesFailsForNegativeNumbers() throws InvalidOptionException { int number = -RAND.nextInt(Integer.MAX_VALUE); String string = Integer.toString(number); // -> InvalidOptionException InputValidator.validateNumberOfCodes(string); } /** * Test, whether {@link InputValidator#validateNumberOfCodes(String)} throws a {@link InvalidOptionException} * for non-numeric inputs, which cannot be parsed into an {@link Integer}. * * @throws InvalidOptionException expected */ @Test(expected = InvalidOptionException.class) public void testValidateNumberOfCodesFailsForNonNumericInput() throws InvalidOptionException { String string = "notNumeric"; // -> InvalidOptionException InputValidator.validateNumberOfCodes(string); } /** * Test, whether {@link InputValidator#validateNumberOfCodes(String)} throws a {@link InvalidOptionException} * for inputs that exceed the range of an {@link Integer} ({@link Integer#MAX_VALUE}). * * @throws InvalidOptionException expected */ @Test(expected = InvalidOptionException.class) public void testValidateNumberOfCodesFailsForNumbersTooLarge() throws InvalidOptionException { // 10 * Integer.MAX_VALUE String string = "21474836470"; InputValidator.validateNumberOfCodes(string); } }