@TestCoversMethods({"toMap", "prepare"}) public void testToMap_POJO3() throws Exception { // to map POJO3 p1 = new POJO3(1, "one", Arrays.asList(new Double[] {1.0})); checkMap( mapper.toMap(p1, (Object[]) null), "id", p1.getId(), "name", p1.getName(), "value", p1.getValue()); // to map for "trees" POJO3 p2 = new POJO3(2, "two"); POJO3Parent pp1 = new POJO3Parent(); pp1.setChildren(Arrays.asList(new POJO3[] {p1})); POJO3Parent pp2 = new POJO3Parent(); pp2.setChildren(Arrays.asList(new POJO3[] {p2})); pp1.setSibling(pp2); Map<String, Object> mp1 = mapper.toMap(pp1, (Object[]) null); Map<String, Object> mp2 = mapper.toMap(pp2, (Object[]) null); Map<String, Object> m1 = mapper.toMap(p1, (Object[]) null); Map<String, Object> m2 = mapper.toMap(p2, (Object[]) null); List<Object> c1 = Arrays.asList(new Object[] {m1}); List<Object> c2 = Arrays.asList(new Object[] {m2}); checkMap(m1, "id", p1.getId(), "name", p1.getName(), "value", p1.getValue()); checkMap(m2, "id", p2.getId(), "name", p2.getName(), "value", p2.getValue()); checkMap(mp2, "sibling", null, "children", c2); checkMap(mp1, "sibling", mp2, "children", c1); }
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((children == null) ? 0 : children.hashCode()); result = prime * result + ((sibling == null) ? 0 : sibling.hashCode()); return result; }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; POJO3Parent other = (POJO3Parent) obj; if (children == null) { if (other.children != null) return false; } else if (!children.equals(other.children)) return false; if (sibling == null) { if (other.sibling != null) return false; } else if (!sibling.equals(other.sibling)) return false; return true; }
@TestCoversMethods("fromMap") public void testFromMap_POJO3() throws Exception { // from map POJO3 p1 = new POJO3(1, "one", Arrays.asList(new Double[] {1.0})); Map<String, Object> m1 = mapper.toMap(p1, (Object[]) null); POJO3 expected; expected = new POJO3(p1.getId(), p1.getName(), p1.getValue()); System.out.println("----"); assertEquals(expected, mapper.fromMap(new POJO3(), m1, (Object[]) null)); // from map for "trees" POJO3 p2 = new POJO3(2, "two"); POJO3Parent pp1 = new POJO3Parent(); pp1.setChildren(Arrays.asList(new POJO3[] {p1})); POJO3Parent pp2 = new POJO3Parent(); pp2.setChildren(Arrays.asList(new POJO3[] {p2})); pp1.setSibling(pp2); Map<String, Object> mp1 = mapper.toMap(pp1, (Object[]) null); assertEquals(pp1, mapper.fromMap(new POJO3Parent(), mp1, (Object[]) null)); }