package lapr.project.model; import lapr.project.utils.StringUtil; import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.util.ArrayList; import java.util.List; import static org.junit.jupiter.api.Assertions.*; /** * Class to demonstrate a Candidatura simple example. * * @author Nuno Bettencourt [nmb@isep.ipp.pt] on 29/05/16. */ public class ApplicationTest { /** * StringUtil variable to access utils for Strings. */ private StringUtil stringUtil = new StringUtil(); /** * Get OS independent line break. * * @return OS independent line break "%n". */ private String getLineBreak() { if (stringUtil == null) { stringUtil = new StringUtil(); } return stringUtil.getLineBreak(); } @Test public void ensureXMLElementExportToStringIsValid() throws Exception { String expected = "" + getLineBreak() + "MyApplication" + getLineBreak() + "" + getLineBreak() + "" + getLineBreak() + "Doors" + getLineBreak() + "" + getLineBreak() + "" + getLineBreak() + "Windows" + getLineBreak() + "" + getLineBreak() + "" + getLineBreak() + "" + getLineBreak(); List keywordList = new ArrayList<>(); keywordList.add(new Keyword("Doors")); keywordList.add(new Keyword("Windows")); Application application = new Application("MyApplication", keywordList); String result = application.exportContentToString(); assertEquals(expected, result); } @Test public void ensureImportFromXMLElementNodeIsValid() throws Exception { List keywordList = new ArrayList<>(); keywordList.add(new Keyword("Doors")); keywordList.add(new Keyword("Windows")); Application expected = new Application("MyApplication", keywordList); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //Create document builder DocumentBuilder builder = factory.newDocumentBuilder(); //Obtain a new document Document document = builder.newDocument(); //Create root element Element elementCandidatura = document.createElement("application"); //Create a sub-element Element elementDescription = document.createElement("description"); //Set the sub-element value elementDescription.setTextContent("MyApplication"); //Add sub-element to root element elementCandidatura.appendChild(elementDescription); //Create a sub-element Element elementKeywords = document.createElement("keywords"); //iterate over keywords for (Keyword keyword : keywordList) { Node keywordNode = keyword.exportContentToXMLNode(); elementKeywords.appendChild(document.importNode(keywordNode, true)); } elementCandidatura.appendChild(elementKeywords); //Add root element to document document.appendChild(elementCandidatura); Application result = new Application(); result = result.importContentFromXMLNode(elementCandidatura); assertEquals(expected, result); }