mardi 30 juillet 2019

JUnit test cases requirement for a java code parsing an XML file

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 sets the parser for the current file.
 * @param filepath passed from main method.
 * @exception ParserConfigurationException on invalid XML
 * */
public void loadsDoc() throws ParserConfigurationException{

    DocumentBuilder dBuilder;
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();        
    dBuilder = dbFactory.newDocumentBuilder();
    try {
        this.doc = dBuilder.parse(xmlFile);
    }
    catch(Exception e){
        System.out.println("******* The XML string is not properly formatted ******* " + e);
        System.exit(0);
    }
}

/**
 * This method updates the file to match the modified values.
 * @exception TransformerException
 * 
 * */
public void writeToFile() throws TransformerException{
    doc.getDocumentElement().normalize();
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(xmlFile);
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(source, result);
    System.out.println("XML file updated successfully");
}

/**
 * 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();

}

/**
 * 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();

}

Aucun commentaire:

Enregistrer un commentaire