Changeset 31795 in osm for applications/editors


Ignore:
Timestamp:
2015-12-03T22:27:47+01:00 (9 years ago)
Author:
floscher
Message:

[mapillary] Add tests for TrafficoSign

Location:
applications/editors/josm/plugins/mapillary
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/oauth/OAuthUtils.java

    r31787 r31795  
    3737        "Bearer " + Main.pref.get("mapillary.access-token"));
    3838
    39     BufferedReader in = new BufferedReader(new InputStreamReader(
    40         con.getInputStream()));
    41     return Json.createReader(in).readObject();
     39    try (
     40      BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))
     41    ) {
     42      return Json.createReader(in).readObject();
     43    }
    4244  }
    4345}
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/traffico/TrafficoGlyph.java

    r31793 r31795  
    1010import javax.json.JsonReader;
    1111
    12 public class TrafficoGlyph {
     12public final class TrafficoGlyph {
    1313  private TrafficoGlyph() {
    1414    // private constructor to avoid instantiation
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/traffico/TrafficoSign.java

    r31787 r31795  
    99import java.util.Map;
    1010import java.util.Set;
    11 import java.util.TreeMap;
    1211
    1312import javax.json.Json;
     
    1514import javax.json.JsonObject;
    1615
    17 public class TrafficoSign {
    18   private static final Map<String, String> isIn = new TreeMap<>();
    19 
    20   static {
    21     isIn.put("be", "europe");
    22     isIn.put("de", "europe");
    23     isIn.put("dk", "europe");
    24     isIn.put("es", "europe");
     16public final class TrafficoSign {
     17  private TrafficoSign() {
     18    // private constructor to avoid instantiation
    2519  }
    2620
     
    3125      System.out.println("Read signs for " + country);
    3226      InputStream countryStream = TrafficoSign.class
    33           .getResourceAsStream("/data/fonts/traffico/signs/" + country
    34               + ".json");
     27          .getResourceAsStream("/data/fonts/traffico/signs/" + country + ".json");
    3528      if (countryStream == null) {
    3629        return null;
     
    8982      return signs.get(country).get(signName);
    9083    }
    91     if (isIn.containsKey(country)) {
    92       return TrafficoSign.getSign(isIn.get(country), signName);
    93     }
    9484    return null;
    9585  }
  • applications/editors/josm/plugins/mapillary/test/unit/org/openstreetmap/josm/plugins/mapillary/traffico/TrafficoGlyphTest.java

    r31793 r31795  
    33import static org.junit.Assert.assertEquals;
    44
    5 import java.lang.reflect.Constructor;
    6 
    75import org.junit.Test;
     6import org.openstreetmap.josm.plugins.mapillary.utils.TestUtil;
    87
    98public class TrafficoGlyphTest {
     
    1716  }
    1817
    19   /**
    20    * The following test has the only purpose to provide code coverage for the private constructor.
    21    */
    2218  @Test
    23   public void testPrivateConstructor() throws Exception {
    24     Constructor<?> c = TrafficoGlyph.class.getDeclaredConstructors()[0];
    25     c.setAccessible(true);
    26     c.newInstance();
     19  public void testUtilityClass() {
     20    TestUtil.testUtilityClass(TrafficoGlyph.class);
    2721  }
    2822
  • applications/editors/josm/plugins/mapillary/test/unit/org/openstreetmap/josm/plugins/mapillary/utils/TestUtil.java

    r31460 r31795  
    11package org.openstreetmap.josm.plugins.mapillary.utils;
    22
     3import static org.junit.Assert.assertEquals;
     4import static org.junit.Assert.assertTrue;
     5
     6import java.lang.reflect.Constructor;
     7import java.lang.reflect.Method;
     8import java.lang.reflect.Modifier;
    39
    410import org.openstreetmap.josm.Main;
     
    4147  }
    4248
     49  /**
     50   * This method tests utility classes for common coding standards (exactly one constructor that's private,
     51   * only static methods, …) and fails the current test if one of those standards is not met.
     52   * This is inspired by http://stackoverflow.com/questions/4520216 .
     53   * @param c the class under test
     54   */
     55  public static void testUtilityClass(final Class<?> c) {
     56    try {
     57      // class must be final
     58      assertTrue(Modifier.isFinal(c.getModifiers()));
     59      // with exactly one constructor
     60      assertEquals(1, c.getDeclaredConstructors().length);
     61      final Constructor<?> constructor = c.getDeclaredConstructors()[0];
     62      // constructor has to be private
     63      assertTrue(!constructor.isAccessible() && Modifier.isPrivate(constructor.getModifiers()));
     64      constructor.setAccessible(true);
     65      // Call private constructor for code coverage
     66      constructor.newInstance();
     67      for (Method m : c.getMethods()) {
     68        // Check if all methods are static
     69        assertTrue(m.getDeclaringClass() != c || Modifier.isStatic(m.getModifiers()));
     70      }
     71    } catch (Exception e) {
     72      assertTrue(e.getMessage(), false);
     73    }
     74  }
    4375}
Note: See TracChangeset for help on using the changeset viewer.