Merge pull request #11123 from hkhan/JAVA-6221-fix-TimeApi-unit-test
[JAVA-6221] Fix TimeApi unit test
This commit is contained in:
commit
e2b8acdc2e
|
@ -13,12 +13,9 @@ import java.util.stream.IntStream;
|
|||
public class TimeApi {
|
||||
|
||||
public static List<Date> getDatesBetweenUsingJava7(Date startDate, Date endDate) {
|
||||
List<Date> datesInRange = new ArrayList<Date>();
|
||||
Calendar calendar = new GregorianCalendar();
|
||||
calendar.setTime(startDate);
|
||||
|
||||
Calendar endCalendar = new GregorianCalendar();
|
||||
endCalendar.setTime(endDate);
|
||||
List<Date> datesInRange = new ArrayList<>();
|
||||
Calendar calendar = getCalendarWithoutTime(startDate);
|
||||
Calendar endCalendar = getCalendarWithoutTime(endDate);
|
||||
|
||||
while (calendar.before(endCalendar)) {
|
||||
Date result = calendar.getTime();
|
||||
|
@ -40,4 +37,15 @@ public class TimeApi {
|
|||
return startDate.datesUntil(endDate).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static Calendar getCalendarWithoutTime(Date date) {
|
||||
Calendar calendar = new GregorianCalendar();
|
||||
calendar.setTime(date);
|
||||
calendar.set(Calendar.HOUR, 0);
|
||||
calendar.set(Calendar.HOUR_OF_DAY, 0);
|
||||
calendar.set(Calendar.MINUTE, 0);
|
||||
calendar.set(Calendar.SECOND, 0);
|
||||
calendar.set(Calendar.MILLISECOND, 0);
|
||||
return calendar;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
package com.baeldung.java9.time;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Test;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TimeApiUnitTest {
|
||||
|
||||
|
@ -18,19 +19,18 @@ public class TimeApiUnitTest {
|
|||
Date endDate = endCalendar.getTime();
|
||||
|
||||
List<Date> dates = TimeApi.getDatesBetweenUsingJava7(startDate, endDate);
|
||||
assertEquals(dates.size(), 2);
|
||||
|
||||
assertThat(dates).hasSize(2);
|
||||
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
Date date1 = calendar.getTime();
|
||||
assertEquals(dates.get(0).getDay(), date1.getDay());
|
||||
assertEquals(dates.get(0).getMonth(), date1.getMonth());
|
||||
assertEquals(dates.get(0).getYear(), date1.getYear());
|
||||
Date expectedDate1 = calendar.getTime();
|
||||
assertThat(dates.get(0)).isInSameDayAs(expectedDate1);
|
||||
assertThatTimeFieldsAreZero(dates.get(0));
|
||||
|
||||
calendar.add(Calendar.DATE, 1);
|
||||
Date date2 = calendar.getTime();
|
||||
assertEquals(dates.get(1).getDay(), date2.getDay());
|
||||
assertEquals(dates.get(1).getMonth(), date2.getMonth());
|
||||
assertEquals(dates.get(1).getYear(), date2.getYear());
|
||||
Date expectedDate2 = calendar.getTime();
|
||||
assertThat(dates.get(1)).isInSameDayAs(expectedDate2);
|
||||
assertThatTimeFieldsAreZero(dates.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -39,9 +39,8 @@ public class TimeApiUnitTest {
|
|||
LocalDate endDate = LocalDate.now().plusDays(2);
|
||||
|
||||
List<LocalDate> dates = TimeApi.getDatesBetweenUsingJava8(startDate, endDate);
|
||||
assertEquals(dates.size(), 2);
|
||||
assertEquals(dates.get(0), LocalDate.now());
|
||||
assertEquals(dates.get(1), LocalDate.now().plusDays(1));
|
||||
|
||||
assertThat(dates).containsExactly(LocalDate.now(), LocalDate.now().plusDays(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -50,9 +49,15 @@ public class TimeApiUnitTest {
|
|||
LocalDate endDate = LocalDate.now().plusDays(2);
|
||||
|
||||
List<LocalDate> dates = TimeApi.getDatesBetweenUsingJava9(startDate, endDate);
|
||||
assertEquals(dates.size(), 2);
|
||||
assertEquals(dates.get(0), LocalDate.now());
|
||||
assertEquals(dates.get(1), LocalDate.now().plusDays(1));
|
||||
|
||||
assertThat(dates).containsExactly(LocalDate.now(), LocalDate.now().plusDays(1));
|
||||
}
|
||||
|
||||
private static void assertThatTimeFieldsAreZero(Date date) {
|
||||
assertThat(date).hasHourOfDay(0);
|
||||
assertThat(date).hasMinute(0);
|
||||
assertThat(date).hasSecond(0);
|
||||
assertThat(date).hasMillisecond(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue