From ce0d7f6c6721fc98adc4d18c7194bd1beb6e5597 Mon Sep 17 00:00:00 2001
From: Robert Scott <code@humanleg.org.uk>
Date: Thu, 25 Oct 2018 21:39:40 +0100
Subject: [PATCH v1] DownloadOpenChangesetsTaskTest: fix for non-headless mode
---
.../josm/gui/io/DownloadOpenChangesetsTask.java | 20 ++---
.../gui/io/DownloadOpenChangesetsTaskTest.java | 88 +++++++++++++++++++++-
2 files changed, 92 insertions(+), 16 deletions(-)
diff --git a/src/org/openstreetmap/josm/gui/io/DownloadOpenChangesetsTask.java b/src/org/openstreetmap/josm/gui/io/DownloadOpenChangesetsTask.java
index 1fefad0c8..d4a36ccce 100644
|
a
|
b
|
public class DownloadOpenChangesetsTask extends PleaseWaitRunnable {
|
| 66 | 66 | + "You have either chosen to work anonymously or you are not entitled<br>" |
| 67 | 67 | + "to know the identity of the user on whose behalf you are working."); |
| 68 | 68 | Logging.warn(msg); |
| 69 | | if (!GraphicsEnvironment.isHeadless()) { |
| 70 | | JOptionPane.showMessageDialog(GuiHelper.getFrameForComponent(parent), |
| 71 | | "<html>" + msg + "</html>", tr("Missing user identity"), JOptionPane.ERROR_MESSAGE); |
| 72 | | } |
| | 69 | JOptionPane.showMessageDialog(GuiHelper.getFrameForComponent(parent), |
| | 70 | "<html>" + msg + "</html>", tr("Missing user identity"), JOptionPane.ERROR_MESSAGE); |
| 73 | 71 | return; |
| 74 | 72 | } |
| 75 | 73 | if (canceled) return; |
| … |
… |
public class DownloadOpenChangesetsTask extends PleaseWaitRunnable {
|
| 78 | 76 | return; |
| 79 | 77 | } |
| 80 | 78 | if (changesets.isEmpty()) { |
| 81 | | if (!GraphicsEnvironment.isHeadless()) { |
| 82 | | JOptionPane.showMessageDialog( |
| 83 | | MainApplication.getMainFrame(), |
| 84 | | tr("There are no open changesets"), |
| 85 | | tr("No open changesets"), |
| 86 | | JOptionPane.INFORMATION_MESSAGE |
| 87 | | ); |
| 88 | | } |
| | 79 | JOptionPane.showMessageDialog( |
| | 80 | MainApplication.getMainFrame(), |
| | 81 | tr("There are no open changesets"), |
| | 82 | tr("No open changesets"), |
| | 83 | JOptionPane.INFORMATION_MESSAGE |
| | 84 | ); |
| 89 | 85 | return; |
| 90 | 86 | } |
| 91 | 87 | SwingUtilities.invokeLater(() -> ChangesetCache.getInstance().update(changesets)); |
diff --git a/test/unit/org/openstreetmap/josm/gui/io/DownloadOpenChangesetsTaskTest.java b/test/unit/org/openstreetmap/josm/gui/io/DownloadOpenChangesetsTaskTest.java
index 669b5ca3b..c7552adbd 100644
|
a
|
b
|
|
| 1 | 1 | // License: GPL. For details, see LICENSE file. |
| 2 | 2 | package org.openstreetmap.josm.gui.io; |
| 3 | 3 | |
| | 4 | import static org.junit.Assert.assertEquals; |
| 4 | 5 | import static org.junit.Assert.assertNotNull; |
| 5 | 6 | import static org.junit.Assert.assertNull; |
| 6 | 7 | import static org.junit.Assert.assertTrue; |
| 7 | 8 | |
| | 9 | import java.awt.GraphicsEnvironment; |
| | 10 | import javax.swing.JOptionPane; |
| 8 | 11 | import javax.swing.JPanel; |
| | 12 | import java.net.URL; |
| 9 | 13 | |
| 10 | 14 | import org.junit.Rule; |
| 11 | 15 | import org.junit.Test; |
| | 16 | import org.openstreetmap.josm.TestUtils; |
| 12 | 17 | import org.openstreetmap.josm.data.UserIdentityManager; |
| | 18 | import org.openstreetmap.josm.gui.oauth.OAuthAuthorizationWizard; |
| 13 | 19 | import org.openstreetmap.josm.testutils.JOSMTestRules; |
| | 20 | import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker; |
| | 21 | import org.openstreetmap.josm.testutils.mockers.WindowMocker; |
| | 22 | import org.openstreetmap.josm.tools.UserCancelException; |
| | 23 | |
| | 24 | import mockit.Invocation; |
| | 25 | import mockit.Mock; |
| | 26 | import mockit.MockUp; |
| | 27 | |
| | 28 | import com.google.common.collect.ImmutableMap; |
| 14 | 29 | |
| 15 | 30 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 16 | 31 | |
| … |
… |
public class DownloadOpenChangesetsTaskTest {
|
| 24 | 39 | */ |
| 25 | 40 | @Rule |
| 26 | 41 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") |
| 27 | | public JOSMTestRules test = new JOSMTestRules().preferences().devAPI().timeout(20000); |
| | 42 | public JOSMTestRules test = new JOSMTestRules().preferences().devAPI(); |
| | 43 | |
| | 44 | public static class OAuthWizardMocker extends MockUp<OAuthAuthorizationWizard> { |
| | 45 | public boolean called = false; |
| | 46 | |
| | 47 | @Mock |
| | 48 | void showDialog() throws UserCancelException { |
| | 49 | this.called = true; |
| | 50 | throw new UserCancelException(); |
| | 51 | } |
| | 52 | |
| | 53 | @Mock |
| | 54 | void obtainAccessToken(final Invocation invocation, final URL serverUrl) throws UserCancelException { |
| | 55 | if (GraphicsEnvironment.isHeadless()) { |
| | 56 | // we can't really let execution proceed any further as construction of the ui |
| | 57 | // elements will fail with a mocked Window |
| | 58 | this.called = true; |
| | 59 | return; |
| | 60 | } |
| | 61 | // else we can allow a bit more of the code to be covered before we raise |
| | 62 | // UserCancelException in showDialog |
| | 63 | invocation.proceed(serverUrl); |
| | 64 | } |
| | 65 | } |
| 28 | 66 | |
| 29 | 67 | /** |
| 30 | | * Test of {@link DownloadOpenChangesetsTask} class. |
| | 68 | * Test of {@link DownloadOpenChangesetsTask} class when anonymous. |
| 31 | 69 | */ |
| 32 | 70 | @Test |
| 33 | | public void testDownloadOpenChangesetsTask() { |
| | 71 | public void testAnonymous() { |
| | 72 | TestUtils.assumeWorkingJMockit(); |
| | 73 | if (GraphicsEnvironment.isHeadless()) { |
| | 74 | new WindowMocker(); |
| | 75 | } |
| | 76 | final OAuthWizardMocker oaWizardMocker = new OAuthWizardMocker(); |
| | 77 | final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker( |
| | 78 | ImmutableMap.<String, Object>of( |
| | 79 | "<html>Could not retrieve the list of your open changesets because<br>JOSM does not know " |
| | 80 | + "your identity.<br>You have either chosen to work anonymously or you are not " |
| | 81 | + "entitled<br>to know the identity of the user on whose behalf you are working.</html>", JOptionPane.OK_OPTION |
| | 82 | ) |
| | 83 | ); |
| | 84 | |
| 34 | 85 | DownloadOpenChangesetsTask task = new DownloadOpenChangesetsTask(new JPanel()); |
| 35 | 86 | assertNull(task.getChangesets()); |
| 36 | 87 | |
| … |
… |
public class DownloadOpenChangesetsTaskTest {
|
| 38 | 89 | task.run(); |
| 39 | 90 | assertNull(task.getChangesets()); |
| 40 | 91 | |
| 41 | | task = new DownloadOpenChangesetsTask(new JPanel()); |
| | 92 | assertEquals(1, jopsMocker.getInvocationLog().size()); |
| | 93 | Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0); |
| | 94 | assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]); |
| | 95 | assertEquals("Missing user identity", invocationLogEntry[2]); |
| | 96 | |
| | 97 | assertTrue(oaWizardMocker.called); |
| | 98 | } |
| | 99 | |
| | 100 | /** |
| | 101 | * Test of {@link DownloadOpenChangesetsTask} class when "partially identified". |
| | 102 | */ |
| | 103 | @Test |
| | 104 | public void testPartiallyIdentified() { |
| | 105 | TestUtils.assumeWorkingJMockit(); |
| | 106 | if (GraphicsEnvironment.isHeadless()) { |
| | 107 | new WindowMocker(); |
| | 108 | } |
| | 109 | final OAuthWizardMocker oaWizardMocker = new OAuthWizardMocker(); |
| | 110 | final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker( |
| | 111 | ImmutableMap.<String, Object>of("There are no open changesets", JOptionPane.OK_OPTION) |
| | 112 | ); |
| | 113 | |
| | 114 | DownloadOpenChangesetsTask task = new DownloadOpenChangesetsTask(new JPanel()); |
| 42 | 115 | UserIdentityManager.getInstance().setPartiallyIdentified(System.getProperty("osm.username", "josm_test")); |
| 43 | 116 | assertTrue(UserIdentityManager.getInstance().isPartiallyIdentified()); |
| 44 | 117 | task.run(); |
| 45 | 118 | assertNotNull(task.getChangesets()); |
| | 119 | |
| | 120 | assertEquals(1, jopsMocker.getInvocationLog().size()); |
| | 121 | Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0); |
| | 122 | assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]); |
| | 123 | assertEquals("No open changesets", invocationLogEntry[2]); |
| | 124 | |
| | 125 | assertTrue(oaWizardMocker.called); |
| 46 | 126 | } |
| 47 | 127 | } |