Ignore:
Timestamp:
2016-07-28T01:24:39+02:00 (10 years ago)
Author:
Don-vip
Message:

fix #13223 - Minor command class fixes (patch by michael2402, modified) - gsoc-core

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/command/ChangeRelationMemberRoleCommandTest.java

    r9943 r10663  
    22package org.openstreetmap.josm.command;
    33
    4 import org.junit.BeforeClass;
     4import static org.junit.Assert.assertArrayEquals;
     5import static org.junit.Assert.assertEquals;
     6import static org.junit.Assert.assertFalse;
     7import static org.junit.Assert.assertNull;
     8import static org.junit.Assert.assertTrue;
     9
     10import java.util.ArrayList;
     11
     12import org.junit.Before;
     13import org.junit.Rule;
    514import org.junit.Test;
    6 import org.openstreetmap.josm.JOSMFixture;
     15import org.openstreetmap.josm.command.CommandTest.CommandTestDataWithRelation;
    716import org.openstreetmap.josm.data.osm.DataSet;
     17import org.openstreetmap.josm.data.osm.OsmPrimitive;
    818import org.openstreetmap.josm.data.osm.Relation;
    919import org.openstreetmap.josm.data.osm.User;
    1020import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     21import org.openstreetmap.josm.testutils.JOSMTestRules;
    1122
     23import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    1224import nl.jqno.equalsverifier.EqualsVerifier;
    1325import nl.jqno.equalsverifier.Warning;
     
    1931
    2032    /**
    21      * Setup test.
     33     * We need prefs for nodes.
    2234     */
    23     @BeforeClass
    24     public static void setUpBeforeClass() {
    25         JOSMFixture.createUnitTestFixture().init(false);
     35    @Rule
     36    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
     37    public JOSMTestRules test = new JOSMTestRules().preferences().i18n();
     38    private CommandTestDataWithRelation testData;
     39
     40    /**
     41     * Set up the test data.
     42     */
     43    @Before
     44    public void createTestData() {
     45        testData = new CommandTestDataWithRelation();
     46    }
     47
     48    /**
     49     * Test if {@link ChangeRelationMemberRoleCommand} changes the role by index
     50     */
     51    @Test
     52    public void testRoleChanged() {
     53        assertTrue(new ChangeRelationMemberRoleCommand(testData.existingRelation, 0, "newRole").executeCommand());
     54        assertEquals("newRole", testData.existingRelation.getMember(0).getRole());
     55        assertEquals(testData.existingNode, testData.existingRelation.getMember(0).getMember());
     56        assertEquals("way", testData.existingRelation.getMember(1).getRole());
     57
     58        assertTrue(testData.existingRelation.isModified());
     59
     60        assertTrue(new ChangeRelationMemberRoleCommand(testData.existingRelation, 1, "newRole").executeCommand());
     61        assertEquals("newRole", testData.existingRelation.getMember(1).getRole());
     62    }
     63
     64    /**
     65     * Wrong index should be ignored.
     66     */
     67    @Test
     68    public void testWrongIndex() {
     69        // should be ignored
     70        ChangeRelationMemberRoleCommand command1 = new ChangeRelationMemberRoleCommand(testData.existingRelation, -1, "newRole");
     71        assertTrue(command1.executeCommand());
     72        ChangeRelationMemberRoleCommand command2 = new ChangeRelationMemberRoleCommand(testData.existingRelation, 8, "newRole");
     73        assertTrue(command2.executeCommand());
     74        assertFalse(testData.existingRelation.isModified());
     75
     76        command1.undoCommand();
     77        command2.undoCommand();
     78        assertFalse(testData.existingRelation.isModified());
     79    }
     80
     81
     82    /**
     83     * Same role should be ignored.
     84     */
     85    @Test
     86    public void testSameRole() {
     87        // should be ignored
     88        assertTrue(new ChangeRelationMemberRoleCommand(testData.existingRelation, 0, "node").executeCommand());
     89        assertFalse(testData.existingRelation.isModified());
     90    }
     91
     92    /**
     93     * Test {@link ChangeRelationMemberRoleCommand#undoCommand()}.
     94     */
     95    @Test
     96    public void testUndo() {
     97        ChangeRelationMemberRoleCommand command = new ChangeRelationMemberRoleCommand(testData.existingRelation, 0, "newRole");
     98        command.executeCommand();
     99        assertEquals("newRole", testData.existingRelation.getMember(0).getRole());
     100        assertTrue(testData.existingRelation.isModified());
     101
     102        command.undoCommand();
     103        assertEquals("node", testData.existingRelation.getMember(0).getRole());
     104        assertFalse(testData.existingRelation.isModified());
     105
     106        command.executeCommand();
     107        assertEquals("newRole", testData.existingRelation.getMember(0).getRole());
     108        assertTrue(testData.existingRelation.isModified());
     109    }
     110
     111    /**
     112     * Tests {@link ChangeCommand#fillModifiedData(java.util.Collection, java.util.Collection, java.util.Collection)}
     113     */
     114    @Test
     115    public void testFillModifiedData() {
     116        ArrayList<OsmPrimitive> modified = new ArrayList<>();
     117        ArrayList<OsmPrimitive> deleted = new ArrayList<>();
     118        ArrayList<OsmPrimitive> added = new ArrayList<>();
     119        new ChangeRelationMemberRoleCommand(testData.existingRelation, 0, "newRole").fillModifiedData(modified, deleted, added);
     120        assertArrayEquals(new Object[] {testData.existingRelation}, modified.toArray());
     121        assertArrayEquals(new Object[] {}, deleted.toArray());
     122        assertArrayEquals(new Object[] {}, added.toArray());
     123    }
     124
     125    /**
     126     * Test {@link ChangeRelationMemberRoleCommand#getDescriptionText()}
     127     */
     128    @Test
     129    public void testDescription() {
     130        testData.existingRelation.put("name", "xy");
     131        assertTrue(new ChangeRelationMemberRoleCommand(testData.existingRelation, 0, "newRole").getDescriptionText()
     132                .matches("Change relation member role for relation.*xy.*"));
     133    }
     134
     135    /**
     136     * Test {@link ChangeRelationMemberRoleCommand#getChildren()}
     137     */
     138    @Test
     139    public void testChildren() {
     140        assertNull(new ChangeRelationMemberRoleCommand(testData.existingRelation, 0, "newRole").getChildren());
    26141    }
    27142
Note: See TracChangeset for help on using the changeset viewer.