MapConverter implements Converter { public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { @SuppressWarnings("unchecked") Map<String, Object> data = (Map<String, Object>) source; for (Map.Entry<String, Object> entry : data.entrySet()) { writer.startNode(entry.getKey()); Object value = entry.getValue(); if (value instanceof Map) { this.marshal(value, writer, context); } else if (value instanceof Collection<?>) { context.convertAnother(value); } else { writer.setValue(value != null ? entry.getValue().toString() : null); } writer.endNode(); } }  boolean canConvert(@SuppressWarnings("rawtypes") Class type); void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context); Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context);  }
@Test public void testMarshal() { this.mapConverter.marshal(this.data, this.writerMock, null); Mockito.verify(this.writerMock).startNode("ONE"); Mockito.verify(this.writerMock).setValue("1.0"); Mockito.verify(this.writerMock).startNode("TWO"); Mockito.verify(this.writerMock).setValue("2"); Mockito.verify(this.writerMock).startNode("THREE"); Mockito.verify(this.writerMock).setValue(null); Mockito.verify(this.writerMock, Mockito.times(3)).endNode(); }
@Test public void testMarshalNestedMap() { Map<String, Object> outerMap = new HashMap<String, Object>(); outerMap.put("Foo", this.data); this.mapConverter.marshal(outerMap, this.writerMock, null); Mockito.verify(this.writerMock).startNode("Foo"); Mockito.verify(this.writerMock).startNode("ONE"); Mockito.verify(this.writerMock).setValue("1.0"); Mockito.verify(this.writerMock).startNode("TWO"); Mockito.verify(this.writerMock).setValue("2"); Mockito.verify(this.writerMock).startNode("THREE"); Mockito.verify(this.writerMock).setValue(null); Mockito.verify(this.writerMock, Mockito.times(4)).endNode(); }