mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Refactor tests, move internal type tests to ebean-core
This commit is contained in:
+93
@@ -0,0 +1,93 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.core.type.ScalarType;
|
||||
import org.postgresql.util.PGobject;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class BasePlatformArrayTypeFactoryTest {
|
||||
|
||||
void assertBindNullTo_Null(ScalarType<?> type) throws SQLException {
|
||||
|
||||
TestDoubleDataBind bind = new TestDoubleDataBind();
|
||||
type.bind(bind, null);
|
||||
assertTrue(bind.wasSetNull);
|
||||
}
|
||||
|
||||
void assertBindNullTo_EmptyArray(ScalarType<?> type) throws SQLException {
|
||||
|
||||
TestDoubleDataBind bind = new TestDoubleDataBind();
|
||||
type.bind(bind, null);
|
||||
|
||||
assertTrue(bind.wasEmptyArray);
|
||||
}
|
||||
|
||||
void assertBindNullTo_EmptyString(ScalarType<?> type) throws SQLException {
|
||||
|
||||
TestDoubleDataBind bind = new TestDoubleDataBind();
|
||||
type.bind(bind, null);
|
||||
assertTrue(bind.setEmptyString);
|
||||
}
|
||||
|
||||
void assertBindNullTo_PGObjectNull(ScalarType<?> type) throws SQLException {
|
||||
|
||||
TestDoubleDataBind bind = new TestDoubleDataBind();
|
||||
type.bind(bind, null);
|
||||
assertTrue(bind.wasPgoNull);
|
||||
}
|
||||
void assertBindNullTo_PGObjectEmpty(ScalarType<?> type) throws SQLException {
|
||||
|
||||
TestDoubleDataBind bind = new TestDoubleDataBind();
|
||||
type.bind(bind, null);
|
||||
assertTrue(bind.wasPgoEmpty);
|
||||
}
|
||||
|
||||
static class TestDoubleDataBind extends DataBind {
|
||||
|
||||
boolean setEmptyString;
|
||||
boolean wasSetNull;
|
||||
boolean setArray;
|
||||
boolean wasNull;
|
||||
boolean wasEmptyArray;
|
||||
boolean wasPgoNull;
|
||||
boolean wasPgoEmpty;
|
||||
|
||||
TestDoubleDataBind() {
|
||||
super(null, null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNull(int jdbcType) {
|
||||
wasSetNull = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setString(String s) {
|
||||
setEmptyString = "[]".equals(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setObject(Object value) {
|
||||
wasNull = value == null;
|
||||
if (!wasNull) {
|
||||
if (value instanceof Object[]) {
|
||||
wasEmptyArray = ((Object[]) value).length == 0;
|
||||
} else {
|
||||
PGobject pgo = (PGobject) value;
|
||||
wasPgoEmpty = "[]".equals(pgo.getValue());
|
||||
wasPgoNull = pgo.getValue() == null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setArray(String arrayType, Object[] elements) {
|
||||
setArray = true;
|
||||
wasNull = elements == null;
|
||||
wasEmptyArray = (elements != null && elements.length == 0);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.InetAddress;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ConvertInetAddressTest {
|
||||
|
||||
@Test
|
||||
public void forString() {
|
||||
|
||||
InetAddress addr = ConvertInetAddresses.forString("128.1.10.23");
|
||||
assertEquals("128.1.10.23", addr.getHostAddress());
|
||||
|
||||
String ip6addr = "2001:db8:85a3:0:0:8a2e:370:7334";
|
||||
InetAddress addr6 = ConvertInetAddresses.forString(ip6addr);
|
||||
String uriAddr6 = ConvertInetAddresses.toUriString(addr6);
|
||||
assertEquals("[" + ip6addr + "]", uriAddr6);
|
||||
assertEquals(ip6addr, addr6.getHostAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ipv6_fromHost_getHostAddress() {
|
||||
InetAddress addr2 = ConvertInetAddresses.fromHost("2001:4f8:3:ba:2e0:81ff:fe22:d1f1");
|
||||
assertEquals("2001:4f8:3:ba:2e0:81ff:fe22:d1f1", addr2.getHostAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ipv6_fromHost_getHostAddress_2() {
|
||||
InetAddress addr2 = ConvertInetAddresses.fromHost("2001:db8:85a3:0:0:8a2e:370:7334");
|
||||
assertEquals("2001:db8:85a3:0:0:8a2e:370:7334", addr2.getHostAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toUriString() {
|
||||
|
||||
InetAddress addr = ConvertInetAddresses.forString("128.1.10.23");
|
||||
assertEquals("128.1.10.23", addr.getHostAddress());
|
||||
|
||||
String uriAddr = ConvertInetAddresses.toUriString(addr);
|
||||
assertEquals("128.1.10.23", uriAddr);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.InetAddress;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class ConvertInetAddressesTest {
|
||||
|
||||
@Test
|
||||
public void testForString() throws Exception {
|
||||
|
||||
InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
|
||||
|
||||
String uri = ConvertInetAddresses.toUriString(loopbackAddress);
|
||||
InetAddress inetAddress = ConvertInetAddresses.forString(uri);
|
||||
|
||||
assertEquals(loopbackAddress, inetAddress);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsInetAddress() throws Exception {
|
||||
|
||||
assertTrue(ConvertInetAddresses.isInetAddress("127.0.0.1"));
|
||||
|
||||
assertFalse(ConvertInetAddresses.isInetAddress("junk"));
|
||||
assertFalse(ConvertInetAddresses.isInetAddress("127.0.0.junk"));
|
||||
assertFalse(ConvertInetAddresses.isInetAddress("junk.0.0.23"));
|
||||
assertFalse(ConvertInetAddresses.isInetAddress(""));
|
||||
assertFalse(ConvertInetAddresses.isInetAddress("127.0.0"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToUriString() throws Exception {
|
||||
|
||||
InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
|
||||
|
||||
String uri = ConvertInetAddresses.toUriString(loopbackAddress);
|
||||
InetAddress inetAddress = ConvertInetAddresses.forUriString(uri);
|
||||
|
||||
assertEquals(loopbackAddress, inetAddress);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.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,28 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.core.type.ScalarType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Types;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
|
||||
public class DefaultTypeFactoryTest {
|
||||
|
||||
DefaultTypeFactory defaultTypeFactory = new DefaultTypeFactory(null);
|
||||
|
||||
@Test
|
||||
public void testCreateBoolean() throws Exception {
|
||||
|
||||
ScalarType<Boolean> stIntBoolean = defaultTypeFactory.createBoolean("0", "1");
|
||||
assertEquals(Types.INTEGER, stIntBoolean.getJdbcType());
|
||||
|
||||
stIntBoolean = defaultTypeFactory.createBoolean("1", "2");
|
||||
assertEquals(Types.INTEGER, stIntBoolean.getJdbcType());
|
||||
|
||||
ScalarType<Boolean> stStringBoolean = defaultTypeFactory.createBoolean("Y", "N");
|
||||
assertEquals(Types.VARCHAR, stStringBoolean.getJdbcType());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebean.config.dbplatform.postgres.PostgresPlatform;
|
||||
import io.ebean.core.type.ScalarType;
|
||||
import io.ebeaninternal.server.core.bootup.BootupClasses;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Car;
|
||||
import org.tests.model.basic.IntEnum;
|
||||
import org.tests.model.basic.VarcharEnum;
|
||||
|
||||
import javax.persistence.EnumType;
|
||||
import java.time.DayOfWeek;
|
||||
import java.time.Month;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class DefaultTypeManagerTest {
|
||||
|
||||
private DefaultTypeManager create() {
|
||||
DatabaseConfig serverConfig = new DatabaseConfig();
|
||||
serverConfig.setDatabasePlatform(new PostgresPlatform());
|
||||
BootupClasses bootupClasses = new BootupClasses();
|
||||
return new DefaultTypeManager(serverConfig, bootupClasses);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isIntegerType() {
|
||||
DefaultTypeManager typeManager = create();
|
||||
|
||||
assertTrue(typeManager.isIntegerType("1"));
|
||||
assertTrue(typeManager.isIntegerType("0"));
|
||||
|
||||
assertFalse(typeManager.isIntegerType("A"));
|
||||
assertFalse(typeManager.isIntegerType("01"));
|
||||
assertFalse(typeManager.isIntegerType(" 01"));
|
||||
assertFalse(typeManager.isIntegerType(" 0"));
|
||||
assertFalse(typeManager.isIntegerType(" 1"));
|
||||
assertFalse(typeManager.isIntegerType(" A"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void enumDayMonth_builtIn_overrideAsString() {
|
||||
DefaultTypeManager typeManager = create();
|
||||
|
||||
ScalarType<?> type = typeManager.createEnumScalarType(Month.class, null);
|
||||
assertThat(type).isInstanceOf(ScalarTypeEnumWithMapping.class).as("built in type");
|
||||
|
||||
// mapped explicitly as JPA EnumType.STRING
|
||||
type = typeManager.createEnumScalarType(Month.class, EnumType.STRING);
|
||||
assertThat(type).isInstanceOf(ScalarTypeEnumStandard.StringEnum.class).as("override built in type");
|
||||
try {
|
||||
typeManager.createEnumScalarType(Month.class, EnumType.ORDINAL);
|
||||
assertThat(true).isFalse().as("never get here");
|
||||
|
||||
} catch (IllegalStateException e) {
|
||||
assertThat(e.getMessage()).contains("It is mapped using 2 different modes when only one is supported");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void enumMonth_builtIn_overrideAsOrdinal() {
|
||||
DefaultTypeManager typeManager = create();
|
||||
|
||||
// mapped explicitly as JPA EnumType.STRING
|
||||
ScalarType<?> type = typeManager.createEnumScalarType(Month.class, EnumType.ORDINAL);
|
||||
assertThat(type).isInstanceOf(ScalarTypeEnumStandard.OrdinalEnum.class).as("override built in type");
|
||||
try {
|
||||
typeManager.createEnumScalarType(Month.class, EnumType.STRING);
|
||||
assertThat(true).isFalse().as("never get here");
|
||||
} catch (IllegalStateException e) {
|
||||
assertThat(e.getMessage()).contains("It is mapped using 2 different modes when only one is supported");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void enumDayOfWeek_builtIn_overrideAsString() {
|
||||
DefaultTypeManager typeManager = create();
|
||||
|
||||
ScalarType<?> type = typeManager.createEnumScalarType(DayOfWeek.class, null);
|
||||
assertThat(type).isInstanceOf(ScalarTypeEnumWithMapping.class).as("built in type");
|
||||
|
||||
// mapped explicitly as JPA EnumType.STRING
|
||||
type = typeManager.createEnumScalarType(DayOfWeek.class, EnumType.STRING);
|
||||
assertThat(type).isInstanceOf(ScalarTypeEnumStandard.StringEnum.class).as("override built in type");
|
||||
try {
|
||||
typeManager.createEnumScalarType(DayOfWeek.class, EnumType.ORDINAL);
|
||||
assertThat(true).isFalse().as("never get here");
|
||||
} catch (IllegalStateException e) {
|
||||
assertThat(e.getMessage()).contains("It is mapped using 2 different modes when only one is supported");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void enumDayOfWeek_builtIn_overrideAsOrdinal() {
|
||||
DefaultTypeManager typeManager = create();
|
||||
|
||||
// mapped explicitly as JPA EnumType.STRING
|
||||
ScalarType<?> type = typeManager.createEnumScalarType(DayOfWeek.class, EnumType.ORDINAL);
|
||||
assertThat(type).isInstanceOf(ScalarTypeEnumStandard.OrdinalEnum.class).as("override built in type");
|
||||
try {
|
||||
typeManager.createEnumScalarType(DayOfWeek.class, EnumType.STRING);
|
||||
assertThat(true).isFalse().as("never get here");
|
||||
} catch (IllegalStateException e) {
|
||||
assertThat(e.getMessage()).contains("It is mapped using 2 different modes when only one is supported");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createEnumScalarTypePerExtentions() {
|
||||
DefaultTypeManager typeManager = create();
|
||||
|
||||
ScalarType<?> type = typeManager.createEnumScalarType(VarcharEnum.class, EnumType.ORDINAL);
|
||||
assertThat(type).isInstanceOf(ScalarTypeEnumWithMapping.class);
|
||||
// withConstraint false
|
||||
assertThat(((ScalarTypeEnumWithMapping) type).getDbCheckConstraintValues()).isNull();
|
||||
|
||||
type = typeManager.createEnumScalarType(IntEnum.class, EnumType.ORDINAL);
|
||||
assertThat(type).isInstanceOf(ScalarTypeEnumWithMapping.class);
|
||||
ScalarTypeEnumWithMapping enumWithMapping = (ScalarTypeEnumWithMapping) type;
|
||||
// withConstraint true
|
||||
assertThat(enumWithMapping.getDbCheckConstraintValues()).hasSize(3);
|
||||
assertThat(enumWithMapping.getDbCheckConstraintValues()).contains("100", "101", "102");
|
||||
|
||||
type = typeManager.createEnumScalarType(Car.Size.class, EnumType.ORDINAL);
|
||||
assertThat(type).isInstanceOf(ScalarTypeEnumWithMapping.class);
|
||||
enumWithMapping = (ScalarTypeEnumWithMapping) type;
|
||||
// withConstraint true
|
||||
assertThat(enumWithMapping.getDbCheckConstraintValues()).hasSize(2);
|
||||
assertThat(enumWithMapping.getDbCheckConstraintValues()).contains("'L'", "'S'");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class IsoJsonDateTimeParserTest {
|
||||
|
||||
private IsoJsonDateTimeParser parser = new IsoJsonDateTimeParser();
|
||||
|
||||
@Test
|
||||
public void parseFormat_when_hasMillis() {
|
||||
parseAndFormat("2016-02-28T20:39:00.123Z", "2016-02-28T20:39:00.123Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFormat_when_noMillis() {
|
||||
parseAndFormat("2016-02-28T20:39:00Z", "2016-02-28T20:39:00.000Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFormat_when_millis_1dp() {
|
||||
parseAndFormat("2016-02-28T20:39:00.0Z", "2016-02-28T20:39:00.000Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFormat_when_millis_2dp() {
|
||||
parseAndFormat("2016-02-28T20:39:00.00Z", "2016-02-28T20:39:00.000Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFormat_when_millis_3dp() {
|
||||
parseAndFormat("2016-02-28T20:39:00.000Z", "2016-02-28T20:39:00.000Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFormat_when_millis_3dp_2() {
|
||||
parseAndFormat("2016-02-28T20:39:32.999000Z", "2016-02-28T20:39:32.999Z");
|
||||
}
|
||||
|
||||
private void parseAndFormat(String input, String expected) {
|
||||
Instant timestamp = parser.parseIso(input);
|
||||
String format = parser.formatIso(timestamp);
|
||||
assertThat(format).isEqualTo(expected);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
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 io.ebean.config.JsonConfig;
|
||||
import io.ebean.core.type.ScalarType;
|
||||
import io.ebeaninternal.server.text.json.WriteJson;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* Base class to help json testing.
|
||||
*/
|
||||
public class JsonTester<T> {
|
||||
|
||||
protected JsonFactory factory = new JsonFactory();
|
||||
|
||||
protected ScalarType<T> type;
|
||||
|
||||
public JsonTester(ScalarType<T> type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String test(T value) throws IOException {
|
||||
StringWriter writer = new StringWriter();
|
||||
|
||||
JsonGenerator generator = factory.createGenerator(writer);
|
||||
generator.writeStartObject();
|
||||
|
||||
WriteJson writeJson = new WriteJson(generator, JsonConfig.Include.ALL);
|
||||
writeJson.writeFieldName("key");
|
||||
type.jsonWrite(generator, value);
|
||||
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);
|
||||
parser.nextToken();
|
||||
|
||||
T val1 = type.jsonRead(parser);
|
||||
assertEquals(value, val1);
|
||||
|
||||
return writer.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class JsonTrimTest {
|
||||
|
||||
@Test
|
||||
void trim_array() {
|
||||
final String trimmed = JsonTrim.trim("[{\"name\": \"one\", \"along\": 1, \"timestamp\": 1629609021559}, {\"name\": \"two\", \"along\": 2, \"timestamp\": 1629609021559}]");
|
||||
assertThat(trimmed).isEqualTo("[{\"name\":\"one\",\"along\":1,\"timestamp\":1629609021559},{\"name\":\"two\",\"along\":2,\"timestamp\":1629609021559}]");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_object() {
|
||||
final String trimmed = JsonTrim.trim("{\"name\": \"one\",\t \t \"along\": 1, \"timestamp\": 1629609021559}");
|
||||
assertThat(trimmed).isEqualTo("{\"name\":\"one\",\"along\":1,\"timestamp\":1629609021559}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_object_embeddedEscapeSlash() {
|
||||
final String trimmed = JsonTrim.trim("{\"a\": \"o\\nf\",\t \t \n \"b\": 1}");
|
||||
assertThat(trimmed).isEqualTo("{\"a\":\"o\\nf\",\"b\":1}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_escapedTabNewLine() {
|
||||
final String trimmed = JsonTrim.trim("{\"a\": \"o\\t\\nf\",\t \t \n \"b\": 1}");
|
||||
assertThat(trimmed).isEqualTo("{\"a\":\"o\\t\\nf\",\"b\":1}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_leadingEscapedNewLine() {
|
||||
final String trimmed = JsonTrim.trim("{\"a\": \"\\no\\t\\nf\\n\",\t \t \n \"b\": 1}");
|
||||
assertThat(trimmed).isEqualTo("{\"a\":\"\\no\\t\\nf\\n\",\"b\":1}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_trailingEscapedNewLine() {
|
||||
final String trimmed = JsonTrim.trim("{\"a\": \"o\\t\\nf\\n\",\t \t \n \"b\": 1}");
|
||||
assertThat(trimmed).isEqualTo("{\"a\":\"o\\t\\nf\\n\",\"b\":1}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_trailingEscapedSlash() {
|
||||
final String trimmed = JsonTrim.trim("{\"a\": \"o\\t\\nf\\\\\",\t \t \n \"b\": 1}");
|
||||
assertThat(trimmed).isEqualTo("{\"a\":\"o\\t\\nf\\\\\",\"b\":1}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_object_embeddedEscaped() {
|
||||
final String trimmed = JsonTrim.trim("{\"name\": \"one\nfoo\nbar\\bazz\tboo\",\t \t \n \"along\": 1}");
|
||||
assertThat(trimmed).isEqualTo("{\"name\":\"one\nfoo\nbar\\bazz\tboo\",\"along\":1}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void trim_escaped() {
|
||||
final String trimmed = JsonTrim.trim(" \t \n {\"a\": \n \"\\t1\\t2\\n3\\\\\",\t \n \"b\": \"\\t1\\t2\\n3\\\\\" , \t \n \"c\": \"\\t1\\t2\\n3\\\\\" \t \n }");
|
||||
assertThat(trimmed).isEqualTo("{\"a\":\"\\t1\\t2\\n3\\\\\",\"b\":\"\\t1\\t2\\n3\\\\\",\"c\":\"\\t1\\t2\\n3\\\\\"}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebeaninternal.json.ModifyAwareFlag;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ModifyAwareFlagTest {
|
||||
|
||||
@Test
|
||||
public void serialise() throws IOException, ClassNotFoundException {
|
||||
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(os);
|
||||
|
||||
ModifyAwareFlag flag = new ModifyAwareFlag();
|
||||
flag.setMarkedDirty(true);
|
||||
oos.writeObject(flag);
|
||||
oos.flush();
|
||||
oos.close();
|
||||
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
||||
ObjectInputStream ois = new ObjectInputStream(is);
|
||||
|
||||
ModifyAwareFlag read = (ModifyAwareFlag)ois.readObject();
|
||||
assertThat(read.isMarkedDirty()).isTrue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,300 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebeaninternal.json.ModifyAwareList;
|
||||
import io.ebeaninternal.json.ModifyAwareSet;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
|
||||
public class ModifyAwareListTest {
|
||||
|
||||
private ModifyAwareList<String> createList() {
|
||||
return new ModifyAwareList<>(new ArrayList<>(Arrays.asList("A", "B", "C", "D", "E")));
|
||||
}
|
||||
|
||||
private ModifyAwareList<String> createEmptyList() {
|
||||
return new ModifyAwareList<>(new ArrayList<>());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSize() {
|
||||
|
||||
assertEquals(5, createList().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsEmpty() {
|
||||
|
||||
assertFalse(createList().isEmpty());
|
||||
assertTrue(createEmptyList().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContains() {
|
||||
|
||||
assertTrue(createList().contains("B"));
|
||||
assertFalse(createList().contains("Z"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIterator() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
Iterator<String> iterator = list.iterator();
|
||||
assertTrue(iterator.hasNext());
|
||||
assertEquals("A", iterator.next());
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
iterator.remove();
|
||||
assertTrue(list.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToArray() {
|
||||
|
||||
Object[] objects = createList().toArray();
|
||||
assertEquals(5, objects.length);
|
||||
assertEquals("A", objects[0]);
|
||||
assertEquals("E", objects[4]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToArray1() {
|
||||
|
||||
String[] objects = createList().toArray(new String[5]);
|
||||
assertEquals(5, objects.length);
|
||||
assertEquals("A", objects[0]);
|
||||
assertEquals("E", objects[4]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
list.add("F");
|
||||
assertTrue(list.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
list.remove("A");
|
||||
assertTrue(list.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsAll() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
|
||||
assertTrue(list.containsAll(Arrays.asList("A", "B")));
|
||||
assertFalse(list.containsAll(Arrays.asList("A", "B", "Z")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddAll() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
assertTrue(list.addAll(Arrays.asList("F", "G")));
|
||||
assertTrue(list.isMarkedDirty());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testRemoveAll() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
assertTrue(list.removeAll(Arrays.asList("A", "G")));
|
||||
assertTrue(list.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRetainAll() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
assertTrue(list.retainAll(Arrays.asList("A", "B")));
|
||||
assertTrue(list.isMarkedDirty());
|
||||
assertEquals(2, list.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClear() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
list.clear();
|
||||
assertTrue(list.isMarkedDirty());
|
||||
assertEquals(0, list.size());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGet() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
|
||||
assertEquals("A", list.get(0));
|
||||
assertEquals("B", list.get(1));
|
||||
assertEquals("E", list.get(4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSet() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
list.set(0, "Z");
|
||||
assertTrue(list.isMarkedDirty());
|
||||
assertEquals(5, list.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIndexOf() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
assertEquals(2, list.indexOf("C"));
|
||||
assertEquals(-1, list.indexOf("Z"));
|
||||
assertFalse(list.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLastIndexOf() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
assertEquals(2, list.lastIndexOf("C"));
|
||||
assertEquals(-1, list.lastIndexOf("Z"));
|
||||
assertFalse(list.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListIterator() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
ListIterator<String> iterator = list.listIterator();
|
||||
assertTrue(iterator.hasNext());
|
||||
assertEquals("A", iterator.next());
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
iterator.remove();
|
||||
assertTrue(list.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListIterator1() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
ListIterator<String> iterator = list.listIterator(2);
|
||||
assertTrue(iterator.hasNext());
|
||||
assertEquals("C", iterator.next());
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
iterator.remove();
|
||||
assertTrue(list.isMarkedDirty());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubList() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
List<String> sub = list.subList(1, 3);
|
||||
assertEquals("B", sub.get(0));
|
||||
assertEquals("C", sub.get(1));
|
||||
|
||||
assertFalse(list.isMarkedDirty());
|
||||
|
||||
sub.remove("C");
|
||||
assertTrue(list.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAsSet() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
ModifyAwareSet<String> set = list.asSet();
|
||||
assertFalse(set.isMarkedDirty());
|
||||
|
||||
set.add("next");
|
||||
|
||||
assertTrue(set.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serialise() throws IOException, ClassNotFoundException {
|
||||
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(os);
|
||||
|
||||
ModifyAwareList<String> orig = createList();
|
||||
oos.writeObject(orig);
|
||||
oos.flush();
|
||||
oos.close();
|
||||
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
||||
ObjectInputStream ois = new ObjectInputStream(is);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ModifyAwareList<String> read = (ModifyAwareList<String>)ois.readObject();
|
||||
assertThat(read).contains("A", "B", "C", "D", "E");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWhenEqual() {
|
||||
|
||||
ModifyAwareList<String> listA = createList();
|
||||
ModifyAwareList<String> listB = createList();
|
||||
|
||||
assertThat(listA).isEqualTo(listB);
|
||||
assertThat(listA.hashCode()).isEqualTo(listB.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWhenNotEqual() {
|
||||
|
||||
ModifyAwareList<String> listA = createList();
|
||||
ModifyAwareList<String> listB = createList();
|
||||
listB.add("F");
|
||||
|
||||
assertThat(listA).isNotEqualTo(listB);
|
||||
assertThat(listA.hashCode()).isNotEqualTo(listB.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsAndHashCode() throws Exception {
|
||||
ModifyAwareList<String> listA = createEmptyList();
|
||||
ArrayList<String> listB = new ArrayList<>();
|
||||
|
||||
assertThat(listA).isEqualTo(listB);
|
||||
assertThat(listA.hashCode()).isEqualTo(listB.hashCode());
|
||||
|
||||
listA.add("foo");
|
||||
assertThat(listA).isNotEqualTo(listB);
|
||||
assertThat(listA.hashCode()).isNotEqualTo(listB.hashCode());
|
||||
|
||||
listB.add("foo");
|
||||
assertThat(listA).isEqualTo(listB);
|
||||
assertThat(listA.hashCode()).isEqualTo(listB.hashCode());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebeaninternal.json.ModifyAwareMap;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.HashMap;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ModifyAwareMapTest {
|
||||
|
||||
private ModifyAwareMap<String, Integer> createMap() {
|
||||
HashMap<String, Integer> set = new HashMap<>();
|
||||
set.put("A", 1);
|
||||
set.put("B", 2);
|
||||
set.put("C", 3);
|
||||
set.put("D", 4);
|
||||
set.put("E", 5);
|
||||
return new ModifyAwareMap<>(set);
|
||||
}
|
||||
|
||||
private ModifyAwareMap<String, Integer> createEmptyMap() {
|
||||
HashMap<String, Integer> set = new HashMap<>();
|
||||
return new ModifyAwareMap<>(set);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serialise() throws IOException, ClassNotFoundException {
|
||||
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(os);
|
||||
|
||||
oos.writeObject(createMap());
|
||||
oos.flush();
|
||||
oos.close();
|
||||
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
||||
ObjectInputStream ois = new ObjectInputStream(is);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ModifyAwareMap<String, Integer> read = (ModifyAwareMap<String, Integer>) ois.readObject();
|
||||
assertThat(read).containsKeys("A", "B", "C", "D", "E").containsValues(1, 2, 3, 4, 5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWhenEqual() {
|
||||
|
||||
ModifyAwareMap<String, Integer> setA = createMap();
|
||||
ModifyAwareMap<String, Integer> setB = createMap();
|
||||
|
||||
assertThat(setA).isEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isEqualTo(setB.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWhenNotEqual() {
|
||||
|
||||
ModifyAwareMap<String, Integer> setA = createMap();
|
||||
ModifyAwareMap<String, Integer> setB = createMap();
|
||||
setB.put("F", 6);
|
||||
|
||||
assertThat(setA).isNotEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isNotEqualTo(setB.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsAndHashCode() throws Exception {
|
||||
ModifyAwareMap<String, Integer> setA = createEmptyMap();
|
||||
HashMap<String, Integer> setB = new HashMap<>();
|
||||
|
||||
assertThat(setA).isEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isEqualTo(setB.hashCode());
|
||||
|
||||
setA.put("foo", 42);
|
||||
assertThat(setA).isNotEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isNotEqualTo(setB.hashCode());
|
||||
|
||||
setB.put("foo", 42);
|
||||
assertThat(setA).isEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isEqualTo(setB.hashCode());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebeaninternal.json.ModifyAwareSet;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ModifyAwareSetTest {
|
||||
|
||||
private ModifyAwareSet<String> createSet() {
|
||||
HashSet<String> set = new HashSet<>();
|
||||
set.addAll(Arrays.asList("A", "B", "C", "D", "E"));
|
||||
return new ModifyAwareSet<>(set);
|
||||
}
|
||||
|
||||
private ModifyAwareSet<String> createEmptySet() {
|
||||
HashSet<String> set = new HashSet<>();
|
||||
return new ModifyAwareSet<>(set);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serialise() throws IOException, ClassNotFoundException {
|
||||
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(os);
|
||||
|
||||
oos.writeObject(createSet());
|
||||
oos.flush();
|
||||
oos.close();
|
||||
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
||||
ObjectInputStream ois = new ObjectInputStream(is);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ModifyAwareSet<String> read = (ModifyAwareSet<String>)ois.readObject();
|
||||
assertThat(read).contains("A", "B", "C", "D", "E");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWhenEqual() {
|
||||
|
||||
ModifyAwareSet<String> setA = createSet();
|
||||
ModifyAwareSet<String> setB = createSet();
|
||||
|
||||
assertThat(setA).isEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isEqualTo(setB.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWhenNotEqual() {
|
||||
|
||||
ModifyAwareSet<String> setA = createSet();
|
||||
ModifyAwareSet<String> setB = createSet();
|
||||
setB.add("F");
|
||||
|
||||
assertThat(setA).isNotEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isNotEqualTo(setB.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsAndHashCode() throws Exception {
|
||||
ModifyAwareSet<String> setA = createEmptySet();
|
||||
HashSet<String> setB = new HashSet<>();
|
||||
|
||||
assertThat(setA).isEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isEqualTo(setB.hashCode());
|
||||
|
||||
setA.add("foo");
|
||||
assertThat(setA).isNotEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isNotEqualTo(setB.hashCode());
|
||||
|
||||
setB.add("foo");
|
||||
assertThat(setA).isEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isEqualTo(setB.hashCode());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ScalarTypeArrayListH2Test extends BasePlatformArrayTypeFactoryTest {
|
||||
|
||||
private final PlatformArrayTypeFactory factory = ScalarTypeArrayListH2.factory();
|
||||
|
||||
@Test
|
||||
public void notSameInstance() {
|
||||
assertThat(factory.typeFor(UUID.class, true))
|
||||
.isNotSameAs(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sameInstance_when_notNull() {
|
||||
assertThat(factory.typeFor(UUID.class, false))
|
||||
.isSameAs(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sameInstance_when_nullable() {
|
||||
assertThat(factory.typeFor(UUID.class, true))
|
||||
.isSameAs(factory.typeFor(UUID.class, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindNullToEmpty_when_nullableIsFalse() throws SQLException {
|
||||
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Integer.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Long.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Double.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(String.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindNullToNull_when_nullable() throws SQLException {
|
||||
|
||||
assertBindNullTo_Null(factory.typeFor(Integer.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(Long.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(Double.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(String.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(UUID.class, true));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.core.type.DataReader;
|
||||
import io.ebean.core.type.ScalarType;
|
||||
import io.ebean.text.json.EJson;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ScalarTypeArrayListTest extends BasePlatformArrayTypeFactoryTest {
|
||||
|
||||
private final PlatformArrayTypeFactory factory = ScalarTypeArrayList.factory();
|
||||
|
||||
@Test
|
||||
public void notSameInstance() {
|
||||
assertThat(factory.typeFor(UUID.class, true))
|
||||
.isNotSameAs(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sameInstance_when_notNull() {
|
||||
assertThat(factory.typeFor(UUID.class, false))
|
||||
.isSameAs(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sameInstance_when_nullable() {
|
||||
assertThat(factory.typeFor(UUID.class, true))
|
||||
.isSameAs(factory.typeFor(UUID.class, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void read_when_null() throws SQLException {
|
||||
|
||||
DataReader mock = Mockito.mock(DataReader.class);
|
||||
Mockito.when(mock.getArray()).thenReturn(null);
|
||||
|
||||
ScalarType<?> scalarType = ScalarTypeArrayList.factory().typeFor(Long.class, true);
|
||||
scalarType.read(mock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindNullToEmpty_when_nullableIsFalse() throws SQLException {
|
||||
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Integer.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Long.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Double.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(String.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindNullToNull_when_nullable() throws SQLException {
|
||||
|
||||
assertBindNullTo_Null(factory.typeFor(Integer.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(Long.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(Double.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(String.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(UUID.class, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void parse_withUuidType_expect_uuidTypeReturned() {
|
||||
|
||||
ScalarType<?> scalarType = factory.typeFor(UUID.class, true);
|
||||
|
||||
List<UUID> input = new ArrayList<>();
|
||||
input.add(UUID.randomUUID());
|
||||
input.add(UUID.randomUUID());
|
||||
|
||||
String formatToJson = scalarType.format(input);
|
||||
|
||||
Object parsed = scalarType.parse(formatToJson);
|
||||
assertThat(parsed).isEqualTo(input);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void parse_withIntegerType_expect_longIntegerReturned() {
|
||||
|
||||
ScalarType<?> scalarType = factory.typeFor(Integer.class, true);
|
||||
|
||||
List<Integer> input = new ArrayList<>();
|
||||
input.add(2);
|
||||
input.add(4);
|
||||
|
||||
String formatToJson = scalarType.format(input);
|
||||
|
||||
Object parsed = scalarType.parse(formatToJson);
|
||||
assertThat(parsed).isEqualTo(input);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void parse_withLongType_expect_longTypeReturned() {
|
||||
|
||||
ScalarType<?> scalarType = factory.typeFor(Long.class, true);
|
||||
|
||||
List<Long> input = new ArrayList<>();
|
||||
input.add(2L);
|
||||
input.add(4L);
|
||||
|
||||
String formatToJson = scalarType.format(input);
|
||||
|
||||
Object parsed = scalarType.parse(formatToJson);
|
||||
assertThat(parsed).isEqualTo(input);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parse_withDoubleType_expect_longTypeReturned() {
|
||||
|
||||
ScalarType<?> scalarType = factory.typeFor(Double.class, true);
|
||||
|
||||
List<Double> input = new ArrayList<>();
|
||||
input.add(2D);
|
||||
input.add(4D);
|
||||
|
||||
String formatToJson = scalarType.format(input);
|
||||
|
||||
Object parsed = scalarType.parse(formatToJson);
|
||||
assertThat(parsed).isEqualTo(input);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonRead_withUuidType() throws IOException {
|
||||
|
||||
ScalarType<?> scalarType = factory.typeFor(UUID.class, true);
|
||||
|
||||
List<UUID> input = new ArrayList<>();
|
||||
input.add(UUID.randomUUID());
|
||||
input.add(UUID.randomUUID());
|
||||
String asJson = EJson.write(input);
|
||||
|
||||
JsonParser parser = DB.json().createParser(new StringReader(asJson));
|
||||
|
||||
Object parsed = scalarType.jsonRead(parser);
|
||||
assertThat(parsed).isEqualTo(input);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.core.type.ScalarType;
|
||||
import io.ebean.text.json.EJson;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.sql.SQLException;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ScalarTypeArraySetH2Test extends BasePlatformArrayTypeFactoryTest {
|
||||
|
||||
private final PlatformArrayTypeFactory factory = ScalarTypeArraySetH2.factory();
|
||||
|
||||
@Test
|
||||
public void notSameInstance() {
|
||||
assertThat(factory.typeFor(UUID.class, true))
|
||||
.isNotSameAs(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sameInstance_when_notNull() {
|
||||
assertThat(factory.typeFor(UUID.class, false))
|
||||
.isSameAs(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sameInstance_when_nullable() {
|
||||
assertThat(factory.typeFor(UUID.class, true))
|
||||
.isSameAs(factory.typeFor(UUID.class, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindNullToEmpty_when_nullableIsFalse() throws SQLException {
|
||||
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Integer.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Long.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Double.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(String.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindNullToNull_when_nullable() throws SQLException {
|
||||
|
||||
assertBindNullTo_Null(factory.typeFor(Integer.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(Long.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(Double.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(String.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(UUID.class, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void parse_withUuidType_expect_uuidTypeReturned() {
|
||||
|
||||
ScalarType<?> scalarType = factory.typeFor(UUID.class, true);
|
||||
|
||||
Set<UUID> input = new LinkedHashSet<>();
|
||||
input.add(UUID.randomUUID());
|
||||
input.add(UUID.randomUUID());
|
||||
|
||||
String formatToJson = scalarType.format(input);
|
||||
|
||||
Set<UUID> parsed = (Set<UUID>)scalarType.parse(formatToJson);
|
||||
assertThat(parsed).isEqualTo(input);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void parse_withIntegerType_expect_longIntegerReturned() {
|
||||
|
||||
ScalarType<?> scalarType = factory.typeFor(Integer.class, true);
|
||||
|
||||
Set<Integer> input = new LinkedHashSet<>();
|
||||
input.add(2);
|
||||
input.add(4);
|
||||
|
||||
String formatToJson = scalarType.format(input);
|
||||
|
||||
Set<Integer> parsed = (Set<Integer>)scalarType.parse(formatToJson);
|
||||
assertThat(parsed).isEqualTo(input);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void parse_withLongType_expect_longTypeReturned() {
|
||||
|
||||
ScalarType<?> scalarType = factory.typeFor(Long.class, true);
|
||||
|
||||
Set<Long> input = new LinkedHashSet<>();
|
||||
input.add(2L);
|
||||
input.add(4L);
|
||||
|
||||
String formatToJson = scalarType.format(input);
|
||||
|
||||
Set<Long> parsed = (Set<Long>)scalarType.parse(formatToJson);
|
||||
assertThat(parsed).isEqualTo(input);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parse_withDoubleType_expect_longTypeReturned() {
|
||||
|
||||
ScalarType<?> scalarType = factory.typeFor(Double.class, true);
|
||||
|
||||
Set<Double> input = new LinkedHashSet<>();
|
||||
input.add(2D);
|
||||
input.add(4D);
|
||||
|
||||
String formatToJson = scalarType.format(input);
|
||||
|
||||
Object parsed = scalarType.parse(formatToJson);
|
||||
assertThat(parsed).isEqualTo(input);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonRead_withUuidType() throws IOException {
|
||||
|
||||
ScalarType<?> scalarType = factory.typeFor(UUID.class, true);
|
||||
|
||||
Set<UUID> input = new LinkedHashSet<>();
|
||||
input.add(UUID.randomUUID());
|
||||
input.add(UUID.randomUUID());
|
||||
String asJson = EJson.write(input);
|
||||
|
||||
JsonParser parser = DB.json().createParser(new StringReader(asJson));
|
||||
|
||||
Object parsed = scalarType.jsonRead(parser);
|
||||
assertThat(parsed).isEqualTo(input);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ScalarTypeArraySetTest extends BasePlatformArrayTypeFactoryTest {
|
||||
|
||||
private final PlatformArrayTypeFactory factory = ScalarTypeArraySet.factory();
|
||||
|
||||
@Test
|
||||
public void notSameInstance() {
|
||||
assertThat(factory.typeFor(UUID.class, true))
|
||||
.isNotSameAs(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sameInstance_when_notNull() {
|
||||
assertThat(factory.typeFor(UUID.class, false))
|
||||
.isSameAs(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sameInstance_when_nullable() {
|
||||
assertThat(factory.typeFor(UUID.class, true))
|
||||
.isSameAs(factory.typeFor(UUID.class, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindNullToEmpty_when_nullableIsFalse() throws SQLException {
|
||||
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Integer.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Long.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(Double.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(String.class, false));
|
||||
assertBindNullTo_EmptyArray(factory.typeFor(UUID.class, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindNullToNull_when_nullable() throws SQLException {
|
||||
|
||||
assertBindNullTo_Null(factory.typeFor(Integer.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(Long.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(Double.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(String.class, true));
|
||||
assertBindNullTo_Null(factory.typeFor(UUID.class, true));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.text.json.JsonContext;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.TOne;
|
||||
|
||||
import java.sql.Types;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class ScalarTypeBooleanTest {
|
||||
|
||||
JsonContext jsonContext = DB.getDefault().json();
|
||||
|
||||
@Test
|
||||
public void json_true() {
|
||||
|
||||
TOne bean = new TOne();
|
||||
bean.setId(42);
|
||||
bean.setActive(true);
|
||||
|
||||
String json = jsonContext.toJson(bean);
|
||||
TOne tOne = jsonContext.toBean(TOne.class, json);
|
||||
|
||||
Assertions.assertTrue(tOne.isActive());
|
||||
assertEquals(json, "{\"id\":42,\"active\":true}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void json_false() {
|
||||
|
||||
TOne bean = new TOne();
|
||||
bean.setId(42);
|
||||
bean.setActive(false);
|
||||
|
||||
String json = jsonContext.toJson(bean);
|
||||
TOne tOne = jsonContext.toBean(TOne.class, json);
|
||||
|
||||
Assertions.assertFalse(tOne.isActive());
|
||||
assertEquals(json, "{\"id\":42,\"active\":false}");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void IntBoolean_isLogicalBoolean() {
|
||||
|
||||
ScalarTypeBoolean.IntBoolean intBoolean = new ScalarTypeBoolean.IntBoolean(1, 0);
|
||||
assertThat(intBoolean).isInstanceOf(ScalarTypeLogicalType.class);
|
||||
|
||||
assertThat(intBoolean.getLogicalType()).isEqualTo(Types.BOOLEAN);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Types;
|
||||
import java.util.Calendar;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ScalarTypeCalendarTest {
|
||||
|
||||
private ScalarTypeCalendar type = new ScalarTypeCalendar(JsonConfig.DateTime.ISO8601, Types.TIMESTAMP);
|
||||
|
||||
@Test
|
||||
public void toJsonISO8601() {
|
||||
|
||||
Calendar instance = Calendar.getInstance();
|
||||
String asUtc = type.toJsonISO8601(instance);
|
||||
Calendar calendar = type.fromJsonISO8601(asUtc);
|
||||
|
||||
assertThat(instance).isEqualTo(calendar);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Date;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ScalarTypeDateTest {
|
||||
|
||||
private ScalarTypeDate type = new ScalarTypeDate(JsonConfig.Date.MILLIS);
|
||||
|
||||
@Test
|
||||
public void formatParse_PG_DATE_POSITIVE_INFINITY() {
|
||||
|
||||
Date postgresInfinityDate = new Date(9223372036825200000L);
|
||||
|
||||
String format = type.formatValue(postgresInfinityDate);
|
||||
Date parsed = type.parse(format);
|
||||
assertEquals(parsed, postgresInfinityDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void json() throws IOException {
|
||||
|
||||
|
||||
Date val = new Date(1557360000000L);
|
||||
|
||||
JsonTester<Date> jsonMillis = new JsonTester<>(type);
|
||||
assertThat(jsonMillis.test(val)).isEqualTo("{\"key\":1557360000000}");
|
||||
|
||||
JsonTester<Date> jsonIso = new JsonTester<>(new ScalarTypeDate(JsonConfig.Date.ISO8601));
|
||||
Date val1 = jsonIso.type.parse("2019-05-09");
|
||||
assertThat(jsonIso.test(val1)).isEqualTo("{\"key\":\"2019-05-09\"}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.text.TextException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.time.Duration;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
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() {
|
||||
Duration duration = type.parse("PT20M34S");
|
||||
assertEquals(Duration.ofSeconds(1234), duration);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsDateTimeCapable() {
|
||||
assertFalse(type.isDateTimeCapable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromMillis() {
|
||||
assertThrows(TextException.class, () -> type.convertFromMillis(1000));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonRead() throws Exception {
|
||||
|
||||
Duration duration = Duration.ofSeconds(1234);
|
||||
|
||||
JsonTester<Duration> jsonTester = new JsonTester<>(type);
|
||||
jsonTester.test(duration);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.text.TextException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Duration;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
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() {
|
||||
Duration duration = Duration.ofSeconds(323, 1500000);
|
||||
Duration val1 = type.parse("PT5M23.0015S");
|
||||
assertEquals(duration, val1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsDateTimeCapable() {
|
||||
assertFalse(type.isDateTimeCapable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromMillis() {
|
||||
assertThrows(TextException.class, () -> type.convertFromMillis(1000));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonRead() throws Exception {
|
||||
Duration duration = Duration.ofSeconds(323, 1500000);
|
||||
|
||||
JsonTester<Duration> jsonTester = new JsonTester<>(type);
|
||||
jsonTester.test(duration);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class ScalarTypeInstantTest {
|
||||
|
||||
ScalarTypeInstant type = new ScalarTypeInstant(JsonConfig.DateTime.MILLIS);
|
||||
|
||||
private Instant now() {
|
||||
// in JDK11 Instant.now() returns a nanosecond precise instant.
|
||||
// as ScalarTypeInstant is only milliSecond precise, tests may fail with
|
||||
// expected:<2019-02-10T15:39:36.702700200Z> but was:<2019-02-10T15:39:36.702Z>
|
||||
// if we use Instant.now() - so we use this workaround here.
|
||||
return Instant.ofEpochMilli(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadData() throws Exception {
|
||||
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
ObjectOutputStream out = new ObjectOutputStream(os);
|
||||
|
||||
Instant now = 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 = 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 = 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 = now();
|
||||
Timestamp timestamp = Timestamp.from(now);
|
||||
String formatted = type.formatValue(now);
|
||||
assertEquals("" + timestamp.getTime(), formatted);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParse_when_epochMillis() throws Exception {
|
||||
|
||||
Instant now = now();
|
||||
Timestamp timestamp = Timestamp.from(now);
|
||||
Instant val1 = type.parse("" + timestamp.getTime());
|
||||
assertEquals(now, val1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParse_when_timestampForm() throws Exception {
|
||||
|
||||
Instant now = now();
|
||||
Timestamp timestamp = Timestamp.from(now);
|
||||
Instant val1 = type.parse(timestamp.toString());
|
||||
assertEquals(now, val1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatAndParse() throws Exception {
|
||||
|
||||
Instant now = now();
|
||||
|
||||
String format = type.format(now);
|
||||
Instant val1 = type.parse(format);
|
||||
assertEquals(now, val1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsDateTimeCapable() throws Exception {
|
||||
|
||||
assertTrue(type.isDateTimeCapable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromMillis() throws Exception {
|
||||
|
||||
Instant now = now();
|
||||
Instant val = type.convertFromMillis(now.toEpochMilli());
|
||||
assertEquals(now, val);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonRead() throws Exception {
|
||||
|
||||
Instant now = now();
|
||||
|
||||
JsonTester<Instant> jsonTester = new JsonTester<>(type);
|
||||
jsonTester.test(now);
|
||||
|
||||
ScalarTypeInstant typeNanos = new ScalarTypeInstant(JsonConfig.DateTime.NANOS);
|
||||
jsonTester = new JsonTester<>(typeNanos);
|
||||
jsonTester.test(now);
|
||||
|
||||
ScalarTypeInstant typeIso = new ScalarTypeInstant(JsonConfig.DateTime.ISO8601);
|
||||
jsonTester = new JsonTester<>(typeIso);
|
||||
jsonTester.test(now);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isoJsonParseFormat() {
|
||||
|
||||
ScalarTypeInstant typeIso = new ScalarTypeInstant(JsonConfig.DateTime.ISO8601);
|
||||
|
||||
Instant instant = Instant.now();
|
||||
String asJson = typeIso.toJsonISO8601(instant);
|
||||
|
||||
Instant value = typeIso.fromJsonISO8601(asJson);
|
||||
assertThat(instant).isEqualTo(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ScalarTypeIntegerTest {
|
||||
|
||||
private final ScalarTypeInteger type = new ScalarTypeInteger();
|
||||
|
||||
@Test
|
||||
public void format_when_string() {
|
||||
assertThat(type.format("1")).isEqualTo("1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void format_when_integer() {
|
||||
assertThat(type.format(1)).isEqualTo("1");
|
||||
}
|
||||
|
||||
}
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class ScalarTypeJodaLocalDateTest {
|
||||
|
||||
private ScalarTypeJodaLocalDate type = new ScalarTypeJodaLocalDate(JsonConfig.Date.MILLIS);
|
||||
|
||||
@Test
|
||||
public void convertToMillis_convertFromMillis() {
|
||||
|
||||
LocalDate localDate = new LocalDate();
|
||||
long millis = type.convertToMillis(localDate);
|
||||
LocalDate localDate1 = type.convertFromMillis(millis);
|
||||
|
||||
assertThat(localDate).isEqualTo(localDate1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertToDate_convertFromDate_westOfUtc() {
|
||||
final TimeZone originaDefaultTimezone = TimeZone.getDefault();
|
||||
try {
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("America/Chicago"));
|
||||
|
||||
convertDate(new LocalDate());
|
||||
convertDate(new LocalDate(1899, 12, 1));
|
||||
convertDate(new LocalDate(1900, 1, 1));
|
||||
convertDate(new LocalDate(2021, 2, 8));
|
||||
|
||||
} finally {
|
||||
TimeZone.setDefault(originaDefaultTimezone);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertToDate_convertFromDate() {
|
||||
|
||||
convertDate(new LocalDate());
|
||||
convertDate(new LocalDate(1899, 12, 1));
|
||||
convertDate(new LocalDate(1900, 1, 1));
|
||||
convertDate(new LocalDate(2021, 2, 8));
|
||||
}
|
||||
|
||||
private void convertDate(LocalDate localDate) {
|
||||
|
||||
Date dateValue = type.convertToDate(localDate);
|
||||
LocalDate localDate1 = type.convertFromDate(dateValue);
|
||||
|
||||
assertThat(localDate).isEqualTo(localDate1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toJdbcType() {
|
||||
|
||||
LocalDate localDate = new LocalDate();
|
||||
Object jdbcType = type.toJdbcType(localDate);
|
||||
Date dateValue = type.convertToDate(localDate);
|
||||
|
||||
assertThat(jdbcType).isEqualTo(dateValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toBeanType() {
|
||||
|
||||
LocalDate localDate = new LocalDate();
|
||||
Date dateValue = type.convertToDate(localDate);
|
||||
LocalDate beanType = type.toBeanType(dateValue);
|
||||
|
||||
assertThat(beanType).isEqualTo(localDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void json() throws IOException {
|
||||
|
||||
LocalDate val = new LocalDate(2019, 5, 9);
|
||||
|
||||
JsonTester<LocalDate> jsonMillis = new JsonTester<>(type);
|
||||
assertThat(jsonMillis.test(val)).isEqualTo("{\"key\":1557360000000}");
|
||||
|
||||
JsonTester<LocalDate> jsonIso = new JsonTester<>(new ScalarTypeJodaLocalDate(JsonConfig.Date.ISO8601) );
|
||||
assertThat(jsonIso.test(val)).isEqualTo("{\"key\":\"2019-05-09\"}");
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.joda.time.LocalDateTime;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ScalarTypeJodaLocalDateTimeTest {
|
||||
|
||||
ScalarTypeJodaLocalDateTime type = new ScalarTypeJodaLocalDateTime(JsonConfig.DateTime.ISO8601);
|
||||
|
||||
@Test
|
||||
public void testConvertFromTimestamp() throws Exception {
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
Timestamp nowTs = new Timestamp(now);
|
||||
|
||||
LocalDateTime ldt1 = type.convertFromTimestamp(nowTs);
|
||||
LocalDateTime ldt2 = localConvertFromTimestamp(nowTs);
|
||||
|
||||
assertEquals(ldt1, ldt2);
|
||||
|
||||
Timestamp ts1 = type.convertToTimestamp(ldt1);
|
||||
Timestamp ts2 = localConvertToTimestamp(ldt2);
|
||||
|
||||
assertEquals(ts1, ts2);
|
||||
}
|
||||
|
||||
|
||||
LocalDateTime localConvertFromTimestamp(Timestamp ts) {
|
||||
return new LocalDateTime(ts.getTime(), DateTimeZone.getDefault());
|
||||
}
|
||||
|
||||
|
||||
Timestamp localConvertToTimestamp(LocalDateTime t) {
|
||||
return new Timestamp(t.toDateTime(DateTimeZone.getDefault()).getMillis());
|
||||
}
|
||||
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.DB;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.joda.time.LocalDateTime;
|
||||
import org.joda.time.LocalTime;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.TJodaEntity;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ScalarTypeJodaLocalTimeTest {
|
||||
|
||||
ScalarTypeJodaLocalTime type = new ScalarTypeJodaLocalTime();
|
||||
|
||||
@Test
|
||||
public void toJdbcType_toBeanType() {
|
||||
|
||||
LocalTime localTime0 = new LocalTime().withMillisOfSecond(0);
|
||||
Object time = type.toJdbcType(localTime0);
|
||||
LocalTime localTime1 = type.toBeanType(time);
|
||||
|
||||
assertThat(localTime0).isEqualTo(localTime1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
|
||||
//DateTimeZone timeZone = DateTimeZone.getDefault();
|
||||
//ISOChronology instance = ISOChronology.getInstance();
|
||||
|
||||
LocalDateTime ldt1 = new LocalDateTime(now, DateTimeZone.getDefault());
|
||||
LocalDateTime ldt2 = new LocalDateTime(now);
|
||||
|
||||
assertEquals(ldt1, ldt2);
|
||||
|
||||
Timestamp ts1 = new Timestamp(ldt1.toDateTime(DateTimeZone.getDefault()).getMillis());
|
||||
Timestamp ts2 = new Timestamp(ldt2.toDateTime().getMillis());
|
||||
|
||||
assertEquals(ts1, ts2);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toJson() {
|
||||
|
||||
LocalTime now = new LocalTime();
|
||||
|
||||
TJodaEntity bean = new TJodaEntity();
|
||||
bean.setId(42);
|
||||
bean.setLocalTime(now);
|
||||
|
||||
String json = DB.json().toJson(bean);
|
||||
TJodaEntity bean1 = DB.json().toBean(TJodaEntity.class, json);
|
||||
|
||||
Assertions.assertEquals(bean1.getLocalTime(), now);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.joda.time.Period;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ScalarTypeJodaPeriodTest {
|
||||
|
||||
private ScalarTypeJodaPeriod scalarType = new ScalarTypeJodaPeriod();
|
||||
|
||||
@Test
|
||||
public void getLength() {
|
||||
assertThat(scalarType.getLength()).isEqualTo(50);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void formatAndParse() {
|
||||
|
||||
Period original = Period.years(1).plusMonths(2).plusDays(4)
|
||||
.plusHours(12).plusMinutes(19).plusSeconds(20);
|
||||
|
||||
String value = scalarType.formatValue(original);
|
||||
assertThat(value).isEqualTo("P1Y2M4DT12H19M20S");
|
||||
|
||||
Period period = scalarType.parse(value);
|
||||
assertThat(period).isEqualTo(original);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void convertFromDbString() {
|
||||
|
||||
Period original = Period.years(1).plusMonths(2).plusDays(4)
|
||||
.plusHours(23).plusMinutes(19).plusSeconds(20).plusMillis(987);
|
||||
|
||||
String stringVal = scalarType.convertToDbString(original);
|
||||
Period period = scalarType.convertFromDbString(stringVal);
|
||||
|
||||
assertThat(period).isEqualTo(original);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.dbplatform.ExtraDbTypes;
|
||||
import io.ebean.core.type.DocPropertyType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class ScalarTypeJsonListTest extends BasePlatformArrayTypeFactoryTest {
|
||||
|
||||
@Test
|
||||
public void typeFor_expect_nullToEmpty_when_postgresNonNull() throws SQLException {
|
||||
|
||||
assertBindNullTo_PGObjectEmpty(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSONB, DocPropertyType.OBJECT, false, false));
|
||||
assertBindNullTo_PGObjectEmpty(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSON, DocPropertyType.OBJECT, false, false));
|
||||
|
||||
assertBindNullTo_EmptyString(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSONVarchar, DocPropertyType.OBJECT, false, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typeFor_expect_nullToNull_when_nullable() throws SQLException {
|
||||
|
||||
assertBindNullTo_PGObjectNull(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSONB, DocPropertyType.OBJECT, true, false));
|
||||
assertBindNullTo_PGObjectNull(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSON, DocPropertyType.OBJECT, true, false));
|
||||
|
||||
assertBindNullTo_Null(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSONVarchar, DocPropertyType.OBJECT, true, false));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Date;
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class ScalarTypeLocalDateTest {
|
||||
|
||||
ScalarTypeLocalDate type = new ScalarTypeLocalDate(JsonConfig.Date.ISO8601);
|
||||
|
||||
@Test
|
||||
public void testConvertToMillis() {
|
||||
|
||||
LocalDate date = LocalDate.of(2014, 5, 20);
|
||||
long millis = type.convertToMillis(date);
|
||||
|
||||
LocalDate parseDate = type.convertFromMillis(millis);
|
||||
assertEquals(date, parseDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromDate() {
|
||||
|
||||
LocalDate localDate = LocalDate.now();
|
||||
Date date = Date.valueOf(localDate);
|
||||
|
||||
LocalDate localDate1 = type.convertFromDate(date);
|
||||
assertEquals(localDate, localDate1);
|
||||
|
||||
Date date1 = type.convertToDate(localDate);
|
||||
assertEquals(date, date1);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testToJdbcType() {
|
||||
|
||||
LocalDate localDate = LocalDate.now();
|
||||
Object o = type.toJdbcType(localDate);
|
||||
assertTrue(o instanceof Date);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void olderDates_ToFromJdbcType() {
|
||||
LocalDate localDate = LocalDate.of(1850,12,1);
|
||||
Object o = type.toJdbcType(localDate);
|
||||
LocalDate localDate1 = type.toBeanType(o);
|
||||
assertThat(localDate1).isEqualTo(localDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToBeanType() {
|
||||
|
||||
LocalDate localDate = LocalDate.now();
|
||||
Date date = Date.valueOf(localDate);
|
||||
|
||||
LocalDate localDate1 = type.toBeanType(date);
|
||||
assertEquals(localDate, localDate1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void json() throws IOException {
|
||||
|
||||
LocalDate val = LocalDate.of(2019, 5, 9);
|
||||
|
||||
JsonTester<LocalDate> jsonMillis = new JsonTester<>(new ScalarTypeLocalDate(JsonConfig.Date.MILLIS));
|
||||
assertThat(jsonMillis.test(val)).isEqualTo("{\"key\":1557360000000}");
|
||||
|
||||
JsonTester<LocalDate> jsonIso = new JsonTester<>(new ScalarTypeLocalDate(JsonConfig.Date.ISO8601) );
|
||||
assertThat(jsonIso.test(val)).isEqualTo("{\"key\":\"2019-05-09\"}");
|
||||
}
|
||||
}
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class ScalarTypeLocalDateTimeTest {
|
||||
|
||||
private final ScalarTypeLocalDateTime type = new ScalarTypeLocalDateTime(JsonConfig.DateTime.MILLIS);
|
||||
|
||||
private final JsonFactory factory = new JsonFactory();
|
||||
|
||||
// warm up
|
||||
private final LocalDateTime warmUp = LocalDateTime.now();
|
||||
|
||||
@Test
|
||||
public void testNowToMillis() {
|
||||
|
||||
warmUp.hashCode();
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
long toMillis = type.convertToMillis(LocalDateTime.now());
|
||||
assertTrue(toMillis - now < 30);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertToMillis() {
|
||||
|
||||
LocalDateTime now = LocalDateTime.now().withNano(123_000_000); // jdk11 workaround
|
||||
long asMillis = type.convertToMillis(now);
|
||||
LocalDateTime fromMillis = type.convertFromMillis(asMillis);
|
||||
|
||||
assertEquals(now, fromMillis);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromTimestamp() {
|
||||
|
||||
Timestamp now = new Timestamp(System.currentTimeMillis());
|
||||
|
||||
LocalDateTime localDateTime = type.convertFromTimestamp(now);
|
||||
Timestamp timestamp = type.convertToTimestamp(localDateTime);
|
||||
|
||||
assertEquals(now, timestamp);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testToJdbcType() throws Exception {
|
||||
|
||||
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 val1 = type.toBeanType(timestamp);
|
||||
assertNotNull(val1);
|
||||
|
||||
LocalDateTime val2 = type.toBeanType(timestamp.toLocalDateTime());
|
||||
assertNotNull(val2);
|
||||
|
||||
Timestamp timestamp1 = type.convertToTimestamp(val1);
|
||||
assertEquals(timestamp, timestamp1);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testJsonRaw() throws Exception {
|
||||
|
||||
final LocalDateTime of = LocalDateTime.of(2020, 5, 4, 13, 20, 40);
|
||||
|
||||
ScalarTypeLocalDateTime typeIso = new ScalarTypeLocalDateTime(JsonConfig.DateTime.ISO8601);
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonGenerator generator = factory.createGenerator(writer);
|
||||
|
||||
typeIso.jsonWrite(generator, of);
|
||||
generator.flush();
|
||||
|
||||
assertThat(of.toString()).isEqualTo("2020-05-04T13:20:40");
|
||||
assertThat(writer.toString()).isEqualTo("\"2020-05-04T13:20:40\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJson() throws Exception {
|
||||
|
||||
LocalDateTime now = LocalDateTime.now().withNano(123_000_000); // jdk11 workaround
|
||||
|
||||
JsonTester<LocalDateTime> jsonTester = new JsonTester<>(type);
|
||||
jsonTester.test(now);
|
||||
|
||||
ScalarTypeLocalDateTime typeNanos = new ScalarTypeLocalDateTime(JsonConfig.DateTime.NANOS);
|
||||
jsonTester = new JsonTester<>(typeNanos);
|
||||
jsonTester.test(now);
|
||||
|
||||
ScalarTypeLocalDateTime typeIso = new ScalarTypeLocalDateTime(JsonConfig.DateTime.ISO8601);
|
||||
jsonTester = new JsonTester<>(typeIso);
|
||||
jsonTester.test(now);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isoJsonFormatParse() {
|
||||
|
||||
ScalarTypeLocalDateTime typeIso = new ScalarTypeLocalDateTime(JsonConfig.DateTime.ISO8601);
|
||||
|
||||
LocalDateTime localDateTime = LocalDateTime.now();
|
||||
String asJson = typeIso.toJsonISO8601(localDateTime);
|
||||
|
||||
LocalDateTime value = typeIso.fromJsonISO8601(asJson);
|
||||
assertThat(localDateTime).isEqualToIgnoringNanos(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.text.TextException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.sql.Time;
|
||||
import java.time.LocalTime;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
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, 123);
|
||||
Time time = Time.valueOf(localTime);
|
||||
|
||||
Object val1 = type.toJdbcType(localTime);
|
||||
Object val2 = type.toJdbcType(time);
|
||||
|
||||
assertEquals(time, val1);
|
||||
assertEquals(time, val2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToBeanType() throws Exception {
|
||||
|
||||
LocalTime localTime = LocalTime.of(9, 23, 45);
|
||||
LocalTime val1 = type.toBeanType(localTime);
|
||||
LocalTime val2 = type.toBeanType(Time.valueOf(localTime));
|
||||
|
||||
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() {
|
||||
LocalTime localTime = LocalTime.of(9, 23, 45);
|
||||
LocalTime val1 = type.parse("09:23:45");
|
||||
assertEquals(localTime, val1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsDateTimeCapable() {
|
||||
assertFalse(type.isDateTimeCapable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromMillis() {
|
||||
assertThrows(TextException.class, () -> type.convertFromMillis(1234));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonRead() throws Exception {
|
||||
LocalTime localTime = LocalTime.of(9, 23, 45);
|
||||
|
||||
JsonTester<LocalTime> jsonTester = new JsonTester<>(type);
|
||||
jsonTester.test(localTime);
|
||||
}
|
||||
|
||||
}
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.text.TextException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.time.LocalTime;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class ScalarTypeLocalTimeWithNanosTest {
|
||||
|
||||
ScalarTypeLocalTimeWithNanos type = new ScalarTypeLocalTimeWithNanos();
|
||||
|
||||
@Test
|
||||
public void testReadData() throws Exception {
|
||||
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
ObjectOutputStream out = new ObjectOutputStream(os);
|
||||
|
||||
LocalTime localTime = LocalTime.of(9, 23, 45, 115);
|
||||
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, 115);
|
||||
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() {
|
||||
LocalTime localTime = LocalTime.of(9, 23, 45);
|
||||
LocalTime val1 = type.parse("09:23:45");
|
||||
assertEquals(localTime, val1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsDateTimeCapable() {
|
||||
assertFalse(type.isDateTimeCapable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromMillis() {
|
||||
assertThrows(TextException.class, () -> type.convertFromMillis(1234));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonRead() throws Exception {
|
||||
LocalTime localTime = LocalTime.of(9, 23, 45);
|
||||
|
||||
JsonTester<LocalTime> jsonTester = new JsonTester<>(type);
|
||||
jsonTester.test(localTime);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ScalarTypeLongTest {
|
||||
|
||||
private final ScalarTypeLong type = new ScalarTypeLong();
|
||||
|
||||
@Test
|
||||
public void format_when_string() {
|
||||
assertThat(type.format("1")).isEqualTo("1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void format_when_long() {
|
||||
assertThat(type.format(1L)).isEqualTo("1");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.time.LocalDate;
|
||||
import java.time.MonthDay;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
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() {
|
||||
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() {
|
||||
assertFalse(type.isDateTimeCapable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromMillis() {
|
||||
assertThrows(RuntimeException.class, () -> type.convertFromMillis(1203));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJson() throws Exception {
|
||||
MonthDay value = MonthDay.of(4, 29);
|
||||
JsonTester<MonthDay> jsonTester = new JsonTester<>(type);
|
||||
jsonTester.test(value);
|
||||
}
|
||||
|
||||
}
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class ScalarTypeOffsetDateTimeTest {
|
||||
|
||||
|
||||
ScalarTypeOffsetDateTime type = new ScalarTypeOffsetDateTime(JsonConfig.DateTime.MILLIS, ZoneOffset.systemDefault());
|
||||
|
||||
OffsetDateTime warmUp = OffsetDateTime.now();
|
||||
|
||||
@Test
|
||||
public void testConvertToMillis() {
|
||||
|
||||
warmUp.hashCode();
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
long toMillis = type.convertToMillis(OffsetDateTime.now());
|
||||
|
||||
assertTrue(toMillis - now < 10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertFromInstant_with_UTC_expect_matchingZoneOffset() {
|
||||
final TimeZone timeZoneToUse = TimeZone.getTimeZone("UTC");
|
||||
final ZoneOffset expectedZoneOffset = ZoneOffset.UTC;
|
||||
|
||||
convertFromInstantWithConfiguredTimeZone(timeZoneToUse, expectedZoneOffset);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertFromInstant_with_EST_expect_matchingZoneOffset() {
|
||||
final TimeZone timeZoneToUse = TimeZone.getTimeZone("EST");
|
||||
final ZoneOffset expectedOffset = OffsetDateTime.now(timeZoneToUse.toZoneId()).getOffset();
|
||||
|
||||
convertFromInstantWithConfiguredTimeZone(timeZoneToUse, expectedOffset);
|
||||
}
|
||||
|
||||
private void convertFromInstantWithConfiguredTimeZone(TimeZone timeZoneToUse, ZoneOffset expectedZoneOffset) {
|
||||
TimeZone previous = TimeZone.getDefault();
|
||||
try {
|
||||
OffsetDateTime dateTime = OffsetDateTime.parse("2021-01-01T00:00:00+11:00");
|
||||
|
||||
// test ScalarTypeOffsetDateTime with the configured timeZone to use
|
||||
ScalarTypeOffsetDateTime type = new ScalarTypeOffsetDateTime(JsonConfig.DateTime.MILLIS, timeZoneToUse.toZoneId());
|
||||
|
||||
// effectively we desire to ignore the system timezone and use the configured one
|
||||
TimeZone.setDefault(timeZoneToUse);
|
||||
|
||||
final OffsetDateTime offsetDateTime = type.convertFromInstant(dateTime.toInstant());
|
||||
|
||||
assertEquals(expectedZoneOffset, offsetDateTime.getOffset());
|
||||
|
||||
} finally {
|
||||
TimeZone.setDefault(previous);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromTimestamp() {
|
||||
|
||||
Timestamp now = new Timestamp(System.currentTimeMillis());
|
||||
|
||||
OffsetDateTime localDateTime = type.convertFromTimestamp(now);
|
||||
Timestamp timestamp = type.convertToTimestamp(localDateTime);
|
||||
|
||||
assertEquals(now, timestamp);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testToJdbcType() throws Exception {
|
||||
|
||||
Object jdbcType = type.toJdbcType(OffsetDateTime.now());
|
||||
assertTrue(jdbcType instanceof Timestamp);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToBeanType() throws Exception {
|
||||
|
||||
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
|
||||
OffsetDateTime localDateTime = type.toBeanType(timestamp);
|
||||
|
||||
assertNotNull(localDateTime);
|
||||
|
||||
Timestamp timestamp1 = type.convertToTimestamp(localDateTime);
|
||||
assertEquals(timestamp, timestamp1);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJson() throws Exception {
|
||||
|
||||
OffsetDateTime now = OffsetDateTime.now().withNano(123_000_000); // jdk11 workaround
|
||||
|
||||
JsonTester<OffsetDateTime> jsonTester = new JsonTester<>(type);
|
||||
jsonTester.test(now);
|
||||
|
||||
ScalarTypeOffsetDateTime typeNanos = new ScalarTypeOffsetDateTime(JsonConfig.DateTime.NANOS, ZoneOffset.systemDefault());
|
||||
jsonTester = new JsonTester<>(typeNanos);
|
||||
jsonTester.test(now);
|
||||
|
||||
ScalarTypeOffsetDateTime typeIso = new ScalarTypeOffsetDateTime(JsonConfig.DateTime.ISO8601, ZoneOffset.systemDefault());
|
||||
jsonTester = new JsonTester<>(typeIso);
|
||||
jsonTester.test(now);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isoJsonFormatParse() {
|
||||
|
||||
ScalarTypeOffsetDateTime typeIso = new ScalarTypeOffsetDateTime(JsonConfig.DateTime.ISO8601, ZoneOffset.systemDefault());
|
||||
|
||||
OffsetDateTime now = OffsetDateTime.now();
|
||||
String asJson = typeIso.toJsonISO8601(now);
|
||||
|
||||
OffsetDateTime value = typeIso.fromJsonISO8601(asJson);
|
||||
assertThat(now).isEqualToIgnoringNanos(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.OffsetTime;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ScalarTypeOffsetTimeTest {
|
||||
|
||||
ScalarTypeOffsetTime type = new ScalarTypeOffsetTime();
|
||||
|
||||
@Test
|
||||
public void testGetLength() throws Exception {
|
||||
|
||||
assertEquals(25, type.getLength());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatParse() throws Exception {
|
||||
|
||||
OffsetTime now = OffsetTime.now();
|
||||
String value = type.formatValue(now);
|
||||
OffsetTime offsetTime = type.parse(value);
|
||||
|
||||
assertEquals(now, offsetTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertDb() throws Exception {
|
||||
|
||||
OffsetTime now = OffsetTime.now();
|
||||
String value = type.convertToDbString(now);
|
||||
OffsetTime offsetTime = type.convertFromDbString(value);
|
||||
|
||||
assertEquals(now, offsetTime);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ScalarTypePathTest {
|
||||
private static final String TEMP_PATH = new File("/tmp").getAbsolutePath();
|
||||
|
||||
private ScalarTypePath type = new ScalarTypePath();
|
||||
|
||||
@Test
|
||||
public void convertFromDbString() throws Exception {
|
||||
|
||||
Path path = Paths.get(TEMP_PATH);
|
||||
|
||||
String asString = type.convertToDbString(path); // "/tmp" will be converted to "file://c:/tmp" on windows
|
||||
Path converted = type.convertFromDbString(asString);
|
||||
|
||||
assertEquals(path, converted);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void formatAndParse() throws Exception {
|
||||
|
||||
Path path = Paths.get(TEMP_PATH);
|
||||
|
||||
String asString = type.formatValue(path);
|
||||
Path converted = type.parse(asString);
|
||||
|
||||
assertEquals(path, converted);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Period;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ScalarTypePeriodTest {
|
||||
|
||||
private ScalarTypePeriod scalarType = new ScalarTypePeriod();
|
||||
|
||||
@Test
|
||||
public void getLength() {
|
||||
assertThat(scalarType.getLength()).isEqualTo(20);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void formatAndParse() {
|
||||
|
||||
Period original = Period.of(1,2, 4);
|
||||
|
||||
String value = scalarType.formatValue(original);
|
||||
assertThat(value).isEqualTo("P1Y2M4D");
|
||||
|
||||
Period period = scalarType.parse(value);
|
||||
assertThat(period).isEqualTo(original);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void convertFromDbString() {
|
||||
|
||||
Period original = Period.of(1, 2, 4);
|
||||
|
||||
String stringVal = scalarType.convertToDbString(original);
|
||||
Period period = scalarType.convertFromDbString(stringVal);
|
||||
|
||||
assertThat(period).isEqualTo(original);
|
||||
}
|
||||
|
||||
}
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
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 io.ebeaninternal.json.ModifyAwareMap;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class ScalarTypePostgresHstoreTest {
|
||||
|
||||
ScalarTypePostgresHstore hstore = new ScalarTypePostgresHstore();
|
||||
|
||||
JsonFactory jsonFactory = new JsonFactory();
|
||||
|
||||
@Test
|
||||
public void testIsMutable() {
|
||||
assertTrue(hstore.isMutable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsDateTimeCapable() {
|
||||
assertFalse(hstore.isDateTimeCapable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsDirty() {
|
||||
Map<String, Object> emptyMap = new HashMap<>();
|
||||
assertTrue(hstore.isDirty(emptyMap));
|
||||
|
||||
ModifyAwareMap<String, Object> modAware = new ModifyAwareMap<>(emptyMap);
|
||||
assertFalse(hstore.isDirty(modAware));
|
||||
modAware.put("foo", "Rob");
|
||||
assertTrue(hstore.isDirty(emptyMap));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testParse() {
|
||||
Map<String, Object> map = (Map<String, Object>) hstore.parse("{\"name\":\"rob\"}");
|
||||
assertEquals(1, map.size());
|
||||
assertEquals("rob", map.get("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testParseDateTime() {
|
||||
assertThrows(RuntimeException.class, () -> {
|
||||
Map<String, Object> map = (Map<String, Object>) hstore.convertFromMillis(1234L);
|
||||
assertEquals(1, map.size());
|
||||
assertEquals("rob", map.get("name"));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonWrite() throws Exception {
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
assertEquals("{\"key\":{}}", generateJson(map));
|
||||
|
||||
map.put("name", "rob");
|
||||
assertEquals("{\"key\":{\"name\":\"rob\"}}", generateJson(map));
|
||||
|
||||
map.put("age", 12);
|
||||
assertEquals("{\"key\":{\"name\":\"rob\",\"age\":12}}", generateJson(map));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonRead() throws Exception {
|
||||
Map<String, Object> map = parseHstore("{\"name\":\"rob\"}");
|
||||
assertEquals(1, map.size());
|
||||
assertEquals("rob", map.get("name"));
|
||||
|
||||
map = parseHstore("{\"name\":\"rob\",\"age\":12}");
|
||||
assertEquals(2, map.size());
|
||||
assertEquals("rob", map.get("name"));
|
||||
assertEquals(12L, map.get("age"));
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Map<String, Object> parseHstore(String json) throws IOException {
|
||||
JsonParser parser = jsonFactory.createParser(json);
|
||||
// BeanProperty reads the first token checking for null so
|
||||
// simulate that here
|
||||
JsonToken token = parser.nextToken();
|
||||
assertEquals(JsonToken.START_OBJECT, token);
|
||||
return (Map<String, Object>) hstore.jsonRead(parser);
|
||||
}
|
||||
|
||||
private String generateJson(Map<String, Object> map) throws IOException {
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonGenerator generator = jsonFactory.createGenerator(writer);
|
||||
// wrap in an object to form proper json
|
||||
generator.writeStartObject();
|
||||
generator.writeFieldName("key");
|
||||
|
||||
hstore.jsonWrite(generator, map);
|
||||
|
||||
generator.writeEndObject();
|
||||
generator.flush();
|
||||
|
||||
return writer.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ScalarTypeTimestampTest {
|
||||
|
||||
@Test
|
||||
public void toJsonISO8601() {
|
||||
|
||||
ScalarTypeTimestamp typeIso = new ScalarTypeTimestamp(JsonConfig.DateTime.ISO8601);
|
||||
|
||||
Timestamp now = new Timestamp(System.currentTimeMillis());
|
||||
String asJson = typeIso.toJsonISO8601(now);
|
||||
|
||||
Timestamp value = typeIso.fromJsonISO8601(asJson);
|
||||
assertThat(now).isEqualTo(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ScalarTypeUUIDBaseTest {
|
||||
|
||||
private final ScalarTypeUUIDBase type = new ScalarTypeUUIDNative();
|
||||
private final UUID uuid = UUID.randomUUID();
|
||||
|
||||
@Test
|
||||
public void format_as_uuid() {
|
||||
assertThat(type.format(uuid)).isEqualTo(uuid.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void format_as_String() {
|
||||
assertThat(type.format(uuid.toString())).isEqualTo(uuid.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ScalarTypeUtilDateTest {
|
||||
|
||||
private ScalarTypeUtilDate.DateType dateType = new ScalarTypeUtilDate.DateType(JsonConfig.Date.MILLIS);
|
||||
|
||||
@Test
|
||||
public void json() throws IOException {
|
||||
|
||||
Date val = new Date(1557316800000L);
|
||||
|
||||
JsonTester<Date> jsonMillis = new JsonTester<>(dateType);
|
||||
assertThat(jsonMillis.test(val)).isEqualTo("{\"key\":1557316800000}");
|
||||
|
||||
JsonTester<Date> jsonIso = new JsonTester<>(new ScalarTypeUtilDate.DateType(JsonConfig.Date.ISO8601) );
|
||||
Date val1 = jsonIso.type.parse("2019-05-09");
|
||||
assertThat(jsonIso.test(val1)).isEqualTo("{\"key\":\"2019-05-09\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toJsonISO8601() {
|
||||
|
||||
ScalarTypeUtilDate.TimestampType typeIso = new ScalarTypeUtilDate.TimestampType(JsonConfig.DateTime.ISO8601);
|
||||
|
||||
Date now = new Date();
|
||||
String asJson = typeIso.toJsonISO8601(now);
|
||||
|
||||
Date value = typeIso.fromJsonISO8601(asJson);
|
||||
assertThat(now).isEqualTo(value);
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.persistence.AttributeConverter;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public class ScalarTypeWrapperAdapterTest {
|
||||
|
||||
private final ScalarTypeString stringType = ScalarTypeString.INSTANCE;
|
||||
private final MyAdapter myAdapter = new MyAdapter();
|
||||
private final AttributeConverterAdapter converterAdapter = new AttributeConverterAdapter(myAdapter);
|
||||
private final ScalarTypeWrapper<Long, String> wrapper = new ScalarTypeWrapper(Long.class, stringType, converterAdapter);
|
||||
|
||||
@Test
|
||||
public void toJdbcType() {
|
||||
assertThat(wrapper.toJdbcType(42L)).isEqualTo("L42");
|
||||
assertThat(wrapper.toJdbcType(93L)).isEqualTo("L93");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toJdbcType_when_nullValue() {
|
||||
assertThat(wrapper.toJdbcType(MyAdapter.NULL_VAL)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toBeanType_when_null_expect_customNullValue() {
|
||||
assertThat(wrapper.toBeanType(null)).isEqualTo(MyAdapter.NULL_VAL);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toBeanType() {
|
||||
assertThat(wrapper.toBeanType("L34")).isEqualTo(34L);
|
||||
}
|
||||
|
||||
/**
|
||||
* An AttributeConverter with a custom null value (of -1L).
|
||||
*/
|
||||
private static class MyAdapter implements AttributeConverter<Long, String> {
|
||||
|
||||
private static final Long NULL_VAL = -1L;
|
||||
|
||||
@Override
|
||||
public String convertToDatabaseColumn(Long val) {
|
||||
if (val == null || val.equals(NULL_VAL)) {
|
||||
return null;
|
||||
}
|
||||
return "L" + val;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long convertToEntityAttribute(String dbData) {
|
||||
if (dbData == null) {
|
||||
return NULL_VAL;
|
||||
}
|
||||
return Long.parseLong(dbData.substring(1));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.ivo.Oid;
|
||||
import org.tests.model.ivo.converter.OidTypeConverter;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public class ScalarTypeWrapperOidTest {
|
||||
|
||||
private final OidTypeConverter oidTypeConverter = new OidTypeConverter();
|
||||
private final ScalarTypeLong longType = new ScalarTypeLong();
|
||||
private final ScalarTypeWrapper<Oid<?>,Long> wrapper = new ScalarTypeWrapper(Oid.class, longType, oidTypeConverter);
|
||||
|
||||
@Test
|
||||
public void toJdbcType() {
|
||||
|
||||
assertThat(wrapper.toJdbcType(new Oid<String>(42))).isEqualTo(42L);
|
||||
assertThat(wrapper.toJdbcType(new Oid<String>(98))).isEqualTo(98L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toJdbcType_when_nullValue() {
|
||||
assertThat(wrapper.toJdbcType(OidTypeConverter.NULL_VALUE)).isNull();
|
||||
}
|
||||
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Date;
|
||||
import java.time.LocalDate;
|
||||
import java.time.YearMonth;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ScalarTypeYearMonthDateTest {
|
||||
|
||||
private ScalarTypeYearMonthDate type = new ScalarTypeYearMonthDate(JsonConfig.Date.MILLIS);
|
||||
|
||||
@Test
|
||||
public void testConvertFromMillis() {
|
||||
|
||||
LocalDate today = LocalDate.now();
|
||||
LocalDate firstMonthDay = today.withDayOfMonth(1);
|
||||
ZonedDateTime zonedDateTime = firstMonthDay.atStartOfDay(ZoneOffset.UTC);
|
||||
|
||||
long epochMilli = zonedDateTime.toInstant().toEpochMilli();
|
||||
|
||||
YearMonth yearMonth = type.convertFromMillis(epochMilli);
|
||||
long val1 = type.convertToMillis(yearMonth);
|
||||
|
||||
assertEquals(epochMilli, val1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertDate() {
|
||||
|
||||
LocalDate today = LocalDate.now();
|
||||
LocalDate firstMonthDay = today.withDayOfMonth(1);
|
||||
Date date = Date.valueOf(firstMonthDay);
|
||||
|
||||
YearMonth yearMonth = type.convertFromDate(date);
|
||||
Date date1 = type.convertToDate(yearMonth);
|
||||
assertEquals(date, date1);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testToJdbcType() {
|
||||
|
||||
LocalDate today = LocalDate.now();
|
||||
LocalDate firstMonthDay = today.withDayOfMonth(1);
|
||||
Date date = Date.valueOf(firstMonthDay);
|
||||
|
||||
YearMonth yearMonth = type.toBeanType(date);
|
||||
Object val1 = type.toJdbcType(yearMonth);
|
||||
assertEquals(date, val1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void json() throws IOException {
|
||||
|
||||
YearMonth val = YearMonth.of(2019, 5);
|
||||
|
||||
JsonTester<YearMonth> jsonMillis = new JsonTester<>(type);
|
||||
assertThat(jsonMillis.test(val)).isEqualTo("{\"key\":1556668800000}");
|
||||
|
||||
JsonTester<YearMonth> jsonIso = new JsonTester<>(new ScalarTypeYearMonthDate(JsonConfig.Date.ISO8601) );
|
||||
assertThat(jsonIso.test(val)).isEqualTo("{\"key\":\"2019-05-01\"}");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.text.TextException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.time.Year;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
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() {
|
||||
Year year = type.parse("2013");
|
||||
assertEquals(Year.of(2013), year);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsDateTimeCapable() {
|
||||
assertFalse(type.isDateTimeCapable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromMillis() {
|
||||
assertThrows(TextException.class, () -> type.convertFromMillis(1000));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJson() throws Exception {
|
||||
JsonTester<Year> jsonTester = new JsonTester<>(type);
|
||||
jsonTester.test(Year.of(2013));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.ZoneId;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ScalarTypeZoneIdTest {
|
||||
|
||||
ScalarTypeZoneId type = new ScalarTypeZoneId();
|
||||
|
||||
@Test
|
||||
public void testGetLength() throws Exception {
|
||||
|
||||
assertEquals(60, type.getLength());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatParse() throws Exception {
|
||||
|
||||
ZoneId zoneId = ZoneId.systemDefault();
|
||||
String value = type.formatValue(zoneId);
|
||||
|
||||
ZoneId val1 = type.parse(value);
|
||||
assertEquals(zoneId, val1);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testConvertDb() throws Exception {
|
||||
|
||||
ZoneId zoneId = ZoneId.systemDefault();
|
||||
String value = type.convertToDbString(zoneId);
|
||||
|
||||
ZoneId val1 = type.convertFromDbString(value);
|
||||
assertEquals(zoneId, val1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ScalarTypeZoneOffsetTest {
|
||||
|
||||
ScalarTypeZoneOffset type = new ScalarTypeZoneOffset();
|
||||
|
||||
@Test
|
||||
public void testGetLength() throws Exception {
|
||||
|
||||
assertEquals(60, type.getLength());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatParse() throws Exception {
|
||||
|
||||
ZonedDateTime now = ZonedDateTime.now();
|
||||
ZoneOffset offset = now.getOffset();
|
||||
|
||||
String value = type.formatValue(offset);
|
||||
|
||||
ZoneOffset val1 = type.parse(value);
|
||||
assertEquals(offset, val1);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testConvertDb() throws Exception {
|
||||
|
||||
ZonedDateTime now = ZonedDateTime.now();
|
||||
ZoneOffset offset = now.getOffset();
|
||||
|
||||
String value = type.convertToDbString(offset);
|
||||
|
||||
ZoneId val1 = type.convertFromDbString(value);
|
||||
assertEquals(offset, val1);
|
||||
}
|
||||
|
||||
}
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class ScalarTypeZonedDateTimeTest {
|
||||
|
||||
|
||||
ScalarTypeZonedDateTime type = new ScalarTypeZonedDateTime(JsonConfig.DateTime.MILLIS, ZoneId.systemDefault());
|
||||
|
||||
ZonedDateTime warmUp = ZonedDateTime.now();
|
||||
|
||||
@Test
|
||||
public void testConvertToMillis() {
|
||||
|
||||
warmUp.hashCode();
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
long toMillis = type.convertToMillis(ZonedDateTime.now());
|
||||
|
||||
assertTrue(toMillis - now < 10);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromTimestamp() {
|
||||
|
||||
Timestamp now = new Timestamp(System.currentTimeMillis());
|
||||
|
||||
ZonedDateTime val1 = type.convertFromTimestamp(now);
|
||||
Timestamp timestamp = type.convertToTimestamp(val1);
|
||||
|
||||
assertEquals(now, timestamp);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertFromInstant_with_UTC_expect_matchingZoneOffset() {
|
||||
final TimeZone timeZoneToUse = TimeZone.getTimeZone("UTC");
|
||||
final ZoneOffset expectedZoneOffset = ZoneOffset.UTC;
|
||||
|
||||
convertFromInstantWithConfiguredTimeZone(timeZoneToUse, expectedZoneOffset);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertFromInstant_with_EST_expect_matchingZoneOffset() {
|
||||
final TimeZone timeZoneToUse = TimeZone.getTimeZone("EST");
|
||||
final ZoneOffset expectedOffset = OffsetDateTime.now(timeZoneToUse.toZoneId()).getOffset();
|
||||
|
||||
convertFromInstantWithConfiguredTimeZone(timeZoneToUse, expectedOffset);
|
||||
}
|
||||
|
||||
private void convertFromInstantWithConfiguredTimeZone(TimeZone timeZoneToUse, ZoneOffset expectedZoneOffset) {
|
||||
TimeZone previous = TimeZone.getDefault();
|
||||
try {
|
||||
OffsetDateTime dateTime = OffsetDateTime.parse("2021-01-01T00:00:00+11:00");
|
||||
|
||||
// test ScalarTypeOffsetDateTime with the configured timeZone to use
|
||||
ScalarTypeZonedDateTime type = new ScalarTypeZonedDateTime(JsonConfig.DateTime.MILLIS, timeZoneToUse.toZoneId());
|
||||
|
||||
// effectively we desire to ignore the system timezone and use the configured one
|
||||
TimeZone.setDefault(timeZoneToUse);
|
||||
|
||||
final ZonedDateTime zonedDateTime = type.convertFromInstant(dateTime.toInstant());
|
||||
|
||||
assertEquals(expectedZoneOffset, zonedDateTime.getOffset());
|
||||
|
||||
} finally {
|
||||
TimeZone.setDefault(previous);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToJdbcType() throws Exception {
|
||||
|
||||
Object jdbcType = type.toJdbcType(ZonedDateTime.now());
|
||||
assertTrue(jdbcType instanceof Timestamp);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToBeanType() throws Exception {
|
||||
|
||||
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
|
||||
ZonedDateTime localDateTime = type.toBeanType(timestamp);
|
||||
|
||||
assertNotNull(localDateTime);
|
||||
|
||||
Timestamp timestamp1 = type.convertToTimestamp(localDateTime);
|
||||
assertEquals(timestamp, timestamp1);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJson() throws Exception {
|
||||
|
||||
ZonedDateTime now = ZonedDateTime.now().withNano(123_000_000); // jdk11 workaround
|
||||
|
||||
JsonTester<ZonedDateTime> jsonTester = new JsonTester<>(type);
|
||||
jsonTester.test(now);
|
||||
|
||||
ScalarTypeZonedDateTime typeNanos = new ScalarTypeZonedDateTime(JsonConfig.DateTime.NANOS, ZoneId.systemDefault());
|
||||
jsonTester = new JsonTester<>(typeNanos);
|
||||
jsonTester.test(now);
|
||||
|
||||
ScalarTypeZonedDateTime typeIso = new ScalarTypeZonedDateTime(JsonConfig.DateTime.ISO8601, ZoneId.systemDefault());
|
||||
jsonTester = new JsonTester<>(typeIso);
|
||||
jsonTester.test(now);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toJsonISO8601() {
|
||||
|
||||
ScalarTypeZonedDateTime typeIso = new ScalarTypeZonedDateTime(JsonConfig.DateTime.ISO8601, ZoneId.systemDefault());
|
||||
|
||||
ZonedDateTime now = ZonedDateTime.now();
|
||||
String asJson = typeIso.toJsonISO8601(now);
|
||||
|
||||
ZonedDateTime value = typeIso.fromJsonISO8601(asJson);
|
||||
assertThat(now).isEqualTo(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebeaninternal.server.type.ScalarTypeEnumStandard.OrdinalEnum;
|
||||
import io.ebeaninternal.server.type.ScalarTypeEnumStandard.StringEnum;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestEnumToBeanType {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
StringEnum stringEnum = new ScalarTypeEnumStandard.StringEnum(Order.Status.class);
|
||||
|
||||
OrdinalEnum ordinalEnum = new ScalarTypeEnumStandard.OrdinalEnum(Order.Status.class);
|
||||
|
||||
EnumToDbValueMap<?> beanDbMap = EnumToDbValueMap.create(false);
|
||||
beanDbMap.add(Customer.Status.ACTIVE, "A", Customer.Status.ACTIVE.name());
|
||||
beanDbMap.add(Customer.Status.NEW, "N", Customer.Status.NEW.name());
|
||||
beanDbMap.add(Customer.Status.INACTIVE, "I", Customer.Status.INACTIVE.name());
|
||||
|
||||
ScalarTypeEnumWithMapping withMapping = new ScalarTypeEnumWithMapping(beanDbMap, Customer.Status.class, 1);
|
||||
|
||||
|
||||
Object approved = stringEnum.toBeanType(Order.Status.APPROVED);
|
||||
assertEquals(approved, Order.Status.APPROVED);
|
||||
|
||||
approved = ordinalEnum.toBeanType(Order.Status.APPROVED);
|
||||
assertEquals(approved, Order.Status.APPROVED);
|
||||
|
||||
Object active = withMapping.toBeanType(Customer.Status.ACTIVE);
|
||||
assertEquals(active, Customer.Status.ACTIVE);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestLocaleParse {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
// Examples: "en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr__MAC"
|
||||
|
||||
Locale l = parse("en");
|
||||
assertEquals("en", l.getLanguage());
|
||||
|
||||
l = parse("de_DE");
|
||||
assertEquals("de", l.getLanguage());
|
||||
assertEquals("DE", l.getCountry());
|
||||
|
||||
l = parse("en_US_WIN");
|
||||
assertEquals("en", l.getLanguage());
|
||||
assertEquals("US", l.getCountry());
|
||||
assertEquals("WIN", l.getVariant());
|
||||
|
||||
l = parse("_GB");
|
||||
assertEquals("", l.getLanguage());
|
||||
assertEquals("GB", l.getCountry());
|
||||
assertEquals("", l.getVariant());
|
||||
|
||||
l = parse("fr__MAC");
|
||||
assertEquals("fr", l.getLanguage());
|
||||
assertEquals("", l.getCountry());
|
||||
assertEquals("MAC", l.getVariant());
|
||||
|
||||
l = parse("de__POSIX");
|
||||
assertEquals("de", l.getLanguage());
|
||||
assertEquals("", l.getCountry());
|
||||
assertEquals("POSIX", l.getVariant());
|
||||
}
|
||||
|
||||
private Locale parse(String value) {
|
||||
return new ScalarTypeLocale().parse(value);
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestScalarTypeUUIDBinaryConversion {
|
||||
|
||||
@Test
|
||||
public void testConversion() {
|
||||
UUID id = UUID.randomUUID();
|
||||
|
||||
byte[] bytes = ScalarTypeUUIDBinary.convertToBytes(id, false);
|
||||
assertEquals(16, bytes.length);
|
||||
|
||||
UUID id2 = ScalarTypeUUIDBinary.convertFromBytes(bytes, false);
|
||||
assertEquals(id, id2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConversionOptimized() {
|
||||
UUID id = UUID.randomUUID();
|
||||
|
||||
byte[] bytes = ScalarTypeUUIDBinary.convertToBytes(id, true);
|
||||
assertEquals(16, bytes.length);
|
||||
|
||||
UUID id2 = ScalarTypeUUIDBinary.convertFromBytes(bytes, true);
|
||||
assertEquals(id, id2);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.EncryptKey;
|
||||
import io.ebeaninternal.server.deploy.BaseTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestSimpleEncryptor extends BaseTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
SimpleAesEncryptor e = new SimpleAesEncryptor();
|
||||
|
||||
EncryptKey key = new BasicEncryptKey("hello");
|
||||
|
||||
byte[] data = "test123".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
byte[] ecData = e.encrypt(data, key);
|
||||
|
||||
byte[] deData = e.decrypt(ecData, key);
|
||||
|
||||
assertThat(data).containsExactly(deData);
|
||||
|
||||
Timestamp t = new Timestamp(System.currentTimeMillis());
|
||||
byte[] ecTimestamp = e.encryptString(t.toString(), key);
|
||||
|
||||
String tsFormat = e.decryptString(ecTimestamp, key);
|
||||
Timestamp t1 = Timestamp.valueOf(tsFormat);
|
||||
assertEquals(t, t1);
|
||||
}
|
||||
|
||||
static class BasicEncryptKey implements EncryptKey {
|
||||
|
||||
private final String key;
|
||||
|
||||
public BasicEncryptKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStringValue() {
|
||||
return key;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebean.config.dbplatform.h2.H2Platform;
|
||||
import io.ebean.core.type.DataReader;
|
||||
import io.ebean.core.type.ScalarType;
|
||||
import io.ebeaninternal.server.core.bootup.BootupClasses;
|
||||
import io.ebeaninternal.server.deploy.BaseTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.enums.MyDayOfWeek;
|
||||
import org.tests.model.basic.enums.MyEnum;
|
||||
import org.tests.model.basic.enums.MySex;
|
||||
import org.tests.model.ivo.Money;
|
||||
import org.tests.model.ivo.converter.MoneyTypeConverter;
|
||||
|
||||
import javax.persistence.EnumType;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class TestTypeManager extends BaseTest {
|
||||
|
||||
@Test
|
||||
void testEnumWithSubclasses() throws SQLException {
|
||||
DefaultTypeManager typeManager = createTypeManager();
|
||||
|
||||
ScalarType<?> type = typeManager.createEnumScalarType(MyEnum.class, null);
|
||||
|
||||
DataReader reader = mock(DataReader.class);
|
||||
when(reader.getString()).thenReturn("A");
|
||||
Object val = type.read(reader);
|
||||
assertThat(val).isEqualTo(MyEnum.Aval);
|
||||
when(reader.getString()).thenReturn("B");
|
||||
val = type.read(reader);
|
||||
assertThat(val).isEqualTo(MyEnum.Bval);
|
||||
when(reader.getString()).thenReturn("C");
|
||||
val = type.read(reader);
|
||||
assertThat(val).isEqualTo(MyEnum.Cval);
|
||||
|
||||
ScalarType<?> typeGeneral = typeManager.getScalarType(MyEnum.class);
|
||||
assertThat(typeGeneral).isNotNull();
|
||||
ScalarType<?> typeB = typeManager.getScalarType(MyEnum.Bval.getClass());
|
||||
assertThat(typeB).isNotNull();
|
||||
ScalarType<?> typeA = typeManager.getScalarType(MyEnum.Aval.getClass());
|
||||
assertThat(typeA).isNotNull();
|
||||
ScalarType<?> typeC = typeManager.getScalarType(MyEnum.Cval.getClass());
|
||||
assertThat(typeC).isNotNull();
|
||||
|
||||
try {
|
||||
typeManager.createEnumScalarType(MyEnum.class, EnumType.STRING);
|
||||
fail("never get here");
|
||||
} catch (IllegalStateException e) {
|
||||
assertThat(e.getMessage()).contains("It is mapped using 2 different modes when only one is supported");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEnumWithChar() throws SQLException {
|
||||
DefaultTypeManager typeManager = createTypeManager();
|
||||
|
||||
ScalarType<?> dayOfWeekType = typeManager.createEnumScalarType(MyDayOfWeek.class, null);
|
||||
DataReader reader = mock(DataReader.class);
|
||||
when(reader.getString()).thenReturn("MONDAY ");
|
||||
Object val = dayOfWeekType.read(reader);
|
||||
assertThat(val).isEqualTo(MyDayOfWeek.MONDAY);
|
||||
|
||||
when(reader.getString()).thenReturn("TUESDAY ");
|
||||
val = dayOfWeekType.read(reader);
|
||||
assertThat(val).isEqualTo(MyDayOfWeek.TUESDAY);
|
||||
|
||||
when(reader.getString()).thenReturn("WEDNESDAY");
|
||||
val = dayOfWeekType.read(reader);
|
||||
assertThat(val).isEqualTo(MyDayOfWeek.WEDNESDAY);
|
||||
|
||||
when(reader.getString()).thenReturn("THURSDAY ");
|
||||
val = dayOfWeekType.read(reader);
|
||||
assertThat(val).isEqualTo(MyDayOfWeek.THURSDAY);
|
||||
|
||||
when(reader.getString()).thenReturn("FRIDAY ");
|
||||
val = dayOfWeekType.read(reader);
|
||||
assertThat(val).isEqualTo(MyDayOfWeek.FRIDAY);
|
||||
|
||||
try {
|
||||
typeManager.createEnumScalarType(MyDayOfWeek.class, EnumType.ORDINAL);
|
||||
fail("never get here");
|
||||
} catch (IllegalStateException e) {
|
||||
assertThat(e.getMessage()).contains("It is mapped using 2 different modes when only one is supported");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
DefaultTypeManager typeManager = createTypeManager();
|
||||
|
||||
ScalarType<?> scalarType = typeManager.getScalarType(Money.class);
|
||||
assertEquals(Types.DECIMAL, scalarType.getJdbcType());
|
||||
assertFalse(scalarType.isJdbcNative());
|
||||
assertEquals(Money.class, scalarType.getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testWithConfig() {
|
||||
DefaultTypeManager typeManager1 = createTypeManager();
|
||||
ScalarType<?> type1 = typeManager1.createEnumScalarType(MySex.class, null);
|
||||
assertThat(type1).isInstanceOf(ScalarTypeEnumStandard.OrdinalEnum.class);
|
||||
//
|
||||
DefaultTypeManager typeManager2 = createTypeManagerDefaultEnumTypeString();
|
||||
ScalarType<?> type2 = typeManager2.createEnumScalarType(MySex.class, null);
|
||||
assertThat(type2).isInstanceOf(ScalarTypeEnumStandard.StringEnum.class);
|
||||
//
|
||||
DefaultTypeManager typeManager3 = createTypeManagerDefaultEnumTypeString();
|
||||
ScalarType<?> type3 = typeManager3.createEnumScalarType(MySex.class, EnumType.ORDINAL);
|
||||
assertThat(type3).isInstanceOf(ScalarTypeEnumStandard.OrdinalEnum.class);
|
||||
}
|
||||
|
||||
private DefaultTypeManager createTypeManager() {
|
||||
DatabaseConfig config = new DatabaseConfig();
|
||||
config.setDatabasePlatform(new H2Platform());
|
||||
|
||||
BootupClasses bootupClasses = new BootupClasses();
|
||||
bootupClasses.getAttributeConverters().add(MoneyTypeConverter.class);
|
||||
|
||||
return new DefaultTypeManager(config, bootupClasses);
|
||||
}
|
||||
|
||||
private DefaultTypeManager createTypeManagerDefaultEnumTypeString() {
|
||||
DatabaseConfig config = new DatabaseConfig();
|
||||
config.setDatabasePlatform(new H2Platform());
|
||||
config.setDefaultEnumType(EnumType.STRING);
|
||||
|
||||
BootupClasses bootupClasses = new BootupClasses();
|
||||
bootupClasses.getAttributeConverters().add(MoneyTypeConverter.class);
|
||||
|
||||
return new DefaultTypeManager(config, bootupClasses);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCalendar() {
|
||||
DefaultTypeManager typeManager = createTypeManager();
|
||||
ScalarType<?> typeB = typeManager.getScalarType(GregorianCalendar.class);
|
||||
assertThat(typeB).isInstanceOf(ScalarTypeCalendar.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.ScalarTypeConverter;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.ivo.Money;
|
||||
import org.tests.model.ivo.Oid;
|
||||
import org.tests.model.ivo.SysTime;
|
||||
import org.tests.model.ivo.converter.MoneyTypeConverter;
|
||||
import org.tests.model.ivo.converter.OidTypeConverter;
|
||||
import org.tests.model.ivo.converter.SysTimeConverter;
|
||||
|
||||
import javax.persistence.AttributeConverter;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TypeReflectHelperTest {
|
||||
|
||||
@Test
|
||||
public void getParams_MoneyTypeConverter() {
|
||||
|
||||
Class<?>[] params = TypeReflectHelper.getParams(MoneyTypeConverter.class, AttributeConverter.class);
|
||||
|
||||
assertThat(params.length).isEqualTo(2);
|
||||
assertThat(params[0]).isEqualTo(Money.class);
|
||||
assertThat(params[1]).isEqualTo(BigDecimal.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getParams_OidTypeConverter() {
|
||||
|
||||
Class<?>[] params = TypeReflectHelper.getParams(OidTypeConverter.class, ScalarTypeConverter.class);
|
||||
|
||||
assertThat(params.length).isEqualTo(2);
|
||||
assertThat(params[0]).isEqualTo(Oid.class);
|
||||
assertThat(params[1]).isEqualTo(Long.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getParams_SysTimeConverter() {
|
||||
|
||||
Class<?>[] params = TypeReflectHelper.getParams(SysTimeConverter.class, ScalarTypeConverter.class);
|
||||
|
||||
assertThat(params.length).isEqualTo(2);
|
||||
assertThat(params[0]).isEqualTo(SysTime.class);
|
||||
assertThat(params[1]).isEqualTo(Timestamp.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getParams_RichTextConverter() {
|
||||
|
||||
Class<?>[] params = TypeReflectHelper.getParams(RichTextConverter.class, ScalarTypeConverter.class);
|
||||
|
||||
assertThat(params.length).isEqualTo(2);
|
||||
assertThat(params[0]).isEqualTo(RichText.class);
|
||||
assertThat(params[1]).isEqualTo(byte[].class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isEnumType_wildCard() throws NoSuchFieldException {
|
||||
|
||||
Field wildOrderStatus = Some.class.getDeclaredField("wildOrderStatus");
|
||||
assertThat(TypeReflectHelper.isEnumType(getValueType(wildOrderStatus.getGenericType()))).isTrue();
|
||||
|
||||
Class<?> aClass = TypeReflectHelper.asEnumClass(getValueType(wildOrderStatus.getGenericType()));
|
||||
assertThat(aClass).isEqualTo(Order.Status.class);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void isEnumType_simpleType() throws NoSuchFieldException {
|
||||
|
||||
Field orderStatus = Some.class.getDeclaredField("orderStatus");
|
||||
|
||||
assertThat(TypeReflectHelper.isEnumType(getValueType(orderStatus.getGenericType()))).isTrue();
|
||||
|
||||
Class<? extends Enum> aClass = TypeReflectHelper.asEnumClass(getValueType(orderStatus.getGenericType()));
|
||||
assertThat(aClass).isEqualTo(Order.Status.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getValueType_simpleType() throws NoSuchFieldException {
|
||||
|
||||
Field orderStatus = Some.class.getDeclaredField("orderStatus");
|
||||
|
||||
Type expected = getValueType(orderStatus.getGenericType());
|
||||
assertThat(TypeReflectHelper.getValueType(orderStatus.getGenericType())).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getValueType_wildcardType() throws NoSuchFieldException {
|
||||
|
||||
Field orderStatus = Some.class.getDeclaredField("wildOrderStatus");
|
||||
|
||||
Type expected = getValueType(orderStatus.getGenericType());
|
||||
assertThat(TypeReflectHelper.getValueType(orderStatus.getGenericType())).isEqualTo(expected);
|
||||
}
|
||||
|
||||
private Type getValueType(Type genericType) {
|
||||
return ((ParameterizedType) genericType).getActualTypeArguments()[0];
|
||||
}
|
||||
|
||||
private static class Some {
|
||||
|
||||
// note: values are accessed via reflection
|
||||
@SuppressWarnings("unused")
|
||||
List<? extends Order.Status> wildOrderStatus = new ArrayList<>();
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
List<Order.Status> orderStatus = new ArrayList<>();
|
||||
}
|
||||
|
||||
private static class RichText {
|
||||
|
||||
}
|
||||
|
||||
private static class RichTextConverter extends Direct<RichText> {}
|
||||
|
||||
private static class Direct<M> implements ScalarTypeConverter<M, byte[]> {
|
||||
|
||||
@Override
|
||||
public M getNullValue() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public M wrapValue(byte[] scalarType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] unwrapValue(M beanType) {
|
||||
return new byte[0];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class UtilDateParserTest {
|
||||
|
||||
@Test
|
||||
public void parse() {
|
||||
|
||||
Date val = UtilDateParser.parse("2019-05-09");
|
||||
String format = UtilDateParser.format(val);
|
||||
assertThat(format).isEqualTo("2019-05-09");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.tests.model.basic;
|
||||
|
||||
import io.ebean.annotation.DbEnumType;
|
||||
import io.ebean.annotation.DbEnumValue;
|
||||
|
||||
public enum IntEnum {
|
||||
ZERO, ONE, TWO;
|
||||
|
||||
@DbEnumValue(storage = DbEnumType.INTEGER)
|
||||
public int dbValue() {
|
||||
return 100 + ordinal();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.tests.model.basic;
|
||||
|
||||
import org.joda.time.LocalTime;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class TJodaEntity {
|
||||
|
||||
@Id
|
||||
Integer id;
|
||||
|
||||
LocalTime localTime;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public LocalTime getLocalTime() {
|
||||
return localTime;
|
||||
}
|
||||
|
||||
public void setLocalTime(LocalTime localTime) {
|
||||
this.localTime = localTime;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package org.tests.model.basic;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
/**
|
||||
* A basic entity to test simple things.
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "t_oneb")
|
||||
public class TOne {
|
||||
|
||||
@Id
|
||||
Integer id;
|
||||
|
||||
String name;
|
||||
|
||||
String description;
|
||||
|
||||
boolean active;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public boolean isActive() {
|
||||
return active;
|
||||
}
|
||||
|
||||
public void setActive(boolean active) {
|
||||
this.active = active;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.tests.model.basic;
|
||||
|
||||
import io.ebean.annotation.DbEnumType;
|
||||
import io.ebean.annotation.DbEnumValue;
|
||||
|
||||
public enum VarcharEnum {
|
||||
ZERO, ONE, TWO;
|
||||
|
||||
@DbEnumValue(storage = DbEnumType.VARCHAR, withConstraint = false)
|
||||
public String dbValue() {
|
||||
return "xXx" + name();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.tests.model.basic.enums;
|
||||
|
||||
import io.ebean.annotation.EnumValue;
|
||||
|
||||
/**
|
||||
* Enum test when DB CHAR column used with spaces.
|
||||
*/
|
||||
public enum MyDayOfWeek {
|
||||
|
||||
@EnumValue("MONDAY ")MONDAY,
|
||||
@EnumValue("TUESDAY ")TUESDAY,
|
||||
@EnumValue("WEDNESDAY")WEDNESDAY,
|
||||
@EnumValue("THURSDAY ")THURSDAY,
|
||||
@EnumValue("FRIDAY ")FRIDAY,
|
||||
@EnumValue("SATURDAY ")SATURDAY,
|
||||
@EnumValue("SUNDAY ")SUNDAY
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.tests.model.basic.enums;
|
||||
|
||||
import io.ebean.annotation.EnumValue;
|
||||
|
||||
/**
|
||||
* Enum with method overrides (and hence multiple actual classes).
|
||||
*/
|
||||
public enum MyEnum {
|
||||
|
||||
@EnumValue("A")Aval {
|
||||
@Override
|
||||
public String doSomething() {
|
||||
return "bar";
|
||||
}
|
||||
},
|
||||
@EnumValue("B")Bval,
|
||||
|
||||
@EnumValue("C")Cval {
|
||||
@Override
|
||||
public String doSomething() {
|
||||
return "baz";
|
||||
}
|
||||
};
|
||||
|
||||
public String doSomething() {
|
||||
return "foo";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.tests.model.basic.enums;
|
||||
|
||||
public enum MySex {
|
||||
MALE, FEMALE
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package org.tests.model.ivo;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* A representation of Money effectively wrapping BigDecimal.
|
||||
* <p>
|
||||
* <p>
|
||||
* </p>
|
||||
*
|
||||
* @author rbygrave
|
||||
*/
|
||||
public final class Money implements Comparable<Money>, Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static final Money ZERO = new Money(BigDecimal.ZERO);
|
||||
|
||||
private final BigDecimal amount;
|
||||
|
||||
public Money(BigDecimal amount) {
|
||||
if (amount == null) {
|
||||
throw new NullPointerException("amount can not be null");
|
||||
}
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public Money(double val) {
|
||||
this(BigDecimal.valueOf(val));
|
||||
}
|
||||
|
||||
public Money(String val) {
|
||||
this(new BigDecimal(val));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return amount.toString();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return amount.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof Money) {
|
||||
// use BigDecimal.compareTo to handle scale differences
|
||||
return amount.compareTo(((Money) obj).getAmount()) == 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Money o) {
|
||||
return amount.compareTo(o.amount);
|
||||
}
|
||||
|
||||
public BigDecimal getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public Money subtract(Money amt) {
|
||||
BigDecimal b = amount.subtract(amt.amount);
|
||||
return new Money(b);
|
||||
}
|
||||
|
||||
public Money subtract(Money amt, MathContext ctx) {
|
||||
BigDecimal b = amount.subtract(amt.amount, ctx);
|
||||
return new Money(b);
|
||||
}
|
||||
|
||||
public Money add(Money amt) {
|
||||
BigDecimal b = amount.add(amt.amount);
|
||||
return new Money(b);
|
||||
}
|
||||
|
||||
public Money add(Money amt, MathContext ctx) {
|
||||
BigDecimal b = amount.add(amt.amount, ctx);
|
||||
return new Money(b);
|
||||
}
|
||||
|
||||
public Money multiply(Money m) {
|
||||
return multiply(m.amount);
|
||||
}
|
||||
|
||||
public Money multiply(int val) {
|
||||
return multiply(BigDecimal.valueOf(val));
|
||||
}
|
||||
|
||||
public Money multiply(float val) {
|
||||
return multiply(BigDecimal.valueOf(val));
|
||||
}
|
||||
|
||||
public Money multiply(double val) {
|
||||
return multiply(BigDecimal.valueOf(val));
|
||||
}
|
||||
|
||||
public Money multiply(BigDecimal m) {
|
||||
BigDecimal b = amount.multiply(m);
|
||||
return new Money(b);
|
||||
}
|
||||
|
||||
public Money divide(BigDecimal divisor) {
|
||||
BigDecimal b = amount.divide(divisor);
|
||||
return new Money(b);
|
||||
}
|
||||
|
||||
public Money divide(BigDecimal divisor, MathContext ctx) {
|
||||
BigDecimal b = amount.divide(divisor, ctx);
|
||||
return new Money(b);
|
||||
}
|
||||
|
||||
public static Money sum(Money... m) {
|
||||
BigDecimal t = BigDecimal.ZERO;
|
||||
for (Money money : m) {
|
||||
t = t.add(money.amount);
|
||||
}
|
||||
return new Money(t);
|
||||
}
|
||||
|
||||
public static Money sum(Iterator<Money> it) {
|
||||
BigDecimal t = BigDecimal.ZERO;
|
||||
while (it.hasNext()) {
|
||||
t = t.add(it.next().amount);
|
||||
}
|
||||
return new Money(t);
|
||||
}
|
||||
|
||||
public static Money sum(Iterator<Money> it, MathContext ctx) {
|
||||
BigDecimal t = BigDecimal.ZERO;
|
||||
while (it.hasNext()) {
|
||||
t = t.add(it.next().amount, ctx);
|
||||
}
|
||||
return new Money(t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package org.tests.model.ivo;
|
||||
|
||||
|
||||
public class Oid<T> {
|
||||
|
||||
private final long value;
|
||||
|
||||
public Oid(long value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
public long getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return (int) (value ^ (value >>> 32));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o instanceof Oid<?>) {
|
||||
return ((Oid<?>) o).value == value;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static Oid<?> valueOf(String s) {
|
||||
return new Oid<>(Integer.valueOf(s));
|
||||
}
|
||||
|
||||
public static Oid<?> valueOf(long i) {
|
||||
return new Oid<>(i);
|
||||
}
|
||||
|
||||
public static <T> Oid<T> valueOf(Class<T> cls, String s) {
|
||||
Integer v = Integer.valueOf(s);
|
||||
return new Oid<>(v);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.tests.model.ivo;
|
||||
|
||||
public class SysTime {
|
||||
|
||||
private final long millis;
|
||||
|
||||
public SysTime(long millis) {
|
||||
this.millis = millis;
|
||||
}
|
||||
|
||||
public long getMillis() {
|
||||
return millis;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.tests.model.ivo.converter;
|
||||
|
||||
import org.tests.model.ivo.Money;
|
||||
|
||||
import javax.persistence.AttributeConverter;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* Converts between Money and BigDecimal.
|
||||
*/
|
||||
public class MoneyTypeConverter implements AttributeConverter<Money,BigDecimal> {
|
||||
|
||||
@Override
|
||||
public BigDecimal convertToDatabaseColumn(Money beanType) {
|
||||
return beanType.getAmount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Money convertToEntityAttribute(BigDecimal scalarType) {
|
||||
return new Money(scalarType);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.tests.model.ivo.converter;
|
||||
|
||||
import io.ebean.config.ScalarTypeConverter;
|
||||
import org.tests.model.ivo.Oid;
|
||||
|
||||
public class OidTypeConverter implements ScalarTypeConverter<Oid<?>,Long> {
|
||||
|
||||
public static final Oid<?> NULL_VALUE = new Oid<>(0);
|
||||
|
||||
@Override
|
||||
public Oid<?> getNullValue() {
|
||||
return NULL_VALUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Oid<?> wrapValue(Long scalarType) {
|
||||
return new Oid<>(scalarType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long unwrapValue(Oid<?> beanType) {
|
||||
if (NULL_VALUE.equals(beanType)) {
|
||||
return null;
|
||||
}
|
||||
return beanType.getValue();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package org.tests.model.ivo.converter;
|
||||
|
||||
import io.ebean.config.ScalarTypeConverter;
|
||||
import org.tests.model.ivo.SysTime;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
public class SysTimeConverter implements ScalarTypeConverter<SysTime, Timestamp> {
|
||||
|
||||
|
||||
@Override
|
||||
public SysTime getNullValue() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Timestamp unwrapValue(SysTime beanType) {
|
||||
return new Timestamp(beanType.getMillis());
|
||||
}
|
||||
|
||||
@Override
|
||||
public SysTime wrapValue(Timestamp scalarType) {
|
||||
return new SysTime(scalarType.getTime());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user