source: josm/trunk/test/unit/org/openstreetmap/josm/actions/UploadActionTest.java@ 18870

Last change on this file since 18870 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.

File size: 4.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
5import static org.junit.jupiter.api.Assertions.assertTrue;
6
7import java.awt.GraphicsEnvironment;
8import java.util.Collections;
9import java.util.concurrent.ExecutionException;
10import java.util.concurrent.TimeUnit;
11import java.util.concurrent.TimeoutException;
12
13import org.junit.jupiter.api.Test;
14import org.openstreetmap.josm.TestUtils;
15import org.openstreetmap.josm.command.AddPrimitivesCommand;
16import org.openstreetmap.josm.data.UserIdentityManager;
17import org.openstreetmap.josm.data.coor.LatLon;
18import org.openstreetmap.josm.data.osm.DataSet;
19import org.openstreetmap.josm.data.osm.Node;
20import org.openstreetmap.josm.gui.MainApplication;
21import org.openstreetmap.josm.gui.io.UploadDialog;
22import org.openstreetmap.josm.gui.layer.OsmDataLayer;
23import org.openstreetmap.josm.gui.util.GuiHelper;
24import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
25import org.openstreetmap.josm.testutils.annotations.Main;
26import org.openstreetmap.josm.testutils.annotations.OsmApi;
27import org.openstreetmap.josm.testutils.annotations.Projection;
28import org.openstreetmap.josm.testutils.annotations.Territories;
29import org.openstreetmap.josm.testutils.mockers.WindowMocker;
30import org.openstreetmap.josm.tools.Logging;
31
32import mockit.Invocation;
33import mockit.Mock;
34import mockit.MockUp;
35
36/**
37 * Test class for {@link UploadAction}
38 * @author Taylor Smock
39 */
40@BasicPreferences
41@Main
42@OsmApi(OsmApi.APIType.FAKE)
43@Projection
44// Territories is needed due to test pollution. One of the listeners
45// that may get registered on SelectionEventManager requires
46// Territories. Rather unfortunately, we also need the external data to
47// avoid the NPE.
48@Territories(Territories.Initialize.ALL)
49class UploadActionTest {
50 /**
51 * Non-regression test for JOSM #21476.
52 */
53 @Test
54 void testNonRegression21476() throws ExecutionException, InterruptedException, TimeoutException {
55 TestUtils.assumeWorkingJMockit();
56 Logging.clearLastErrorAndWarnings();
57 new WindowMocker();
58 new UploadDialogMock();
59 // Set up the preconditions. This test acts more like an integration test, in so far as it also tests
60 // unrelated code.
61 UserIdentityManager.getInstance().setAnonymous();
62 final DataSet testData = new DataSet();
63 MainApplication.getLayerManager().addLayer(new OsmDataLayer(testData, "testNonRegression21476", null));
64 final Node toAdd = new Node(new LatLon(20, 20));
65 toAdd.put("shop", "butcher");
66 final AddPrimitivesCommand command = new AddPrimitivesCommand(Collections.singletonList(toAdd.save()), testData);
67 command.executeCommand();
68 final UploadAction uploadAction = new UploadAction();
69 uploadAction.updateEnabledState();
70 assertTrue(uploadAction.isEnabled());
71 // Perform the actual "test" -- we don't want it to throw an exception
72 assertDoesNotThrow(() -> uploadAction.actionPerformed(null));
73 // Sync threads
74 GuiHelper.runInEDT(() -> {
75 // sync edt
76 });
77 try {
78 MainApplication.worker.submit(() -> {
79 // sync worker
80 }).get(1, TimeUnit.SECONDS);
81 assertTrue(Logging.getLastErrorAndWarnings().isEmpty());
82 } finally {
83 Logging.clearLastErrorAndWarnings();
84 }
85 }
86
87 private static class UploadDialogMock extends MockUp<UploadDialog> {
88 @Mock
89 public void pack(final Invocation invocation) {
90 if (!GraphicsEnvironment.isHeadless()) {
91 invocation.proceed();
92 }
93 }
94
95 @Mock
96 public void setVisible(final Invocation invocation, final boolean visible) {
97 if (!GraphicsEnvironment.isHeadless()) {
98 invocation.proceed(visible);
99 }
100 }
101
102 @Mock
103 public final boolean isCanceled(final Invocation invocation) {
104 if (!GraphicsEnvironment.isHeadless()) {
105 return Boolean.TRUE.equals(invocation.proceed());
106 }
107 return true;
108 }
109 }
110}
Note: See TracBrowser for help on using the repository browser.