1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertNull;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
8 |
|
---|
9 | import java.awt.GraphicsEnvironment;
|
---|
10 | import java.net.URL;
|
---|
11 | import java.util.Collections;
|
---|
12 |
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 | import javax.swing.JPanel;
|
---|
15 |
|
---|
16 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
17 | import org.junit.jupiter.api.Test;
|
---|
18 | import org.openstreetmap.josm.TestUtils;
|
---|
19 | import org.openstreetmap.josm.data.UserIdentityManager;
|
---|
20 | import org.openstreetmap.josm.gui.oauth.OAuthAuthorizationWizard;
|
---|
21 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
22 | import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
|
---|
23 | import org.openstreetmap.josm.testutils.mockers.WindowMocker;
|
---|
24 | import org.openstreetmap.josm.tools.UserCancelException;
|
---|
25 |
|
---|
26 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
27 | import mockit.Invocation;
|
---|
28 | import mockit.Mock;
|
---|
29 | import mockit.MockUp;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Unit tests of {@link DownloadOpenChangesetsTask} class.
|
---|
33 | */
|
---|
34 | class DownloadOpenChangesetsTaskTest {
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Setup tests
|
---|
38 | */
|
---|
39 | @RegisterExtension
|
---|
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 | 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 | 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 | }
|
---|