source: josm/trunk/test/unit/org/openstreetmap/josm/actions/CopyActionTest.java@ 17360

Last change on this file since 17360 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.2 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.assertFalse;
6import static org.junit.jupiter.api.Assertions.assertNotNull;
7import static org.junit.jupiter.api.Assertions.assertTrue;
8import static org.junit.jupiter.api.Assertions.fail;
9
10import java.awt.datatransfer.Clipboard;
11import java.awt.datatransfer.DataFlavor;
12import java.awt.datatransfer.StringSelection;
13import java.awt.datatransfer.UnsupportedFlavorException;
14import java.io.IOException;
15import java.util.Arrays;
16
17import org.junit.jupiter.api.Test;
18import org.junit.jupiter.api.extension.RegisterExtension;
19import org.openstreetmap.josm.data.coor.LatLon;
20import org.openstreetmap.josm.data.osm.DataSet;
21import org.openstreetmap.josm.data.osm.Node;
22import org.openstreetmap.josm.data.osm.Way;
23import org.openstreetmap.josm.gui.MainApplication;
24import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
25import org.openstreetmap.josm.gui.datatransfer.data.PrimitiveTransferData;
26import org.openstreetmap.josm.gui.layer.OsmDataLayer;
27import org.openstreetmap.josm.testutils.JOSMTestRules;
28
29import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
30
31/**
32 * Unit tests for class {@link CopyAction}.
33 */
34class CopyActionTest {
35 private static final class CapturingCopyAction extends CopyAction {
36 private boolean warningShown;
37
38 @Override
39 protected void showEmptySelectionWarning() {
40 warningShown = true;
41 }
42 }
43
44 /**
45 * We need prefs for this.
46 */
47 @RegisterExtension
48 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
49 public JOSMTestRules test = new JOSMTestRules().preferences().fakeAPI();
50
51 /**
52 * Test that copy action copies the selected primitive
53 * @throws IOException if an I/O error occurs
54 * @throws UnsupportedFlavorException if the requested data flavor is not supported
55 */
56 @Test
57 void testWarnOnEmpty() throws UnsupportedFlavorException, IOException {
58 Clipboard clipboard = ClipboardUtils.getClipboard();
59 clipboard.setContents(new StringSelection("test"), null);
60
61 CapturingCopyAction action = new CapturingCopyAction();
62
63 action.updateEnabledState();
64 assertFalse(action.isEnabled());
65 action.actionPerformed(null);
66 assertTrue(action.warningShown);
67
68 MainApplication.getLayerManager().addLayer(new OsmDataLayer(new DataSet(), "test", null));
69 action.warningShown = false;
70
71 action.updateEnabledState();
72 assertFalse(action.isEnabled());
73 action.actionPerformed(null);
74 assertTrue(action.warningShown);
75
76 assertEquals("test", clipboard.getContents(null).getTransferData(DataFlavor.stringFlavor));
77 }
78
79 /**
80 * Test that copy action copies the selected primitive
81 * @throws Exception if an error occurs
82 */
83 @Test
84 void testCopySinglePrimitive() throws Exception {
85 DataSet data = new DataSet();
86
87 Node node1 = new Node();
88 node1.setCoor(LatLon.ZERO);
89 data.addPrimitive(node1);
90
91 Node node2 = new Node();
92 node2.setCoor(LatLon.ZERO);
93 data.addPrimitive(node2);
94 Way way = new Way();
95 way.setNodes(Arrays.asList(node1, node2));
96 data.addPrimitive(way);
97 data.setSelected(way);
98
99 MainApplication.getLayerManager().addLayer(new OsmDataLayer(data, "test", null));
100
101 CopyAction action = new CopyAction() {
102 @Override
103 protected void showEmptySelectionWarning() {
104 fail("Selection is not empty.");
105 }
106 };
107 action.updateEnabledState();
108 assertTrue(action.isEnabled());
109 action.actionPerformed(null);
110
111 Object copied = ClipboardUtils.getClipboard().getContents(null).getTransferData(PrimitiveTransferData.DATA_FLAVOR);
112 assertNotNull(copied);
113 assertTrue(copied instanceof PrimitiveTransferData);
114 PrimitiveTransferData ptd = (PrimitiveTransferData) copied;
115 Object[] direct = ptd.getDirectlyAdded().toArray();
116 assertEquals(1, direct.length);
117 Object[] referenced = ptd.getReferenced().toArray();
118 assertEquals(2, referenced.length);
119 }
120}
Note: See TracBrowser for help on using the repository browser.