source: josm/trunk/test/unit/org/openstreetmap/josm/command/PurgeCommandTest.java

Last change on this file was 18037, checked in by Don-vip, 3 years ago

fix #21064 - Add JUnit 5 extension for preferences (patch by taylor.smock)

File size: 6.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.junit.jupiter.api.Assertions.assertArrayEquals;
5import static org.junit.jupiter.api.Assertions.assertFalse;
6import static org.junit.jupiter.api.Assertions.assertNotNull;
7import static org.junit.jupiter.api.Assertions.assertNull;
8import static org.junit.jupiter.api.Assertions.assertSame;
9import static org.junit.jupiter.api.Assertions.assertTrue;
10
11import java.util.ArrayList;
12import java.util.Arrays;
13import java.util.List;
14
15import org.openstreetmap.josm.TestUtils;
16import org.openstreetmap.josm.command.CommandTest.CommandTestDataWithRelation;
17import org.openstreetmap.josm.data.conflict.Conflict;
18import org.openstreetmap.josm.data.osm.DataSet;
19import org.openstreetmap.josm.data.osm.Hash;
20import org.openstreetmap.josm.data.osm.Node;
21import org.openstreetmap.josm.data.osm.OsmPrimitive;
22import org.openstreetmap.josm.data.osm.Relation;
23import org.openstreetmap.josm.data.osm.RelationMember;
24import org.openstreetmap.josm.data.osm.Storage;
25import org.openstreetmap.josm.data.osm.User;
26import org.openstreetmap.josm.gui.layer.OsmDataLayer;
27import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
28
29import nl.jqno.equalsverifier.EqualsVerifier;
30import nl.jqno.equalsverifier.Warning;
31import org.junit.jupiter.api.BeforeEach;
32import org.junit.jupiter.api.Test;
33
34/**
35 * Unit tests of {@link PurgeCommand} class.
36 */
37// We need prefs for nodes.
38@BasicPreferences
39class 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}
Note: See TracBrowser for help on using the repository browser.