Commit 8d4a48e7 authored by Piotr Gawron's avatar Piotr Gawron

validation for Integer, Color, Calendar, Enum, Class types added

parent d6f62241
package lcsb.mapviewer.persist.dao;
import java.awt.Color;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.*;
......@@ -369,8 +370,9 @@ public abstract class BaseDao<T> {
u + " is too long (limit: " + column.length() + "; found: " + newObject.length() + ")" + newObject);
}
}
} else if (u.getType() == Double.class) {
Double newObject = (Double) u.get(t);
} else if (u.getType() == Double.class || u.getType() == Integer.class || u.getType() == Class.class
|| u.getType() == Color.class || u.getType() == Calendar.class || u.getType().isEnum()) {
Object newObject = u.get(t);
Column column = getAnnotationColumn(u);
if (column != null && !column.nullable()) {
if (newObject == null) {
......@@ -403,41 +405,56 @@ public abstract class BaseDao<T> {
@Override
public void accept(Object t, Field u) {
try {
if (u.getType() == String.class) {
u.setAccessible(true);
String newObject = (String) u.get(t);
Column column = getAnnotationColumn(u);
if (column != null) {
u.setAccessible(true);
Column column = getAnnotationColumn(u);
Object newObject = u.get(t);
if (column != null) {
if (u.getType() == String.class) {
String newString = (String) u.get(t);
if (!column.nullable()) {
if (newObject == null) {
if (newString == null) {
u.set(t, "");
}
}
if (newObject != null && column.length() <= newObject.length()
if (newString != null && column.length() <= newString.length()
&& !column.columnDefinition().equals("TEXT")) {
u.set(t, newObject.substring(0, column.length() - 1));
u.set(t, newString.substring(0, column.length() - 1));
}
}
} else if (u.getType() == Double.class) {
u.setAccessible(true);
Object newObject = u.get(t);
Column column = getAnnotationColumn(u);
if (column != null && !column.nullable()) {
if (newObject == null) {
} else if (u.getType() == Double.class) {
if (!column.nullable() && newObject == null) {
u.set(t, 0.0);
}
} else if (u.getType() == Integer.class) {
if (!column.nullable() && newObject == null) {
u.set(t, 0);
}
} else if (u.getType() == Color.class) {
if (!column.nullable() && newObject == null) {
u.set(t, Color.BLACK);
}
} else if (u.getType() == Class.class) {
if (!column.nullable() && newObject == null) {
u.set(t, Object.class);
}
} else if (u.getType() == Calendar.class) {
if (!column.nullable() && newObject == null) {
u.set(t, Calendar.getInstance());
}
} else if (u.getType().isEnum()) {
if (!column.nullable() && newObject == null) {
u.set(t, u.getType().getEnumConstants()[0]);
}
} else {
logger.error("Don't know how to handle class: " + u.getType());
}
} else {
logger.error("Don't know how to handle class: " + u.getType());
}
} catch (IllegalArgumentException | IllegalAccessException e) {
logger.error("Problem with validating property", e);
}
}
});
}
private void processValidationIssues(T o, BiConsumer<Object, Field> processIssue) {
......@@ -456,7 +473,6 @@ public abstract class BaseDao<T> {
skippedClasses.add(char.class);
skippedClasses.add(short.class);
skippedClasses.add(byte.class);
skippedClasses.add(Calendar.class);
skippedClasses.add(User.class);
......@@ -487,10 +503,12 @@ public abstract class BaseDao<T> {
processIssue.accept(object, arg0);
}
}
} else if (arg0.getType() == Double.class) {
} else if (arg0.getType() == Double.class || arg0.getType() == Integer.class || arg0.getType() == Color.class
|| arg0.getType() == Calendar.class || arg0.getType() == Class.class
|| arg0.getType() == Calendar.class || arg0.getType().isEnum()) {
arg0.setAccessible(true);
Double newObject = (Double) arg0.get(object);
Object newObject = arg0.get(object);
Column column = getAnnotationColumn(arg0);
if (column != null) {
if (!column.nullable()) {
......
......@@ -2,6 +2,7 @@ package lcsb.mapviewer.persist.dao;
import static org.junit.Assert.assertEquals;
import java.awt.Color;
import java.io.IOException;
import java.util.*;
......@@ -13,6 +14,7 @@ import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.mockito.Mockito;
import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.persist.PersistTestFunctions;
@RunWith(Parameterized.class)
......@@ -31,6 +33,31 @@ public class BaseDaoTest extends PersistTestFunctions {
Double val = null;
}
static class NullInteger {
@Column(nullable = false)
Integer val = null;
}
static class NullColor {
@Column(nullable = false)
Color val = null;
}
static class NullEnum {
@Column(nullable = false)
MiriamType val = null;
}
static class NullCalendar {
@Column(nullable = false)
Calendar val = null;
}
static class NullClass {
@Column(nullable = false)
Class<?> val = null;
}
static class ToLongString {
@Column(length = 3)
String val = "xxxx";
......@@ -47,8 +74,13 @@ public class BaseDaoTest extends PersistTestFunctions {
Collection<Object[]> data = new ArrayList<Object[]>();
data.add(new Object[] { "Too long String", ToLongString.class });
data.add(new Object[] { "Null String when not nullable", NullString.class });
data.add(new Object[] { "Null Double when not nullable", NullDouble.class });
data.add(new Object[] { "Null String for not nullable", NullString.class });
data.add(new Object[] { "Null Double for not nullable", NullDouble.class });
data.add(new Object[] { "Null Integer for not nullable", NullInteger.class });
data.add(new Object[] { "Null Color for not nullable", NullColor.class });
data.add(new Object[] { "Null Enum for not nullable", NullEnum.class });
data.add(new Object[] { "Null Calendar for not nullable", NullCalendar.class });
data.add(new Object[] { "Null Class for not nullable", NullClass.class });
return data;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment