Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade PESEL to version 2.0.1 #5

Merged
merged 2 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jacocoTestReport {
}

group 'io.github.viepovsky'
version '2.0.0'
version '2.0.1'

repositories {
mavenCentral()
Expand All @@ -39,7 +39,7 @@ publishing {
mavenJava(MavenPublication) {
groupId = 'io.github.viepovsky'
artifactId = 'pesel'
version = "2.0.0"
version = "2.0.1"
from components.java

pom {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.viepovsky.polishutils.pesel;

/**
* Checked exception thrown when an invalid PESEL number is encountered.
* Unchecked exception thrown when an invalid PESEL number is encountered.
* Is thrown when the PESEL:
* <ul>
* <li>has an invalid length (other than 11)
Expand All @@ -10,7 +10,7 @@
* <li>has an invalid birth date (e.g. the 32nd of any month)
* </ul>
*/
public class InvalidPeselException extends Exception {
public class InvalidPeselException extends RuntimeException {
/**
* Constructs a {@code InvalidPeselException} with the specified
* detail message.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class Pesel extends PeselNumbers {
* @param pesel the PESEL number
* @throws InvalidPeselException if the given PESEL is invalid
*/
public Pesel(String pesel) throws InvalidPeselException {
public Pesel(String pesel) {
this(new PeselDecoder(), pesel);
}

Expand All @@ -59,7 +59,7 @@ public Pesel(String pesel) throws InvalidPeselException {
* @param pesel the PESEL number
* @throws InvalidPeselException if the given PESEL is invalid
*/
private Pesel(PeselDecoder peselDecoder, String pesel) throws InvalidPeselException {
private Pesel(PeselDecoder peselDecoder, String pesel) {
super(pesel);
PeselValidator.assertIsValid(pesel);
this.birthDate = peselDecoder.decodeBirthDate(pesel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public class PeselGenerator {
/**
* The minimum date range from which a PESEL number will be generated.
*/
private LocalDate minDate;
private final LocalDate minDate;
/**
* The maximum date range to which a PESEL number will be generated.
*/
private LocalDate maxDate;
private final LocalDate maxDate;

/**
* Creates a new instance of the {@code PeselGenerator} class with the specified {@code PeselGeneratorParams}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class PeselGeneratorParams {
/**
* The gender that will be included in the generated PESEL numbers.
*/
private Gender gender;
private final Gender gender;
/**
* The earliest date from which a PESEL number will be generated.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class PeselNumbers {
* @param pesel the PESEL number
* @throws InvalidPeselException if the given PESEL is invalid
*/
PeselNumbers(String pesel) throws InvalidPeselException {
PeselNumbers(String pesel) {
PeselValidator.assertIsNotNull(pesel);
PeselValidator.assertIsLengthValid(pesel);
PeselValidator.assertIsOnlyDigits(pesel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static boolean isValid(String pesel) {
* @param pesel the PESEL number
* @throws InvalidPeselException if the given PESEL is invalid
*/
public static void assertIsValid(String pesel) throws InvalidPeselException {
public static void assertIsValid(String pesel) {
assertIsNotNull(pesel);
assertIsLengthValid(pesel);
assertIsOnlyDigits(pesel);
Expand All @@ -74,9 +74,9 @@ static void assertIsNotNull(String pesel) {
* @param pesel the PESEL number
* @throws InvalidPeselException if given PESEL length is other than 11
*/
static void assertIsLengthValid(String pesel) throws InvalidPeselException {
static void assertIsLengthValid(String pesel) {
if (pesel.length() != 11) {
throw new InvalidPeselException("PESEL length is invalid");
throw new InvalidPeselException("PESEL length is invalid, should be 11 numbers");
}
}

Expand All @@ -86,9 +86,9 @@ static void assertIsLengthValid(String pesel) throws InvalidPeselException {
* @param pesel the PESEL number
* @throws InvalidPeselException if given PESEL has other characters than digits
*/
static void assertIsOnlyDigits(String pesel) throws InvalidPeselException {
static void assertIsOnlyDigits(String pesel) {
if (!pesel.matches("[0-9]*")) {
throw new InvalidPeselException("PESEL contains invalid characters");
throw new InvalidPeselException("PESEL contains invalid characters, should contain only digits 0-9");
}
}

Expand All @@ -98,7 +98,7 @@ static void assertIsOnlyDigits(String pesel) throws InvalidPeselException {
* @param pesel the PESEL number
* @throws InvalidPeselException if given PESEL has invalid control number
*/
static void assertIsControlDigitValid(String pesel) throws InvalidPeselException {
static void assertIsControlDigitValid(String pesel) {
if (!isControlDigitValid(pesel)) {
throw new InvalidPeselException("PESEL control sum number is invalid");
}
Expand Down Expand Up @@ -127,7 +127,7 @@ private static boolean isControlDigitValid(String pesel) {
* @param pesel the PESEL number
* @throws InvalidPeselException if the given PESEL date of birth is invalid
*/
static void assertIsBirthDateValid(String pesel) throws InvalidPeselException {
static void assertIsBirthDateValid(String pesel) {
if (!isBirthDateValid(pesel)) {
throw new InvalidPeselException("PESEL birth date is invalid");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class PeselGeneratorTest {
@ParameterizedTest
@MethodSource("providePesels")
void should_generate_pesel_with_given_params(LocalDate minDate, LocalDate maxDate) throws InvalidPeselException {
void should_generate_pesel_with_given_params(LocalDate minDate, LocalDate maxDate) {
PeselGeneratorParams.Gender gender = PeselGeneratorParams.Gender.FEMALE;

var params = PeselGeneratorParams.builder()
Expand Down Expand Up @@ -45,7 +45,7 @@ private static Stream<Arguments> providePesels() {
}

@RepeatedTest(100)
void should_generate_only_female_pesel() throws InvalidPeselException {
void should_generate_only_female_pesel() {
var params = PeselGeneratorParams.builder()
.gender(PeselGeneratorParams.Gender.FEMALE)
.build();
Expand All @@ -60,7 +60,7 @@ void should_generate_only_female_pesel() throws InvalidPeselException {
}

@RepeatedTest(100)
void should_generate_only_male_pesel() throws InvalidPeselException {
void should_generate_only_male_pesel() {
var params = PeselGeneratorParams.builder()
.gender(PeselGeneratorParams.Gender.MALE)
.build();
Expand All @@ -75,7 +75,7 @@ void should_generate_only_male_pesel() throws InvalidPeselException {
}

@RepeatedTest(100)
void should_generate_pesel_with_default_params() throws InvalidPeselException {
void should_generate_pesel_with_default_params() {
var params = PeselGeneratorParams.builder().build();
var generator = new PeselGenerator(params);
String generatedPesel = generator.generatePesel();
Expand Down Expand Up @@ -114,7 +114,7 @@ void should_swap_min_and_max_date_if_put_in_incorrect_way() {
}

@RepeatedTest(100)
void should_generate_correct_pesel_if_only_min_param_is_given() throws InvalidPeselException {
void should_generate_correct_pesel_if_only_min_param_is_given() {
LocalDate minDate = LocalDate.of(2200, 1, 1);
var params = PeselGeneratorParams.builder()
.minDate(minDate)
Expand All @@ -131,7 +131,7 @@ void should_generate_correct_pesel_if_only_min_param_is_given() throws InvalidPe
}

@RepeatedTest(100)
void should_generate_correct_pesel_if_only_max_param_is_given() throws InvalidPeselException {
void should_generate_correct_pesel_if_only_max_param_is_given() {
LocalDate maxDate = LocalDate.of(2200, 1, 1);
var params = PeselGeneratorParams.builder()
.maxDate(maxDate)
Expand All @@ -148,7 +148,7 @@ void should_generate_correct_pesel_if_only_max_param_is_given() throws InvalidPe
}

@Test
void should_generate_pesel_with_given_date() throws InvalidPeselException {
void should_generate_pesel_with_given_date() {
LocalDate minDate = LocalDate.of(1955, 11, 22);
LocalDate maxDate = LocalDate.of(1955, 11, 22);
var params = PeselGeneratorParams.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private static Stream<Arguments> provideInvalidPesels() {

@ParameterizedTest
@MethodSource("providePesels")
void should_return_correct_birth_date(String givenParam, LocalDate expectedDate) throws InvalidPeselException {
void should_return_correct_birth_date(String givenParam, LocalDate expectedDate) {
var pesel = new Pesel(givenParam);
assertEquals(expectedDate, pesel.getBirthDate());
}
Expand Down Expand Up @@ -70,7 +70,7 @@ private static Stream<Arguments> providePesels() {
"81122018287",
"64031643742"
})
void should_return_gender_female(String givenPesel) throws InvalidPeselException {
void should_return_gender_female(String givenPesel) {
var pesel = new Pesel(givenPesel);
assertEquals(Pesel.Gender.FEMALE.toString(), pesel.getGender());
}
Expand All @@ -88,13 +88,13 @@ void should_return_gender_female(String givenPesel) throws InvalidPeselException
"66020829795",
"74040152795"
})
void should_return_gender_male(String givenPesel) throws InvalidPeselException {
void should_return_gender_male(String givenPesel) {
var pesel = new Pesel(givenPesel);
assertEquals(Pesel.Gender.MALE.toString(), pesel.getGender());
}

@Test
void should_get_correct_pesel_numbers() throws InvalidPeselException {
void should_get_correct_pesel_numbers() {
var pesel = new Pesel("74040152795");

int retrievedBirthDate = pesel.getBirthDateNumbers();
Expand Down