This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from vanherpen/master
Adds validation of Lithuanian license plates.
- Loading branch information
Showing
4 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/net/contargo/types/truck/LithuanianLicensePlateHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/test/java/net/contargo/types/truck/LithuanianLicensePlateHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |