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.HashMap;
|
---|
12 | import java.util.Map;
|
---|
13 |
|
---|
14 | import javax.swing.JOptionPane;
|
---|
15 | import javax.swing.JPanel;
|
---|
16 |
|
---|
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.annotations.BasicPreferences;
|
---|
22 | import org.openstreetmap.josm.testutils.annotations.OsmApi;
|
---|
23 | import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
|
---|
24 | import org.openstreetmap.josm.testutils.mockers.WindowMocker;
|
---|
25 | import org.openstreetmap.josm.tools.UserCancelException;
|
---|
26 |
|
---|
27 | import mockit.Invocation;
|
---|
28 | import mockit.Mock;
|
---|
29 | import mockit.MockUp;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Unit tests of {@link DownloadOpenChangesetsTask} class.
|
---|
33 | */
|
---|
34 | @BasicPreferences
|
---|
35 | @OsmApi(OsmApi.APIType.DEV)
|
---|
36 | class DownloadOpenChangesetsTaskTest {
|
---|
37 | /**
|
---|
38 | * OAuth wizard mocker.
|
---|
39 | */
|
---|
40 | public static class OAuthWizardMocker extends MockUp<OAuthAuthorizationWizard> {
|
---|
41 | /** {@code true} if wizard has been called */
|
---|
42 | public boolean called;
|
---|
43 |
|
---|
44 | @Mock
|
---|
45 | void showDialog() throws UserCancelException {
|
---|
46 | this.called = true;
|
---|
47 | throw new UserCancelException();
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Mock
|
---|
51 | void obtainAccessToken(final Invocation invocation, final URL serverUrl) {
|
---|
52 | if (GraphicsEnvironment.isHeadless()) {
|
---|
53 | // we can't really let execution proceed any further as construction of the ui
|
---|
54 | // elements will fail with a mocked Window
|
---|
55 | this.called = true;
|
---|
56 | return;
|
---|
57 | }
|
---|
58 | // else we can allow a bit more of the code to be covered before we raise
|
---|
59 | // UserCancelException in showDialog
|
---|
60 | invocation.proceed(serverUrl);
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Test of {@link DownloadOpenChangesetsTask} class when anonymous.
|
---|
66 | */
|
---|
67 | @Test
|
---|
68 | void testAnonymous() {
|
---|
69 | TestUtils.assumeWorkingJMockit();
|
---|
70 | if (GraphicsEnvironment.isHeadless()) {
|
---|
71 | new WindowMocker();
|
---|
72 | }
|
---|
73 | final Map<String, Object> optionPaneMock = new HashMap<>(2);
|
---|
74 | optionPaneMock.put("<html>Could not retrieve the list of your open changesets because<br>JOSM does not know "
|
---|
75 | + "your identity.<br>You have either chosen to work anonymously or you are not "
|
---|
76 | + "entitled<br>to know the identity of the user on whose behalf you are working.</html>", JOptionPane.OK_OPTION);
|
---|
77 | optionPaneMock.put("Obtain OAuth 2.0 token for authentication?", JOptionPane.NO_OPTION);
|
---|
78 | final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker(optionPaneMock);
|
---|
79 |
|
---|
80 | DownloadOpenChangesetsTask task = new DownloadOpenChangesetsTask(new JPanel());
|
---|
81 | assertNull(task.getChangesets());
|
---|
82 |
|
---|
83 | assertTrue(UserIdentityManager.getInstance().isAnonymous());
|
---|
84 | task.run();
|
---|
85 | assertNull(task.getChangesets());
|
---|
86 |
|
---|
87 | assertEquals(2, jopsMocker.getInvocationLog().size());
|
---|
88 | Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(1);
|
---|
89 | assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
|
---|
90 | assertEquals("Missing user identity", invocationLogEntry[2]);
|
---|
91 |
|
---|
92 | invocationLogEntry = jopsMocker.getInvocationLog().get(0);
|
---|
93 | assertEquals(JOptionPane.NO_OPTION, (int) invocationLogEntry[0]);
|
---|
94 | assertEquals("Obtain authentication to OSM servers", invocationLogEntry[2]);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Test of {@link DownloadOpenChangesetsTask} class when "partially identified".
|
---|
99 | */
|
---|
100 | @Test
|
---|
101 | void testPartiallyIdentified() {
|
---|
102 | TestUtils.assumeWorkingJMockit();
|
---|
103 | if (GraphicsEnvironment.isHeadless()) {
|
---|
104 | new WindowMocker();
|
---|
105 | }
|
---|
106 | final Map<String, Object> optionPaneMock = new HashMap<>(2);
|
---|
107 | optionPaneMock.put("There are no open changesets", JOptionPane.OK_OPTION);
|
---|
108 | optionPaneMock.put("Obtain OAuth 2.0 token for authentication?", JOptionPane.NO_OPTION);
|
---|
109 | final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker(optionPaneMock);
|
---|
110 |
|
---|
111 | DownloadOpenChangesetsTask task = new DownloadOpenChangesetsTask(new JPanel());
|
---|
112 | UserIdentityManager.getInstance().setPartiallyIdentified(System.getProperty("osm.username", "josm_test"));
|
---|
113 | assertTrue(UserIdentityManager.getInstance().isPartiallyIdentified());
|
---|
114 | task.run();
|
---|
115 | assertNotNull(task.getChangesets());
|
---|
116 |
|
---|
117 | assertEquals(2, jopsMocker.getInvocationLog().size());
|
---|
118 | Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(1);
|
---|
119 | assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
|
---|
120 | assertEquals("No open changesets", invocationLogEntry[2]);
|
---|
121 |
|
---|
122 | invocationLogEntry = jopsMocker.getInvocationLog().get(0);
|
---|
123 | assertEquals(JOptionPane.NO_OPTION, (int) invocationLogEntry[0]);
|
---|
124 | assertEquals("Obtain authentication to OSM servers", invocationLogEntry[2]);
|
---|
125 | }
|
---|
126 | }
|
---|