Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #14 from vanherpen/master
Browse files Browse the repository at this point in the history
Adds validation of Lithuanian license plates.
  • Loading branch information
vanherpen authored Jul 18, 2019
2 parents e99c473 + a53f898 commit 4c8ee86
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public enum LicensePlateCountry implements Country {
CZECH_REPUBLIC("CZ", "2H2 7149", new CzechLicensePlateHandler()),
ROMANIA("RO", "B 183 CTL", new RomanianLicensePlateHandler()),
BULGARIA("BG", "CA 7845 XC", new BulgarianLicensePlateHandler()),
LITHUANIA("LT", "KRK 365", new LithuanianLicensePlateHandler()),
UNKNOWN_COUNTRY("", "", new UnknownCountryLicensePlateHandler());

private final String countryCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package net.contargo.types.truck;

/**
* Can handle Lithuanian {@link LicensePlate}s.
*
* <p>Examples of Lithuanian license plates:</p>
*
* <ul>
* <li>KRK 365</li>
* <li>DFZ 289</li>
* </ul>
*
* <p>For more more information: <a href="https://en.wikipedia.org/wiki/Vehicle_registration_plates_of_Lithuania" ></a>
* </p>
*
* <p>Lithiuania also allows so called vanity plates. Those are not correctly validated, as they don't follow the
* structure of a normal license plate.</p>
*
* @author Marius van Herpen - [email protected]
*/
public class LithuanianLicensePlateHandler implements LicensePlateHandler {

private static final String WHITESPACE = " ";

/**
* Normalizes the given {@link LicensePlate} value by upper casing it and replacing all hyphens by whitespaces, and
* ensure numbers are seperated from letters.
*
* @param value to get the normalized value for, never {@code null}
*
* @return the normalized value, never {@code null}
*/
@Override
public String normalize(String value) {

return LicensePlateHandler.trim(value)
.replaceAll("\\-", WHITESPACE)
.replaceAll("(?<=\\d)(?=\\p{L})|(?<=\\p{L})(?=\\d)", WHITESPACE);
}


/**
* Validates the given {@link LicensePlate} value.
*
* <p>A Lithuan license plate consists of a group of 3 letters and 3 digits. There is also the possibility of a
* vanity plate, which allow any combination of letters and digits, and has no minimum length. Those will not be
* validated.</p>
*
* @param value to be validated, never {@code null}
*
* @return {@code true} if the given {@link LicensePlate} is valid, else {@code false}
*/
@Override
public boolean validate(String value) {

// KRK 565
return normalize(value).matches("[A-Z]{3}\\s[0-9]{3}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public void ensureCorrectHandlerMatching() {
RomanianLicensePlateHandler.class);
assertCorrectHandler.accept(LicensePlateCountry.BULGARIA.getLicensePlateHandler(),
BulgarianLicensePlateHandler.class);
assertCorrectHandler.accept(LicensePlateCountry.LITHUANIA.getLicensePlateHandler(),
LithuanianLicensePlateHandler.class);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package net.contargo.types.truck;

import org.junit.Before;
import org.junit.Test;


/**
* @author Marius van Herpen - [email protected]
*/
public class LithuanianLicensePlateHandlerTest extends AbstractLicensePlateHandlerTest {

@Before
public void setUp() {

handler = new LithuanianLicensePlateHandler();
}


// NORMALIZING -----------------------------------------------------------------------------------------------------

@Test
public void ensureLicensePlateIsNormalizedCorrectly() {

assertIsNormalizedFromTo.accept("abc-123", "ABC 123");
assertIsNormalizedFromTo.accept("abc 123", "ABC 123");
assertIsNormalizedFromTo.accept("abc--123", "ABC 123");
assertIsNormalizedFromTo.accept("abc123", "ABC 123");
}


// VALIDATION ------------------------------------------------------------------------------------------------------

@Test
public void assertCorrectlyGroupedIsValid() {

assertIsValid.accept("ABC 123");
assertIsValid.accept("KRK 365");
assertIsValid.accept("JKS645");
}


@Test
public void assertIncorrectGroupsInvalid() {

assertIsNotValid.accept("645 564");
assertIsNotValid.accept("645 JKZ");
assertIsNotValid.accept("FDS JKR");
assertIsNotValid.accept("JKR 65");
assertIsNotValid.accept("JKR 6435");
assertIsNotValid.accept("JK 645");
assertIsNotValid.accept("JKST 645");
assertIsNotValid.accept("JH-TL-05");
assertIsNotValid.accept("JHTL505");
}
}

0 comments on commit 4c8ee86

Please sign in to comment.