View Javadoc
1   package org.opentrafficsim.core.unit;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertTrue;
5   import static org.opentrafficsim.core.unit.unitsystem.UnitSystem.OTHER;
6   
7   import java.util.Locale;
8   
9   import org.junit.Before;
10  import org.junit.Test;
11  import org.opentrafficsim.core.locale.DefaultLocale;
12  
13  /**
14   * <p>
15   * Copyright (c) 2013-2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
17   * <p>
18   * @version Jun 4, 2014 <br>
19   * @author <a href="http://tudelft.nl/pknoppers">Peter Knoppers</a>
20   */
21  public class TimeUnitTest extends AbstractUnitTest<TimeUnit>
22  {
23      /**
24       * Set the locale to "en" so we know what texts should be retrieved from the resources.
25       */
26      @SuppressWarnings("static-method")
27      @Before
28      public final void setup()
29      {
30          DefaultLocale.setLocale(new Locale("en"));
31      }
32  
33      /**
34       * Verify the result of some get*Key methods.
35       */
36      @Test
37      public final void keys()
38      {
39          checkKeys(TimeUnit.SECOND, "TimeUnit.second", "TimeUnit.s");
40      }
41  
42      /**
43       * Verify conversion factors, English names and abbreviations.
44       */
45      @Test
46      public final void conversions()
47      {
48          checkUnitRatioNameAndAbbreviation(TimeUnit.SECOND, 1, 0.00000001, "second", "s");
49          checkUnitRatioNameAndAbbreviation(TimeUnit.HOUR, 3600, 0.0005, "hour", "h");
50          checkUnitRatioNameAndAbbreviation(TimeUnit.DAY, 86400, 0.001, "day", "d");
51          // Check two conversions between non-standard units
52          assertEquals("one DAY is 24 HOUR", 24, getMultiplicationFactorTo(TimeUnit.DAY, TimeUnit.HOUR), 0.0001);
53          assertEquals("one HOUR is about 0.0417 DAY", 0.0417, getMultiplicationFactorTo(TimeUnit.HOUR, TimeUnit.DAY), 0.0001);
54          // Check conversion factor to standard unit for all remaining time units
55          checkUnitRatioNameAndAbbreviation(TimeUnit.MILLISECOND, 0.001, 0.00000001, "millisecond", "ms");
56          checkUnitRatioNameAndAbbreviation(TimeUnit.MINUTE, 60, 0.000001, "minute", "m");
57          checkUnitRatioNameAndAbbreviation(TimeUnit.WEEK, 7 * 86400, 0.1, "week", "w");
58      }
59  
60      /**
61       * Verify that we can create our own length unit.
62       */
63      @Test
64      public final void createLengthUnit()
65      {
66          TimeUnit myTU =
67              new TimeUnit(UnitLocalizationsTest.DONOTCHECKPREFIX + "TimeUnit.Fortnight",
68                  UnitLocalizationsTest.DONOTCHECKPREFIX + "TimeUnit.fn", OTHER, TimeUnit.SECOND, 14 * 86400);
69          assertTrue("Can create a new TimeUnit", null != myTU);
70          checkUnitRatioNameAndAbbreviation(myTU, 14 * 86400, 1, "!Fortnight!", "!fn!");
71      }
72  
73  }