WIP Java8 types - ScalarTypeDuration etc

This commit is contained in:
rbygrave
2014-11-17 00:05:20 +13:00
parent 4a882b537c
commit 7f30dc7b1e
59 changed files with 1857 additions and 558 deletions
@@ -0,0 +1,44 @@
package com.avaje.ebeaninternal.server.type;
import org.junit.Test;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.Duration;
import java.time.Instant;
import static org.junit.Assert.assertEquals;
public class DecimalUtilsTest {
@Test
public void testToDecimal() throws Exception {
Instant now = Instant.now();
BigDecimal value = DecimalUtils.toDecimal(now);
Instant instant = DecimalUtils.toInstant(value);
Timestamp timestamp = DecimalUtils.toTimestamp(value);
BigDecimal decimal = DecimalUtils.toDecimal(timestamp);
Instant instant1 = timestamp.toInstant();
assertEquals(instant, instant1);
assertEquals(value, decimal);
assertEquals(now, instant);
}
@Test
public void testDuration() throws Exception {
Duration duration = Duration.ofSeconds(323, 1500000);
BigDecimal bigDecimal = DecimalUtils.toDecimal(duration);
Duration duration1 = DecimalUtils.toDuration(bigDecimal);
assertEquals(duration, duration1);
assertEquals("PT5M23.0015S", duration1.toString());
}
}
@@ -0,0 +1,121 @@
package com.avaje.ebeaninternal.server.type;
import com.avaje.ebean.text.TextException;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import org.junit.Test;
import java.io.*;
import java.time.Duration;
import static org.junit.Assert.*;
public class ScalarTypeDurationTest {
ScalarTypeDuration type = new ScalarTypeDuration();
@Test
public void testReadData() throws Exception {
Duration duration = Duration.ofSeconds(1234);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(os);
type.writeData(out, duration);
type.writeData(out, null);
out.flush();
out.close();
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
ObjectInputStream in = new ObjectInputStream(is);
Duration val1 = type.readData(in);
Duration val2 = type.readData(in);
assertEquals(duration, val1);
assertNull(val2);
}
@Test
public void testToJdbcType() throws Exception {
Duration duration = Duration.ofSeconds(1234);
long seconds = duration.getSeconds();
Object val1 = type.toJdbcType(duration);
Object val2 = type.toJdbcType(seconds);
assertEquals(seconds, val1);
assertEquals(seconds, val2);
}
@Test
public void testToBeanType() throws Exception {
Duration duration = Duration.ofSeconds(1234);
long seconds = duration.getSeconds();
Duration val1 = type.toBeanType(duration);
Duration val2 = type.toBeanType(seconds);
Duration val3 = type.toBeanType((int)seconds);
assertEquals(duration, val1);
assertEquals(duration, val2);
assertEquals(duration, val3);
}
@Test
public void testFormatValue() throws Exception {
Duration duration = Duration.ofSeconds(1234);
String formatValue = type.formatValue(duration);
assertEquals("PT20M34S", formatValue);
}
@Test
public void testParse() throws Exception {
Duration duration = type.parse("PT20M34S");
assertEquals(Duration.ofSeconds(1234), duration);
}
@Test
public void testIsDateTimeCapable() throws Exception {
assertFalse(type.isDateTimeCapable());
}
@Test(expected = TextException.class)
public void testConvertFromMillis() throws Exception {
type.convertFromMillis(1000);
}
@Test
public void testJsonRead() throws Exception {
Duration duration = Duration.ofSeconds(1234);
StringWriter writer = new StringWriter();
JsonFactory factory = new JsonFactory();
JsonGenerator generator = factory.createGenerator(writer);
generator.writeStartObject();
type.jsonWrite(generator, "key", duration);
generator.writeEndObject();
generator.flush();
JsonParser parser = factory.createParser(writer.toString());
JsonToken token = parser.nextToken();
assertEquals(JsonToken.START_OBJECT, token);
token = parser.nextToken();
assertEquals(JsonToken.FIELD_NAME, token);
token = parser.nextToken();
Duration val1 = type.jsonRead(parser, token);
assertEquals(duration, val1);
}
}
@@ -0,0 +1,121 @@
package com.avaje.ebeaninternal.server.type;
import com.avaje.ebean.text.TextException;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import org.junit.Test;
import java.io.*;
import java.math.BigDecimal;
import java.time.Duration;
import static org.junit.Assert.*;
public class ScalarTypeDurationWithNanosTest {
ScalarTypeDurationWithNanos type = new ScalarTypeDurationWithNanos();
@Test
public void testReadData() throws Exception {
Duration duration = Duration.ofSeconds(323, 1500000);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(os);
type.writeData(out, duration);
type.writeData(out, null);
out.flush();
out.close();
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
ObjectInputStream in = new ObjectInputStream(is);
Duration val1 = type.readData(in);
Duration val2 = type.readData(in);
assertEquals(duration, val1);
assertNull(val2);
}
@Test
public void testToJdbcType() throws Exception {
Duration duration = Duration.ofSeconds(323, 1500000);
BigDecimal bigDecimal = DecimalUtils.toDecimal(duration);
Object val1 = type.toJdbcType(duration);
Object val2 = type.toJdbcType(bigDecimal);
assertEquals(bigDecimal, val1);
assertEquals(bigDecimal, val2);
}
@Test
public void testToBeanType() throws Exception {
Duration duration = Duration.ofSeconds(323, 1500000);
BigDecimal bigDecimal = DecimalUtils.toDecimal(duration);
Duration val1 = type.toBeanType(duration);
Duration val2 = type.toBeanType(bigDecimal);
assertEquals(duration, val1);
assertEquals(duration, val2);
}
@Test
public void testFormatValue() throws Exception {
Duration duration = Duration.ofSeconds(323, 1500000);
String formatValue = type.formatValue(duration);
assertEquals("PT5M23.0015S", formatValue);
}
@Test
public void testParse() throws Exception {
Duration duration = Duration.ofSeconds(323, 1500000);
Duration val1 = type.parse("PT5M23.0015S");
assertEquals(duration, val1);
}
@Test
public void testIsDateTimeCapable() throws Exception {
assertFalse(type.isDateTimeCapable());
}
@Test(expected = TextException.class)
public void testConvertFromMillis() throws Exception {
type.convertFromMillis(1000);
}
@Test
public void testJsonRead() throws Exception {
Duration duration = Duration.ofSeconds(323, 1500000);
StringWriter writer = new StringWriter();
JsonFactory factory = new JsonFactory();
JsonGenerator generator = factory.createGenerator(writer);
generator.writeStartObject();
type.jsonWrite(generator, "key", duration);
generator.writeEndObject();
generator.flush();
JsonParser parser = factory.createParser(writer.toString());
JsonToken token = parser.nextToken();
assertEquals(JsonToken.START_OBJECT, token);
token = parser.nextToken();
assertEquals(JsonToken.FIELD_NAME, token);
token = parser.nextToken();
Duration val1 = type.jsonRead(parser, token);
assertEquals(duration, val1);
}
}
@@ -0,0 +1,125 @@
package com.avaje.ebeaninternal.server.type;
import com.avaje.ebean.config.JsonConfig;
import com.avaje.ebean.text.TextException;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import org.junit.Test;
import java.io.*;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.Year;
import static org.junit.Assert.*;
public class ScalarTypeInstantTest {
ScalarTypeInstant type = new ScalarTypeInstant(JsonConfig.DateTime.MILLIS);
@Test
public void testReadData() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(os);
Instant now = Instant.now();
type.writeData(out, now);
type.writeData(out, null);
out.flush();
out.close();
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
ObjectInputStream in = new ObjectInputStream(is);
Instant val1 = type.readData(in);
Instant val2 = type.readData(in);
assertEquals(now, val1);
assertNull(val2);
}
@Test
public void testToJdbcType() throws Exception {
Instant now = Instant.now();
Timestamp timestamp = Timestamp.from(now);
Object val1 = type.toJdbcType(now);
Object val2 = type.toJdbcType(timestamp);
assertEquals(timestamp, val1);
assertEquals(timestamp, val2);
}
@Test
public void testToBeanType() throws Exception {
Instant now = Instant.now();
Instant val1 = type.toBeanType(now);
Instant val2 = type.toBeanType(Timestamp.from(now));
assertEquals(now, val1);
assertEquals(now, val2);
}
@Test
public void testFormatValue() throws Exception {
Instant now = Instant.now();
Timestamp timestamp = Timestamp.from(now);
String formatted = type.formatValue(now);
assertEquals(timestamp.toString(), formatted);
}
@Test
public void testParse() throws Exception {
Instant now = Instant.now();
Timestamp timestamp = Timestamp.from(now);
Instant val1 = type.parse(timestamp.toString());
assertEquals(now, val1);
}
@Test
public void testIsDateTimeCapable() throws Exception {
assertTrue(type.isDateTimeCapable());
}
@Test
public void testConvertFromMillis() throws Exception {
Instant now = Instant.now();
Instant val = type.convertFromMillis(now.toEpochMilli());
assertEquals(now, val);
}
@Test
public void testJsonRead() throws Exception {
Instant now = Instant.now();
StringWriter writer = new StringWriter();
JsonFactory factory = new JsonFactory();
JsonGenerator generator = factory.createGenerator(writer);
generator.writeStartObject();
type.jsonWrite(generator, "key", now);
generator.writeEndObject();
generator.flush();
JsonParser parser = factory.createParser(writer.toString());
JsonToken token = parser.nextToken();
assertEquals(JsonToken.START_OBJECT, token);
token = parser.nextToken();
assertEquals(JsonToken.FIELD_NAME, token);
token = parser.nextToken();
Instant val1 = type.jsonRead(parser, token);
assertEquals(now, val1);
}
}
@@ -19,7 +19,7 @@ public class ScalarTypeLocalDateTest {
System.out.println(date);
LocalDate parseDate = type.parseDateTime(millis);
LocalDate parseDate = type.convertFromMillis(millis);
assertEquals(date, parseDate);
}
@@ -1,5 +1,6 @@
package com.avaje.ebeaninternal.server.type;
import com.avaje.ebean.config.JsonConfig;
import org.junit.Test;
import java.sql.Timestamp;
@@ -9,7 +10,7 @@ import static org.junit.Assert.*;
public class ScalarTypeLocalDateTimeTest {
ScalarTypeLocalDateTime type = new ScalarTypeLocalDateTime();
ScalarTypeLocalDateTime type = new ScalarTypeLocalDateTime(JsonConfig.DateTime.MILLIS);
// warm up
LocalDateTime warmUp = LocalDateTime.now();
@@ -20,7 +21,7 @@ public class ScalarTypeLocalDateTimeTest {
warmUp.hashCode();
long now = System.currentTimeMillis();
long toMillis = type.convertToMillis( LocalDateTime.now());
long toMillis = type.convertToMillis(LocalDateTime.now());
assertTrue(toMillis - now < 10);
}
@@ -42,17 +43,22 @@ public class ScalarTypeLocalDateTimeTest {
Object jdbcType = type.toJdbcType(LocalDateTime.now());
assertTrue(jdbcType instanceof Timestamp);
jdbcType = type.toJdbcType(new Timestamp(System.currentTimeMillis()));
assertTrue(jdbcType instanceof Timestamp);
}
@Test
public void testToBeanType() throws Exception {
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
LocalDateTime localDateTime = type.toBeanType(timestamp);
LocalDateTime val1 = type.toBeanType(timestamp);
assertNotNull(val1);
assertNotNull(localDateTime);
Timestamp timestamp1 = type.convertToTimestamp(localDateTime);
LocalDateTime val2 = type.toBeanType(timestamp.toLocalDateTime());
assertNotNull(val2);
Timestamp timestamp1 = type.convertToTimestamp(val1);
assertEquals(timestamp, timestamp1);
}
}
@@ -0,0 +1,119 @@
package com.avaje.ebeaninternal.server.type;
import com.avaje.ebean.text.TextException;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import org.junit.Test;
import java.io.*;
import java.time.LocalTime;
import static org.junit.Assert.*;
public class ScalarTypeLocalTimeTest {
ScalarTypeLocalTime type = new ScalarTypeLocalTime();
@Test
public void testReadData() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(os);
LocalTime localTime = LocalTime.of(9, 23, 45);
type.writeData(out, localTime);
type.writeData(out, null);
out.flush();
out.close();
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
ObjectInputStream in = new ObjectInputStream(is);
LocalTime val1 = type.readData(in);
LocalTime val2 = type.readData(in);
assertEquals(localTime, val1);
assertNull(val2);
}
@Test
public void testToJdbcType() throws Exception {
LocalTime localTime = LocalTime.of(9, 23, 45);
long asNanos = localTime.toNanoOfDay();
Object val1 = type.toJdbcType(localTime);
Object val2 = type.toJdbcType(asNanos);
assertEquals(asNanos, val1);
assertEquals(asNanos, val2);
}
@Test
public void testToBeanType() throws Exception {
LocalTime localTime = LocalTime.of(9, 23, 45);
LocalTime val1 = type.toBeanType(localTime);
LocalTime val2 = type.toBeanType(localTime.toNanoOfDay());
assertEquals(localTime, val1);
assertEquals(localTime, val2);
}
@Test
public void testFormatValue() throws Exception {
LocalTime localTime = LocalTime.of(9, 23, 45);
String formatted = type.formatValue(localTime);
assertEquals("09:23:45", formatted);
}
@Test
public void testParse() throws Exception {
LocalTime localTime = LocalTime.of(9, 23, 45);
LocalTime val1 = type.parse("09:23:45");
assertEquals(localTime, val1);
}
@Test
public void testIsDateTimeCapable() throws Exception {
assertFalse(type.isDateTimeCapable());
}
@Test(expected = TextException.class)
public void testConvertFromMillis() throws Exception {
type.convertFromMillis(1234);
}
@Test
public void testJsonRead() throws Exception {
LocalTime localTime = LocalTime.of(9, 23, 45);
StringWriter writer = new StringWriter();
JsonFactory factory = new JsonFactory();
JsonGenerator generator = factory.createGenerator(writer);
generator.writeStartObject();
type.jsonWrite(generator, "key", localTime);
generator.writeEndObject();
generator.flush();
JsonParser parser = factory.createParser(writer.toString());
JsonToken token = parser.nextToken();
assertEquals(JsonToken.START_OBJECT, token);
token = parser.nextToken();
assertEquals(JsonToken.FIELD_NAME, token);
token = parser.nextToken();
LocalTime val1 = type.jsonRead(parser, token);
assertEquals(localTime, val1);
}
}
@@ -1,9 +1,9 @@
package com.avaje.ebeaninternal.server.type;
import com.avaje.ebean.config.JsonConfig;
import org.junit.Test;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import static org.junit.Assert.*;
@@ -12,7 +12,7 @@ import static org.junit.Assert.assertEquals;
public class ScalarTypeOffsetDateTimeTest {
ScalarTypeOffsetDateTime type = new ScalarTypeOffsetDateTime();
ScalarTypeOffsetDateTime type = new ScalarTypeOffsetDateTime(JsonConfig.DateTime.MILLIS);
OffsetDateTime warmUp = OffsetDateTime.now();
@@ -22,7 +22,7 @@ public class ScalarTypeOffsetDateTimeTest {
warmUp.hashCode();
long now = System.currentTimeMillis();
long toMillis = type.convertToMillis( OffsetDateTime.now());
long toMillis = type.convertToMillis(OffsetDateTime.now());
assertTrue(toMillis - now < 10);
@@ -50,7 +50,7 @@ public class ScalarTypePostgresHstoreTest {
@Test(expected = RuntimeException.class)
public void testParseDateTime() throws Exception {
Map<String,Object> map = (Map<String,Object>)hstore.parseDateTime(1234L);
Map<String,Object> map = (Map<String,Object>)hstore.convertFromMillis(1234L);
assertEquals(1, map.size());
assertEquals("rob", map.get("name"));
}
@@ -0,0 +1,114 @@
package com.avaje.ebeaninternal.server.type;
import com.avaje.ebean.text.TextException;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import org.junit.Test;
import java.io.*;
import java.time.Year;
import static org.junit.Assert.*;
public class ScalarTypeYearTest {
ScalarTypeYear type = new ScalarTypeYear();
@Test
public void testReadData() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(os);
type.writeData(out, Year.of(2013));
type.writeData(out, null);
out.flush();
out.close();
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
ObjectInputStream in = new ObjectInputStream(is);
Year year1 = type.readData(in);
Year year2 = type.readData(in);
assertEquals(Year.of(2013), year1);
assertNull(year2);
}
@Test
public void testToJdbcType() throws Exception {
Integer year = 2013;
Object val1 = type.toJdbcType(Year.of(2013));
Object val2 = type.toJdbcType(2013);
Object val3 = type.toJdbcType(2013L);
assertEquals(year, val1);
assertEquals(year, val2);
assertEquals(year, val3);
}
@Test
public void testToBeanType() throws Exception {
Year year = Year.of(2013);
Year val1 = type.toBeanType(year);
Year val2 = type.toBeanType(2013);
Year val3 = type.toBeanType(2013L);
assertEquals(year, val1);
assertEquals(year, val2);
assertEquals(year, val3);
}
@Test
public void testFormatValue() throws Exception {
String formatted = type.formatValue(Year.of(2013));
assertEquals("2013", formatted);
}
@Test
public void testParse() throws Exception {
Year year = type.parse("2013");
assertEquals(Year.of(2013), year);
}
@Test
public void testIsDateTimeCapable() throws Exception {
assertFalse(type.isDateTimeCapable());
}
@Test(expected = TextException.class)
public void testConvertFromMillis() throws Exception {
type.convertFromMillis(1000);
}
@Test
public void testJsonRead() throws Exception {
StringWriter writer = new StringWriter();
JsonFactory factory = new JsonFactory();
JsonGenerator generator = factory.createGenerator(writer);
generator.writeStartObject();
type.jsonWrite(generator, "key", Year.of(2013));
generator.writeEndObject();
generator.flush();
JsonParser parser = factory.createParser(writer.toString());
JsonToken token = parser.nextToken();
assertEquals(JsonToken.START_OBJECT, token);
token = parser.nextToken();
assertEquals(JsonToken.FIELD_NAME, token);
token = parser.nextToken();
Year year = type.jsonRead(parser, token);
assertEquals(Year.of(2013), year);
}
}