source: josm/trunk/test/unit/org/openstreetmap/josm/gui/io/DownloadOpenChangesetsTaskTest.java@ 16159

Last change on this file since 16159 was 16159, checked in by simon04, 4 years ago

see #18948 - Use Collections.singletonMap instead of ImmutableMap.of

  • Property svn:eol-style set to native
File size: 4.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertNotNull;
6import static org.junit.Assert.assertNull;
7import static org.junit.Assert.assertTrue;
8
9import java.awt.GraphicsEnvironment;
10import java.net.URL;
11import java.util.Collections;
12
13import javax.swing.JOptionPane;
14import javax.swing.JPanel;
15
16import org.junit.Rule;
17import org.junit.Test;
18import org.openstreetmap.josm.TestUtils;
19import org.openstreetmap.josm.data.UserIdentityManager;
20import org.openstreetmap.josm.gui.oauth.OAuthAuthorizationWizard;
21import org.openstreetmap.josm.testutils.JOSMTestRules;
22import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
23import org.openstreetmap.josm.testutils.mockers.WindowMocker;
24import org.openstreetmap.josm.tools.UserCancelException;
25
26import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
27import mockit.Invocation;
28import mockit.Mock;
29import mockit.MockUp;
30
31/**
32 * Unit tests of {@link DownloadOpenChangesetsTask} class.
33 */
34public class DownloadOpenChangesetsTaskTest {
35
36 /**
37 * Setup tests
38 */
39 @Rule
40 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
41 public JOSMTestRules test = new JOSMTestRules().preferences().devAPI();
42
43 /**
44 * OAuth wizard mocker.
45 */
46 public static class OAuthWizardMocker extends MockUp<OAuthAuthorizationWizard> {
47 /** {@code true} if wizard has been called */
48 public boolean called;
49
50 @Mock
51 void showDialog() throws UserCancelException {
52 this.called = true;
53 throw new UserCancelException();
54 }
55
56 @Mock
57 void obtainAccessToken(final Invocation invocation, final URL serverUrl) {
58 if (GraphicsEnvironment.isHeadless()) {
59 // we can't really let execution proceed any further as construction of the ui
60 // elements will fail with a mocked Window
61 this.called = true;
62 return;
63 }
64 // else we can allow a bit more of the code to be covered before we raise
65 // UserCancelException in showDialog
66 invocation.proceed(serverUrl);
67 }
68 }
69
70 /**
71 * Test of {@link DownloadOpenChangesetsTask} class when anonymous.
72 */
73 @Test
74 public void testAnonymous() {
75 TestUtils.assumeWorkingJMockit();
76 if (GraphicsEnvironment.isHeadless()) {
77 new WindowMocker();
78 }
79 final OAuthWizardMocker oaWizardMocker = new OAuthWizardMocker();
80 final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker(
81 Collections.singletonMap(
82 "<html>Could not retrieve the list of your open changesets because<br>JOSM does not know "
83 + "your identity.<br>You have either chosen to work anonymously or you are not "
84 + "entitled<br>to know the identity of the user on whose behalf you are working.</html>", JOptionPane.OK_OPTION
85 )
86 );
87
88 DownloadOpenChangesetsTask task = new DownloadOpenChangesetsTask(new JPanel());
89 assertNull(task.getChangesets());
90
91 assertTrue(UserIdentityManager.getInstance().isAnonymous());
92 task.run();
93 assertNull(task.getChangesets());
94
95 assertEquals(1, jopsMocker.getInvocationLog().size());
96 Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
97 assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
98 assertEquals("Missing user identity", invocationLogEntry[2]);
99
100 assertTrue(oaWizardMocker.called);
101 }
102
103 /**
104 * Test of {@link DownloadOpenChangesetsTask} class when "partially identified".
105 */
106 @Test
107 public void testPartiallyIdentified() {
108 TestUtils.assumeWorkingJMockit();
109 if (GraphicsEnvironment.isHeadless()) {
110 new WindowMocker();
111 }
112 final OAuthWizardMocker oaWizardMocker = new OAuthWizardMocker();
113 final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker(
114 Collections.singletonMap("There are no open changesets", JOptionPane.OK_OPTION)
115 );
116
117 DownloadOpenChangesetsTask task = new DownloadOpenChangesetsTask(new JPanel());
118 UserIdentityManager.getInstance().setPartiallyIdentified(System.getProperty("osm.username", "josm_test"));
119 assertTrue(UserIdentityManager.getInstance().isPartiallyIdentified());
120 task.run();
121 assertNotNull(task.getChangesets());
122
123 assertEquals(1, jopsMocker.getInvocationLog().size());
124 Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
125 assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
126 assertEquals("No open changesets", invocationLogEntry[2]);
127
128 assertTrue(oaWizardMocker.called);
129 }
130}
Note: See TracBrowser for help on using the repository browser.