-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXmlPojoConvertTest.java
193 lines (140 loc) · 5.36 KB
/
XmlPojoConvertTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package coding.toast.bread.xml_pojo_convert;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
import jakarta.xml.bind.Unmarshaller;
import jakarta.xml.bind.annotation.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import static org.springframework.test.util.AssertionErrors.fail;
/**
* A test class for converting between XML and POJO.<br>
* By the way, the pre-generated XML files are located in the <br>
* <a href="https://github.com/CodingToastBread/java-playground/tree/main/src/test/resources/coding/toast/bread/xml_pojo_convert">xml_pojo_convert directory</a>
*/
@Slf4j
public class XmlPojoConvertTest {
private static final ClassLoader CLASS_LOADER = XmlPojoConvertTest.class.getClassLoader();
@Test
@DisplayName("converting simple xml file to pojo (= unmarshalling)")
void simpleXmlToPojoConvertingTest() {
try (InputStream is = CLASS_LOADER.getResourceAsStream("xml_pojo_convert/simple.xml")) {
JAXBContext jaxbContext = JAXBContext.newInstance(Person.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Person unmarshal = (Person) unmarshaller.unmarshal(is);
log.debug("unmarshal = " + unmarshal);
} catch (IOException | JAXBException e) {
fail(e.getMessage());
}
}
/**
* Pojo Matching with simple.xml file
*/
@XmlRootElement(name = "PERSON")
@XmlAccessorType(XmlAccessType.FIELD)
@Data
private static class Person {
@XmlElement(name = "NAME")
private String name;
@XmlElement(name = "AGE")
private Integer age;
}
@Test
@DisplayName("converting complicate xml file to pojo (= unmarshalling)")
void complicateXmlToPojoConvertingTest() {
try (InputStream is = CLASS_LOADER.getResourceAsStream("xml_pojo_convert/complicate.xml")) {
JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Response buildingInfoResponse = (Response) unmarshaller.unmarshal(is);
log.debug("header info = {}", buildingInfoResponse.getHeader());
log.debug("body info = {}", buildingInfoResponse.getBody());
log.debug("building list info = {}", buildingInfoResponse.getBody().getBldgList());
} catch (IOException | JAXBException e) {
fail(e.getMessage());
}
}
@XmlRootElement(name = "RESPONSE")
@XmlAccessorType(XmlAccessType.FIELD)
@Data
private static class Response {
@XmlElement(name = "HEADER")
private Header header;
@XmlElement(name = "BODY")
private Body body;
}
@XmlRootElement(name = "HEADER")
@XmlAccessorType(XmlAccessType.FIELD)
@Data
private static class Header {
@XmlElement(name = "CODE")
private String code;
@XmlElement(name = "MESSAGE")
private String message;
}
@XmlRootElement(name = "BODY")
@XmlAccessorType(XmlAccessType.FIELD)
@Data
private static class Body {
@XmlElementWrapper(name = "BLDG_LIST")
@XmlElement(name = "BLDG_INFO")
private List<BldgInfo> bldgList;
}
@XmlRootElement(name = "BODY")
@XmlAccessorType(XmlAccessType.FIELD)
@Data @NoArgsConstructor @AllArgsConstructor
private static class BldgInfo {
@XmlElement(name = "ZIPCODE")
private String zipcode;
@XmlElement(name = "BLDG_NM")
private String bldgNm;
}
@Test
@DisplayName("converting pojo to xml string or file (= marshalling)")
void convertPojoToXmlTest() {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);
Marshaller marshaller = jaxbContext.createMarshaller();
Response response = new Response();
Header header = new Header();
Body body = new Body();
List<BldgInfo> bldgInfos = List.of(
new BldgInfo("111-222", "luxury hotel"),
new BldgInfo("222-333", "luxury motel")
);
body.setBldgList(bldgInfos);
header.setCode("200");
header.setMessage("GET BUILDING INFO LIST - SUCCESS");
response.setHeader(header);
response.setBody(body);
// The following code adds a new line to improve readability.
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// read xml string from marshaller
ByteArrayOutputStream os = new ByteArrayOutputStream();
marshaller.marshal(response, os);
String xmlString = os.toString(StandardCharsets.UTF_8);
// remark: if your jdk version is lower than 10, use code below
// ==> String s = new String(os.toByteArray(), StandardCharsets.UTF_8);
log.debug("\n{}", xmlString);
// if you want <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
// to... <?xml version="1.0" encoding="UTF-8"> , then use the code below
// ( replace this code right after the comment "read xml string from marshaller" )
//
// marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
// StringWriter stringWriter = new StringWriter(); // java.io.StringWriter
// stringWriter.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
// marshaller.marshal(response, stringWriter);
// System.out.println("stringWriter.toString() = " + stringWriter.toString());
} catch (JAXBException e) {
fail(e.getMessage());
}
}
}