View Javadoc
1   package org.djunits.unit;
2   
3   import static org.junit.Assert.assertTrue;
4   
5   import java.lang.reflect.Field;
6   
7   import org.junit.Assert;
8   import org.junit.Test;
9   
10  /**
11   * <p>
12   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
14   * <p>
15   * $LastChangedDate: 2018-01-28 03:17:44 +0100 (Sun, 28 Jan 2018) $, @version $Revision: 256 $, by $Author: averbraeck $,
16   * initial version 12 jan. 2015 <br>
17   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
18   * @author <a href="http://Hansvanlint.weblog.tudelft.nl">Hans van Lint</a>
19   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
20   */
21  public class DefinesSITest
22  {
23      /**
24       * Check that all unit classes that extend Unit define the SI field.
25       */
26      @SuppressWarnings("rawtypes")
27      @Test
28      public final void definesSI()
29      {
30          for (String className : Unit.STANDARD_UNITS)
31          {
32              if (className.endsWith("SIUnit") || className.endsWith("LinearUnit") || className.contains("Money"))
33              {
34                  continue;
35              }
36  
37              Class c;
38              try
39              {
40                  c = Class.forName("org.djunits.unit." + className);
41              }
42              catch (Exception exception)
43              {
44                  Assert.fail("Class org.djunits.unit." + className + " could not be loaded");
45                  return;
46              }
47  
48              Field[] fields = c.getDeclaredFields();
49              boolean foundSI = false;
50              for (Field f : fields)
51              {
52                  // TODO: Test for SI for relative classes; BASE for absolute classes
53                  if (f.getName().equals("SI") || f.getName().equals("BASE"))
54                  {
55                      foundSI = true;
56                  }
57              }
58              assertTrue("Class " + className + " does not declare field SI or BASE", foundSI);
59          }
60      }
61  }