XmlAttributeSetterImpl implements XmlAttributeSetter { @Override public String setAttributeValue(String docString, String xPathExpr, String attributeName, String newValue) throws XmlAttributeSetterException { try { final Document doc = documentXmlConverter.loadDocument(docString); final Node node = documentAccessor.getNode(doc, xPathExpr); final Node attribute = node.getAttributes().getNamedItem( attributeName); if (attribute == null || "".equals(attribute.getNodeValue())) { final Node newAttribute = doc.createAttribute(attributeName); newAttribute.setNodeValue(newValue); node.getAttributes().setNamedItem(newAttribute); } else { attribute.setNodeValue(newValue); } doc.normalizeDocument(); return documentXmlConverter.convertXmlDocToString(doc); } catch (final Exception e) { throw new XmlAttributeSetterException(e.getMessage(), e); } }  @Override String setAttributeValue(String docString, String xPathExpr,
			String attributeName, String newValue);  }
@Test public void testSetAttributeValue_With_Existing_Attribute_Empty_Value() throws Exception { final String actualOutput = sut.setAttributeValue( XML_STRING_WITH_ATTRIBUTE_EMPTY_VALUE, XPATH_EXPR, TEST_ATTRIBUTE, TEST_VALUE); assertEquals(EXPECTED_OUTPUT, actualOutput); }
@Test public void testSetAttributeValue_With_Existing_Attribute_Not_Empty_Value() throws Exception { final String actualOutput = sut.setAttributeValue( XML_STRING_WITH_ATTRIBUTE_NOT_EMPTY_VALUE, XPATH_EXPR, TEST_ATTRIBUTE, TEST_VALUE); assertEquals(EXPECTED_OUTPUT, actualOutput); }
@Test public void testSetAttributeValue_Without_Existing_Attribute() throws Exception { final String actualOutput = sut.setAttributeValue( XML_STRING_NO_EXISTING_ATTRIBUTE, XPATH_EXPR, TEST_ATTRIBUTE, TEST_VALUE); assertEquals(EXPECTED_OUTPUT, actualOutput); }