This repository has been archived by the owner on Aug 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add common JPA date time attribute converters
- Loading branch information
Matt Rich
committed
Apr 8, 2016
1 parent
2516dc8
commit d230243
Showing
6 changed files
with
176 additions
and
4 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
...main/java/uk/gov/justice/services/common/jpa/converter/LocalDatePersistenceConverter.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,22 @@ | ||
package uk.gov.justice.services.common.jpa.converter; | ||
|
||
import javax.persistence.AttributeConverter; | ||
import javax.persistence.Converter; | ||
import java.sql.Date; | ||
import java.time.LocalDate; | ||
|
||
import static java.sql.Date.valueOf; | ||
|
||
@Converter(autoApply = true) | ||
public class LocalDatePersistenceConverter implements AttributeConverter<LocalDate, Date> { | ||
|
||
@Override | ||
public Date convertToDatabaseColumn(final LocalDate entityValue) { | ||
return entityValue != null ? valueOf(entityValue) : null; | ||
} | ||
|
||
@Override | ||
public LocalDate convertToEntityAttribute(final Date databaseValue) { | ||
return databaseValue != null ? databaseValue.toLocalDate() : null; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
.../java/uk/gov/justice/services/common/jpa/converter/LocalDateTimePersistenceConverter.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,22 @@ | ||
package uk.gov.justice.services.common.jpa.converter; | ||
|
||
import javax.persistence.AttributeConverter; | ||
import javax.persistence.Converter; | ||
import java.sql.Timestamp; | ||
import java.time.LocalDateTime; | ||
|
||
import static java.sql.Timestamp.valueOf; | ||
|
||
@Converter(autoApply = true) | ||
public class LocalDateTimePersistenceConverter implements AttributeConverter<LocalDateTime, Timestamp> { | ||
|
||
@Override | ||
public Timestamp convertToDatabaseColumn(final LocalDateTime entityValue) { | ||
return entityValue != null ? valueOf(entityValue) : null; | ||
} | ||
|
||
@Override | ||
public LocalDateTime convertToEntityAttribute(final Timestamp databaseValue) { | ||
return databaseValue != null ? databaseValue.toLocalDateTime() : null; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
.../java/uk/gov/justice/services/common/jpa/converter/LocalDatePersistenceConverterTest.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,62 @@ | ||
package uk.gov.justice.services.common.jpa.converter; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.sql.Date; | ||
import java.time.LocalDate; | ||
import java.time.Month; | ||
import java.util.Calendar; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.Assert.assertNull; | ||
|
||
public class LocalDatePersistenceConverterTest { | ||
|
||
private static final int DAY = 25; | ||
private static final Month MONTH = Month.DECEMBER; | ||
private static final int YEAR = 2016; | ||
|
||
private LocalDatePersistenceConverter localDatePersistenceConverter; | ||
|
||
@Before | ||
public void setup() { | ||
localDatePersistenceConverter = new LocalDatePersistenceConverter(); | ||
} | ||
|
||
@Test | ||
public void shouldReturnValidConvertedDatabaseDate() { | ||
LocalDate date = LocalDate.of(YEAR, MONTH, DAY); | ||
Date convertedDate = localDatePersistenceConverter.convertToDatabaseColumn(date); | ||
|
||
Calendar calendar = Calendar.getInstance(); | ||
calendar.setTime(convertedDate); | ||
assertThat(calendar.get(Calendar.DATE), equalTo(DAY)); | ||
assertThat(calendar.get(Calendar.MONTH), equalTo(MONTH.getValue() - 1)); | ||
assertThat(calendar.get(Calendar.YEAR), equalTo(YEAR)); | ||
} | ||
|
||
@Test | ||
public void shouldReturnNullDatabaseValueWhenGivenNullDate() { | ||
assertNull(localDatePersistenceConverter.convertToDatabaseColumn(null)); | ||
} | ||
|
||
@Test | ||
public void shouldReturnValidConvertedAttributeDate() { | ||
Date date = Date.valueOf(String.format("%s-%s-%s", YEAR, MONTH.getValue() - 1, DAY)); | ||
|
||
LocalDate convertedDate = localDatePersistenceConverter.convertToEntityAttribute(date); | ||
|
||
Calendar calendar = Calendar.getInstance(); | ||
calendar.setTime(date); | ||
assertThat(convertedDate.getDayOfMonth(), equalTo(calendar.get(Calendar.DATE))); | ||
assertThat(convertedDate.getMonth().getValue(), equalTo(calendar.get(Calendar.MONTH) + 1)); | ||
assertThat(convertedDate.getYear(), equalTo(calendar.get(Calendar.YEAR))); | ||
} | ||
|
||
@Test | ||
public void shouldReturnEntityAttributeNullWhenGivenNullDate() { | ||
assertNull(localDatePersistenceConverter.convertToEntityAttribute(null)); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...a/uk/gov/justice/services/common/jpa/converter/LocalDateTimePersistenceConverterTest.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,69 @@ | ||
package uk.gov.justice.services.common.jpa.converter; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.sql.Timestamp; | ||
import java.time.LocalDateTime; | ||
import java.time.Month; | ||
import java.util.Calendar; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.Assert.assertNull; | ||
|
||
public class LocalDateTimePersistenceConverterTest { | ||
|
||
public static final int HOUR = 11; | ||
public static final int MINUTE = 59; | ||
public static final int SECOND = 58; | ||
private static final int DAY = 25; | ||
private static final Month MONTH = Month.DECEMBER; | ||
private static final int YEAR = 2016; | ||
private LocalDateTimePersistenceConverter localDateTimePersistenceConverter; | ||
|
||
@Before | ||
public void setup() { | ||
localDateTimePersistenceConverter = new LocalDateTimePersistenceConverter(); | ||
} | ||
|
||
@Test | ||
public void shouldReturnValidConvertedDatabaseDate() { | ||
LocalDateTime dateTime = LocalDateTime.of(YEAR, MONTH, DAY, HOUR, MINUTE, SECOND); | ||
Timestamp timestamp = localDateTimePersistenceConverter.convertToDatabaseColumn(dateTime); | ||
|
||
Calendar calendar = Calendar.getInstance(); | ||
calendar.setTime(timestamp); | ||
assertThat(calendar.get(Calendar.DATE), equalTo(DAY)); | ||
assertThat(calendar.get(Calendar.MONTH), equalTo(MONTH.getValue() - 1)); | ||
assertThat(calendar.get(Calendar.YEAR), equalTo(YEAR)); | ||
assertThat(calendar.get(Calendar.HOUR), equalTo(HOUR)); | ||
assertThat(calendar.get(Calendar.MINUTE), equalTo(MINUTE)); | ||
assertThat(calendar.get(Calendar.SECOND), equalTo(SECOND)); | ||
} | ||
|
||
@Test | ||
public void shouldReturnNullDatabaseValueWhenGivenNullDate() { | ||
assertNull(localDateTimePersistenceConverter.convertToDatabaseColumn(null)); | ||
} | ||
|
||
@Test | ||
public void shouldReturnValidConvertedAttributeDate() { | ||
Timestamp timestamp = Timestamp.valueOf(String.format("%s-%s-%s %d:%d:%d", YEAR, MONTH.getValue() - 1, DAY, HOUR, MINUTE, SECOND)); | ||
LocalDateTime localDateTime = localDateTimePersistenceConverter.convertToEntityAttribute(timestamp); | ||
|
||
Calendar calendar = Calendar.getInstance(); | ||
calendar.setTime(timestamp); | ||
assertThat(localDateTime.getDayOfMonth(), equalTo(calendar.get(Calendar.DATE))); | ||
assertThat(localDateTime.getMonth().getValue(), equalTo(calendar.get(Calendar.MONTH) + 1)); | ||
assertThat(localDateTime.getYear(), equalTo(calendar.get(Calendar.YEAR))); | ||
assertThat(localDateTime.getHour(), equalTo(calendar.get(Calendar.HOUR))); | ||
assertThat(localDateTime.getMinute(), equalTo(calendar.get(Calendar.MINUTE))); | ||
assertThat(localDateTime.getSecond(), equalTo(calendar.get(Calendar.SECOND))); | ||
} | ||
|
||
@Test | ||
public void shouldReturnEntityAttributeNullWhenGivenNullDate() { | ||
assertNull(localDateTimePersistenceConverter.convertToEntityAttribute(null)); | ||
} | ||
} |
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
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