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