Refactor xml examples

This commit is contained in:
Grzegorz Piwowarek 2016-06-26 11:59:31 +02:00
parent 330c8680c9
commit 40ed303abc
6 changed files with 51 additions and 53 deletions

View File

@ -1,20 +1,22 @@
package com.baeldung.xml; package com.baeldung.xml;
import static org.junit.Assert.*;
import java.io.File;
import org.junit.Test; import org.junit.Test;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import java.io.File;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class DefaultParserTest { public class DefaultParserTest {
final String fileName = "src/test/resources/example.xml"; private final String fileName = "src/test/resources/example.xml";
final String fileNameSpace = "src/test/resources/example_namespace.xml"; private final String fileNameSpace = "src/test/resources/example_namespace.xml";
DefaultParser parser; private DefaultParser parser;
@Test @Test
public void getFirstLevelNodeListTest() { public void getFirstLevelNodeListTest() {
@ -30,7 +32,7 @@ public class DefaultParserTest {
parser = new DefaultParser(new File(fileName)); parser = new DefaultParser(new File(fileName));
NodeList list = parser.getNodeListByTitle("XML"); NodeList list = parser.getNodeListByTitle("XML");
for (int i = 0; null != list && i < list.getLength(); i++) { for (int i = 0; i < list.getLength(); i++) {
Node nod = list.item(i); Node nod = list.item(i);
assertEquals("java", nod.getAttributes().getNamedItem("type").getTextContent()); assertEquals("java", nod.getAttributes().getNamedItem("type").getTextContent());
assertEquals("02", nod.getAttributes().getNamedItem("tutId").getTextContent()); assertEquals("02", nod.getAttributes().getNamedItem("tutId").getTextContent());
@ -56,7 +58,7 @@ public class DefaultParserTest {
public void getNodeListByDateTest(){ public void getNodeListByDateTest(){
parser = new DefaultParser(new File(fileName)); parser = new DefaultParser(new File(fileName));
NodeList list = parser.getNodeListByTitle("04022016"); NodeList list = parser.getNodeListByTitle("04022016");
for (int i = 0; null != list && i < list.getLength(); i++) { for (int i = 0; i < list.getLength(); i++) {
Node nod = list.item(i); Node nod = list.item(i);
assertEquals("java", nod.getAttributes().getNamedItem("type").getTextContent()); assertEquals("java", nod.getAttributes().getNamedItem("type").getTextContent());
assertEquals("04", nod.getAttributes().getNamedItem("tutId").getTextContent()); assertEquals("04", nod.getAttributes().getNamedItem("tutId").getTextContent());

View File

@ -1,25 +1,25 @@
package com.baeldung.xml; package com.baeldung.xml;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.util.List;
import org.dom4j.Element; import org.dom4j.Element;
import org.dom4j.Node; import org.dom4j.Node;
import org.junit.Test; import org.junit.Test;
import java.io.File;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class Dom4JParserTest { public class Dom4JParserTest {
final String fileName = "src/test/resources/example.xml"; private static final String FILE_NAME = "src/test/resources/example.xml";
Dom4JParser parser; private Dom4JParser parser;
@Test @Test
public void getRootElementTest() { public void getRootElementTest() {
parser = new Dom4JParser(new File(fileName)); parser = new Dom4JParser(new File(FILE_NAME));
Element root = parser.getRootElement(); Element root = parser.getRootElement();
assertNotNull(root); assertNotNull(root);
@ -28,7 +28,7 @@ public class Dom4JParserTest {
@Test @Test
public void getFirstElementListTest() { public void getFirstElementListTest() {
parser = new Dom4JParser(new File(fileName)); parser = new Dom4JParser(new File(FILE_NAME));
List<Element> firstList = parser.getFirstElementList(); List<Element> firstList = parser.getFirstElementList();
assertNotNull(firstList); assertNotNull(firstList);
@ -38,7 +38,7 @@ public class Dom4JParserTest {
@Test @Test
public void getElementByIdTest() { public void getElementByIdTest() {
parser = new Dom4JParser(new File(fileName)); parser = new Dom4JParser(new File(FILE_NAME));
Node element = parser.getNodeById("03"); Node element = parser.getNodeById("03");
String type = element.valueOf("@type"); String type = element.valueOf("@type");
@ -47,7 +47,7 @@ public class Dom4JParserTest {
@Test @Test
public void getElementsListByTitleTest() { public void getElementsListByTitleTest() {
parser = new Dom4JParser(new File(fileName)); parser = new Dom4JParser(new File(FILE_NAME));
Node element = parser.getElementsListByTitle("XML"); Node element = parser.getElementsListByTitle("XML");
assertEquals("java", element.valueOf("@type")); assertEquals("java", element.valueOf("@type"));
@ -58,7 +58,7 @@ public class Dom4JParserTest {
@Test @Test
public void generateModifiedDocumentTest() { public void generateModifiedDocumentTest() {
parser = new Dom4JParser(new File(fileName)); parser = new Dom4JParser(new File(FILE_NAME));
parser.generateModifiedDocument(); parser.generateModifiedDocument();
File generatedFile = new File("src/test/resources/example_updated.xml"); File generatedFile = new File("src/test/resources/example_updated.xml");
@ -73,7 +73,7 @@ public class Dom4JParserTest {
@Test @Test
public void generateNewDocumentTest() { public void generateNewDocumentTest() {
parser = new Dom4JParser(new File(fileName)); parser = new Dom4JParser(new File(FILE_NAME));
parser.generateNewDocument(); parser.generateNewDocument();
File newFile = new File("src/test/resources/example_new.xml"); File newFile = new File("src/test/resources/example_new.xml");

View File

@ -1,20 +1,20 @@
package com.baeldung.xml; package com.baeldung.xml;
import org.jdom2.Element;
import org.junit.Test;
import java.io.File;
import java.util.List;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.io.File;
import java.util.List;
import org.jdom2.Element;
import org.junit.Test;
public class JDomParserTest { public class JDomParserTest {
final String fileName = "src/test/resources/example.xml"; private final String fileName = "src/test/resources/example.xml";
JDomParser parser; private JDomParser parser;
@Test @Test
public void getFirstElementListTest() { public void getFirstElementListTest() {

View File

@ -1,22 +1,19 @@
package com.baeldung.xml; package com.baeldung.xml;
import static org.junit.Assert.assertEquals; import com.baeldung.xml.binding.Tutorials;
import static org.junit.Assert.assertNotNull; import org.junit.Test;
import static org.junit.Assert.assertTrue;
import java.io.File; import java.io.File;
import org.dom4j.Node; import static org.junit.Assert.assertNotNull;
import org.junit.Test; import static org.junit.Assert.assertTrue;
import com.baeldung.xml.binding.Tutorials;
public class JaxbParserTest { public class JaxbParserTest {
final String fileName = "src/test/resources/example.xml"; private final String fileName = "src/test/resources/example.xml";
JaxbParser parser; private JaxbParser parser;
@Test @Test
public void getFullDocumentTest(){ public void getFullDocumentTest(){

View File

@ -1,18 +1,18 @@
package com.baeldung.xml; package com.baeldung.xml;
import static org.junit.Assert.assertNotNull; import org.junit.Test;
import static org.junit.Assert.assertTrue;
import java.io.File; import java.io.File;
import java.util.List; import java.util.List;
import org.junit.Test; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class JaxenDemoTest { public class JaxenDemoTest {
final String fileName = "src/test/resources/example.xml"; private final String fileName = "src/test/resources/example.xml";
JaxenDemo jaxenDemo; private JaxenDemo jaxenDemo;
@Test @Test
public void getFirstLevelNodeListTest() { public void getFirstLevelNodeListTest() {

View File

@ -1,20 +1,19 @@
package com.baeldung.xml; package com.baeldung.xml;
import static org.junit.Assert.assertNotNull; import com.baeldung.xml.model.Tutorial;
import static org.junit.Assert.assertTrue; import org.junit.Test;
import java.io.File; import java.io.File;
import java.util.List; import java.util.List;
import org.junit.Test; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import com.baeldung.xml.model.Tutorial;
public class StaxParserTest { public class StaxParserTest {
final String fileName = "src/test/resources/example.xml"; private final String fileName = "src/test/resources/example.xml";
StaxParser parser; private StaxParser parser;
@Test @Test
public void getAllTutorialsTest(){ public void getAllTutorialsTest(){