View Javadoc
1   package org.opentrafficsim.xml.bindings;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   
5   import javax.xml.bind.JAXBException;
6   
7   import org.djunits.unit.LengthUnit;
8   import org.djunits.value.vdouble.scalar.Length;
9   import org.djutils.exceptions.Try;
10  import org.opentrafficsim.xml.bindings.types.LengthBeginEndType;
11  import org.opentrafficsim.xml.bindings.types.LengthBeginEndType.LengthBeginEnd;
12  
13  /**
14   * LengthBeginEndAdapterTest for LengthBeginEnd.
15   * <p>
16   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
18   * </p>
19   * @author <a href="https://github.com/averbraeck" target="_blank">Alexander Verbraeck</a>
20   */
21  public class LengthBeginEndAdapterTest
22  {
23      /** the allowed units. */
24      LengthUnit[] units = new LengthUnit[] {LengthUnit.MILLIMETER, LengthUnit.CENTIMETER, LengthUnit.DECIMETER, LengthUnit.METER,
25              LengthUnit.DECAMETER, LengthUnit.HECTOMETER, LengthUnit.KILOMETER, LengthUnit.MILE, LengthUnit.YARD,
26              LengthUnit.FOOT};
27  
28      /** the corresponding strings. */
29      String[] unitStrings = new String[] {"mm", "cm", "dm", "m", "dam", "hm", "km", "mi", "yd", "ft"};
30  
31      /**
32       * Test the LengthBeginEndAdapter
33       */
34      // TODO: repair @Test (problem with Locale when creating Length from String "2.3mm")
35      public void testLengthBeginEndAdapter() throws JAXBException
36      {
37          LengthBeginEndAdapter lbeAdapter = new LengthBeginEndAdapter();
38  
39          LengthBeginEndType lbeBegin = new LengthBeginEndType(new LengthBeginEnd(true, Length.ZERO));
40          LengthBeginEndType lbeEnd = new LengthBeginEndType(new LengthBeginEnd(false, Length.ZERO));
41  
42          assertEquals(lbeBegin.getValue(), lbeAdapter.unmarshal("BEGIN").getValue());
43          assertEquals(lbeEnd.getValue(), lbeAdapter.unmarshal("END").getValue());
44  
45          assertEquals("BEGIN", lbeAdapter.marshal(lbeBegin));
46          assertEquals("END", lbeAdapter.marshal(lbeEnd));
47  
48          LengthBeginEndType lbeFraction00 = new LengthBeginEndType(new LengthBeginEnd(0.0));
49          LengthBeginEndType lbeFraction05 = new LengthBeginEndType(new LengthBeginEnd(0.5));
50          LengthBeginEndType lbeFraction10 = new LengthBeginEndType(new LengthBeginEnd(1.0));
51  
52          assertEquals(lbeFraction00.getValue(), lbeAdapter.unmarshal("0.0").getValue());
53          assertEquals(lbeFraction00.getValue(), lbeAdapter.unmarshal(".0").getValue());
54          assertEquals(lbeFraction00.getValue(), lbeAdapter.unmarshal("0").getValue());
55          assertEquals(lbeFraction00.getValue(), lbeAdapter.unmarshal("0.00").getValue());
56  
57          assertEquals(lbeFraction05.getValue(), lbeAdapter.unmarshal("0.5").getValue());
58          assertEquals(lbeFraction05.getValue(), lbeAdapter.unmarshal(".5").getValue());
59          assertEquals(lbeFraction05.getValue(), lbeAdapter.unmarshal("0.50").getValue());
60  
61          assertEquals(lbeFraction10.getValue(), lbeAdapter.unmarshal("1.0").getValue());
62          assertEquals(lbeFraction10.getValue(), lbeAdapter.unmarshal("1").getValue());
63          assertEquals(lbeFraction10.getValue(), lbeAdapter.unmarshal("1.00").getValue());
64  
65          assertEquals(lbeFraction00.getValue(), lbeAdapter.unmarshal("0.0%").getValue());
66          assertEquals(lbeFraction00.getValue(), lbeAdapter.unmarshal(".0%").getValue());
67          assertEquals(lbeFraction00.getValue(), lbeAdapter.unmarshal("0%").getValue());
68          assertEquals(lbeFraction00.getValue(), lbeAdapter.unmarshal("0.00%").getValue());
69  
70          assertEquals(lbeFraction00.getValue(), lbeAdapter.unmarshal("0.0 %").getValue());
71          assertEquals(lbeFraction00.getValue(), lbeAdapter.unmarshal(".0 %").getValue());
72          assertEquals(lbeFraction00.getValue(), lbeAdapter.unmarshal("0 %").getValue());
73          assertEquals(lbeFraction00.getValue(), lbeAdapter.unmarshal("0.00 %").getValue());
74  
75          assertEquals(lbeFraction05.getValue(), lbeAdapter.unmarshal("50%").getValue());
76          assertEquals(lbeFraction05.getValue(), lbeAdapter.unmarshal("50.0%").getValue());
77  
78          assertEquals(lbeFraction05.getValue(), lbeAdapter.unmarshal("50 %").getValue());
79          assertEquals(lbeFraction05.getValue(), lbeAdapter.unmarshal("50.0 %").getValue());
80  
81          assertEquals(lbeFraction10.getValue(), lbeAdapter.unmarshal("100%").getValue());
82          assertEquals(lbeFraction10.getValue(), lbeAdapter.unmarshal("100.0%").getValue());
83  
84          assertEquals(lbeFraction10.getValue(), lbeAdapter.unmarshal("100 %").getValue());
85          assertEquals(lbeFraction10.getValue(), lbeAdapter.unmarshal("100.0 %").getValue());
86  
87          assertEquals("0.0", lbeAdapter.marshal(lbeFraction00));
88          assertEquals("0.5", lbeAdapter.marshal(lbeFraction05));
89          assertEquals("1.0", lbeAdapter.marshal(lbeFraction10));
90  
91          Try.testFail(() -> lbeAdapter.unmarshal("XYZ"));
92          Try.testFail(() -> lbeAdapter.unmarshal("BEGIN XYZ"));
93          Try.testFail(() -> lbeAdapter.unmarshal("END XYZ"));
94          Try.testFail(() -> lbeAdapter.unmarshal("END-XYZ"));
95          Try.testFail(() -> lbeAdapter.unmarshal("END-100"));
96          Try.testFail(() -> lbeAdapter.unmarshal("-0.5"));
97          Try.testFail(() -> lbeAdapter.unmarshal("-50%"));
98          Try.testFail(() -> lbeAdapter.unmarshal("END+10m"));
99  
100         for (boolean begin : new boolean[] {false, true})
101         {
102             System.out.println(begin);
103             for (int i = 0; i < this.units.length; i++)
104             {
105                 final LengthUnit unit = this.units[i];
106                 final String us = this.unitStrings[i];
107                 final String prefix = begin ? "" : "END-";
108                 final LengthBeginEndType lbe23 = new LengthBeginEndType(new LengthBeginEnd(begin, new Length(2.3, unit)));
109                 final LengthBeginEndType lbe00 = new LengthBeginEndType(new LengthBeginEnd(begin, new Length(0.0, unit)));
110 
111                 assertEquals(lbe23.getValue(), lbeAdapter.unmarshal(prefix + "2.3 " + us).getValue());
112                 assertEquals(lbe23.getValue(), lbeAdapter.unmarshal(prefix + "2.3" + us).getValue());
113                 Try.testFail(() -> lbeAdapter.unmarshal(prefix + "-2.3 " + us));
114                 Try.testFail(() -> lbeAdapter.unmarshal(prefix + "-2.3" + us));
115 
116                 assertEquals(prefix + "2.3 " + us, lbeAdapter.marshal(lbe23));
117                 Try.testFail(
118                         () -> lbeAdapter.marshal(new LengthBeginEndType(new LengthBeginEnd(begin, new Length(-2.3, unit)))));
119 
120                 assertEquals(lbe00.getValue(), lbeAdapter.unmarshal(prefix + "0.0 " + us).getValue());
121                 assertEquals(lbe00.getValue(), lbeAdapter.unmarshal(prefix + "0.0" + us).getValue());
122                 Try.testFail(() -> lbeAdapter.unmarshal(prefix + "-0.0 " + us));
123                 Try.testFail(() -> lbeAdapter.unmarshal(prefix + "-0.0" + us));
124 
125                 assertEquals(begin ? "BEGIN" : "END", lbeAdapter.marshal(lbe00));
126                 Try.testFail(
127                         () -> lbeAdapter.marshal(new LengthBeginEndType(new LengthBeginEnd(begin, new Length(-0.0, unit)))));
128             }
129         }
130     }
131 
132 }