Skip to content

Commit f214e0a

Browse files
committed
Xstream - XML to Object
xml moved to resources folder, test cases fixed
1 parent f7594ee commit f214e0a

16 files changed

Lines changed: 97 additions & 49 deletions

‎xstream-xmlToObject/.project‎

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<projectDescription>
3-
<name>xstream-xmlToObject</name>
4-
<comment></comment>
5-
<projects>
6-
</projects>
7-
<buildSpec>
8-
<buildCommand>
9-
<name>org.eclipse.jdt.core.javabuilder</name>
10-
<arguments>
11-
</arguments>
12-
</buildCommand>
13-
<buildCommand>
14-
<name>org.eclipse.m2e.core.maven2Builder</name>
15-
<arguments>
16-
</arguments>
17-
</buildCommand>
18-
</buildSpec>
19-
<natures>
20-
<nature>org.eclipse.jdt.core.javanature</nature>
21-
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22-
</natures>
23-
</projectDescription>
3+
<name>xstream-xmlToObject</name>
4+
<comment>NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.</comment>
5+
<projects/>
6+
<buildSpec>
7+
<buildCommand>
8+
<name>org.eclipse.jdt.core.javabuilder</name>
9+
</buildCommand>
10+
</buildSpec>
11+
<natures>
12+
<nature>org.eclipse.jdt.core.javanature</nature>
13+
</natures>
14+
</projectDescription>

