formatting work

This commit is contained in:
eugenp
2017-01-29 16:06:01 +02:00
parent 034cde6e20
commit 966e53a85b
58 changed files with 932 additions and 1046 deletions

View File

@@ -20,7 +20,7 @@ import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class DefaultParser {
private File file;
public DefaultParser(File file) {
@@ -69,7 +69,7 @@ public class DefaultParser {
}
return node;
}
public NodeList getNodeListByTitle(String name) {
NodeList nodeList = null;
try {
@@ -125,11 +125,11 @@ public class DefaultParser {
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(this.getFile());
this.clean(xmlDocument);
XPath xPath = XPathFactory.newInstance().newXPath();
xPath.setNamespaceContext(new NamespaceContext() {
@Override

View File

@@ -17,115 +17,114 @@ import org.dom4j.io.XMLWriter;
public class Dom4JParser {
private File file;
private File file;
public Dom4JParser(File file) {
this.file = file;
}
public Dom4JParser(File file) {
this.file = file;
}
public Element getRootElement() {
try {
SAXReader reader = new SAXReader();
Document document = reader.read(file);
return document.getRootElement();
} catch (DocumentException e) {
e.printStackTrace();
return null;
}
}
public Element getRootElement() {
try {
SAXReader reader = new SAXReader();
Document document = reader.read(file);
return document.getRootElement();
} catch (DocumentException e) {
e.printStackTrace();
return null;
}
}
public List<Element> getFirstElementList() {
try {
SAXReader reader = new SAXReader();
Document document = reader.read(file);
return document.getRootElement().elements();
} catch (DocumentException e) {
e.printStackTrace();
return null;
}
}
public List<Element> getFirstElementList() {
try {
SAXReader reader = new SAXReader();
Document document = reader.read(file);
return document.getRootElement().elements();
} catch (DocumentException e) {
e.printStackTrace();
return null;
}
}
public Node getNodeById(String id) {
try {
SAXReader reader = new SAXReader();
Document document = reader.read(file);
List<Node> elements = document.selectNodes("//*[@tutId='" + id + "']");
return elements.get(0);
} catch (DocumentException e) {
e.printStackTrace();
return null;
}
}
public Node getNodeById(String id) {
try {
SAXReader reader = new SAXReader();
Document document = reader.read(file);
List<Node> elements = document.selectNodes("//*[@tutId='" + id + "']");
return elements.get(0);
} catch (DocumentException e) {
e.printStackTrace();
return null;
}
}
public Node getElementsListByTitle(String name) {
try {
SAXReader reader = new SAXReader();
Document document = reader.read(file);
List<Node> elements = document
.selectNodes("//tutorial[descendant::title[text()=" + "'" + name + "'" + "]]");
return elements.get(0);
} catch (DocumentException e) {
e.printStackTrace();
return null;
}
}
public Node getElementsListByTitle(String name) {
try {
SAXReader reader = new SAXReader();
Document document = reader.read(file);
List<Node> elements = document.selectNodes("//tutorial[descendant::title[text()=" + "'" + name + "'" + "]]");
return elements.get(0);
} catch (DocumentException e) {
e.printStackTrace();
return null;
}
}
public void generateModifiedDocument() {
try {
SAXReader reader = new SAXReader();
Document document = reader.read(file);
List<Node> nodes = document.selectNodes("/tutorials/tutorial");
for (Node node : nodes) {
Element element = (Element) node;
Iterator<Element> iterator = element.elementIterator("title");
while (iterator.hasNext()) {
Element title = (Element) iterator.next();
title.setText(title.getText() + " updated");
}
}
XMLWriter writer = new XMLWriter(new FileWriter(new File("src/test/resources/example_updated.xml")));
writer.write(document);
writer.close();
} catch (DocumentException e) {
e.printStackTrace();
public void generateModifiedDocument() {
try {
SAXReader reader = new SAXReader();
Document document = reader.read(file);
List<Node> nodes = document.selectNodes("/tutorials/tutorial");
for (Node node : nodes) {
Element element = (Element) node;
Iterator<Element> iterator = element.elementIterator("title");
while (iterator.hasNext()) {
Element title = (Element) iterator.next();
title.setText(title.getText() + " updated");
}
}
XMLWriter writer = new XMLWriter(new FileWriter(new File("src/test/resources/example_updated.xml")));
writer.write(document);
writer.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void generateNewDocument() {
try {
Document document = DocumentHelper.createDocument();
Element root = document.addElement("XMLTutorials");
Element tutorialElement = root.addElement("tutorial").addAttribute("tutId", "01");
tutorialElement.addAttribute("type", "xml");
public void generateNewDocument() {
try {
Document document = DocumentHelper.createDocument();
Element root = document.addElement("XMLTutorials");
Element tutorialElement = root.addElement("tutorial").addAttribute("tutId", "01");
tutorialElement.addAttribute("type", "xml");
tutorialElement.addElement("title").addText("XML with Dom4J");
tutorialElement.addElement("title").addText("XML with Dom4J");
tutorialElement.addElement("description").addText("XML handling with Dom4J");
tutorialElement.addElement("description").addText("XML handling with Dom4J");
tutorialElement.addElement("date").addText("14/06/2016");
tutorialElement.addElement("date").addText("14/06/2016");
tutorialElement.addElement("author").addText("Dom4J tech writer");
tutorialElement.addElement("author").addText("Dom4J tech writer");
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileWriter(new File("src/test/resources/example_new.xml")), format);
writer.write(document);
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileWriter(new File("src/test/resources/example_new.xml")), format);
writer.write(document);
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public File getFile() {
return file;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public void setFile(File file) {
this.file = file;
}
}

View File

@@ -12,51 +12,50 @@ import org.jdom2.input.SAXBuilder;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;
public class JDomParser {
private File file;
private File file;
public JDomParser(File file) {
this.file = file;
}
public JDomParser(File file) {
this.file = file;
}
public List<Element> getAllTitles() {
try {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(this.getFile());
Element tutorials = doc.getRootElement();
List<Element> titles = tutorials.getChildren("tutorial");
return titles;
} catch (JDOMException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public Element getNodeById(String id) {
try {
SAXBuilder builder = new SAXBuilder();
Document document = (Document) builder.build(file);
String filter = "//*[@tutId='" + id + "']";
XPathFactory xFactory = XPathFactory.instance();
XPathExpression<Element> expr = xFactory.compile(filter, Filters.element());
List<Element> node = expr.evaluate(document);
return node.get(0);
} catch (JDOMException | IOException e ) {
e.printStackTrace();
return null;
}
}
public List<Element> getAllTitles() {
try {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(this.getFile());
Element tutorials = doc.getRootElement();
List<Element> titles = tutorials.getChildren("tutorial");
return titles;
} catch (JDOMException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public File getFile() {
return file;
}
public Element getNodeById(String id) {
try {
SAXBuilder builder = new SAXBuilder();
Document document = (Document) builder.build(file);
String filter = "//*[@tutId='" + id + "']";
XPathFactory xFactory = XPathFactory.instance();
XPathExpression<Element> expr = xFactory.compile(filter, Filters.element());
List<Element> node = expr.evaluate(document);
public void setFile(File file) {
this.file = file;
}
return node.get(0);
} catch (JDOMException | IOException e) {
e.printStackTrace();
return null;
}
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
}

View File

@@ -13,56 +13,56 @@ import com.baeldung.xml.binding.Tutorials;
public class JaxbParser {
private File file;
private File file;
public JaxbParser(File file) {
this.file = file;
}
public JaxbParser(File file) {
this.file = file;
}
public Tutorials getFullDocument() {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Tutorials.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Tutorials tutorials = (Tutorials) jaxbUnmarshaller.unmarshal(this.getFile());
return tutorials;
} catch (JAXBException e) {
e.printStackTrace();
return null;
}
}
public Tutorials getFullDocument() {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Tutorials.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Tutorials tutorials = (Tutorials) jaxbUnmarshaller.unmarshal(this.getFile());
return tutorials;
} catch (JAXBException e) {
e.printStackTrace();
return null;
}
}
public void createNewDocument() {
Tutorials tutorials = new Tutorials();
tutorials.setTutorial(new ArrayList<Tutorial>());
Tutorial tut = new Tutorial();
tut.setTutId("01");
tut.setType("XML");
tut.setTitle("XML with Jaxb");
tut.setDescription("XML Binding with Jaxb");
tut.setDate("04/02/2015");
tut.setAuthor("Jaxb author");
tutorials.getTutorial().add(tut);
public void createNewDocument() {
Tutorials tutorials = new Tutorials();
tutorials.setTutorial(new ArrayList<Tutorial>());
Tutorial tut = new Tutorial();
tut.setTutId("01");
tut.setType("XML");
tut.setTitle("XML with Jaxb");
tut.setDescription("XML Binding with Jaxb");
tut.setDate("04/02/2015");
tut.setAuthor("Jaxb author");
tutorials.getTutorial().add(tut);
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Tutorials.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Tutorials.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(tutorials, file);
jaxbMarshaller.marshal(tutorials, file);
} catch (JAXBException e) {
e.printStackTrace();
}
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
public File getFile() {
return file;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public void setFile(File file) {
this.file = file;
}
}

View File

@@ -17,13 +17,13 @@ import org.xml.sax.SAXException;
public class JaxenDemo {
private File file;
private File file;
public JaxenDemo(File file) {
this.file = file;
}
public List getAllTutorial(){
public JaxenDemo(File file) {
this.file = file;
}
public List getAllTutorial() {
try {
FileInputStream fileIS = new FileInputStream(this.getFile());
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
@@ -33,25 +33,24 @@ public class JaxenDemo {
Document xmlDocument = builder.parse(fileIS);
String expression = "/tutorials/tutorial";
XPath path = new DOMXPath(expression);
List result = path.selectNodes(xmlDocument);
return result;
} catch (SAXException | IOException | ParserConfigurationException | JaxenException e) {
e.printStackTrace();
return null;
}
}
public File getFile() {
return file;
}
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public void setFile(File file) {
this.file = file;
}
}

View File

@@ -21,100 +21,100 @@ import com.baeldung.xml.binding.Tutorial;
public class StaxParser {
private File file;
private File file;
public StaxParser(File file) {
this.file = file;
}
public StaxParser(File file) {
this.file = file;
}
public List<Tutorial> getAllTutorial() {
boolean bTitle = false;
boolean bDescription = false;
boolean bDate = false;
boolean bAuthor = false;
List<Tutorial> tutorials = new ArrayList<Tutorial>();
try {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader eventReader = factory.createXMLEventReader(new FileReader(this.getFile()));
Tutorial current = null;
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
switch (event.getEventType()) {
case XMLStreamConstants.START_ELEMENT:
StartElement startElement = event.asStartElement();
String qName = startElement.getName().getLocalPart();
if (qName.equalsIgnoreCase("tutorial")) {
current = new Tutorial();
Iterator<Attribute> attributes = startElement.getAttributes();
while (attributes.hasNext()) {
Attribute currentAt = attributes.next();
if (currentAt.getName().toString().equalsIgnoreCase("tutId")) {
current.setTutId(currentAt.getValue());
} else if (currentAt.getName().toString().equalsIgnoreCase("type")) {
current.setType(currentAt.getValue());
}
}
} else if (qName.equalsIgnoreCase("title")) {
bTitle = true;
} else if (qName.equalsIgnoreCase("description")) {
bDescription = true;
} else if (qName.equalsIgnoreCase("date")) {
bDate = true;
} else if (qName.equalsIgnoreCase("author")) {
bAuthor = true;
}
break;
case XMLStreamConstants.CHARACTERS:
Characters characters = event.asCharacters();
if (bTitle) {
if (current != null) {
current.setTitle(characters.getData());
}
bTitle = false;
}
if (bDescription) {
if (current != null) {
current.setDescription(characters.getData());
}
bDescription = false;
}
if (bDate) {
if (current != null) {
current.setDate(characters.getData());
}
bDate = false;
}
if (bAuthor) {
if (current != null) {
current.setAuthor(characters.getData());
}
bAuthor = false;
}
break;
case XMLStreamConstants.END_ELEMENT:
EndElement endElement = event.asEndElement();
if (endElement.getName().getLocalPart().equalsIgnoreCase("tutorial")) {
if(current != null){
tutorials.add(current);
}
}
break;
}
}
public List<Tutorial> getAllTutorial() {
boolean bTitle = false;
boolean bDescription = false;
boolean bDate = false;
boolean bAuthor = false;
List<Tutorial> tutorials = new ArrayList<Tutorial>();
try {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader eventReader = factory.createXMLEventReader(new FileReader(this.getFile()));
Tutorial current = null;
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
switch (event.getEventType()) {
case XMLStreamConstants.START_ELEMENT:
StartElement startElement = event.asStartElement();
String qName = startElement.getName().getLocalPart();
if (qName.equalsIgnoreCase("tutorial")) {
current = new Tutorial();
Iterator<Attribute> attributes = startElement.getAttributes();
while (attributes.hasNext()) {
Attribute currentAt = attributes.next();
if (currentAt.getName().toString().equalsIgnoreCase("tutId")) {
current.setTutId(currentAt.getValue());
} else if (currentAt.getName().toString().equalsIgnoreCase("type")) {
current.setType(currentAt.getValue());
}
}
} else if (qName.equalsIgnoreCase("title")) {
bTitle = true;
} else if (qName.equalsIgnoreCase("description")) {
bDescription = true;
} else if (qName.equalsIgnoreCase("date")) {
bDate = true;
} else if (qName.equalsIgnoreCase("author")) {
bAuthor = true;
}
break;
case XMLStreamConstants.CHARACTERS:
Characters characters = event.asCharacters();
if (bTitle) {
if (current != null) {
current.setTitle(characters.getData());
}
bTitle = false;
}
if (bDescription) {
if (current != null) {
current.setDescription(characters.getData());
}
bDescription = false;
}
if (bDate) {
if (current != null) {
current.setDate(characters.getData());
}
bDate = false;
}
if (bAuthor) {
if (current != null) {
current.setAuthor(characters.getData());
}
bAuthor = false;
}
break;
case XMLStreamConstants.END_ELEMENT:
EndElement endElement = event.asEndElement();
if (endElement.getName().getLocalPart().equalsIgnoreCase("tutorial")) {
if (current != null) {
tutorials.add(current);
}
}
break;
}
}
} catch (FileNotFoundException | XMLStreamException e) {
e.printStackTrace();
}
} catch (FileNotFoundException | XMLStreamException e) {
e.printStackTrace();
}
return tutorials;
}
return tutorials;
}
public File getFile() {
return file;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public void setFile(File file) {
this.file = file;
}
}

View File

@@ -5,55 +5,64 @@ import javax.xml.bind.annotation.XmlElement;
public class Tutorial {
private String tutId;
private String type;
private String title;
private String description;
private String date;
private String author;
public String getTutId() {
return tutId;
}
@XmlAttribute
public void setTutId(String tutId) {
this.tutId = tutId;
}
public String getType() {
return type;
}
@XmlAttribute
public void setType(String type) {
this.type = type;
}
public String getTitle() {
return title;
}
@XmlElement
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
@XmlElement
public void setDescription(String description) {
this.description = description;
}
public String getDate() {
return date;
}
@XmlElement
public void setDate(String date) {
this.date = date;
}
public String getAuthor() {
return author;
}
@XmlElement
public void setAuthor(String author) {
this.author = author;
}
private String tutId;
private String type;
private String title;
private String description;
private String date;
private String author;
public String getTutId() {
return tutId;
}
@XmlAttribute
public void setTutId(String tutId) {
this.tutId = tutId;
}
public String getType() {
return type;
}
@XmlAttribute
public void setType(String type) {
this.type = type;
}
public String getTitle() {
return title;
}
@XmlElement
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
@XmlElement
public void setDescription(String description) {
this.description = description;
}
public String getDate() {
return date;
}
@XmlElement
public void setDate(String date) {
this.date = date;
}
public String getAuthor() {
return author;
}
@XmlElement
public void setAuthor(String author) {
this.author = author;
}
}

View File

@@ -8,16 +8,15 @@ import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Tutorials {
private List<Tutorial> tutorial;
private List<Tutorial> tutorial;
public List<Tutorial> getTutorial() {
return tutorial;
}
@XmlElement
public void setTutorial(List<Tutorial> tutorial) {
this.tutorial = tutorial;
}
public List<Tutorial> getTutorial() {
return tutorial;
}
@XmlElement
public void setTutorial(List<Tutorial> tutorial) {
this.tutorial = tutorial;
}
}

View File

@@ -11,7 +11,7 @@ import org.w3c.dom.NodeList;
public class DefaultParserUnitTest {
final String fileName = "src/test/resources/example.xml";
final String fileNameSpace = "src/test/resources/example_namespace.xml";
DefaultParser parser;
@@ -51,9 +51,9 @@ public class DefaultParserUnitTest {
String type = node.getAttributes().getNamedItem("type").getNodeValue();
assertEquals("android", type);
}
@Test
public void getNodeListByDateTest(){
public void getNodeListByDateTest() {
parser = new DefaultParser(new File(fileName));
NodeList list = parser.getNodeListByTitle("04022016");
for (int i = 0; null != list && i < list.getLength(); i++) {
@@ -68,9 +68,9 @@ public class DefaultParserUnitTest {
assertEquals("SpringAuthor", nod.getLastChild().getTextContent());
}
}
@Test
public void getNodeListWithNamespaceTest(){
public void getNodeListWithNamespaceTest() {
parser = new DefaultParser(new File(fileNameSpace));
NodeList list = parser.getAllTutorials();
assertNotNull(list);

View File

@@ -13,76 +13,76 @@ import org.junit.Test;
public class Dom4JParserUnitTest {
final String fileName = "src/test/resources/example.xml";
final String fileName = "src/test/resources/example.xml";
Dom4JParser parser;
Dom4JParser parser;
@Test
public void getRootElementTest() {
parser = new Dom4JParser(new File(fileName));
Element root = parser.getRootElement();
@Test
public void getRootElementTest() {
parser = new Dom4JParser(new File(fileName));
Element root = parser.getRootElement();
assertNotNull(root);
assertTrue(root.elements().size() == 4);
}
assertNotNull(root);
assertTrue(root.elements().size() == 4);
}
@Test
public void getFirstElementListTest() {
parser = new Dom4JParser(new File(fileName));
List<Element> firstList = parser.getFirstElementList();
@Test
public void getFirstElementListTest() {
parser = new Dom4JParser(new File(fileName));
List<Element> firstList = parser.getFirstElementList();
assertNotNull(firstList);
assertTrue(firstList.size() == 4);
assertTrue(firstList.get(0).attributeValue("type").equals("java"));
}
assertNotNull(firstList);
assertTrue(firstList.size() == 4);
assertTrue(firstList.get(0).attributeValue("type").equals("java"));
}
@Test
public void getElementByIdTest() {
parser = new Dom4JParser(new File(fileName));
Node element = parser.getNodeById("03");
@Test
public void getElementByIdTest() {
parser = new Dom4JParser(new File(fileName));
Node element = parser.getNodeById("03");
String type = element.valueOf("@type");
assertEquals("android", type);
}
String type = element.valueOf("@type");
assertEquals("android", type);
}
@Test
public void getElementsListByTitleTest() {
parser = new Dom4JParser(new File(fileName));
Node element = parser.getElementsListByTitle("XML");
@Test
public void getElementsListByTitleTest() {
parser = new Dom4JParser(new File(fileName));
Node element = parser.getElementsListByTitle("XML");
assertEquals("java", element.valueOf("@type"));
assertEquals("02", element.valueOf("@tutId"));
assertEquals("XML", element.selectSingleNode("title").getText());
assertEquals("title", element.selectSingleNode("title").getName());
}
assertEquals("java", element.valueOf("@type"));
assertEquals("02", element.valueOf("@tutId"));
assertEquals("XML", element.selectSingleNode("title").getText());
assertEquals("title", element.selectSingleNode("title").getName());
}
@Test
public void generateModifiedDocumentTest() {
parser = new Dom4JParser(new File(fileName));
parser.generateModifiedDocument();
@Test
public void generateModifiedDocumentTest() {
parser = new Dom4JParser(new File(fileName));
parser.generateModifiedDocument();
File generatedFile = new File("src/test/resources/example_updated.xml");
assertTrue(generatedFile.exists());
File generatedFile = new File("src/test/resources/example_updated.xml");
assertTrue(generatedFile.exists());
parser.setFile(generatedFile);
Node element = parser.getNodeById("02");
parser.setFile(generatedFile);
Node element = parser.getNodeById("02");
assertEquals("XML updated", element.selectSingleNode("title").getText());
assertEquals("XML updated", element.selectSingleNode("title").getText());
}
@Test
public void generateNewDocumentTest() {
parser = new Dom4JParser(new File(fileName));
parser.generateNewDocument();
}
File newFile = new File("src/test/resources/example_new.xml");
assertTrue(newFile.exists());
@Test
public void generateNewDocumentTest() {
parser = new Dom4JParser(new File(fileName));
parser.generateNewDocument();
parser.setFile(newFile);
Node element = parser.getNodeById("01");
File newFile = new File("src/test/resources/example_new.xml");
assertTrue(newFile.exists());
assertEquals("XML with Dom4J", element.selectSingleNode("title").getText());
parser.setFile(newFile);
Node element = parser.getNodeById("01");
}
assertEquals("XML with Dom4J", element.selectSingleNode("title").getText());
}
}

View File

@@ -11,28 +11,28 @@ import org.jdom2.Element;
import org.junit.Test;
public class JDomParserUnitTest {
final String fileName = "src/test/resources/example.xml";
JDomParser parser;
@Test
public void getFirstElementListTest() {
parser = new JDomParser(new File(fileName));
List<Element> firstList = parser.getAllTitles();
final String fileName = "src/test/resources/example.xml";
JDomParser parser;
@Test
public void getFirstElementListTest() {
parser = new JDomParser(new File(fileName));
List<Element> firstList = parser.getAllTitles();
assertNotNull(firstList);
assertTrue(firstList.size() == 4);
assertTrue(firstList.get(0).getAttributeValue("type").equals("java"));
}
@Test
public void getElementByIdTest() {
parser = new JDomParser(new File(fileName));
Element el = parser.getNodeById("03");
String type = el.getAttributeValue("type");
assertEquals("android", type);
}
assertNotNull(firstList);
assertTrue(firstList.size() == 4);
assertTrue(firstList.get(0).getAttributeValue("type").equals("java"));
}
@Test
public void getElementByIdTest() {
parser = new JDomParser(new File(fileName));
Element el = parser.getNodeById("03");
String type = el.getAttributeValue("type");
assertEquals("android", type);
}
}

View File

@@ -11,34 +11,32 @@ import com.baeldung.xml.binding.Tutorials;
public class JaxbParserUnitTest {
final String fileName = "src/test/resources/example.xml";
final String fileName = "src/test/resources/example.xml";
JaxbParser parser;
@Test
public void getFullDocumentTest(){
parser = new JaxbParser(new File(fileName));
Tutorials tutorials = parser.getFullDocument();
JaxbParser parser;
assertNotNull(tutorials);
assertTrue(tutorials.getTutorial().size() == 4);
assertTrue(tutorials.getTutorial().get(0).getType().equalsIgnoreCase("java"));
}
@Test
public void createNewDocumentTest(){
File newFile = new File("src/test/resources/example_new.xml");
parser = new JaxbParser(newFile);
parser.createNewDocument();
@Test
public void getFullDocumentTest() {
parser = new JaxbParser(new File(fileName));
Tutorials tutorials = parser.getFullDocument();
assertTrue(newFile.exists());
assertNotNull(tutorials);
assertTrue(tutorials.getTutorial().size() == 4);
assertTrue(tutorials.getTutorial().get(0).getType().equalsIgnoreCase("java"));
}
Tutorials tutorials = parser.getFullDocument();
@Test
public void createNewDocumentTest() {
File newFile = new File("src/test/resources/example_new.xml");
parser = new JaxbParser(newFile);
parser.createNewDocument();
assertNotNull(tutorials);
assertTrue(tutorials.getTutorial().size() == 1);
assertTrue(tutorials.getTutorial().get(0).getTitle().equalsIgnoreCase("XML with Jaxb"));
}
assertTrue(newFile.exists());
Tutorials tutorials = parser.getFullDocument();
assertNotNull(tutorials);
assertTrue(tutorials.getTutorial().size() == 1);
assertTrue(tutorials.getTutorial().get(0).getTitle().equalsIgnoreCase("XML with Jaxb"));
}
}

View File

@@ -10,13 +10,13 @@ import org.junit.Test;
public class JaxenDemoUnitTest {
final String fileName = "src/test/resources/example.xml";
JaxenDemo jaxenDemo;
@Test
final String fileName = "src/test/resources/example.xml";
JaxenDemo jaxenDemo;
@Test
public void getFirstLevelNodeListTest() {
jaxenDemo = new JaxenDemo(new File(fileName));
jaxenDemo = new JaxenDemo(new File(fileName));
List<?> list = jaxenDemo.getAllTutorial();
assertNotNull(list);

View File

@@ -12,17 +12,17 @@ import com.baeldung.xml.binding.Tutorial;
public class StaxParserUnitTest {
final String fileName = "src/test/resources/example.xml";
final String fileName = "src/test/resources/example.xml";
StaxParser parser;
@Test
public void getAllTutorialsTest(){
parser = new StaxParser(new File(fileName));
List<Tutorial> tutorials = parser.getAllTutorial();
assertNotNull(tutorials);
assertTrue(tutorials.size() == 4);
assertTrue(tutorials.get(0).getType().equalsIgnoreCase("java"));
}
StaxParser parser;
@Test
public void getAllTutorialsTest() {
parser = new StaxParser(new File(fileName));
List<Tutorial> tutorials = parser.getAllTutorial();
assertNotNull(tutorials);
assertTrue(tutorials.size() == 4);
assertTrue(tutorials.get(0).getType().equalsIgnoreCase("java"));
}
}