View Javadoc
1   package org.opentrafficsim.core.unit;
2   
3   import static org.junit.Assert.assertTrue;
4   
5   import java.io.FileInputStream;
6   import java.util.ArrayList;
7   import java.util.HashSet;
8   import java.util.List;
9   import java.util.Locale;
10  import java.util.Properties;
11  import java.util.ResourceBundle;
12  import java.util.Set;
13  
14  import org.junit.Test;
15  import org.opentrafficsim.core.AvailableLocalizations;
16  import org.opentrafficsim.core.locale.DefaultLocale;
17  import org.reflections.Reflections;
18  
19  /**
20   * <p>
21   * Copyright (c) 2013-2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
22   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
23   * <p>
24   * @version Jun 10, 2014 <br>
25   * @author <a href="http://tudelft.nl/pknoppers">Peter Knoppers</a>
26   */
27  public class UnitLocalizationsTest
28  {
29      /** Prefix keys of units made during testing with this string. */
30      public static final String DONOTCHECKPREFIX = "~~~~DONOTCHECK";
31  
32      /**
33       * Check that all defined units have all localizations.
34       */
35      @SuppressWarnings({"rawtypes", "unchecked"})
36      @Test
37      public final void checkDefinedUnits()
38      {
39          final String head = "localeunit";
40          Set<String> usedKeys = new HashSet<String>();
41          ArrayList<String> errors = new ArrayList<String>();
42          List<String> localeNames =
43              AvailableLocalizations.availableLocalizations(head, this.getClass().getResource("").getPath() + "../../../../");
44          for (String localeName : localeNames)
45          {
46              // System.out.println("Checking internationalization to " + localeName);
47              DefaultLocale.setLocale(new Locale(localeName));
48  
49              Reflections reflections = new Reflections("org.opentrafficsim.core.unit");
50              Set<Class<? extends Unit>> classes = reflections.getSubTypesOf(Unit.class);
51  
52              for (Class c : classes)
53              {
54                  // System.out.println(c.getSimpleName() + ": " + Unit.getUnits(c));
55                  for (Object o : Unit.getUnits(c))
56                  {
57                      Unit<?> u = (Unit<?>) o;
58                      String nameKey = u.getNameKey();
59                      assertTrue("Name key must be non-null", null != nameKey);
60                      assertTrue("Name key must be non-empty", nameKey.length() > 0);
61                      String abbreviationKey = u.getAbbreviationKey();
62                      assertTrue("Abbreviation key must be non-null", null != abbreviationKey);
63                      assertTrue("Abbreviation key must be non-empty", abbreviationKey.length() > 0);
64                      if (nameKey.startsWith(DONOTCHECKPREFIX))
65                      {
66                          continue;
67                      }
68                      if (nameKey.equals("SIUnit.m2"))
69                      {
70                          continue; // FIXME: Vector and Matrix tests make these and then cause this test to fail
71                      }
72                      if (nameKey.equals("SIUnit.kg.m2/s2"))
73                      {
74                          continue; // FIXME: Vector and Matrix tests make these and then cause this test to fail
75                      }
76                      if (nameKey.equals("SIUnit.s"))
77                      {
78                          continue; // FIXME: Scalar tests make these and then cause this test to fail
79                      }
80                      if (nameKey.equals("SIUnit.kg2.m4/s6/A2"))
81                      {
82                          continue;
83                      }
84                      if (nameKey.equals("SIUnit.1/A"))
85                      {
86                          continue;
87                      }
88                      if (abbreviationKey.startsWith(DONOTCHECKPREFIX))
89                      {
90                          continue;
91                      }
92                      usedKeys.add(nameKey);
93                      usedKeys.add(abbreviationKey);
94                      String name = u.getName();
95                      // assertFalse("Name may not begin AND end with an exclamation mark",
96                      // name.startsWith("!") && name.endsWith("!"));
97                      String abbreviation = u.getAbbreviation();
98                      // System.out.println("nameKey " + nameKey + "->" + name + " abbreviationKey " + abbreviationKey
99                      // + "->" + abbreviation);
100                     if (abbreviation.startsWith("!") && abbreviation.endsWith("!"))
101                     {
102                         errors.add(String.format("Missing translation for abbreviation %s to %s", abbreviationKey,
103                             localeName));
104                     }
105                     if (name.startsWith("!") && name.endsWith("!"))
106                     {
107                         errors.add(String.format("Missing translation for name %s to %s", nameKey, localeName));
108                     }
109                 }
110             }
111         }
112         for (String localeName : localeNames)
113         {
114             Properties properties = new Properties();
115             String middlePart = "";
116             if (!localeName.equals("en"))
117             {
118                 middlePart = "_" + localeName;
119             }
120 
121             String path = this.getClass().getResource("").getPath() + "../../../../" + head + middlePart + ".properties";
122             try
123             {
124                 FileInputStream fileInput = new FileInputStream(path);
125                 properties.load(fileInput);
126                 fileInput.close();
127             }
128             catch (Exception exception)
129             {
130                 exception.printStackTrace();
131             }
132             ResourceBundle.clearCache();
133             Set<String> unusedKeys = new HashSet<String>();
134             for (Object key : properties.keySet())
135             {
136                 String keyString = (String) key;
137                 if (usedKeys.contains(keyString))
138                 {
139                     continue;
140                 }
141                 unusedKeys.add(keyString);
142             }
143             for (String unusedKey : unusedKeys)
144             {
145                 errors.add(String.format("Unused key %s for locale %s", unusedKey, localeName));
146             }
147         }
148         for (String s : errors)
149         {
150             System.err.println("UnitLocalizations error: " + s);
151         }
152         assertTrue("There should be no errors in UnitLocalizations", errors.isEmpty());
153     }
154 
155 }