source: josm/trunk/test/unit/org/openstreetmap/josm/data/osm/ChangesetDataSetTest.java@ 12035

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

add more unit tests

File size: 4.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertFalse;
6import static org.junit.Assert.assertTrue;
7import static org.junit.Assert.fail;
8
9import java.util.Date;
10import java.util.Iterator;
11import java.util.Set;
12
13import org.junit.Rule;
14import org.junit.Test;
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.TestUtils;
17import org.openstreetmap.josm.data.coor.LatLon;
18import org.openstreetmap.josm.data.osm.ChangesetDataSet.ChangesetDataSetEntry;
19import org.openstreetmap.josm.data.osm.ChangesetDataSet.ChangesetModificationType;
20import org.openstreetmap.josm.data.osm.history.HistoryNode;
21import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
22import org.openstreetmap.josm.testutils.JOSMTestRules;
23import org.openstreetmap.josm.tools.Logging;
24
25import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
26
27/**
28 * Unit tests for class {@link ChangesetDataSet}.
29 */
30public class ChangesetDataSetTest {
31
32 /**
33 * Setup test.
34 */
35 @Rule
36 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
37 public JOSMTestRules test = new JOSMTestRules();
38
39 /**
40 * Unit test of method {@link ChangesetDataSet#getPrimitivesByModificationType}.
41 */
42 @Test
43 public void testGetPrimitivesByModificationType() {
44 final ChangesetDataSet cds = new ChangesetDataSet();
45 // empty object, null parameter => IllegalArgumentException
46 try {
47 cds.getPrimitivesByModificationType(null);
48 fail("Should have thrown an IllegalArgumentException as we gave a null argument.");
49 } catch (IllegalArgumentException e) {
50 Main.trace(e);
51 // Was expected
52 }
53
54 // empty object, a modification type => empty list
55 assertTrue(
56 "Empty data set should produce an empty list.",
57 cds.getPrimitivesByModificationType(
58 ChangesetModificationType.CREATED).isEmpty()
59 );
60
61 // object with various items and modification types, fetch for CREATED
62 // => list containing only the CREATED item
63 HistoryNode prim1 = new HistoryNode(1, 1, true, User.getAnonymous(), 1, new Date(), LatLon.ZERO);
64 HistoryNode prim2 = new HistoryNode(2, 1, true, User.createLocalUser("test"), 1, new Date(), LatLon.NORTH_POLE);
65 HistoryNode prim3 = new HistoryNode(3, 1, true, User.getAnonymous(), 1, new Date(), LatLon.SOUTH_POLE);
66 cds.put(prim1, ChangesetModificationType.CREATED);
67 cds.put(prim2, ChangesetModificationType.DELETED);
68 cds.put(prim3, ChangesetModificationType.UPDATED);
69 Set<HistoryOsmPrimitive> result = cds.getPrimitivesByModificationType(
70 ChangesetModificationType.CREATED);
71 assertEquals("We should have found only one item.", 1, result.size());
72 assertTrue("The item found is prim1.", result.contains(prim1));
73 }
74
75 /**
76 * Unit test of method {@link ChangesetDataSet#iterator}.
77 */
78 @Test
79 public void testIterator() {
80 final ChangesetDataSet cds = new ChangesetDataSet();
81 HistoryNode prim1 = new HistoryNode(1, 1, true, User.getAnonymous(), 1, new Date(), LatLon.ZERO);
82 cds.put(prim1, ChangesetModificationType.CREATED);
83 Iterator<ChangesetDataSetEntry> it = cds.iterator();
84 assertTrue(it.hasNext());
85 ChangesetDataSetEntry cdse = it.next();
86 assertEquals(ChangesetModificationType.CREATED, cdse.getModificationType());
87 assertEquals(prim1, cdse.getPrimitive());
88 assertFalse(it.hasNext());
89 try {
90 it.remove();
91 fail("remove should throw UnsupportedOperationException");
92 } catch (UnsupportedOperationException e) {
93 Logging.trace(e.getMessage());
94 }
95 }
96
97 /**
98 * Unit test of {@link ChangesetModificationType} enum.
99 */
100 @Test
101 public void testEnumChangesetModificationType() {
102 TestUtils.superficialEnumCodeCoverage(ChangesetModificationType.class);
103 }
104}
Note: See TracBrowser for help on using the repository browser.