From c5fbba8e73fe71f4831eea44622dd41927001999 Mon Sep 17 00:00:00 2001 From: sergey Date: Thu, 21 Nov 2019 16:16:46 +0300 Subject: [PATCH] Add to birthday() currentMonth and currentDay because it is tests sometimes failed with incorrect date Add to birthday() currentMonth and currentDay because it is tests sometimes failed with incorrect date Fix test coverage --- .../java/com/github/javafaker/DateAndTime.java | 10 ++++++++-- .../com/github/javafaker/DateAndTimeTest.java | 18 +++++++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/github/javafaker/DateAndTime.java b/src/main/java/com/github/javafaker/DateAndTime.java index ed12549c7..45c2fcc11 100644 --- a/src/main/java/com/github/javafaker/DateAndTime.java +++ b/src/main/java/com/github/javafaker/DateAndTime.java @@ -144,6 +144,10 @@ public Date between(Date from, Date to) throws IllegalArgumentException { throw new IllegalArgumentException("Invalid date range, the upper bound date is before the lower bound."); } + if (from.equals(to)) { + return from; + } + long offsetMillis = faker.random().nextLong(to.getTime() - from.getTime()); return new Date(from.getTime() + offsetMillis); } @@ -170,8 +174,10 @@ public Date birthday() { */ public Date birthday(int minAge, int maxAge) { int currentYear = Calendar.getInstance().get(Calendar.YEAR); - Calendar from = new GregorianCalendar(currentYear - maxAge, 0, 1); - Calendar to = new GregorianCalendar(currentYear - minAge, 11, 31); + int currentMonth = Calendar.getInstance().get(Calendar.MONTH); + int currentDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH); + Calendar from = new GregorianCalendar(currentYear - maxAge, currentMonth, currentDay); + Calendar to = new GregorianCalendar(currentYear - minAge, currentMonth, currentDay); return between(from.getTime(), to.getTime()); } diff --git a/src/test/java/com/github/javafaker/DateAndTimeTest.java b/src/test/java/com/github/javafaker/DateAndTimeTest.java index 4c014e538..2d3eb336c 100644 --- a/src/test/java/com/github/javafaker/DateAndTimeTest.java +++ b/src/test/java/com/github/javafaker/DateAndTimeTest.java @@ -84,10 +84,12 @@ public void testBetween() { @Test public void testBirthday() { int currentYear = Calendar.getInstance().get(Calendar.YEAR); - long from = new GregorianCalendar(currentYear - 65, 0, 1).getTime().getTime(); - long to = new GregorianCalendar(currentYear - 18, 11, 31).getTime().getTime(); + int currentMonth = Calendar.getInstance().get(Calendar.MONTH); + int currentDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH); + long from = new GregorianCalendar(currentYear - 65, currentMonth, currentDay).getTime().getTime(); + long to = new GregorianCalendar(currentYear - 18, currentMonth, currentDay).getTime().getTime(); - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < 5000; i++) { Date birthday = faker.date().birthday(); assertThat("birthday is after upper bound", birthday.getTime(), lessThan(to)); assertThat("birthday is before lower bound", birthday.getTime(), greaterThanOrEqualTo(from)); @@ -97,16 +99,18 @@ public void testBirthday() { @Test public void testBirthdayWithAges() { int currentYear = Calendar.getInstance().get(Calendar.YEAR); + int currentMonth = Calendar.getInstance().get(Calendar.MONTH); + int currentDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH); - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < 5000; i++) { int minAge = faker.number().numberBetween(1, 99); int maxAge = faker.number().numberBetween(minAge, 100); - long from = new GregorianCalendar(currentYear - maxAge, 0, 1).getTime().getTime(); - long to = new GregorianCalendar(currentYear - minAge, 11, 31).getTime().getTime(); + long from = new GregorianCalendar(currentYear - maxAge, currentMonth, currentDay).getTime().getTime(); + long to = new GregorianCalendar(currentYear - minAge, currentMonth, currentDay).getTime().getTime(); Date birthday = faker.date().birthday(minAge, maxAge); - assertThat("birthday is after upper bound", birthday.getTime(), lessThan(to)); + assertThat("birthday is after upper bound", birthday.getTime(), lessThanOrEqualTo(to)); assertThat("birthday is before lower bound", birthday.getTime(), greaterThanOrEqualTo(from)); } }