1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.bremersee.gpx;
18
19 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22
23 import java.io.StringReader;
24 import java.io.StringWriter;
25 import java.math.BigDecimal;
26 import java.util.List;
27 import java.util.Optional;
28 import java.util.ServiceLoader;
29 import javax.xml.datatype.XMLGregorianCalendar;
30 import org.assertj.core.api.SoftAssertions;
31 import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension;
32 import org.bremersee.garmin.creationtime.v1.model.ext.CreationTimeExtension;
33 import org.bremersee.garmin.gpx.v3.model.ext.AddressT;
34 import org.bremersee.garmin.gpx.v3.model.ext.CategoriesT;
35 import org.bremersee.garmin.gpx.v3.model.ext.WaypointExtension;
36 import org.bremersee.gpx.model.ExtensionsType;
37 import org.bremersee.gpx.model.Gpx;
38 import org.bremersee.gpx.model.LinkType;
39 import org.bremersee.gpx.model.WptType;
40 import org.bremersee.xml.JaxbContextBuilder;
41 import org.bremersee.xml.JaxbContextDataProvider;
42 import org.bremersee.xml.SchemaMode;
43 import org.junit.jupiter.api.BeforeAll;
44 import org.junit.jupiter.api.Test;
45 import org.junit.jupiter.api.extension.ExtendWith;
46 import org.springframework.core.io.DefaultResourceLoader;
47 import org.springframework.core.io.ResourceLoader;
48
49
50
51
52
53
54 @ExtendWith(SoftAssertionsExtension.class)
55 class JaxbContextBuilderTest {
56
57 private static final ResourceLoader RESOURCE_LOADER = new DefaultResourceLoader();
58
59 private static JaxbContextBuilder jaxbContextBuilder;
60
61
62
63
64 @BeforeAll
65 static void createJaxbContextBuilder() {
66 jaxbContextBuilder = JaxbContextBuilder.newInstance()
67 .withSchemaMode(SchemaMode.NEVER)
68 .processAll(ServiceLoader.load(JaxbContextDataProvider.class))
69 .initJaxbContext();
70 }
71
72 private static Object unmarshalClassPathResource(final String classPathResource)
73 throws Exception {
74 return jaxbContextBuilder
75 .buildUnmarshaller()
76 .unmarshal(RESOURCE_LOADER.getResource(classPathResource).getInputStream());
77 }
78
79
80
81
82
83
84
85 @Test
86 void testGpxWithWpt(SoftAssertions softly) throws Exception {
87 CategoriesT categories = new CategoriesT();
88 categories.getCategories().add("JUNIT");
89
90 AddressT address = new AddressT();
91 address.setCountry("Italy");
92 address.setCountry("Rom");
93
94 WaypointExtension waypointExtension = new WaypointExtension();
95 waypointExtension.setCategories(categories);
96 waypointExtension.setAddress(address);
97
98 ExtensionsType extensionsType = ExtensionsTypeBuilder.newInstance()
99 .addElement(waypointExtension, jaxbContextBuilder.buildMarshaller(waypointExtension))
100 .build(true);
101
102 LinkType link = new LinkType();
103 link.setHref("http://localhost");
104
105 WptType wpt = new WptType();
106 wpt.setLat(new BigDecimal("52.4"));
107 wpt.setLon(new BigDecimal("10.8"));
108 wpt.setSrc("test");
109 wpt.getLinks().add(link);
110 wpt.setExtensions(extensionsType);
111
112 Gpx gpx = new Gpx();
113 gpx.setCreator("org.bremersee");
114 gpx.setVersion("1.1");
115 gpx.getWpts().add(wpt);
116
117 StringWriter sw = new StringWriter();
118 jaxbContextBuilder.buildMarshaller(gpx).marshal(gpx, sw);
119 String xml = sw.toString();
120 softly.assertThat(xml).isNotNull();
121
122 Gpx readGpx = (Gpx) jaxbContextBuilder.buildUnmarshaller(Gpx.class)
123 .unmarshal(new StringReader(xml));
124 softly.assertThat(readGpx).isNotNull();
125 softly.assertThat(readGpx.getWpts()).isNotEmpty();
126
127 WptType readWpt = readGpx.getWpts().get(0);
128 softly.assertThat(readWpt.getLat()).isEqualTo(wpt.getLat());
129 softly.assertThat(readWpt.getLon()).isEqualTo(wpt.getLon());
130 softly.assertThat(readWpt.getSrc()).isEqualTo(wpt.getSrc());
131 }
132
133
134
135
136
137
138
139 @Test
140 void testAddressData(SoftAssertions softly) throws Exception {
141 final Object obj = unmarshalClassPathResource("classpath:Adresse.GPX");
142 softly.assertThat(obj).isInstanceOf(Gpx.class);
143
144 final Gpx gpx = (Gpx) obj;
145 softly.assertThat(gpx.getWpts()).isNotEmpty();
146
147 final WptType wpt = gpx.getWpts().get(0);
148 softly.assertThat(wpt.getExtensions()).isNotNull();
149
150 final ExtensionsType extensions = wpt.getExtensions();
151 softly.assertThat(extensions.getAnies()).isNotEmpty();
152
153 final List<WaypointExtension> waypointExtensions = GpxJaxbContextHelper.findExtensions(
154 WaypointExtension.class,
155 true,
156 GpxJaxbContextHelper.parseExtensions(extensions, jaxbContextBuilder.buildJaxbContext()));
157 softly.assertThat(waypointExtensions).isNotEmpty();
158
159 WaypointExtension wptExt = waypointExtensions.get(0);
160 softly.assertThat(wptExt).isNotNull();
161 softly.assertThat(wptExt.getAddress()).isNotNull();
162 softly.assertThat(wptExt.getAddress().getStreetAddresses()).isNotEmpty();
163 softly.assertThat(wptExt.getAddress().getStreetAddresses()).contains("Seerosenweg 1");
164
165 Optional<WaypointExtension> optionalWaypointExtension = GpxJaxbContextHelper
166 .findFirstExtension(
167 WaypointExtension.class,
168 true,
169 extensions,
170 jaxbContextBuilder.buildUnmarshaller());
171
172 softly.assertThat(optionalWaypointExtension)
173 .map(WaypointExtension::getAddress)
174 .map(AddressT::getStreetAddresses)
175 .contains(List.of("Seerosenweg 1"));
176 }
177
178
179
180
181
182
183
184 @Test
185 void testPictureData(SoftAssertions softly) throws Exception {
186 final Object obj = unmarshalClassPathResource("classpath:Bild.GPX");
187 softly.assertThat(obj).isInstanceOf(Gpx.class);
188
189 final Gpx gpx = (Gpx) obj;
190 softly.assertThat(gpx.getWpts()).isNotEmpty();
191
192 final WptType wpt = gpx.getWpts().get(0);
193 softly.assertThat(wpt.getExtensions()).isNotNull();
194
195 final ExtensionsType extensions = wpt.getExtensions();
196 softly.assertThat(extensions.getAnies()).isNotEmpty();
197
198 Optional<CreationTimeExtension> cr = GpxJaxbContextHelper.findFirstExtension(
199 CreationTimeExtension.class, true, extensions, jaxbContextBuilder.buildJaxbContext());
200
201 softly.assertThat(cr)
202 .map(CreationTimeExtension::getCreationTime)
203 .map(XMLGregorianCalendar::getYear)
204 .get()
205 .isEqualTo(2012);
206 }
207
208
209
210
211
212
213
214 @Test
215 void testRoute(SoftAssertions softly) throws Exception {
216 Object obj = unmarshalClassPathResource("classpath:Route.GPX");
217 softly.assertThat(obj).isInstanceOf(Gpx.class);
218
219 StringWriter sw = new StringWriter();
220 jaxbContextBuilder.buildMarshaller(obj).marshal(obj, sw);
221 String xml = sw.toString();
222 softly.assertThat(xml).containsIgnoringCase("<gpx");
223 }
224
225
226
227
228
229
230
231 @Test
232 void testTrack(SoftAssertions softly) throws Exception {
233 Object obj = unmarshalClassPathResource("classpath:Track.GPX");
234 assertNotNull(obj);
235 assertInstanceOf(Gpx.class, obj);
236
237 StringWriter sw = new StringWriter();
238 jaxbContextBuilder.buildMarshaller(obj).marshal(obj, sw);
239 String xml = sw.toString();
240 softly.assertThat(xml).containsIgnoringCase("<gpx");
241 }
242
243 }