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

Last change on this file since 9203 was 9203, checked in by Don-vip, 8 years ago

add more unit tests, javadoc

  • 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.ArrayList;
9import java.util.Date;
10import java.util.HashMap;
11import java.util.Map;
12
13import org.junit.BeforeClass;
14import org.junit.Test;
15import org.openstreetmap.josm.JOSMFixture;
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
18import org.openstreetmap.josm.data.osm.User;
19import org.openstreetmap.josm.gui.DefaultNameFormatter;
20
21/**
22 * Unit tests for class {@link HistoryWay}.
23 */
24public class HistoryWayTest {
25
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 */
48 @Test
49 public void testHistoryWay() {
50 Date d = new Date();
51 HistoryWay way = create(d);
52
53 assertEquals(1, way.getId());
54 assertEquals(2, way.getVersion());
55 assertTrue(way.isVisible());
56 assertEquals("testuser", way.getUser().getName());
57 assertEquals(3, way.getUser().getId());
58 assertEquals(4, way.getChangesetId());
59 assertEquals(d, way.getTimestamp());
60
61 assertEquals(0, way.getNumNodes());
62 }
63
64 /**
65 * Unit test for {@link HistoryWay#getType}.
66 */
67 @Test
68 public void testGetType() {
69 assertEquals(OsmPrimitiveType.WAY, create(new Date()).getType());
70 }
71
72 @Test
73 public void testNodeManipulation() {
74 HistoryWay way = create(new Date());
75
76 way.addNode(1);
77 assertEquals(1, way.getNumNodes());
78 assertEquals(1, way.getNodeId(0));
79 try {
80 way.getNodeId(1);
81 fail("expected expection of type " + IndexOutOfBoundsException.class.toString());
82 } catch (IndexOutOfBoundsException e) {
83 // OK
84 if (Main.isTraceEnabled()) {
85 Main.trace(e.getMessage());
86 }
87 }
88
89 way.addNode(5);
90 assertEquals(2, way.getNumNodes());
91 assertEquals(5, way.getNodeId(1));
92 }
93
94 @Test
95 public void testIterating() {
96 HistoryWay way = create(new Date());
97
98 way.addNode(1);
99 way.addNode(2);
100 ArrayList<Long> ids = new ArrayList<>();
101 for (long id : way.getNodes()) {
102 ids.add(id);
103 }
104
105 assertEquals(2, ids.size());
106 assertEquals(1, (long) ids.get(0));
107 assertEquals(2, (long) ids.get(1));
108 }
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 }
139}
Note: See TracBrowser for help on using the repository browser.