Changeset 9203 in josm


Ignore:
Timestamp:
2015-12-28T19:15:45+01:00 (8 years ago)
Author:
Don-vip
Message:

add more unit tests, javadoc

Location:
trunk
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/NameFormatter.java

    r8510 r9203  
    44import java.util.Comparator;
    55
     6/**
     7 * Formats a name for a {@link OsmPrimitive}.
     8 * @since 1990
     9 */
    610public interface NameFormatter {
     11
     12    /**
     13     * Formats a name for a {@link Node}.
     14     *
     15     * @param node the node
     16     * @return the name
     17     */
    718    String format(Node node);
    819
     20    /**
     21     * Formats a name for a {@link Way}.
     22     *
     23     * @param way the way
     24     * @return the name
     25     */
    926    String format(Way way);
    1027
     28    /**
     29     * Formats a name for a {@link Relation}.
     30     *
     31     * @param relation the relation
     32     * @return the name
     33     */
    1134    String format(Relation relation);
    1235
     36    /**
     37     * Formats a name for a {@link Changeset}.
     38     *
     39     * @param changeset the changeset
     40     * @return the name
     41     */
    1342    String format(Changeset changeset);
    1443
  • trunk/src/org/openstreetmap/josm/data/osm/history/HistoryNameFormatter.java

    r8510 r9203  
    22package org.openstreetmap.josm.data.osm.history;
    33
     4/**
     5 * Formats a name for a {@link HistoryOsmPrimitive}.
     6 * @since 2686
     7 */
    48public interface HistoryNameFormatter {
     9
     10    /**
     11     * Formats a name for a {@link HistoryNode}.
     12     *
     13     * @param node the node
     14     * @return the name
     15     */
    516    String format(HistoryNode node);
    617
    7     String format(HistoryWay node);
     18    /**
     19     * Formats a name for a {@link HistoryWay}.
     20     *
     21     * @param way the way
     22     * @return the name
     23     */
     24    String format(HistoryWay way);
    825
    9     String format(HistoryRelation node);
     26    /**
     27     * Formats a name for a {@link HistoryRelation}.
     28     *
     29     * @param relation the relation
     30     * @return the name
     31     */
     32    String format(HistoryRelation relation);
    1033}
  • trunk/src/org/openstreetmap/josm/gui/DefaultNameFormatter.java

    r8882 r9203  
    4545
    4646/**
    47  * This is the default implementation of a {@link NameFormatter} for names of {@link OsmPrimitive}s.
    48  *
     47 * This is the default implementation of a {@link NameFormatter} for names of {@link OsmPrimitive}s
     48 * and {@link HistoryOsmPrimitive}s.
     49 * @since 1990
    4950 */
    5051public class DefaultNameFormatter implements NameFormatter, HistoryNameFormatter {
     
    135136    }
    136137
    137     /**
    138      * Formats a name for a node
    139      *
    140      * @param node the node
    141      * @return the name
    142      */
    143138    @Override
    144139    public String format(Node node) {
     
    211206    }
    212207
    213 
    214     /**
    215      * Formats a name for a way
    216      *
    217      * @param way the way
    218      * @return the name
    219      */
    220208    @Override
    221209    public String format(Way way) {
     
    249237                }
    250238                if (n == null) {
    251                     n =     (way.get("highway") != null) ? tr("highway") :
    252                                 (way.get("railway") != null) ? tr("railway") :
    253                                     (way.get("waterway") != null) ? tr("waterway") :
    254                                             (way.get("landuse") != null) ? tr("landuse") : null;
     239                    n = (way.get("highway") != null) ? tr("highway") :
     240                            (way.get("railway") != null) ? tr("railway") :
     241                                (way.get("waterway") != null) ? tr("waterway") :
     242                                        (way.get("landuse") != null) ? tr("landuse") : null;
    255243                }
    256244                if (n == null) {
     
    313301    }
    314302
    315 
    316     /**
    317      * Formats a name for a relation
    318      *
    319      * @param relation the relation
    320      * @return the name
    321      */
    322303    @Override
    323304    public String format(Relation relation) {
     
    488469    }
    489470
    490     /**
    491      * Formats a name for a changeset
    492      *
    493      * @param changeset the changeset
    494      * @return the name
    495      */
    496471    @Override
    497472    public String format(Changeset changeset) {
     
    554529    }
    555530
    556     /**
    557      * Formats a name for a history node
    558      *
    559      * @param node the node
    560      * @return the name
    561      */
    562531    @Override
    563532    public String format(HistoryNode node) {
     
    586555    }
    587556
    588     /**
    589      * Formats a name for a way
    590      *
    591      * @param way the way
    592      * @return the name
    593      */
    594557    @Override
    595558    public String format(HistoryWay way) {
     
    628591    }
    629592
    630     /**
    631      * Formats a name for a {@link HistoryRelation})
    632      *
    633      * @param relation the relation
    634      * @return the name
    635      */
    636593    @Override
    637594    public String format(HistoryRelation relation) {
     
    684641    }
    685642
     643    /**
     644     * Formats the given collection of primitives as an HTML unordered list.
     645     * @param primitives collection of primitives to format
     646     * @return HTML unordered list
     647     */
    686648    public String formatAsHtmlUnorderedList(Collection<? extends OsmPrimitive> primitives) {
    687649        return Utils.joinAsHtmlUnorderedList(Utils.transform(primitives, new Function<OsmPrimitive, String>() {
     
    694656    }
    695657
     658    /**
     659     * Formats the given primitive(s) as an HTML unordered list.
     660     * @param primitives primitive(s) to format
     661     * @return HTML unordered list
     662     */
    696663    public String formatAsHtmlUnorderedList(OsmPrimitive... primitives) {
    697664        return formatAsHtmlUnorderedList(Arrays.asList(primitives));
  • trunk/test/unit/org/openstreetmap/josm/data/osm/history/HistoryNodeTest.java

    r8857 r9203  
    66
    77import java.util.Date;
     8import java.util.HashMap;
     9import java.util.Map;
    810
     11import org.junit.BeforeClass;
    912import org.junit.Test;
     13import org.openstreetmap.josm.JOSMFixture;
    1014import org.openstreetmap.josm.data.coor.LatLon;
     15import org.openstreetmap.josm.data.osm.Node;
    1116import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    1217import org.openstreetmap.josm.data.osm.User;
     18import org.openstreetmap.josm.gui.DefaultNameFormatter;
    1319
    1420/**
     
    1723public class HistoryNodeTest {
    1824
    19     @Test
    20     public void historyNode() {
    21         Date d = new Date();
    22         HistoryNode node = new HistoryNode(
    23                 1L,
    24                 2L,
    25                 true,
     25    /**
     26     * Setup test.
     27     */
     28    @BeforeClass
     29    public static void init() {
     30        JOSMFixture.createUnitTestFixture().init();
     31    }
     32
     33    private static HistoryNode create(Date d) {
     34        return new HistoryNode(
     35                1L,   // id
     36                2L,   // version
     37                true, // visible
    2638                User.createOsmUser(3, "testuser"),
    27                 4L,
    28                 d,
     39                4L,   // changesetId
     40                d,    // timestamp
    2941                new LatLon(0, 0)
    3042                );
     43    }
     44
     45    /**
     46     * Unit test for {@link HistoryNode#HistoryNode}.
     47     */
     48    @Test
     49    public void testHistoryNode() {
     50        Date d = new Date();
     51        HistoryNode node = create(d);
    3152
    3253        assertEquals(1, node.getId());
     
    3960    }
    4061
     62    /**
     63     * Unit test for {@link HistoryNode#getType}.
     64     */
    4165    @Test
    42     public void getType() {
    43         Date d = new Date();
    44         HistoryNode node = new HistoryNode(
    45                 1,
    46                 2,
    47                 true,
    48                 User.createOsmUser(3, "testuser"),
    49                 4,
    50                 d,
    51                 new LatLon(0, 0)
    52                 );
     66    public void testGetType() {
     67        assertEquals(OsmPrimitiveType.NODE, create(new Date()).getType());
     68    }
    5369
    54         assertEquals(OsmPrimitiveType.NODE, node.getType());
     70    /**
     71     * Unit test for {@link HistoryNode#getCoords}.
     72     */
     73    @Test
     74    public void testGetCoords() {
     75        Node n = new Node(new LatLon(45, 0));
     76        n.setOsmId(1, 2);
     77        n.setUser(User.createOsmUser(3, "testuser"));
     78        n.setChangesetId(4);
     79        assertEquals(n.getCoor(), new HistoryNode(n).getCoords());
     80    }
     81
     82    /**
     83     * Unit test for {@link HistoryNode#getDisplayName}.
     84     */
     85    @Test
     86    public void testGetDisplayName() {
     87        HistoryNode node = create(new Date());
     88        HistoryNameFormatter hnf = DefaultNameFormatter.getInstance();
     89        assertEquals("1 (0.0, 0.0)", node.getDisplayName(hnf));
     90        LatLon ll = node.getCoords();
     91        node.setCoords(null);
     92        assertEquals("1", node.getDisplayName(hnf));
     93        node.setCoords(ll);
     94        Map<String, String> map = new HashMap<>();
     95        map.put("name", "NodeName");
     96        node.setTags(map);
     97        assertEquals("NodeName (0.0, 0.0)", node.getDisplayName(hnf));
     98        node.setCoords(null);
     99        assertEquals("NodeName", node.getDisplayName(hnf));
    55100    }
    56101}
  • trunk/test/unit/org/openstreetmap/josm/data/osm/history/HistoryWayTest.java

    r8857 r9203  
    88import java.util.ArrayList;
    99import java.util.Date;
     10import java.util.HashMap;
     11import java.util.Map;
    1012
     13import org.junit.BeforeClass;
    1114import org.junit.Test;
     15import org.openstreetmap.josm.JOSMFixture;
    1216import org.openstreetmap.josm.Main;
    1317import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    1418import org.openstreetmap.josm.data.osm.User;
     19import org.openstreetmap.josm.gui.DefaultNameFormatter;
    1520
    1621/**
     
    1924public class HistoryWayTest {
    2025
     26    /**
     27     * Setup test.
     28     */
     29    @BeforeClass
     30    public static void init() {
     31        JOSMFixture.createUnitTestFixture().init();
     32    }
     33
     34    private static HistoryWay create(Date d) {
     35        return new HistoryWay(
     36                1,    // id
     37                2,    // version
     38                true, // visible
     39                User.createOsmUser(3, "testuser"),
     40                4,    // changesetId
     41                d     // timestamp
     42                );
     43    }
     44
     45    /**
     46     * Unit test for {@link HistoryWay#HistoryWay}.
     47     */
    2148    @Test
    22     public void wayTest() {
     49    public void testHistoryWay() {
    2350        Date d = new Date();
    24         HistoryWay way = new HistoryWay(
    25                 1,
    26                 2,
    27                 true,
    28                 User.createOsmUser(3, "testuser"),
    29                 4,
    30                 d
    31                 );
     51        HistoryWay way = create(d);
    3252
    3353        assertEquals(1, way.getId());
     
    4262    }
    4363
     64    /**
     65     * Unit test for {@link HistoryWay#getType}.
     66     */
    4467    @Test
    45     public void getType() {
    46         Date d = new Date();
    47         HistoryWay way = new HistoryWay(
    48                 1,
    49                 2,
    50                 true,
    51                 User.createOsmUser(3, "testuser"),
    52                 4,
    53                 d
    54                 );
    55 
    56         assertEquals(OsmPrimitiveType.WAY, way.getType());
     68    public void testGetType() {
     69        assertEquals(OsmPrimitiveType.WAY, create(new Date()).getType());
    5770    }
    5871
    5972    @Test
    60     public void nodeManipulation() {
    61         Date d = new Date();
    62         HistoryWay way = new HistoryWay(
    63                 1,
    64                 2,
    65                 true,
    66                 User.createOsmUser(3, "testuser"),
    67                 4,
    68                 d
    69                 );
     73    public void testNodeManipulation() {
     74        HistoryWay way = create(new Date());
    7075
    7176        way.addNode(1);
     
    8893
    8994    @Test
    90     public void iterating() {
    91         Date d = new Date();
    92         HistoryWay way = new HistoryWay(
    93                 1,
    94                 2,
    95                 true,
    96                 User.createOsmUser(3, "testuser"),
    97                 4,
    98                 d
    99                 );
     95    public void testIterating() {
     96        HistoryWay way = create(new Date());
    10097
    10198        way.addNode(1);
     
    110107        assertEquals(2, (long) ids.get(1));
    111108    }
     109
     110    /**
     111     * Unit test for {@link HistoryWay#getDisplayName}.
     112     */
     113    @Test
     114    public void testGetDisplayName() {
     115        HistoryNameFormatter hnf = DefaultNameFormatter.getInstance();
     116        HistoryWay way0 = create(new Date()); // no node
     117        HistoryWay way1 = create(new Date()); // 1 node
     118        HistoryWay way2 = create(new Date()); // 2 nodes
     119
     120        way1.addNode(1);
     121        way2.addNode(1);
     122        way2.addNode(2);
     123
     124        assertEquals("1 (0 nodes)", way0.getDisplayName(hnf));
     125        assertEquals("1 (1 node)",  way1.getDisplayName(hnf));
     126        assertEquals("1 (2 nodes)", way2.getDisplayName(hnf));
     127
     128        Map<String, String> map = new HashMap<>();
     129        map.put("name", "WayName");
     130
     131        way0.setTags(map);
     132        way1.setTags(map);
     133        way2.setTags(map);
     134
     135        assertEquals("WayName (0 nodes)", way0.getDisplayName(hnf));
     136        assertEquals("WayName (1 node)",  way1.getDisplayName(hnf));
     137        assertEquals("WayName (2 nodes)", way2.getDisplayName(hnf));
     138    }
    112139}
Note: See TracChangeset for help on using the changeset viewer.