source: josm/trunk/test/unit/org/openstreetmap/josm/data/osm/history/HistoryWayTest.java@ 16445

Last change on this file since 16445 was 16445, checked in by simon04, 4 years ago

see #19251 - Java 8: use Stream

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm.history;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertTrue;
6import static org.junit.Assert.fail;
7
8import java.util.Date;
9import java.util.HashMap;
10import java.util.List;
11import java.util.Map;
12
13import org.junit.Rule;
14import org.junit.Test;
15import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
16import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
17import org.openstreetmap.josm.data.osm.User;
18import org.openstreetmap.josm.testutils.JOSMTestRules;
19import org.openstreetmap.josm.tools.Logging;
20
21import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
22
23/**
24 * Unit tests for class {@link HistoryWay}.
25 */
26public class HistoryWayTest {
27
28 /**
29 * Setup test.
30 */
31 @Rule
32 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
33 public JOSMTestRules test = new JOSMTestRules();
34
35 private static HistoryWay create(Date d) {
36 return new HistoryWay(
37 1, // id
38 2, // version
39 true, // visible
40 User.createOsmUser(3, "testuser"),
41 4, // changesetId
42 d // timestamp
43 );
44 }
45
46 /**
47 * Unit test for {@link HistoryWay#HistoryWay}.
48 */
49 @Test
50 public void testHistoryWay() {
51 Date d = new Date();
52 HistoryWay way = create(d);
53
54 assertEquals(1, way.getId());
55 assertEquals(2, way.getVersion());
56 assertTrue(way.isVisible());
57 assertEquals("testuser", way.getUser().getName());
58 assertEquals(3, way.getUser().getId());
59 assertEquals(4, way.getChangesetId());
60 assertEquals(d, way.getTimestamp());
61
62 assertEquals(0, way.getNumNodes());
63 }
64
65 /**
66 * Unit test for {@link HistoryWay#getType}.
67 */
68 @Test
69 public void testGetType() {
70 assertEquals(OsmPrimitiveType.WAY, create(new Date()).getType());
71 }
72
73 @Test
74 public void testNodeManipulation() {
75 HistoryWay way = create(new Date());
76
77 way.addNode(1);
78 assertEquals(1, way.getNumNodes());
79 assertEquals(1, way.getNodeId(0));
80 try {
81 way.getNodeId(1);
82 fail("expected expection of type " + IndexOutOfBoundsException.class.toString());
83 } catch (IndexOutOfBoundsException e) {
84 // OK
85 Logging.trace(e);
86 }
87
88 way.addNode(5);
89 assertEquals(2, way.getNumNodes());
90 assertEquals(5, way.getNodeId(1));
91 }
92
93 @Test
94 public void testIterating() {
95 HistoryWay way = create(new Date());
96
97 way.addNode(1);
98 way.addNode(2);
99 List<Long> ids = way.getNodes();
100
101 assertEquals(2, ids.size());
102 assertEquals(1, (long) ids.get(0));
103 assertEquals(2, (long) ids.get(1));
104 }
105
106 /**
107 * Unit test for {@link HistoryWay#getDisplayName}.
108 */
109 @Test
110 public void testGetDisplayName() {
111 HistoryNameFormatter hnf = DefaultNameFormatter.getInstance();
112 HistoryWay way0 = create(new Date()); // no node
113 HistoryWay way1 = create(new Date()); // 1 node
114 HistoryWay way2 = create(new Date()); // 2 nodes
115
116 way1.addNode(1);
117 way2.addNode(1);
118 way2.addNode(2);
119
120 // CHECKSTYLE.OFF: SingleSpaceSeparator
121 assertEquals("1 (0 nodes)", way0.getDisplayName(hnf));
122 assertEquals("1 (1 node)", way1.getDisplayName(hnf));
123 assertEquals("1 (2 nodes)", way2.getDisplayName(hnf));
124
125 Map<String, String> map = new HashMap<>();
126 map.put("name", "WayName");
127
128 way0.setTags(map);
129 way1.setTags(map);
130 way2.setTags(map);
131
132 assertEquals("WayName (0 nodes)", way0.getDisplayName(hnf));
133 assertEquals("WayName (1 node)", way1.getDisplayName(hnf));
134 assertEquals("WayName (2 nodes)", way2.getDisplayName(hnf));
135 // CHECKSTYLE.ON: SingleSpaceSeparator
136 }
137}
Note: See TracBrowser for help on using the repository browser.