View Javadoc
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   * LengthAdapterTest for Length and SignedLength.
17   * <p>
18   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
20   * </p>
21   * @author <a href="https://github.com/averbraeck" target="_blank">Alexander Verbraeck</a>
22   */
23  public class LengthAdapterTest
24  {
25      /** the allowed units. */
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      /** the corresponding strings. */
31      String[] unitStrings = new String[] {"mm", "cm", "dm", "m", "dam", "hm", "km", "mi", "yd", "ft"};
32  
33      /**
34       * Test the SignedLengthAdapter
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              // assertEquals(new LengthType(new Length(2.3, unit)), lengthAdapter.unmarshal("+2.3 " + us));
49              assertEquals(new LengthType(new Length(2.3, unit)), lengthAdapter.unmarshal("2.3" + us));
50              // assertEquals(new LengthType(new Length(2.3, unit)), lengthAdapter.unmarshal("+2.3" + us));
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              // assertEquals(new LengthType(new Length(0.0, unit)), lengthAdapter.unmarshal("+0.0 " + us));
59              assertEquals(new LengthType(new Length(0.0, unit)), lengthAdapter.unmarshal("0.0" + us));
60              // assertEquals(new LengthType(new Length(0.0, unit)), lengthAdapter.unmarshal("+0.0" + us));
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       * Test the LengthAdapter
71       */
72      // TODO: Repair @Test
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              // assertEquals(new LengthType(new Length(2.3, unit)), lengthAdapter.unmarshal("+2.3 " + us));
85              assertEquals(new LengthType(new Length(2.3, unit)), lengthAdapter.unmarshal("2.3" + us));
86              // assertEquals(new LengthType(new Length(2.3, unit)), lengthAdapter.unmarshal("+2.3" + us));
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              // assertEquals(new LengthType(new Length(0.0, unit)), lengthAdapter.unmarshal("+0.0 " + us));
95              assertEquals(new LengthType(new Length(0.0, unit)), lengthAdapter.unmarshal("0.0" + us));
96              // assertEquals(new LengthType(new Length(0.0, unit)), lengthAdapter.unmarshal("+0.0" + us));
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 }