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

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

See #16567: Update to JUnit 5

This converts most tests to use @Annotations. There are also some performance
improvements as it relates to tests.

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