source: josm/trunk/test/unit/org/openstreetmap/josm/testutils/annotations/TestUser.java@ 18974

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

Fix #23465: Remove custom checkstyle plugin

TopLevelJavadocCheck.java is duplicating functionality from MissingJavadocType.
Our custom class is from #14794 (closed 2017-10-16). The check that makes it
redundant was added in checkstyle 8.20 (released 2019-04-28).

This adds the missing javadocs for the more comprehensive checkstyle version.

File size: 2.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.testutils.annotations;
3
4
5import static org.junit.jupiter.api.Assumptions.assumeTrue;
6
7import java.lang.annotation.ElementType;
8import java.lang.annotation.Retention;
9import java.lang.annotation.RetentionPolicy;
10import java.lang.annotation.Target;
11import java.net.Authenticator;
12import java.net.PasswordAuthentication;
13
14import org.junit.jupiter.api.extension.AfterEachCallback;
15import org.junit.jupiter.api.extension.BeforeAllCallback;
16import org.junit.jupiter.api.extension.BeforeEachCallback;
17import org.junit.jupiter.api.extension.ExtendWith;
18import org.junit.jupiter.api.extension.ExtensionContext;
19import org.openstreetmap.josm.TestUtils;
20import org.openstreetmap.josm.data.UserIdentityManager;
21import org.openstreetmap.josm.io.auth.CredentialsManager;
22import org.openstreetmap.josm.spi.preferences.Config;
23import org.openstreetmap.josm.tools.Utils;
24
25/**
26 * Used for tests that require a test user to be defined via -Dosm.username and -Dosm.password.
27 * This uses the {@link OsmApi.APIType#DEV} server.
28 */
29@BasicPreferences
30@OsmApi(OsmApi.APIType.DEV)
31@ExtendWith(TestUser.TestUserExtension.class)
32@Retention(RetentionPolicy.RUNTIME)
33@Target({ElementType.TYPE, ElementType.METHOD})
34public @interface TestUser {
35 /**
36 * Initialize a user for tests
37 */
38 class TestUserExtension implements BeforeAllCallback, BeforeEachCallback, AfterEachCallback {
39 @Override
40 public void afterEach(ExtensionContext context) throws Exception {
41 UserIdentityManager.getInstance().setAnonymous();
42 CredentialsManager.getInstance().purgeCredentialsCache(Authenticator.RequestorType.SERVER);
43 }
44
45 @Override
46 public void beforeAll(ExtensionContext context) throws Exception {
47 this.beforeEach(context);
48 }
49
50 @Override
51 public void beforeEach(ExtensionContext context) throws Exception {
52 assumeTrue(TestUtils.areCredentialsProvided(),
53 "OSM DEV API credentials not provided. Please define them with -Dosm.username and -Dosm.password");
54 final String username = Utils.getSystemProperty("osm.username");
55 final String password = Utils.getSystemProperty("osm.password");
56 assumeTrue(username != null && !username.isEmpty(), "Please add -Dosm.username for the OSM DEV API");
57 assumeTrue(password != null && !password.isEmpty(), "Please add -Dosm.password for the OSM DEV API");
58 Config.getPref().put("osm-server.auth-method", "basic");
59
60 // don't use atomic upload, the test API server can't cope with large diff uploads
61 Config.getPref().putBoolean("osm-server.atomic-upload", false);
62 CredentialsManager.getInstance().store(Authenticator.RequestorType.SERVER, org.openstreetmap.josm.io.OsmApi.getOsmApi().getHost(),
63 new PasswordAuthentication(username, password.toCharArray()));
64 }
65 }
66}
Note: See TracBrowser for help on using the repository browser.