Skip to content

Commit 8442fc3

Browse files
committed
Merge changes from branch
1 parent 66e6ae6 commit 8442fc3

6 files changed

Lines changed: 106 additions & 10 deletions

File tree

‎jaxb/book.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<book id="1">
3+
<title>Book1</title>
4+
<date>2016-12-16T17:28:49.718Z</date>
5+
</book>

‎jaxb/pom.xml‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@
2323
<dependency>
2424
<groupId>com.sun.istack</groupId>
2525
<artifactId>istack-commons-runtime</artifactId>
26-
<version>${istack.version}</version>
26+
<version>3.0.2</version>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>commons-io</groupId>
31+
<artifactId>commons-io</artifactId>
32+
<version>2.5</version>
2733
</dependency>
2834

2935
<dependency>
@@ -43,7 +49,18 @@
4349
<artifactId>logback-core</artifactId>
4450
<version>${logback.version}</version>
4551
</dependency>
46-
52+
53+
<dependency>
54+
<groupId>junit</groupId>
55+
<artifactId>junit</artifactId>
56+
<version>${junit.version}</version>
57+
</dependency>
58+
59+
<dependency>
60+
<groupId>org.apache.commons</groupId>
61+
<artifactId>commons-lang3</artifactId>
62+
<version>3.5</version>
63+
</dependency>
4764
</dependencies>
4865

4966
<build>
@@ -60,7 +77,7 @@
6077
<plugin>
6178
<groupId>org.eclipse.m2e</groupId>
6279
<artifactId>lifecycle-mapping</artifactId>
63-
<version>${lifecycle-mapping.version}</version>
80+
<version>1.0.0</version>
6481
<configuration>
6582
<lifecycleMappingMetadata>
6683
<pluginExecutions>
@@ -158,16 +175,14 @@
158175
<!-- jaxb -->
159176
<jaxb.version>2.2.11</jaxb.version>
160177

161-
<istack.version>3.0.2</istack.version>
162-
163178
<!-- logging -->
164179
<org.slf4j.version>1.7.21</org.slf4j.version>
165180
<logback.version>1.1.7</logback.version>
166181

167182
<!-- maven plugins -->
168-
<lifecycle-mapping.version>1.0.0</lifecycle-mapping.version>
169-
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
183+
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
170184
<jaxb2-maven-plugin.version>2.3</jaxb2-maven-plugin.version>
185+
<junit.version>4.12</junit.version>
171186
</properties>
172187

173-
</project>
188+
</project>

‎jaxb/sample_book.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<book id="1">
3+
<title>Book1</title>
4+
<date>2016-12-16T17:28:49.718Z</date>
5+
</book>

‎jaxb/src/main/java/com/baeldung/jaxb/Book.java‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
import javax.xml.bind.annotation.XmlTransient;
99
import javax.xml.bind.annotation.XmlType;
1010

11+
import org.apache.commons.lang3.builder.EqualsBuilder;
12+
import org.apache.commons.lang3.builder.HashCodeBuilder;
13+
import org.apache.commons.lang3.builder.ToStringBuilder;
14+
1115
@XmlRootElement(name = "book")
1216
@XmlType(propOrder = { "id", "name", "date" })
1317
public class Book {
@@ -53,6 +57,17 @@ public String getAuthor() {
5357

5458
@Override
5559
public String toString() {
56-
return "Book [id=" + id + ", name=" + name + ", author=" + author + ", date=" + date + "]";
60+
return ToStringBuilder.reflectionToString(this);
61+
}
62+
63+
@Override
64+
public boolean equals(Object obj) {
65+
return EqualsBuilder.reflectionEquals(this, obj);
5766
}
67+
68+
@Override
69+
public int hashCode() {
70+
return HashCodeBuilder.reflectionHashCode(this);
71+
}
72+
5873
}

‎jaxb/src/main/java/com/baeldung/jaxb/Main.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.FileReader;
55
import java.io.IOException;
66
import java.util.Date;
7+
import java.util.TimeZone;
78

89
import javax.xml.bind.JAXBContext;
910
import javax.xml.bind.JAXBException;
@@ -16,7 +17,8 @@ public static void marshal() throws JAXBException, IOException {
1617
book.setId(1L);
1718
book.setName("Book1");
1819
book.setAuthor("Author1");
19-
book.setDate(new Date());
20+
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
21+
book.setDate(new Date(1481909329718L));
2022

2123
JAXBContext context = JAXBContext.newInstance(Book.class);
2224
Marshaller marshaller = context.createMarshaller();
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.jaxb.test;
2+
3+
import java.io.File;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
import java.util.Date;
7+
import java.util.TimeZone;
8+
9+
import javax.xml.bind.JAXBContext;
10+
import javax.xml.bind.JAXBException;
11+
import javax.xml.bind.Marshaller;
12+
import javax.xml.bind.Unmarshaller;
13+
14+
import org.apache.commons.io.FileUtils;
15+
import org.junit.Assert;
16+
import org.junit.Before;
17+
import org.junit.FixMethodOrder;
18+
import org.junit.Test;
19+
import org.junit.runners.MethodSorters;
20+
21+
import com.baeldung.jaxb.Book;
22+
23+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
24+
public class JaxbTest {
25+
Book book;
26+
JAXBContext context;
27+
28+
@Before
29+
public void before() throws JAXBException {
30+
book = new Book();
31+
book.setId(1L);
32+
book.setName("Book1");
33+
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
34+
book.setDate(new Date(1481909329718L));
35+
context = JAXBContext.newInstance(Book.class);
36+
}
37+
38+
@Test
39+
public void marshal() throws JAXBException, IOException {
40+
Marshaller marshaller = context.createMarshaller();
41+
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
42+
marshaller.marshal(book, new File("./book.xml"));
43+
String sampleBookXML = FileUtils.readFileToString(new File("./sample_book.xml"), "UTF-8");
44+
String marshallerBookXML = FileUtils.readFileToString(new File("./book.xml"), "UTF-8");
45+
Assert.assertEquals(sampleBookXML, marshallerBookXML);
46+
}
47+
48+
@Test
49+
public void unMashal() throws JAXBException, IOException {
50+
Unmarshaller unmarshaller = context.createUnmarshaller();
51+
Book unMarshallerbook = (Book) unmarshaller.unmarshal(new FileReader("./book.xml"));
52+
Assert.assertEquals(book, unMarshallerbook);
53+
}
54+
}

0 commit comments

Comments
 (0)