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

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

See r18798: Actually use the @Territories annotation

This also fixes some tests that fail with specific options, but were passing due
to run order.

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.assertDoesNotThrow;
5import static org.junit.jupiter.api.Assertions.assertTrue;
6import static org.junit.jupiter.api.Assertions.fail;
7
8import java.awt.GraphicsEnvironment;
9import java.util.Collections;
10import java.util.concurrent.TimeUnit;
11
12import org.junit.jupiter.api.Test;
13import org.junit.jupiter.api.extension.RegisterExtension;
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.JOSMTestRules;
25import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
26import org.openstreetmap.josm.testutils.annotations.Main;
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@Projection
43// Territories is needed due to test pollution. One of the listeners
44// that may get registered on SelectionEventManager requires
45// Territories. Rather unfortunately, we also need the external data to
46// avoid the NPE.
47@Territories(Territories.Initialize.ALL)
48class UploadActionTest {
49 // Only needed for layer cleanup. And user identity cleanup. And ensuring that data isn't accidentally uploaded.
50 // Note that the setUp method can be replaced by the @Territories extension, when that is merged.
51 @RegisterExtension
52 static JOSMTestRules josmTestRules = new JOSMTestRules().fakeAPI();
53
54 /**
55 * Non-regression test for JOSM #21476.
56 */
57 @Test
58 void testNonRegression21476() {
59 TestUtils.assumeWorkingJMockit();
60 Logging.clearLastErrorAndWarnings();
61 new WindowMocker();
62 new UploadDialogMock();
63 // Set up the preconditions. This test acts more like an integration test, in so far as it also tests
64 // unrelated code.
65 UserIdentityManager.getInstance().setAnonymous();
66 final DataSet testData = new DataSet();
67 MainApplication.getLayerManager().addLayer(new OsmDataLayer(testData, "testNonRegression21476", null));
68 final Node toAdd = new Node(new LatLon(20, 20));
69 toAdd.put("shop", "butcher");
70 final AddPrimitivesCommand command = new AddPrimitivesCommand(Collections.singletonList(toAdd.save()), testData);
71 command.executeCommand();
72 final UploadAction uploadAction = new UploadAction();
73 uploadAction.updateEnabledState();
74 assertTrue(uploadAction.isEnabled());
75 // Perform the actual "test" -- we don't want it to throw an exception
76 assertDoesNotThrow(() -> uploadAction.actionPerformed(null));
77 // Sync threads
78 GuiHelper.runInEDT(() -> {
79 // sync edt
80 });
81 try {
82 MainApplication.worker.submit(() -> {
83 // sync worker
84 }).get(1, TimeUnit.SECONDS);
85 assertTrue(Logging.getLastErrorAndWarnings().isEmpty());
86 } catch (Exception exception) {
87 fail(exception);
88 } finally {
89 Logging.clearLastErrorAndWarnings();
90 }
91 }
92
93 private static class UploadDialogMock extends MockUp<UploadDialog> {
94 @Mock
95 public void pack(final Invocation invocation) {
96 if (!GraphicsEnvironment.isHeadless()) {
97 invocation.proceed();
98 }
99 }
100
101 @Mock
102 public void setVisible(final Invocation invocation, final boolean visible) {
103 if (!GraphicsEnvironment.isHeadless()) {
104 invocation.proceed(visible);
105 }
106 }
107
108 @Mock
109 public final boolean isCanceled(final Invocation invocation) {
110 if (!GraphicsEnvironment.isHeadless()) {
111 return invocation.proceed();
112 }
113 return true;
114 }
115 }
116}
Note: See TracBrowser for help on using the repository browser.