Refactor and fixes for interfaces (like Path) + extend existing tests

This commit is contained in:
Jonas Pöhler
2022-01-05 14:22:24 +01:00
parent 3dc6c567a0
commit eb21b41fba
3 changed files with 36 additions and 19 deletions
@@ -250,8 +250,7 @@ public final class DefaultTypeManager implements TypeManager {
@Override
public ScalarType<?> getScalarType(Class<?> type) {
ScalarType<?> found = typeMap.get(type);
if (found == null || found == ScalarTypeNotFound.INSTANCE) {
found = null;
if (found == null) {
if (type.getName().equals("org.joda.time.LocalTime")) {
throw new IllegalStateException(
"ScalarType of Joda LocalTime not defined. You need to set DatabaseConfig.jodaLocalTimeMode to"
@@ -259,19 +258,19 @@ public final class DefaultTypeManager implements TypeManager {
}
found = checkInheritedTypes(type);
}
return found;
return found != ScalarTypeNotFound.INSTANCE ? found : null; // Do not return ScalarTypeNotFound, otherwise checks will fail
}
/**
* Checks the typeMap for inherited types.
*
* If <code>type</code> is a <code>GregorianCalendar</code>, then this method
*
* If e.g. <code>type</code> is a <code>GregorianCalendar</code>, then this method
* will check the class hierarchy and will probably return a
* <code>ScalarTypeCalendar</code> To speed up a second lookup, it will write
* back the found scalarType to typeMap.
*
* @param type
* @return
*
* @param type the for which to search for a <code>ScalarType</code>
* @return either a valid <code>ScalarType</code> if one could be found or {@link ScalarTypeNotFound#INSTANCE} if not
*/
private ScalarType<?> checkInheritedTypes(Class<?> type) {
// first step loop through inheritance chain
@@ -282,18 +281,18 @@ public final class DefaultTypeManager implements TypeManager {
typeMap.put(type, found); // store type for next lookup
return found;
}
// second step - loop through interfaces of this type
for (Class<?> iface: parent.getInterfaces()) {
found = checkInheritedTypes(iface);
if (found != null && found != ScalarTypeNotFound.INSTANCE) {
typeMap.put(type, found); // store type for next lookup
return found;
}
}
parent = parent.getSuperclass();
}
// second step - loop through interface chain
for (Class<?> iface: type.getInterfaces()) {
ScalarType<?> found = checkInheritedTypes(iface);
if (found != null && found != ScalarTypeNotFound.INSTANCE) {
typeMap.put(type, found); // store type for next lookup
return found;
}
}
typeMap.put(type, ScalarTypeNotFound.INSTANCE);
return null; // no success
return ScalarTypeNotFound.INSTANCE; // no success
}
@Override
@@ -399,7 +398,7 @@ public final class DefaultTypeManager implements TypeManager {
private DocPropertyType getDocType(Type genericType) {
if (genericType instanceof Class<?>) {
ScalarType<?> found = getScalarType((Class<?>)genericType);
if (found != null && found != ScalarTypeNotFound.INSTANCE) {
if (found != null) {
return found.getDocType();
}
}
@@ -7,6 +7,7 @@ import javax.persistence.Version;
import java.nio.file.Path;
import java.sql.Date;
import java.time.*;
import java.util.Calendar;
@Entity
public class SomeNewTypesBean {
@@ -58,6 +59,8 @@ public class SomeNewTypesBean {
Duration duration;
Calendar calendar;
public Long getId() {
return id;
}
@@ -217,4 +220,12 @@ public class SomeNewTypesBean {
public void setDuration(Duration duration) {
this.duration = duration;
}
public Calendar getCalendar() {
return calendar;
}
public void setCalendar(final Calendar calendar) {
this.calendar = calendar;
}
}
@@ -10,6 +10,8 @@ import org.tests.model.types.SomeNewTypesBean;
import java.io.File;
import java.nio.file.Paths;
import java.time.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@@ -44,6 +46,7 @@ public class TestNewTypes extends BaseTestCase {
bean.setPath(Paths.get(TEMP_PATH));
bean.setPeriod(Period.of(4,3,2));
bean.setDuration(Duration.ofMinutes(5));
bean.setCalendar(Calendar.getInstance());
DB.save(bean);
@@ -104,6 +107,9 @@ public class TestNewTypes extends BaseTestCase {
list = DB.find(SomeNewTypesBean.class).where().eq("duration", Duration.ofMinutes(5)).findList();
assertThat(list).isNotEmpty();
list = DB.find(SomeNewTypesBean.class).where().le("calendar", GregorianCalendar.getInstance()).findList();
assertThat(list).isNotEmpty();
SomeNewTypesBean fetched = DB.find(SomeNewTypesBean.class, bean.getId());
assertEquals(bean.getZoneId(), fetched.getZoneId());
@@ -122,6 +128,7 @@ public class TestNewTypes extends BaseTestCase {
assertEquals(bean.getPath(), fetched.getPath());
assertEquals(bean.getPeriod(), fetched.getPeriod());
assertEquals(bean.getDuration(), fetched.getDuration());
assertEquals(bean.getCalendar(), fetched.getCalendar());
String asJson = DB.json().toJson(fetched);
@@ -139,10 +146,10 @@ public class TestNewTypes extends BaseTestCase {
assertThat(toBean.getOffsetDateTime()).isEqualToIgnoringNanos(bean.getOffsetDateTime());
assertEquals(bean.getLocalTime().toSecondOfDay(), toBean.getLocalTime().toSecondOfDay());
assertEquals(bean.getInstant().toEpochMilli() / 1000, toBean.getInstant().toEpochMilli() / 1000);
// FIXME: This test fails on Windows with: expected:<\tmp> but was:<C:\tmp>
assertEquals(bean.getPath(), toBean.getPath());
assertEquals(bean.getPeriod(), toBean.getPeriod());
assertEquals(bean.getDuration(), toBean.getDuration());
assertEquals(bean.getCalendar(), toBean.getCalendar());
}