‎xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
public class SimpleXstreamInitializer {
66

7-
private static XStream xtreamInstance;
7+
private XStream xtreamInstance;
88

9-
public static XStream getXstreamInstance() {
9+
public XStream getXstreamInstance() {
1010
if (xtreamInstance == null) {
1111
synchronized (SimpleXstreamInitializer.class) {
1212
if (xtreamInstance == null) {

‎xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.baeldung.pojo.test;
22

3-
import java.io.File;
43
import java.io.FileReader;
54
import java.io.IOException;
65

@@ -18,14 +17,16 @@ public class ComplexXmlToObjectAnnotationTest {
1817

1918
@Before
2019
public void dataSetup() {
21-
xstream = SimpleXstreamInitializer.getXstreamInstance();
20+
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
21+
xstream = simpleXstreamInitializer.getXstreamInstance();
2222
xstream.processAnnotations(Customer.class);
2323
}
2424

2525
@Test
2626
public void convertXmlToObjectFromFile() {
2727
try {
28-
FileReader reader = new FileReader(new File("data-file-alias-field-complex.xml"));
28+
ClassLoader classLoader = getClass().getClassLoader();
29+
FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field-complex.xml").getFile());
2930
Customer customer = (Customer) xstream.fromXML(reader);
3031
Assert.assertNotNull(customer);
3132
Assert.assertNotNull(customer.getContactDetailsList());

‎xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package com.baeldung.pojo.test;
22

3-
import java.io.File;
43
import java.io.FileReader;
54
import java.io.IOException;
65

76
import org.junit.Assert;
87
import org.junit.Before;
98
import org.junit.Test;
109

11-
import com.baeldung.implicit.collection.pojo.ContactDetails;
12-
import com.baeldung.implicit.collection.pojo.Customer;
10+
import com.baeldung.complex.pojo.ContactDetails;
11+
import com.baeldung.complex.pojo.Customer;
1312
import com.baeldung.initializer.SimpleXstreamInitializer;
1413
import com.thoughtworks.xstream.XStream;
1514

@@ -19,14 +18,16 @@ public class ComplexXmlToObjectAttributeCollectionTest {
1918

2019
@Before
2120
public void dataSetup() {
22-
xstream = SimpleXstreamInitializer.getXstreamInstance();
21+
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
22+
xstream = simpleXstreamInitializer.getXstreamInstance();
2323
xstream.processAnnotations(Customer.class);
2424
}
2525

2626
@Test
2727
public void convertXmlToObjectFromFile() {
2828
try {
29-
FileReader reader = new FileReader(new File("data-file-alias-implicit-collection.xml"));
29+
ClassLoader classLoader = getClass().getClassLoader();
30+
FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field-complex.xml").getFile());
3031
Customer customer = (Customer) xstream.fromXML(reader);
3132
Assert.assertNotNull(customer);
3233
Assert.assertNotNull(customer.getContactDetailsList());

‎xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.baeldung.pojo.test;
22

3-
import java.io.File;
43
import java.io.FileReader;
54
import java.io.IOException;
65

@@ -18,14 +17,16 @@ public class ComplexXmlToObjectCollectionTest {
1817

1918
@Before
2019
public void dataSetup() {
21-
xstream = SimpleXstreamInitializer.getXstreamInstance();
20+
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
21+
xstream = simpleXstreamInitializer.getXstreamInstance();
2222
xstream.processAnnotations(Customer.class);
2323
}
2424

2525
@Test
2626
public void convertXmlToObjectFromFile() {
2727
try {
28-
FileReader reader = new FileReader(new File("data-file-alias-implicit-collection.xml"));
28+
ClassLoader classLoader = getClass().getClassLoader();
29+
FileReader reader = new FileReader(classLoader.getResource("data-file-alias-implicit-collection.xml").getFile());
2930
Customer customer = (Customer) xstream.fromXML(reader);
3031
Assert.assertNotNull(customer);
3132
Assert.assertNotNull(customer.getContactDetailsList());

‎xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.baeldung.pojo.test;
22

3-
import java.io.File;
43
import java.io.FileReader;
54
import java.io.IOException;
65

@@ -18,14 +17,16 @@ public class XmlToObjectAliasTest {
1817

1918
@Before
2019
public void dataSetup() {
21-
xstream = SimpleXstreamInitializer.getXstreamInstance();
20+
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
21+
xstream = simpleXstreamInitializer.getXstreamInstance();
2222
xstream.alias("customer" , Customer.class);
2323
}
2424

2525
@Test
2626
public void convertXmlToObjectFromFile() {
2727
try {
28-
FileReader reader = new FileReader(new File("data-file-alias.xml"));
28+
ClassLoader classLoader = getClass().getClassLoader();
29+
FileReader reader = new FileReader(classLoader.getResource("data-file-alias.xml").getFile());
2930
Customer customer = (Customer) xstream.fromXML(reader);
3031
Assert.assertNotNull(customer);
3132

‎xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.baeldung.pojo.test;
22

3-
import java.io.File;
43
import java.io.FileReader;
54
import java.io.IOException;
65

@@ -18,14 +17,16 @@ public class XmlToObjectAnnotationTest {
1817

1918
@Before
2019
public void dataSetup() {
21-
xstream = SimpleXstreamInitializer.getXstreamInstance();
20+
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
21+
xstream = simpleXstreamInitializer.getXstreamInstance();
2222
xstream.processAnnotations(Customer.class);
2323
}
2424

2525
@Test
2626
public void convertXmlToObjectFromFile() {
2727
try {
28-
FileReader reader = new FileReader(new File("data-file-alias-field.xml"));
28+
ClassLoader classLoader = getClass().getClassLoader();
29+
FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field.xml").getFile());
2930
Customer customer = (Customer) xstream.fromXML(reader);
3031
Assert.assertNotNull(customer);
3132
Assert.assertNotNull(customer.getFirstName());

‎xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.baeldung.pojo.test;
22

3-
import java.io.File;
43
import java.io.FileReader;
54
import java.io.IOException;
65

@@ -18,15 +17,17 @@ public class XmlToObjectFieldAliasTest {
1817

1918
@Before
2019
public void dataSetup() {
21-
xstream = SimpleXstreamInitializer.getXstreamInstance();
20+
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
21+
xstream = simpleXstreamInitializer.getXstreamInstance();
2222
xstream.alias("customer" , Customer.class);
2323
xstream.aliasField("fn", Customer.class, "firstName");
2424
}
2525

2626
@Test
2727
public void convertXmlToObjectFromFile() {
2828
try {
29-
FileReader reader = new FileReader(new File("data-file-alias-field.xml"));
29+
ClassLoader classLoader = getClass().getClassLoader();
30+
FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field.xml").getFile());
3031
Customer customer = (Customer) xstream.fromXML(reader);
3132
Assert.assertNotNull(customer);
3233
Assert.assertNotNull(customer.getFirstName());

‎xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.baeldung.pojo.test;
22

3-
import java.io.File;
43
import java.io.FileReader;
54
import java.io.IOException;
65

@@ -18,15 +17,17 @@ public class XmlToObjectIgnoreFieldsTest {
1817

1918
@Before
2019
public void dataSetup() {
21-
xstream = SimpleXstreamInitializer.getXstreamInstance();
20+
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
21+
xstream = simpleXstreamInitializer.getXstreamInstance();
2222
xstream.alias("customer" , Customer.class);
2323
xstream.ignoreUnknownElements();
2424
}
2525

2626
@Test
2727
public void convertXmlToObjectFromFile() {
2828
try {
29-
FileReader reader = new FileReader(new File("data-file-ignore-field.xml"));
29+
ClassLoader classLoader = getClass().getClassLoader();
30+
FileReader reader = new FileReader(classLoader.getResource("data-file-ignore-field.xml").getFile());
3031
Customer customer = (Customer) xstream.fromXML(reader);
3132
Assert.assertNotNull(customer);
3233
System.out.println(customer);

‎xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.baeldung.pojo.test;
22

3-
import java.io.File;
43
import java.io.FileReader;
54
import java.io.IOException;
65

@@ -19,13 +18,15 @@ public class XmlToObjectTest {
1918

2019
@Before
2120
public void dataSetup() {
22-
xstream = SimpleXstreamInitializer.getXstreamInstance();
21+
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
22+
xstream = simpleXstreamInitializer.getXstreamInstance();
2323
}
2424

2525
@Test
2626
public void convertXmlToObjectFromFile() {
2727
try {
28-
FileReader reader = new FileReader(new File("data-file.xml"));
28+
ClassLoader classLoader = getClass().getClassLoader();
29+
FileReader reader = new FileReader(classLoader.getResource("data-file.xml").getFile());
2930
Customer customer = (Customer) xstream.fromXML(reader);
3031
Assert.assertNotNull(customer);
3132

0 commit comments

Comments
 (0)