mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Add MonthDay support, ScalarTypeMonthDay
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.time.LocalDate;
|
||||
import java.time.MonthDay;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class ScalarTypeMonthDayTest {
|
||||
|
||||
ScalarTypeMonthDay type = new ScalarTypeMonthDay();
|
||||
|
||||
@Test
|
||||
public void testToJdbcType() throws Exception {
|
||||
|
||||
MonthDay value = MonthDay.of(4, 29);
|
||||
Date date = Date.valueOf(LocalDate.of(2000, 4, 29));
|
||||
|
||||
Object val1 = type.toJdbcType(value);
|
||||
Object val2 = type.toJdbcType(date);
|
||||
|
||||
assertEquals(date, val1);
|
||||
assertEquals(date, val2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToBeanType() throws Exception {
|
||||
|
||||
MonthDay value = MonthDay.of(4, 29);
|
||||
Date date = Date.valueOf(LocalDate.of(2000, 4, 29));
|
||||
|
||||
MonthDay val1 = type.toBeanType(value);
|
||||
MonthDay val2 = type.toBeanType(date);
|
||||
|
||||
assertEquals(value, val1);
|
||||
assertEquals(value, val2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatParse() throws Exception {
|
||||
|
||||
MonthDay value = MonthDay.of(4, 29);
|
||||
String val1 = type.formatValue(value);
|
||||
MonthDay monthDay = type.parse(val1);
|
||||
|
||||
assertEquals("--04-29", val1);
|
||||
assertEquals(value, monthDay);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testIsDateTimeCapable() throws Exception {
|
||||
assertFalse(type.isDateTimeCapable());
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void testConvertFromMillis() throws Exception {
|
||||
type.convertFromMillis(1203);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJson() throws Exception {
|
||||
|
||||
MonthDay value = MonthDay.of(4, 29);
|
||||
JsonTester<MonthDay> jsonTester = new JsonTester<MonthDay>(type);
|
||||
jsonTester.test(value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadWriteData() throws Exception {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,8 @@ public class SomePeriodBean {
|
||||
|
||||
Period period;
|
||||
|
||||
MonthDay anniversary;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
@@ -39,4 +41,12 @@ public class SomePeriodBean {
|
||||
public void setPeriod(Period period) {
|
||||
this.period = period;
|
||||
}
|
||||
|
||||
public MonthDay getAnniversary() {
|
||||
return anniversary;
|
||||
}
|
||||
|
||||
public void setAnniversary(MonthDay anniversary) {
|
||||
this.anniversary = anniversary;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,11 @@ import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.types.SomePeriodBean;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.time.LocalDate;
|
||||
import java.time.MonthDay;
|
||||
import java.time.Period;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
@@ -19,10 +23,12 @@ public class TestPeriodType extends BaseTestCase {
|
||||
|
||||
SomePeriodBean bean = new SomePeriodBean();
|
||||
bean.setPeriod(Period.of(3, 4, 5));
|
||||
bean.setAnniversary(MonthDay.of(4, 29));
|
||||
Ebean.save(bean);
|
||||
|
||||
SomePeriodBean bean1 = Ebean.find(SomePeriodBean.class, bean.getId());
|
||||
assertEquals(bean.getPeriod(), bean1.getPeriod());
|
||||
assertEquals(bean.getAnniversary(), bean1.getAnniversary());
|
||||
|
||||
// insert fetch null value
|
||||
SomePeriodBean bean2 = new SomePeriodBean();
|
||||
@@ -30,6 +36,37 @@ public class TestPeriodType extends BaseTestCase {
|
||||
|
||||
SomePeriodBean bean3 = Ebean.find(SomePeriodBean.class, bean2.getId());
|
||||
assertNull(bean3.getPeriod());
|
||||
assertNull(bean3.getAnniversary());
|
||||
|
||||
List<SomePeriodBean> anniversaryList = Ebean.find(SomePeriodBean.class)
|
||||
.where()
|
||||
.eq("anniversary", MonthDay.of(4, 29))
|
||||
.eq("period_years", 3)
|
||||
.eq("period_months", 4)
|
||||
.findList();
|
||||
|
||||
assertEquals(1, anniversaryList.size());
|
||||
|
||||
// must use year 2000 for range predicates
|
||||
// ... using 2001 here so not finding anything
|
||||
anniversaryList = Ebean.find(SomePeriodBean.class)
|
||||
.where()
|
||||
.gt("anniversary", Date.valueOf(LocalDate.of(2001,4, 29)))
|
||||
.findList();
|
||||
|
||||
assertEquals(0, anniversaryList.size());
|
||||
|
||||
// can use year 2000 for range predicates
|
||||
// ... and can use LocalDate to bind
|
||||
anniversaryList = Ebean.find(SomePeriodBean.class)
|
||||
.where()
|
||||
.gt("anniversary", LocalDate.of(2000,4, 22))
|
||||
.findList();
|
||||
|
||||
assertEquals(1, anniversaryList.size());
|
||||
|
||||
Ebean.delete(bean);
|
||||
Ebean.delete(bean2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user