1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.command;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertNull;
|
---|
8 | import static org.junit.jupiter.api.Assertions.assertSame;
|
---|
9 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
10 |
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Arrays;
|
---|
13 | import java.util.List;
|
---|
14 |
|
---|
15 | import org.junit.jupiter.api.BeforeEach;
|
---|
16 | import org.junit.jupiter.api.Test;
|
---|
17 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
18 | import org.openstreetmap.josm.TestUtils;
|
---|
19 | import org.openstreetmap.josm.command.CommandTest.CommandTestDataWithRelation;
|
---|
20 | import org.openstreetmap.josm.data.conflict.Conflict;
|
---|
21 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
22 | import org.openstreetmap.josm.data.osm.Hash;
|
---|
23 | import org.openstreetmap.josm.data.osm.Node;
|
---|
24 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
25 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
26 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
27 | import org.openstreetmap.josm.data.osm.Storage;
|
---|
28 | import org.openstreetmap.josm.data.osm.User;
|
---|
29 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
30 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
31 |
|
---|
32 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
33 | import nl.jqno.equalsverifier.EqualsVerifier;
|
---|
34 | import nl.jqno.equalsverifier.Warning;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Unit tests of {@link PurgeCommand} class.
|
---|
38 | */
|
---|
39 | class PurgeCommandTest {
|
---|
40 | /**
|
---|
41 | * We need prefs for nodes.
|
---|
42 | */
|
---|
43 | @RegisterExtension
|
---|
44 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
45 | public JOSMTestRules test = new JOSMTestRules().preferences();
|
---|
46 | private CommandTestDataWithRelation testData;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Set up the test data.
|
---|
50 | */
|
---|
51 | @BeforeEach
|
---|
52 | public void createTestData() {
|
---|
53 | testData = new CommandTestDataWithRelation();
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Test {@link PurgeCommand#executeCommand()}
|
---|
58 | */
|
---|
59 | @Test
|
---|
60 | void testExecute() {
|
---|
61 | Relation relationParent = testData.createRelation(100, new RelationMember("child", testData.existingRelation));
|
---|
62 | Relation relationParent2 = testData.createRelation(101, new RelationMember("child", testData.existingRelation));
|
---|
63 | // to check that algorithm ignores it:
|
---|
64 | Relation relationParent3 = testData.createRelation(102, new RelationMember("child", testData.existingRelation));
|
---|
65 | PurgeCommand command = new PurgeCommand(testData.layer.getDataSet(),
|
---|
66 | Arrays.<OsmPrimitive>asList(testData.existingNode, testData.existingNode2, testData.existingWay,
|
---|
67 | testData.existingRelation, relationParent, relationParent2),
|
---|
68 | Arrays.<OsmPrimitive>asList(testData.existingNode2, testData.existingWay, testData.existingRelation));
|
---|
69 | command.executeCommand();
|
---|
70 | assertTrue(testData.existingNode2.isIncomplete());
|
---|
71 | assertTrue(testData.existingWay.isIncomplete());
|
---|
72 | assertTrue(testData.existingRelation.isIncomplete());
|
---|
73 | assertNull(relationParent.getDataSet());
|
---|
74 | assertNull(relationParent2.getDataSet());
|
---|
75 | assertNotNull(relationParent3.getDataSet());
|
---|
76 | assertFalse(relationParent3.isIncomplete());
|
---|
77 | assertNull(testData.existingNode.getDataSet());
|
---|
78 | assertFalse(testData.existingNode.isIncomplete());
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Test {@link PurgeCommand#undoCommand()}
|
---|
83 | */
|
---|
84 | @Test
|
---|
85 | void testUndo() {
|
---|
86 | PurgeCommand command = new PurgeCommand(testData.layer.getDataSet(),
|
---|
87 | Arrays.<OsmPrimitive>asList(testData.existingNode, testData.existingWay),
|
---|
88 | Arrays.<OsmPrimitive>asList(testData.existingWay));
|
---|
89 | command.executeCommand();
|
---|
90 | assertTrue(testData.existingWay.isIncomplete());
|
---|
91 | assertNull(testData.existingNode.getDataSet());
|
---|
92 |
|
---|
93 | command.undoCommand();
|
---|
94 | assertFalse(testData.existingWay.isIncomplete());
|
---|
95 | assertSame(testData.layer.data, testData.existingNode.getDataSet());
|
---|
96 |
|
---|
97 | command.executeCommand();
|
---|
98 | assertTrue(testData.existingWay.isIncomplete());
|
---|
99 | assertNull(testData.existingNode.getDataSet());
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Tests {@link PurgeCommand#fillModifiedData(java.util.Collection, java.util.Collection, java.util.Collection)}
|
---|
104 | */
|
---|
105 | @Test
|
---|
106 | void testFillModifiedData() {
|
---|
107 | ArrayList<OsmPrimitive> modified = new ArrayList<>();
|
---|
108 | ArrayList<OsmPrimitive> deleted = new ArrayList<>();
|
---|
109 | ArrayList<OsmPrimitive> added = new ArrayList<>();
|
---|
110 | PurgeCommand command = new PurgeCommand(testData.layer.getDataSet(), Arrays.<OsmPrimitive>asList(testData.existingNode),
|
---|
111 | Arrays.<OsmPrimitive>asList(testData.existingRelation));
|
---|
112 | command.fillModifiedData(modified, deleted, added);
|
---|
113 | // intentianally empty (?)
|
---|
114 | assertArrayEquals(new Object[] {}, modified.toArray());
|
---|
115 | assertArrayEquals(new Object[] {}, deleted.toArray());
|
---|
116 | assertArrayEquals(new Object[] {}, added.toArray());
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Tests {@link PurgeCommand#getParticipatingPrimitives()}
|
---|
121 | */
|
---|
122 | @Test
|
---|
123 | void testGetParticipatingPrimitives() {
|
---|
124 | PurgeCommand command = new PurgeCommand(testData.layer.getDataSet(), Arrays.<OsmPrimitive>asList(testData.existingNode),
|
---|
125 | Arrays.<OsmPrimitive>asList(testData.existingRelation));
|
---|
126 | assertArrayEquals(new Object[] {testData.existingNode }, command.getParticipatingPrimitives().toArray());
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Test {@link PurgeCommand#getDescriptionText()}
|
---|
131 | */
|
---|
132 | @Test
|
---|
133 | void testDescription() {
|
---|
134 | List<OsmPrimitive> shortList = Arrays.<OsmPrimitive>asList(testData.existingWay);
|
---|
135 | assertTrue(new PurgeCommand(testData.layer.getDataSet(), shortList, Arrays.<OsmPrimitive>asList()).getDescriptionText()
|
---|
136 | .matches("Purged 1 object"));
|
---|
137 | List<OsmPrimitive> longList = Arrays.<OsmPrimitive>asList(testData.existingNode, testData.existingNode2,
|
---|
138 | testData.existingWay);
|
---|
139 | assertTrue(new PurgeCommand(testData.layer.getDataSet(), longList, Arrays.<OsmPrimitive>asList()).getDescriptionText()
|
---|
140 | .matches("Purged 3 objects"));
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Unit test of methods {@link PurgeCommand#equals} and {@link PurgeCommand#hashCode}.
|
---|
145 | */
|
---|
146 | @Test
|
---|
147 | void testEqualsContract() {
|
---|
148 | TestUtils.assumeWorkingEqualsVerifier();
|
---|
149 | EqualsVerifier.forClass(PurgeCommand.class).usingGetClass()
|
---|
150 | .withPrefabValues(DataSet.class,
|
---|
151 | new DataSet(), new DataSet())
|
---|
152 | .withPrefabValues(User.class,
|
---|
153 | User.createOsmUser(1, "foo"), User.createOsmUser(2, "bar"))
|
---|
154 | .withPrefabValues(Conflict.class,
|
---|
155 | new Conflict<>(new Node(1, 1), new Node(2, 1)),
|
---|
156 | new Conflict<>(new Node(1, 1), new Node(3, 1)))
|
---|
157 | .withPrefabValues(OsmDataLayer.class,
|
---|
158 | new OsmDataLayer(new DataSet(), "1", null), new OsmDataLayer(new DataSet(), "2", null))
|
---|
159 | .withPrefabValues(Hash.class,
|
---|
160 | Storage.<OsmPrimitive>defaultHash(), Storage.<OsmPrimitive>defaultHash())
|
---|
161 | .suppress(Warning.NONFINAL_FIELDS)
|
---|
162 | .verify();
|
---|
163 | }
|
---|
164 | }
|
---|