diff --git a/jaxb/book.xml b/jaxb/book.xml
deleted file mode 100644
index 605d531b16..0000000000
--- a/jaxb/book.xml
+++ /dev/null
@@ -1,5 +0,0 @@
-
-
- Book1
- 2016-12-16T17:28:49.718Z
-
diff --git a/jaxb/pom.xml b/jaxb/pom.xml
index 115a632d7b..b636a4dc82 100644
--- a/jaxb/pom.xml
+++ b/jaxb/pom.xml
@@ -1,188 +1,167 @@
- 4.0.0
- com.baeldung
- jaxb
- 0.0.1-SNAPSHOT
- jaxb
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ 4.0.0
+ com.baeldung
+ jaxb
+ 0.0.1-SNAPSHOT
+ jaxb
-
-
- org.glassfish.jaxb
- jaxb-runtime
- ${jaxb.version}
-
+
+
+ org.glassfish.jaxb
+ jaxb-runtime
+ ${jaxb.version}
+
-
- org.glassfish.jaxb
- jaxb-core
- ${jaxb.version}
-
+
+ org.glassfish.jaxb
+ jaxb-core
+ ${jaxb.version}
+
-
-
- com.sun.istack
- istack-commons-runtime
- 3.0.2
-
+
+
+ com.sun.istack
+ istack-commons-runtime
+ 3.0.2
+
-
- commons-io
- commons-io
- 2.5
-
+
+ commons-io
+ commons-io
+ 2.5
+
-
- org.slf4j
- slf4j-api
- ${org.slf4j.version}
-
+
+ org.slf4j
+ slf4j-api
+ ${org.slf4j.version}
+
-
- ch.qos.logback
- logback-classic
- ${logback.version}
-
+
+ ch.qos.logback
+ logback-classic
+ ${logback.version}
+
-
- ch.qos.logback
- logback-core
- ${logback.version}
-
-
-
- junit
- junit
- ${junit.version}
-
-
-
- org.apache.commons
- commons-lang3
- 3.5
-
-
+
+ ch.qos.logback
+ logback-core
+ ${logback.version}
+
-
- jaxb
-
-
- src/main/resources
- true
-
-
+
+ junit
+ junit
+ ${junit.version}
+
-
-
-
- org.eclipse.m2e
- lifecycle-mapping
- 1.0.0
-
-
-
-
-
- org.codehaus.mojo
- jaxb2-maven-plugin
- [${jaxb2-maven-plugin.version},)
-
- schemagen
- xjc
-
-
-
-
-
-
-
-
-
-
-
-
+
+ org.apache.commons
+ commons-lang3
+ 3.5
+
+
-
-
- org.apache.maven.plugins
- maven-compiler-plugin
- ${maven-compiler-plugin.version}
-
- 1.8
- 1.8
-
-
+
+ jaxb
+
+
+ src/main/resources
+ true
+
+
-
-
- org.codehaus.mojo
- jaxb2-maven-plugin
- ${jaxb2-maven-plugin.version}
-
-
- xjc
-
- xjc
-
-
-
-
-
- src/main/resources/global.xjb
-
-
- src/main/resources/user.xsd
-
- ${basedir}/src/main/java
- false
-
-
+
+
+
+ org.eclipse.m2e
+ lifecycle-mapping
+ 1.0.0
+
+
+
+
+
+ org.codehaus.mojo
+ jaxb2-maven-plugin
+ [${jaxb2-maven-plugin.version},)
+
+ schemagen
+ xjc
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ ${maven-compiler-plugin.version}
+
+ 1.8
+ 1.8
+
+
-
-
- 2.2.11
+
+
+ org.codehaus.mojo
+ jaxb2-maven-plugin
+ ${jaxb2-maven-plugin.version}
+
+
+ xjc
+
+ xjc
+
+
+
+
+
+ src/main/resources/global.xjb
+
+
+ src/main/resources/user.xsd
+
+ ${basedir}/src/main/java
+ false
+
+
-
- 1.7.21
- 1.1.7
+
+
+
+
-
- 3.5.1
- 2.3
- 4.12
-
+
+
+ 2.2.11
-
+
+ 1.7.21
+ 1.1.7
+
+
+ 3.5.1
+ 2.3
+ 4.12
+
+
+
\ No newline at end of file
diff --git a/jaxb/src/main/java/com/baeldung/jaxb/Main.java b/jaxb/src/main/java/com/baeldung/jaxb/Main.java
deleted file mode 100644
index 46adfb3c60..0000000000
--- a/jaxb/src/main/java/com/baeldung/jaxb/Main.java
+++ /dev/null
@@ -1,41 +0,0 @@
-package com.baeldung.jaxb;
-
-import java.io.File;
-import java.io.FileReader;
-import java.io.IOException;
-import java.util.Date;
-import java.util.TimeZone;
-
-import javax.xml.bind.JAXBContext;
-import javax.xml.bind.JAXBException;
-import javax.xml.bind.Marshaller;
-import javax.xml.bind.Unmarshaller;
-
-public class Main {
- public static void marshal() throws JAXBException, IOException {
- Book book = new Book();
- book.setId(1L);
- book.setName("Book1");
- book.setAuthor("Author1");
- TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
- book.setDate(new Date(1481909329718L));
-
- JAXBContext context = JAXBContext.newInstance(Book.class);
- Marshaller marshaller = context.createMarshaller();
- marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
- marshaller.marshal(book, new File("./book.xml"));
- }
-
- public static Book unMashal() throws JAXBException, IOException {
- JAXBContext context = JAXBContext.newInstance(Book.class);
- Unmarshaller unmarshaller = context.createUnmarshaller();
- Book book = (Book) unmarshaller.unmarshal(new FileReader("./book.xml"));
- return book;
- }
-
- public static void main(String[] args) throws JAXBException, IOException {
- marshal();
- Book book = unMashal();
- System.out.println(book.toString());
- }
-}
diff --git a/jaxb/src/test/java/com/baeldung/jaxb/test/JaxbTest.java b/jaxb/src/test/java/com/baeldung/jaxb/test/JaxbTest.java
index 10febabacd..7721490de8 100644
--- a/jaxb/src/test/java/com/baeldung/jaxb/test/JaxbTest.java
+++ b/jaxb/src/test/java/com/baeldung/jaxb/test/JaxbTest.java
@@ -1,6 +1,16 @@
package com.baeldung.jaxb.test;
-import com.baeldung.jaxb.Book;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.Date;
+import java.util.TimeZone;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Before;
@@ -8,15 +18,7 @@ import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
-import javax.xml.bind.JAXBContext;
-import javax.xml.bind.JAXBException;
-import javax.xml.bind.Marshaller;
-import javax.xml.bind.Unmarshaller;
-import java.io.File;
-import java.io.FileReader;
-import java.io.IOException;
-import java.util.Date;
-import java.util.TimeZone;
+import com.baeldung.jaxb.Book;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class JaxbTest {
@@ -37,18 +39,19 @@ public class JaxbTest {
public void marshal() throws JAXBException, IOException {
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
- marshaller.marshal(book, new File("./book.xml"));
- String sampleBookXML = FileUtils.readFileToString(new File("./sample_book.xml"), "UTF-8");
- String marshallerBookXML = FileUtils.readFileToString(new File("./book.xml"), "UTF-8");
-
+ marshaller.marshal(book, new File(this.getClass().getResource("/").getPath() + "/book.xml"));
+ File sampleBookFile = new File(this.getClass().getResource("/sample_book.xml").getFile());
+ File bookFile = new File(this.getClass().getResource("/book.xml").getFile());
+ String sampleBookXML = FileUtils.readFileToString(sampleBookFile, "UTF-8");
+ String marshallerBookXML = FileUtils.readFileToString(bookFile, "UTF-8");
Assert.assertEquals(sampleBookXML, marshallerBookXML);
}
@Test
public void unMashal() throws JAXBException, IOException {
Unmarshaller unmarshaller = context.createUnmarshaller();
- Book unMarshallerbook = (Book) unmarshaller.unmarshal(new FileReader("./book.xml"));
-
+ String bookFile = this.getClass().getResource("/book.xml").getFile();
+ Book unMarshallerbook = (Book) unmarshaller.unmarshal(new FileReader(bookFile));
Assert.assertEquals(book, unMarshallerbook);
}
}
diff --git a/jaxb/sample_book.xml b/jaxb/src/test/resources/sample_book.xml
similarity index 100%
rename from jaxb/sample_book.xml
rename to jaxb/src/test/resources/sample_book.xml