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

Last change on this file since 11241 was 10758, checked in by Don-vip, 8 years ago

sonar - squid:S3578 - Test methods should comply with a naming convention

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