@TestCoversMethods("fromMap") public void testFromMap_Mapable() throws Exception { // from map POJO2 p1 = new POJO2(1, "one", Arrays.asList(new Object[] {1.0})); Map<String, Object> m1 = mapper.toMap(p1, (Object[]) null); POJO2 expected; expected = new POJO2(p1.getId(), p1.getName(), p1.getValue()); assertEquals(expected, mapper.fromMap(new POJO2(), m1, (Object[]) null)); expected = new POJO2(p1.getId(), null, null); assertEquals(expected, mapper.fromMap(new POJO2(), m1, new Object[] {"id"})); expected = new POJO2(0, p1.getName(), null); assertEquals(expected, mapper.fromMap(new POJO2(), m1, new Object[] {"name"})); expected = new POJO2(0, null, p1.getValue()); assertEquals(expected, mapper.fromMap(new POJO2(), m1, new Object[] {"value"})); expected = new POJO2(p1.getId(), p1.getName(), p1.getValue()); assertEquals(expected, mapper.fromMap(new POJO2(), m1, new Object[] {"id", "name", "value"})); // from map for "trees" POJO2 p2 = new POJO2(2, "two"); POJO2Parent pp1 = new POJO2Parent(); pp1.setChildren(Arrays.asList(new POJO2[] {p1})); POJO2Parent pp2 = new POJO2Parent(); pp2.setChildren(Arrays.asList(new POJO2[] {p2})); pp1.setSibling(pp2); Map<String, Object> mp1 = mapper.toMap(pp1, (Object[]) null); assertEquals(pp1, mapper.fromMap(new POJO2Parent(), mp1, (Object[]) null)); }
@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; }
@TestCoversMethods({"toMap", "prepare"}) public void testToMap_Mapable() throws Exception { // to map POJO2 p1 = new POJO2(1, "one", Arrays.asList(new Object[] {1.0})); checkMap( mapper.toMap(p1, (Object[]) null), "id", p1.getId(), "name", p1.getName(), "value", p1.getValue()); checkMap(mapper.toMap(p1, new Object[] {"id"}), "id", p1.getId()); checkMap(mapper.toMap(p1, new Object[] {"name"}), "name", p1.getName()); checkMap(mapper.toMap(p1, new Object[] {"value"}), "value", p1.getValue()); checkMap( mapper.toMap(p1, new Object[] {"id", "name", "value"}), "id", p1.getId(), "name", p1.getName(), "value", p1.getValue()); // to map for "trees" POJO2 p2 = new POJO2(2, "two"); POJO2Parent pp1 = new POJO2Parent(); pp1.setChildren(Arrays.asList(new POJO2[] {p1})); POJO2Parent pp2 = new POJO2Parent(); pp2.setChildren(Arrays.asList(new POJO2[] {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 boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; POJO2Parent other = (POJO2Parent) 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; }