use of com.serotonin.m2m2.util.JsonSerializableUtility in project ma-core-public by infiniteautomation.
the class JsonSerializableUtilityTest method testIntArrayOrder.
@Test
public void testIntArrayOrder() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, JsonException, IOException {
JsonSerializableTestObject o1 = new JsonSerializableTestObject();
JsonSerializableTestObject o2 = new JsonSerializableTestObject();
JsonSerializableUtility util = new JsonSerializableUtility();
Map<String, Object> changes = util.findChanges(o1, o2);
if (changes.size() != 0)
Assert.fail("Int array should not have changed");
o1.setIntArray(new int[] { 3, 2, 1 });
o2.setIntArray(new int[] { 1, 2, 3 });
changes = util.findChanges(o1, o2);
if (changes.size() != 1)
Assert.fail("Int array should have changed");
int[] changed = (int[]) changes.get("intArray");
Assert.assertNotNull("Int array should have changed", changed);
Assert.assertEquals((int) 3, changed.length);
Assert.assertEquals(1, changed[0]);
Assert.assertEquals(2, changed[1]);
Assert.assertEquals(3, changed[2]);
}
use of com.serotonin.m2m2.util.JsonSerializableUtility in project ma-core-public by infiniteautomation.
the class JsonSerializableUtilityTest method testwriterListLength.
// TODO Test writerMap
@Test
public void testwriterListLength() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, JsonException, IOException {
List<Object> annotatedList1 = new ArrayList<>();
annotatedList1.add(new Integer(1));
annotatedList1.add(new Integer(2));
List<Object> annotatedList2 = new ArrayList<>();
annotatedList2.add(new Integer(1));
annotatedList2.add(new Integer(2));
JsonSerializableTestObject o1 = new JsonSerializableTestObject();
o1.setAnnotatedList(annotatedList1);
JsonSerializableTestObject o2 = new JsonSerializableTestObject();
o2.setAnnotatedList(annotatedList2);
JsonSerializableUtility util = new JsonSerializableUtility();
Map<String, Object> changes = util.findChanges(o1, o2);
// No Changes
if (changes.size() != 0)
Assert.fail("List should not have changed");
// Modify the List
annotatedList2.remove(1);
changes = util.findChanges(o1, o2);
if (changes.size() != 1)
Assert.fail("List should have changed");
@SuppressWarnings("unchecked") List<Object> changedList = (List<Object>) changes.get("annotatedList");
Assert.assertNotNull("List should have changed", changedList);
Assert.assertEquals((int) 1, changedList.size());
Assert.assertEquals((int) 1, changedList.get(0));
}
use of com.serotonin.m2m2.util.JsonSerializableUtility in project ma-core-public by infiniteautomation.
the class JsonSerializableUtilityTest method testWriterListOrder.
@Test
public void testWriterListOrder() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, JsonException, IOException {
List<Object> writerList1 = new ArrayList<>();
writerList1.add(new Integer(1));
writerList1.add(new Integer(2));
List<Object> writerList2 = new ArrayList<>();
writerList2.add(new Integer(1));
writerList2.add(new Integer(2));
JsonSerializableTestObject o1 = new JsonSerializableTestObject();
o1.setWriterList(writerList1);
JsonSerializableTestObject o2 = new JsonSerializableTestObject();
o2.setWriterList(writerList2);
JsonSerializableUtility util = new JsonSerializableUtility();
Map<String, Object> changes = util.findChanges(o1, o2);
// No Changes
if (changes.size() != 0)
Assert.fail("List should not have changed");
// Modify the List
writerList2.clear();
writerList2.add(new Integer(2));
writerList2.add(new Integer(1));
changes = util.findChanges(o1, o2);
if (changes.size() != 1)
Assert.fail("List should have changed");
@SuppressWarnings("unchecked") List<Object> changedList = (List<Object>) changes.get("writerList");
Assert.assertNotNull("List should have changed", changedList);
Assert.assertEquals((int) 2, changedList.size());
Assert.assertEquals((int) 2, changedList.get(0));
Assert.assertEquals((int) 1, changedList.get(1));
}
use of com.serotonin.m2m2.util.JsonSerializableUtility in project ma-core-public by infiniteautomation.
the class JsonSerializableUtilityTest method testObjectArray.
@Test
public void testObjectArray() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, JsonException, IOException {
JsonSerializableTestObject o1 = new JsonSerializableTestObject();
JsonSerializableTestObject o2 = new JsonSerializableTestObject();
JsonSerializableUtility util = new JsonSerializableUtility();
Map<String, Object> changes = util.findChanges(o1, o2);
if (changes.size() != 0)
Assert.fail("Object array should not have changed");
o1.setObjectArray(new Object[] { new Integer(0), new Integer(1) });
o2.setObjectArray(new Object[] { new Integer(2), new Integer(3) });
changes = util.findChanges(o1, o2);
if (changes.size() != 1)
Assert.fail("Object array should have changed");
Object[] changed = (Object[]) changes.get("objectArray");
Assert.assertNotNull("Object array should have changed", changed);
Assert.assertEquals((int) 2, changed.length);
Assert.assertEquals(2, changed[0]);
Assert.assertEquals(3, changed[1]);
}
use of com.serotonin.m2m2.util.JsonSerializableUtility in project ma-core-public by infiniteautomation.
the class JsonSerializableUtilityTest method testAnnotatedMap.
@Test
public void testAnnotatedMap() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, JsonException, IOException {
Map<String, Object> annotatedMap1 = new HashMap<>();
annotatedMap1.put("int", new Integer(1));
annotatedMap1.put("string", new String("one"));
Map<String, Object> annotatedMap2 = new HashMap<>();
annotatedMap2.put("int", new Integer(1));
annotatedMap2.put("string", new String("one"));
JsonSerializableTestObject o1 = new JsonSerializableTestObject();
o1.setAnnotatedMap(annotatedMap1);
JsonSerializableTestObject o2 = new JsonSerializableTestObject();
o2.setAnnotatedMap(annotatedMap2);
JsonSerializableUtility util = new JsonSerializableUtility();
Map<String, Object> changes = util.findChanges(o1, o2);
if (changes.size() != 0)
Assert.fail("annotatedMap should not have changed.");
// Make change
annotatedMap2.put("string", new String("two"));
annotatedMap2.put("int", new Integer(2));
changes = util.findChanges(o1, o2);
if (changes.size() != 1)
Assert.fail("annotatedMap should have changed");
@SuppressWarnings("unchecked") Map<String, Object> changedMap = (Map<String, Object>) changes.get("annotatedMap");
Assert.assertNotNull("annotatedMap change should have found", changedMap);
Assert.assertEquals("two", changedMap.get("string"));
Assert.assertEquals((int) 2, (int) changedMap.get("int"));
}
Aggregations