source: josm/trunk/test/unit/org/openstreetmap/josm/io/remotecontrol/RemoteControlTest.java

Last change on this file was 18893, checked in by taylor.smock, 6 months ago

Fix #16567: Upgrade to JUnit 5

JOSMTestRules and JOSMTestFixture can reset the default JOSM profile, which can
be unexpected for new contributors. This updates all tests to use JUnit 5 and
the new JUnit 5 annotations.

This also renames MapCSSStyleSourceFilterTest to MapCSSStyleSourceFilterPerformanceTest
to match the naming convention for performance tests and fixes some lint issues.

This was tested by running all tests individually and together.

  • Property svn:eol-style set to native
File size: 2.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.remotecontrol;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5
6import java.io.IOException;
7import java.io.InputStream;
8import java.net.HttpURLConnection;
9import java.net.URL;
10import java.nio.charset.StandardCharsets;
11import java.security.GeneralSecurityException;
12
13import org.junit.jupiter.api.AfterEach;
14import org.junit.jupiter.api.BeforeEach;
15import org.junit.jupiter.api.Test;
16import org.openstreetmap.josm.spi.preferences.Config;
17import org.openstreetmap.josm.testutils.annotations.AssertionsInEDT;
18import org.openstreetmap.josm.testutils.annotations.HTTPS;
19import org.openstreetmap.josm.tools.Utils;
20
21/**
22 * Unit tests for Remote Control
23 */
24@AssertionsInEDT
25@HTTPS
26class RemoteControlTest {
27
28 private String httpBase;
29
30 /**
31 * Starts Remote control before testing requests.
32 * @throws GeneralSecurityException if a security error occurs
33 */
34 @BeforeEach
35 public void setUp() throws GeneralSecurityException {
36 RemoteControl.start();
37 httpBase = "http://127.0.0.1:"+Config.getPref().getInt("remote.control.port", 8111);
38 }
39
40 /**
41 * Stops Remote control after testing requests.
42 */
43 @AfterEach
44 public void tearDown() {
45 RemoteControl.stop();
46 }
47
48 /**
49 * Tests that sending an HTTP request without command results in HTTP 400, with all available commands in error message.
50 * @throws Exception if an error occurs
51 */
52 @Test
53 void testHttpListOfCommands() throws Exception {
54 testListOfCommands(httpBase);
55 }
56
57 private void testListOfCommands(String url) throws IOException, ReflectiveOperationException {
58 HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
59 connection.connect();
60 assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, connection.getResponseCode());
61 try (InputStream is = connection.getErrorStream()) {
62 String responseBody = new String(Utils.readBytesFromStream(is), StandardCharsets.UTF_8);
63 assert responseBody.contains(RequestProcessor.getUsageAsHtml());
64 }
65 }
66}
Note: See TracBrowser for help on using the repository browser.