GenericAttributeContent { public String generateContent(Map<String, String> properties) { if (properties.size() == 0) return "<" + elementName + "/>"; StringBuilder sb = new StringBuilder(); sb.append("<" + elementName + ">\r\n"); for (Map.Entry<String, String> entry: properties.entrySet()) { String key = entry.getKey(); sb.append(" <field>\r\n"); if (key != null) { sb.append(" <name>" + key + "</name>\r\n"); } else { log.warn("null key encountered"); } String value = entry.getValue(); if (value != null) { sb.append(" <value>" + entry.getValue() + "</value>\r\n"); } else { log.warn("null value encountered for key: " + key); } sb.append(" </field>\r\n"); } sb.append("</" + elementName + ">\r\n"); return sb.toString(); } GenericAttributeContent(Class clazz);  GenericAttributeContent(String elementName); String generateContent(Map<String, String> properties); List<Map<String, String>> parseContent(Element attributeContent);  }
@Test public void testGenerateContent() throws Exception { DocumentContent dc = new StandardDocumentContent(TEST_CONTENT); GenericAttributeContent gac = new GenericAttributeContent("boringAttribute"); List<Map<String, String>> attrs = gac.parseContent(dc.getAttributeContent()); assertEquals(1, attrs.size()); Map<String, String> properties = attrs.get(0); assertEquals(2, properties.size()); assertEquals("green", properties.get("color")); assertEquals("circle", properties.get("shape")); String content = gac.generateContent(properties); assertEquals(content.replaceAll("\\s+", ""), ATTRIB1_CONTENT.replaceAll("\\s+", "")); gac = new GenericAttributeContent("coolAttribute"); attrs = gac.parseContent(dc.getAttributeContent()); assertEquals(1, attrs.size()); properties = attrs.get(0); assertEquals(2, properties.size()); assertEquals("hasselhof", properties.get("driver")); assertEquals("KIT", properties.get("car")); content = gac.generateContent(properties); List<String> validValues = Arrays.asList(ATTRIB2_CONTENT.replaceAll("\\s+", ""), ATTRIB2_CONTENT_REVERSED.replaceAll( "\\s+", "")); content = content.replaceAll("\\s+", ""); assertTrue(validValues.contains(content)); }