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

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

simplify handling of ignored/traced exceptions

  • 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 Main.trace(e);
85 }
86
87 way.addNode(5);
88 assertEquals(2, way.getNumNodes());
89 assertEquals(5, way.getNodeId(1));
90 }
91
92 @Test
93 public void testIterating() {
94 HistoryWay way = create(new Date());
95
96 way.addNode(1);
97 way.addNode(2);
98 ArrayList<Long> ids = new ArrayList<>();
99 for (long id : way.getNodes()) {
100 ids.add(id);
101 }
102
103 assertEquals(2, ids.size());
104 assertEquals(1, (long) ids.get(0));
105 assertEquals(2, (long) ids.get(1));
106 }
107
108 /**
109 * Unit test for {@link HistoryWay#getDisplayName}.
110 */
111 @Test
112 public void testGetDisplayName() {
113 HistoryNameFormatter hnf = DefaultNameFormatter.getInstance();
114 HistoryWay way0 = create(new Date()); // no node
115 HistoryWay way1 = create(new Date()); // 1 node
116 HistoryWay way2 = create(new Date()); // 2 nodes
117
118 way1.addNode(1);
119 way2.addNode(1);
120 way2.addNode(2);
121
122 // CHECKSTYLE.OFF: SingleSpaceSeparator
123 assertEquals("1 (0 nodes)", way0.getDisplayName(hnf));
124 assertEquals("1 (1 node)", way1.getDisplayName(hnf));
125 assertEquals("1 (2 nodes)", way2.getDisplayName(hnf));
126
127 Map<String, String> map = new HashMap<>();
128 map.put("name", "WayName");
129
130 way0.setTags(map);
131 way1.setTags(map);
132 way2.setTags(map);
133
134 assertEquals("WayName (0 nodes)", way0.getDisplayName(hnf));
135 assertEquals("WayName (1 node)", way1.getDisplayName(hnf));
136 assertEquals("WayName (2 nodes)", way2.getDisplayName(hnf));
137 // CHECKSTYLE.ON: SingleSpaceSeparator
138 }
139}
Note: See TracBrowser for help on using the repository browser.