import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import se.ssns.iban.validator.test.IbanValidator; import javax.inject.Inject; import java.util.HashSet; import java.util.Set; /** * @author Simon Lindgren * @since 2017-01-14. */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath*:**/iban-validator-test.xml"}) public class IbanValidatorUnitTest { @Inject public IbanValidator ibanValidator; @Test public void testValidator() { Set<String> validIbans = new HashSet<>(); validIbans.add("DE44 5001 0517 5407 3249 31"); //German validIbans.add("GR16 0110 1250 0000 0001 2300 695"); //Greece validIbans.add("GB29 NWBK 6016 1331 9268 19"); //United Kingdom validIbans.add("SA03 8000 0000 6080 1016 7519"); //Saudi Arabia validIbans.add("CH93 0076 2011 6238 5295 7"); //Switzerland validIbans.add("TR33 0006 1005 1978 6457 8413 26"); //Turkey for (String validIban : validIbans) { Assert.assertTrue(String.format("Failed to validate valid iban, %s", validIban), ibanValidator.validator(validIban)); } } @Test public void testInvalidValidation() { Set<String> invalidIbans = new HashSet<>(); invalidIbans.add("DE44 Z001 0517 5407 3249 31"); //Only Number allowed invalidIbans.add("GR16 0110 1250 0000 0001 2300 695 654"); //To long invalidIbans.add("GB29 N&BK 6016 1331 9268 19"); //special char invalidIbans.add("BG20 bBBB 6016 1331 9268 19"); //Has to be uppercase invalidIbans.add("BR64 0110 1250 0000 0001 2300 695A c"); //Check digits has to be 39 for (String invalidIban : invalidIbans) { Assert.assertFalse(String.format("Failed to catch invalid iban, %s", invalidIban), ibanValidator.validator(invalidIban)); } } }