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

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

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

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