I am new to java testing and I made this code (I'll just show the methods) which parses and XML file and modifies tag values/ attribute values according to user. I have been asked to Write down some JUnit cases for the same but I am unable to understand how.
I have tried all the tutorials which has given me a theoretical understanding but they've shown tutorials to make JUnit cases for simple addition subtraction programs and I can't think of any for my code.
/** This method modifies the value of all the tags with the user specified name regardless of order * @param tagname this is the name of the tag user wants to change the value of. * @param value This is the second parameter that is the new value of the tag. * @throws CustomException * */ public void modify_All_Occurences(String tagName, String value) throws TransformerException, ParserConfigurationException, SAXException, SAXParseException, IOException, CustomException {
boolean occuranceFound = false;
Element element = doc.getDocumentElement();
NodeList list = element.getElementsByTagName(tagName);
for (int i=0;i<list.getLength() && list != null && list.getLength() > 0;i++) {
NodeList subList = list.item(i).getChildNodes();
if (subList != null && subList.getLength() > 0) {
System.out.println("The value of the tag is :- "+subList.item(0).getNodeValue());
subList.item(0).setNodeValue(value);
System.out.println("The updated value of the tag is :- "+subList.item(0).getNodeValue());
occuranceFound=true;
}
}
if(!occuranceFound){
throw new CustomException(tagName);
}
writeToFile();
}
/*********************************************/
/** This method modifies the values of all user specified tag via its xpath * @param String path to the xml tag * @throws CustomException * */ public void modify_via_Xpath(String path, String value) throws XPathExpressionException, TransformerException, CustomException{ NodeList nodes = (NodeList) xpath.evaluate(path, doc,XPathConstants.NODESET); if(nodes.getLength()==0){ throw new CustomException(path); } for (int i = 0; i < nodes.getLength(); i++) { nodes.item(i).setTextContent(value); } writeToFile();
}
/*********************************************/
/** This method is for modifying the attribute of user specified tag. * @param tagname this is the name of the tag user wants to change the attribute value of. * @param attribute This is the attribute of the tag that the user wants to change the value of * @param value This is the third parameter that is the new value of the attribute. * @throws CustomException * */ public void modifyAttribute( String tagName, String attribute, String value) throws TransformerException, CustomException{
Element element = (Element) doc.getElementsByTagName(tagName).item(0);
if(element==null){
throw new CustomException(tagName);
}
System.out.println(element.getAttribute(attribute));
element.setAttribute(attribute, value);
System.out.println(element.getAttribute(attribute));
Generic g = new Generic();
writeToFile();
}
Aucun commentaire:
Enregistrer un commentaire