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

Last change on this file since 18690 was 18690, checked in by taylor.smock, 14 months ago

See #16567: Convert all assertion calls to JUnit 5 (patch by gaben, modified)

The modifications are as follows:

  • Merge DomainValidatorTest.testIDN and DomainValidatorTest.testIDNJava6OrLater
  • Update some tests to use @ParameterizedTest (DomainValidatorTest)
  • Replace various exception blocks with assertThrows. These typically looked like
        try {
            // Something that should throw an exception here
            fail("An exception should have been thrown");
        } catch (Exception e) {
            // Verify the exception matches expectations here
        }
    
  • Replace assertTrue(val instanceof Clazz) with assertInstanceOf
  • Replace JUnit 4 @Suite with JUnit 5 @Suite

Both the original patch and the modified patch fix various lint issues.

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