Skip to content

Commit cf2ec68

Browse files
committed
Improve error handling for wrong types set to attributes
1 parent a2b417f commit cf2ec68

File tree

2 files changed

+68
-35
lines changed

2 files changed

+68
-35
lines changed

src/main/java/org/gephi/graph/impl/ElementImpl.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ private void setTimeAttribute(Column column, Object value, Object timeObject) {
269269
checkColumn(column);
270270
checkColumnDynamic(column);
271271
checkReadOnlyColumn(column);
272-
checkType(column, value);
272+
checkDynamicType(column, value);
273273

274274
Object newValue = attributes.setAttribute(column, value, timeObject);
275275
updateIndex(column, null, newValue);
@@ -558,7 +558,7 @@ void checkColumnDynamicAttribute(Column column) {
558558
}
559559
}
560560

561-
void checkType(Column column, Object value) {
561+
void checkDynamicType(Column column, Object value) {
562562
if (value != null) {
563563
Class typeClass = column.getTypeClass();
564564
if (TimestampMap.class.isAssignableFrom(typeClass)) {
@@ -591,24 +591,31 @@ void checkType(Column column, Object value) {
591591
throw new IllegalArgumentException(
592592
"The object class does not match with the dynamic type (" + typeClass.getName() + ")");
593593
}
594-
} else if (List.class.isAssignableFrom(typeClass)) {
594+
}
595+
}
596+
}
597+
598+
void checkType(Column column, Object value) {
599+
if (value != null) {
600+
Class typeClass = column.getTypeClass();
601+
if (List.class.isAssignableFrom(typeClass)) {
595602
if (!(value instanceof List)) {
596-
throw new IllegalArgumentException(
597-
"The object class does not match with the list type (" + typeClass.getName() + ")");
603+
throw new IllegalArgumentException("The object class " + value.getClass()
604+
.getName() + " does not match with the list type (" + typeClass.getName() + ")");
598605
}
599606
} else if (Set.class.isAssignableFrom(typeClass)) {
600607
if (!(value instanceof Set)) {
601-
throw new IllegalArgumentException(
602-
"The object class does not match with the set type (" + typeClass.getName() + ")");
608+
throw new IllegalArgumentException("The object class " + value.getClass()
609+
.getName() + " does not match with the set type (" + typeClass.getName() + ")");
603610
}
604611
} else if (Map.class.isAssignableFrom(typeClass)) {
605612
if (!(value instanceof Map)) {
606-
throw new IllegalArgumentException(
607-
"The object class does not match with the map type (" + typeClass.getName() + ")");
613+
throw new IllegalArgumentException("The object class " + value.getClass()
614+
.getName() + " does not match with the map type (" + typeClass.getName() + ")");
608615
}
609616
} else if (!value.getClass().equals(typeClass)) {
610-
throw new IllegalArgumentException(
611-
"The object class does not match with the column type (" + typeClass.getName() + ")");
617+
throw new IllegalArgumentException("The object class " + value.getClass()
618+
.getName() + " does not match with the column type (" + typeClass.getName() + ")");
612619
}
613620
}
614621
}

src/test/java/org/gephi/graph/impl/ElementImplTest.java

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,51 +1117,77 @@ public void testGetTimestampAttributeInView() {
11171117
}
11181118

11191119
@Test
1120-
public void testCheckType() {
1120+
public void testCheckDynamicType() {
11211121
GraphStore store = new GraphStore();
11221122

11231123
NodeImpl node = new NodeImpl("0", store);
1124-
node.checkType(new ColumnImpl("0", TimestampIntegerMap.class, null, null, Origin.DATA, false, false), 1);
1125-
node.checkType(new ColumnImpl("0", TimestampDoubleMap.class, null, null, Origin.DATA, false, false), 1.0);
1126-
node.checkType(new ColumnImpl("0", TimestampFloatMap.class, null, null, Origin.DATA, false, false), 1f);
1127-
node.checkType(new ColumnImpl("0", TimestampByteMap.class, null, null, Origin.DATA, false, false), (byte) 1);
1128-
node.checkType(new ColumnImpl("0", TimestampShortMap.class, null, null, Origin.DATA, false, false), (short) 1);
1129-
node.checkType(new ColumnImpl("0", TimestampLongMap.class, null, null, Origin.DATA, false, false), 1l);
1130-
node.checkType(new ColumnImpl("0", TimestampCharMap.class, null, null, Origin.DATA, false, false), 'a');
1131-
node.checkType(new ColumnImpl("0", TimestampBooleanMap.class, null, null, Origin.DATA, false, false), true);
1132-
node.checkType(new ColumnImpl("0", TimestampStringMap.class, null, null, Origin.DATA, false, false), "foo");
1124+
node.checkDynamicType(new ColumnImpl("0", TimestampIntegerMap.class, null, null, Origin.DATA, false, false), 1);
1125+
node.checkDynamicType(new ColumnImpl("0", TimestampDoubleMap.class, null, null, Origin.DATA, false,
1126+
false), 1.0);
1127+
node.checkDynamicType(new ColumnImpl("0", TimestampFloatMap.class, null, null, Origin.DATA, false, false), 1f);
1128+
node.checkDynamicType(new ColumnImpl("0", TimestampByteMap.class, null, null, Origin.DATA, false,
1129+
false), (byte) 1);
1130+
node.checkDynamicType(new ColumnImpl("0", TimestampShortMap.class, null, null, Origin.DATA, false,
1131+
false), (short) 1);
1132+
node.checkDynamicType(new ColumnImpl("0", TimestampLongMap.class, null, null, Origin.DATA, false, false), 1l);
1133+
node.checkDynamicType(new ColumnImpl("0", TimestampCharMap.class, null, null, Origin.DATA, false, false), 'a');
1134+
node.checkDynamicType(new ColumnImpl("0", TimestampBooleanMap.class, null, null, Origin.DATA, false,
1135+
false), true);
1136+
node.checkDynamicType(new ColumnImpl("0", TimestampStringMap.class, null, null, Origin.DATA, false,
1137+
false), "foo");
11331138
}
11341139

11351140
@Test(expectedExceptions = RuntimeException.class)
1136-
public void testCheckTypeWithWrongIntervalConfiguration() {
1141+
public void testCheckDynamicTypeWithWrongIntervalConfiguration() {
11371142
GraphStore store = new GraphStore();
11381143

11391144
NodeImpl node = new NodeImpl("0", store);
1140-
node.checkType(new ColumnImpl("0", IntervalIntegerMap.class, null, null, Origin.DATA, false, false), 1);
1145+
node.checkDynamicType(new ColumnImpl("0", IntervalIntegerMap.class, null, null, Origin.DATA, false, false), 1);
11411146
}
11421147

11431148
@Test(expectedExceptions = RuntimeException.class)
1144-
public void testCheckTypeWithWrongTimestampConfiguration() {
1149+
public void testCheckDynamicTypeWithWrongTimestampConfiguration() {
11451150
GraphStore store = getIntervalGraphStore();
11461151

11471152
NodeImpl node = new NodeImpl("0", store);
1148-
node.checkType(new ColumnImpl("0", TimestampIntegerMap.class, null, null, Origin.DATA, false, false), 1);
1153+
node.checkDynamicType(new ColumnImpl("0", TimestampIntegerMap.class, null, null, Origin.DATA, false, false), 1);
11491154
}
11501155

11511156
@Test
1152-
public void testCheckTypeInterval() {
1157+
public void testDynamicCheckTypeInterval() {
11531158
GraphStore store = getIntervalGraphStore();
11541159

11551160
NodeImpl node = new NodeImpl("0", store);
1156-
node.checkType(new ColumnImpl("0", IntervalIntegerMap.class, null, null, Origin.DATA, false, false), 1);
1157-
node.checkType(new ColumnImpl("0", IntervalDoubleMap.class, null, null, Origin.DATA, false, false), 1.0);
1158-
node.checkType(new ColumnImpl("0", IntervalFloatMap.class, null, null, Origin.DATA, false, false), 1f);
1159-
node.checkType(new ColumnImpl("0", IntervalByteMap.class, null, null, Origin.DATA, false, false), (byte) 1);
1160-
node.checkType(new ColumnImpl("0", IntervalShortMap.class, null, null, Origin.DATA, false, false), (short) 1);
1161-
node.checkType(new ColumnImpl("0", IntervalLongMap.class, null, null, Origin.DATA, false, false), 1l);
1162-
node.checkType(new ColumnImpl("0", IntervalCharMap.class, null, null, Origin.DATA, false, false), 'a');
1163-
node.checkType(new ColumnImpl("0", IntervalBooleanMap.class, null, null, Origin.DATA, false, false), true);
1164-
node.checkType(new ColumnImpl("0", IntervalStringMap.class, null, null, Origin.DATA, false, false), "foo");
1161+
node.checkDynamicType(new ColumnImpl("0", IntervalIntegerMap.class, null, null, Origin.DATA, false, false), 1);
1162+
node.checkDynamicType(new ColumnImpl("0", IntervalDoubleMap.class, null, null, Origin.DATA, false, false), 1.0);
1163+
node.checkDynamicType(new ColumnImpl("0", IntervalFloatMap.class, null, null, Origin.DATA, false, false), 1f);
1164+
node.checkDynamicType(new ColumnImpl("0", IntervalByteMap.class, null, null, Origin.DATA, false,
1165+
false), (byte) 1);
1166+
node.checkDynamicType(new ColumnImpl("0", IntervalShortMap.class, null, null, Origin.DATA, false,
1167+
false), (short) 1);
1168+
node.checkDynamicType(new ColumnImpl("0", IntervalLongMap.class, null, null, Origin.DATA, false, false), 1l);
1169+
node.checkDynamicType(new ColumnImpl("0", IntervalCharMap.class, null, null, Origin.DATA, false, false), 'a');
1170+
node.checkDynamicType(new ColumnImpl("0", IntervalBooleanMap.class, null, null, Origin.DATA, false,
1171+
false), true);
1172+
node.checkDynamicType(new ColumnImpl("0", IntervalStringMap.class, null, null, Origin.DATA, false,
1173+
false), "foo");
1174+
}
1175+
1176+
@Test
1177+
public void checkType() {
1178+
GraphStore store = new GraphStore();
1179+
1180+
NodeImpl node = new NodeImpl("0", store);
1181+
node.checkType(new ColumnImpl("0", Integer.class, null, null, Origin.DATA, false, false), 1);
1182+
node.checkType(new ColumnImpl("0", Double.class, null, null, Origin.DATA, false, false), 1.0);
1183+
node.checkType(new ColumnImpl("0", Float.class, null, null, Origin.DATA, false, false), 1f);
1184+
}
1185+
1186+
@Test(expectedExceptions = IllegalArgumentException.class)
1187+
public void checkTypeException() {
1188+
GraphStore store = new GraphStore();
1189+
NodeImpl node = new NodeImpl("0", store);
1190+
node.checkType(new ColumnImpl("0", Integer.class, null, null, Origin.DATA, false, false), "foo");
11651191
}
11661192

11671193
@Test

0 commit comments

Comments
 (0)