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

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

add unit tests

File size: 3.5 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.data.coor.LatLon;
17import org.openstreetmap.josm.data.osm.ChangesetDataSet.ChangesetDataSetEntry;
18import org.openstreetmap.josm.data.osm.ChangesetDataSet.ChangesetModificationType;
19import org.openstreetmap.josm.data.osm.history.HistoryNode;
20import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
21import org.openstreetmap.josm.testutils.JOSMTestRules;
22
23import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
24
25/**
26 * Unit tests for class {@link ChangesetDataSet}.
27 */
28public class ChangesetDataSetTest {
29
30 /**
31 * Setup test.
32 */
33 @Rule
34 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
35 public JOSMTestRules test = new JOSMTestRules();
36
37 /**
38 * Unit test of method {@link ChangesetDataSet#getPrimitivesByModificationType}.
39 */
40 @Test
41 public void testGetPrimitivesByModificationType() {
42 final ChangesetDataSet cds = new ChangesetDataSet();
43 // empty object, null parameter => IllegalArgumentException
44 try {
45 cds.getPrimitivesByModificationType(null);
46 fail("Should have thrown an IllegalArgumentException as we gave a null argument.");
47 } catch (IllegalArgumentException e) {
48 Main.trace(e);
49 // Was expected
50 }
51
52 // empty object, a modification type => empty list
53 assertTrue(
54 "Empty data set should produce an empty list.",
55 cds.getPrimitivesByModificationType(
56 ChangesetModificationType.CREATED).isEmpty()
57 );
58
59 // object with various items and modification types, fetch for CREATED
60 // => list containing only the CREATED item
61 HistoryNode prim1 = new HistoryNode(1, 1, true, User.getAnonymous(), 1, new Date(), LatLon.ZERO);
62 HistoryNode prim2 = new HistoryNode(2, 1, true, User.createLocalUser("test"), 1, new Date(), LatLon.NORTH_POLE);
63 HistoryNode prim3 = new HistoryNode(3, 1, true, User.getAnonymous(), 1, new Date(), LatLon.SOUTH_POLE);
64 cds.put(prim1, ChangesetModificationType.CREATED);
65 cds.put(prim2, ChangesetModificationType.DELETED);
66 cds.put(prim3, ChangesetModificationType.UPDATED);
67 Set<HistoryOsmPrimitive> result = cds.getPrimitivesByModificationType(
68 ChangesetModificationType.CREATED);
69 assertEquals("We should have found only one item.", 1, result.size());
70 assertTrue("The item found is prim1.", result.contains(prim1));
71 }
72
73 /**
74 * Unit test of method {@link ChangesetDataSet#iterator}.
75 */
76 @Test
77 public void testIterator() {
78 final ChangesetDataSet cds = new ChangesetDataSet();
79 HistoryNode prim1 = new HistoryNode(1, 1, true, User.getAnonymous(), 1, new Date(), LatLon.ZERO);
80 cds.put(prim1, ChangesetModificationType.CREATED);
81 Iterator<ChangesetDataSetEntry> it = cds.iterator();
82 assertTrue(it.hasNext());
83 ChangesetDataSetEntry cdse = it.next();
84 assertEquals(ChangesetModificationType.CREATED, cdse.getModificationType());
85 assertEquals(prim1, cdse.getPrimitive());
86 assertFalse(it.hasNext());
87 }
88}
Note: See TracBrowser for help on using the repository browser.