source: josm/trunk/test/unit/org/openstreetmap/josm/actions/MergeLayerActionTest.java

Last change on this file was 18870, checked in by taylor.smock, 7 months ago

See #16567: Update to JUnit 5

This converts most tests to use @Annotations. There are also some performance
improvements as it relates to tests.

  • Property svn:eol-style set to native
File size: 4.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertNull;
6
7import java.util.Collections;
8
9import javax.swing.JLabel;
10import javax.swing.JPanel;
11
12import org.junit.jupiter.api.BeforeEach;
13import org.junit.jupiter.api.Test;
14import org.openstreetmap.josm.TestUtils;
15import org.openstreetmap.josm.data.osm.DataSet;
16import org.openstreetmap.josm.gui.ExtendedDialog;
17import org.openstreetmap.josm.gui.MainApplication;
18import org.openstreetmap.josm.gui.layer.LayerManagerTest.TestLayer;
19import org.openstreetmap.josm.gui.layer.OsmDataLayer;
20import org.openstreetmap.josm.gui.widgets.JosmComboBox;
21import org.openstreetmap.josm.testutils.annotations.Main;
22import org.openstreetmap.josm.testutils.annotations.Projection;
23import org.openstreetmap.josm.testutils.mockers.ExtendedDialogMocker;
24import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
25
26/**
27 * Unit tests for class {@link MergeLayerAction}.
28 */
29@Main
30@Projection
31public class MergeLayerActionTest {
32 /**
33 * MergeLayerExtendedDialog mocker.
34 */
35 public static class MergeLayerExtendedDialogMocker extends ExtendedDialogMocker {
36 @Override
37 protected void act(final ExtendedDialog instance) {
38 ((JosmComboBox<?>) ((JPanel) this.getContent(instance)).getComponent(1)).setSelectedIndex(0);
39 }
40
41 @Override
42 protected String getString(final ExtendedDialog instance) {
43 return ((JLabel) ((JPanel) this.getContent(instance)).getComponent(0)).getText();
44 }
45 }
46
47 private MergeLayerAction action;
48
49 /**
50 * Setup test.
51 */
52 @BeforeEach
53 public void setUp() {
54 if (action == null) {
55 action = new MergeLayerAction();
56 }
57 for (TestLayer testLayer : MainApplication.getLayerManager().getLayersOfType(TestLayer.class)) {
58 MainApplication.getLayerManager().removeLayer(testLayer);
59 }
60 }
61
62 /**
63 * Tests that no error occurs when no source layer exists.
64 */
65 @Test
66 void testMergeNoSourceLayer() {
67 assertNull(MainApplication.getLayerManager().getActiveLayer());
68 action.actionPerformed(null);
69 assertEquals(0, MainApplication.getLayerManager().getLayers().size());
70 }
71
72 /**
73 * Tests that no error occurs when no target layer exists.
74 */
75 @Test
76 void testMergeNoTargetLayer() {
77 TestUtils.assumeWorkingJMockit();
78 final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker(
79 Collections.singletonMap("<html>There are no layers the source layer<br>'onion'<br>could be merged to.</html>", 0)
80 );
81
82 OsmDataLayer layer = new OsmDataLayer(new DataSet(), "onion", null);
83 MainApplication.getLayerManager().addLayer(layer);
84 assertEquals(1, MainApplication.getLayerManager().getLayers().size());
85 assertNull(action.merge(layer));
86 assertEquals(1, MainApplication.getLayerManager().getLayers().size());
87
88 assertEquals(1, jopsMocker.getInvocationLog().size());
89 Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
90 assertEquals(0, (int) invocationLogEntry[0]);
91 assertEquals("No target layers", invocationLogEntry[2]);
92 }
93
94 /**
95 * Tests that the merge is done with two empty layers.
96 * @throws Exception if any error occurs
97 */
98 @Test
99 void testMergeTwoEmptyLayers() throws Exception {
100 TestUtils.assumeWorkingJMockit();
101 final MergeLayerExtendedDialogMocker edMocker = new MergeLayerExtendedDialogMocker();
102 edMocker.getMockResultMap().put("Please select the target layer.", "Merge layer");
103
104 OsmDataLayer layer1 = new OsmDataLayer(new DataSet(), "1", null);
105 OsmDataLayer layer2 = new OsmDataLayer(new DataSet(), "2", null);
106 MainApplication.getLayerManager().addLayer(layer1);
107 MainApplication.getLayerManager().addLayer(layer2);
108 assertEquals(2, MainApplication.getLayerManager().getLayers().size());
109 action.merge(layer2).get();
110 assertEquals(1, MainApplication.getLayerManager().getLayers().size());
111
112 assertEquals(1, edMocker.getInvocationLog().size());
113 Object[] invocationLogEntry = edMocker.getInvocationLog().get(0);
114 assertEquals(1, (int) invocationLogEntry[0]);
115 assertEquals("Select target layer", invocationLogEntry[2]);
116 }
117}
Note: See TracBrowser for help on using the repository browser.