1 package org.opentrafficsim.xml.bindings;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4
5 import java.util.Locale;
6
7 import javax.xml.bind.JAXBException;
8
9 import org.djunits.unit.LengthUnit;
10 import org.djunits.value.vdouble.scalar.Length;
11 import org.djutils.exceptions.Try;
12 import org.junit.jupiter.api.Test;
13 import org.opentrafficsim.xml.bindings.types.LengthType;
14
15
16
17
18
19
20
21
22
23 public class LengthAdapterTest
24 {
25
26 LengthUnit[] units = new LengthUnit[] {LengthUnit.MILLIMETER, LengthUnit.CENTIMETER, LengthUnit.DECIMETER, LengthUnit.METER,
27 LengthUnit.DECAMETER, LengthUnit.HECTOMETER, LengthUnit.KILOMETER, LengthUnit.MILE, LengthUnit.YARD,
28 LengthUnit.FOOT};
29
30
31 String[] unitStrings = new String[] {"mm", "cm", "dm", "m", "dam", "hm", "km", "mi", "yd", "ft"};
32
33
34
35
36 @Test
37 public void testSignedLengthAdapter() throws JAXBException
38 {
39 Locale.setDefault(Locale.US);
40 LengthAdapter lengthAdapter = new LengthAdapter();
41
42 for (int i = 0; i < this.units.length; i++)
43 {
44 final LengthUnit unit = this.units[i];
45 final String us = this.unitStrings[i];
46
47 assertEquals(new LengthType(new Length(2.3, unit)), lengthAdapter.unmarshal("2.3 " + us));
48
49 assertEquals(new LengthType(new Length(2.3, unit)), lengthAdapter.unmarshal("2.3" + us));
50
51 assertEquals(new LengthType(new Length(-2.3, unit)), lengthAdapter.unmarshal("-2.3 " + us));
52 assertEquals(new LengthType(new Length(-2.3, unit)), lengthAdapter.unmarshal("-2.3" + us));
53
54 assertEquals("2.3 " + us, lengthAdapter.marshal(new LengthType(new Length(2.3, unit))));
55 assertEquals("-2.3 " + us, lengthAdapter.marshal(new LengthType(new Length(-2.3, unit))));
56
57 assertEquals(new LengthType(new Length(0.0, unit)), lengthAdapter.unmarshal("0.0 " + us));
58
59 assertEquals(new LengthType(new Length(0.0, unit)), lengthAdapter.unmarshal("0.0" + us));
60
61 assertEquals(new LengthType(new Length(-0.0, unit)), lengthAdapter.unmarshal("-0.0 " + us));
62 assertEquals(new LengthType(new Length(-0.0, unit)), lengthAdapter.unmarshal("-0.0" + us));
63
64 assertEquals("0.0 " + us, lengthAdapter.marshal(new LengthType(new Length(0.0, unit))));
65 assertEquals("-0.0 " + us, lengthAdapter.marshal(new LengthType(new Length(-0.0, unit))));
66 }
67 }
68
69
70
71
72
73 public void testLengthAdapter() throws JAXBException
74 {
75 Locale.setDefault(Locale.US);
76 PositiveLengthAdapter lengthAdapter = new PositiveLengthAdapter();
77
78 for (int i = 0; i < this.units.length; i++)
79 {
80 final LengthUnit unit = this.units[i];
81 final String us = this.unitStrings[i];
82
83 assertEquals(new LengthType(new Length(2.3, unit)), lengthAdapter.unmarshal("2.3 " + us));
84
85 assertEquals(new LengthType(new Length(2.3, unit)), lengthAdapter.unmarshal("2.3" + us));
86
87 Try.testFail(() -> lengthAdapter.unmarshal("-2.3 " + us));
88 Try.testFail(() -> lengthAdapter.unmarshal("-2.3" + us));
89
90 assertEquals("2.3 " + us, lengthAdapter.marshal(new LengthType(new Length(2.3, unit))));
91 Try.testFail(() -> lengthAdapter.marshal(new LengthType(new Length(-2.3, unit))));
92
93 assertEquals(new LengthType(new Length(0.0, unit)), lengthAdapter.unmarshal("0.0 " + us));
94
95 assertEquals(new LengthType(new Length(0.0, unit)), lengthAdapter.unmarshal("0.0" + us));
96
97 Try.testFail(() -> lengthAdapter.unmarshal("-0.0 " + us));
98 Try.testFail(() -> lengthAdapter.unmarshal("-0.0" + us));
99
100 assertEquals("0.0 " + us, lengthAdapter.marshal(new LengthType(new Length(0.0, unit))));
101 Try.testFail(() -> lengthAdapter.marshal(new LengthType(new Length(-0.0, unit))));
102 }
103 }
104
105 }