aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJacek Poreda <jacek.poreda@sonarsource.com>2024-12-11 16:30:30 +0100
committersonartech <sonartech@sonarsource.com>2024-12-18 20:03:11 +0000
commit4e41444298e93514d23586ffc21c8d0cce1eadba (patch)
treee1b081a22d9ee996d0af3f2a0a5eb53bf3147c2a
parentd9bf1b18c061109315886932b7e8847a329fa957 (diff)
downloadsonarqube-4e41444298e93514d23586ffc21c8d0cce1eadba.tar.gz
sonarqube-4e41444298e93514d23586ffc21c8d0cce1eadba.zip
SONAR-17786 Update Jetty to 11.X
-rw-r--r--build.gradle2
-rw-r--r--server/sonar-webserver-auth/src/test/java/org/sonar/server/tester/AttributeHolderServletContext.java20
-rw-r--r--server/sonar-webserver-auth/src/test/java/org/sonar/server/tester/AttributeHolderServletContextTest.java26
-rw-r--r--server/sonar-webserver/src/test/java/org/sonar/server/platform/web/StaticResourcesServletTest.java132
4 files changed, 71 insertions, 109 deletions
diff --git a/build.gradle b/build.gradle
index 4a3eb84409b..ebfb8102a24 100644
--- a/build.gradle
+++ b/build.gradle
@@ -454,7 +454,7 @@ subprojects {
exclude 'org.slf4j:slf4j-api'
}
dependency 'com.fasterxml.staxmate:staxmate:2.4.1'
- dependencySet(group: 'org.eclipse.jetty', version: '9.4.6.v20170531') {
+ dependencySet(group: 'org.eclipse.jetty', version: '11.0.24') {
entry 'jetty-proxy'
entry 'jetty-server'
entry 'jetty-servlet'
diff --git a/server/sonar-webserver-auth/src/test/java/org/sonar/server/tester/AttributeHolderServletContext.java b/server/sonar-webserver-auth/src/test/java/org/sonar/server/tester/AttributeHolderServletContext.java
index 62b2200c88c..2c9e595b883 100644
--- a/server/sonar-webserver-auth/src/test/java/org/sonar/server/tester/AttributeHolderServletContext.java
+++ b/server/sonar-webserver-auth/src/test/java/org/sonar/server/tester/AttributeHolderServletContext.java
@@ -103,31 +103,11 @@ class AttributeHolderServletContext implements ServletContext {
}
@Override
- public Servlet getServlet(String s) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Enumeration<Servlet> getServlets() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Enumeration<String> getServletNames() {
- throw new UnsupportedOperationException();
- }
-
- @Override
public void log(String s) {
throw new UnsupportedOperationException();
}
@Override
- public void log(Exception e, String s) {
- throw new UnsupportedOperationException();
- }
-
- @Override
public void log(String s, Throwable throwable) {
throw new UnsupportedOperationException();
}
diff --git a/server/sonar-webserver-auth/src/test/java/org/sonar/server/tester/AttributeHolderServletContextTest.java b/server/sonar-webserver-auth/src/test/java/org/sonar/server/tester/AttributeHolderServletContextTest.java
index 3eb984793fa..9751b1b06f6 100644
--- a/server/sonar-webserver-auth/src/test/java/org/sonar/server/tester/AttributeHolderServletContextTest.java
+++ b/server/sonar-webserver-auth/src/test/java/org/sonar/server/tester/AttributeHolderServletContextTest.java
@@ -95,42 +95,16 @@ public class AttributeHolderServletContextTest {
@Test(expected = UnsupportedOperationException.class)
public void getRequestDispatcher_is_not_supported() {
servletContext.getRequestDispatcher(SOME_STRING);
-
}
@Test(expected = UnsupportedOperationException.class)
public void getNamedDispatcher_is_not_supported() {
servletContext.getNamedDispatcher(SOME_STRING);
-
- }
-
- @Test(expected = UnsupportedOperationException.class)
- public void getServlet_is_not_supported() {
- servletContext.getServlet(SOME_STRING);
-
- }
-
- @Test(expected = UnsupportedOperationException.class)
- public void getServlets_is_not_supported() {
- servletContext.getServlets();
-
- }
-
- @Test(expected = UnsupportedOperationException.class)
- public void getServletNames_is_not_supported() {
- servletContext.getServletNames();
-
}
@Test(expected = UnsupportedOperationException.class)
public void log_is_not_supported() {
servletContext.log(SOME_STRING);
-
- }
-
- @Test(expected = UnsupportedOperationException.class)
- public void log1_is_not_supported() {
- servletContext.log(SOME_EXCEPTION, SOME_STRING);
}
@Test(expected = UnsupportedOperationException.class)
diff --git a/server/sonar-webserver/src/test/java/org/sonar/server/platform/web/StaticResourcesServletTest.java b/server/sonar-webserver/src/test/java/org/sonar/server/platform/web/StaticResourcesServletTest.java
index 492e8511677..527b031ec41 100644
--- a/server/sonar-webserver/src/test/java/org/sonar/server/platform/web/StaticResourcesServletTest.java
+++ b/server/sonar-webserver/src/test/java/org/sonar/server/platform/web/StaticResourcesServletTest.java
@@ -19,26 +19,30 @@
*/
package org.sonar.server.platform.web;
+import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
+import java.net.InetSocketAddress;
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.nio.charset.Charset;
+import java.util.Optional;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
-import jakarta.servlet.http.HttpServletResponse;
-import okhttp3.OkHttpClient;
-import okhttp3.Request;
-import okhttp3.Response;
import org.apache.catalina.connector.ClientAbortException;
import org.apache.commons.io.IOUtils;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
import org.slf4j.event.Level;
import org.sonar.api.testfixtures.log.LogAndArguments;
-import org.sonar.api.testfixtures.log.LogTester;
+import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonar.core.extension.CoreExtensionRepository;
import org.sonar.core.platform.PluginInfo;
import org.sonar.core.platform.PluginRepository;
@@ -49,18 +53,19 @@ import static org.mockito.Mockito.when;
public class StaticResourcesServletTest {
- @Rule
- public LogTester logTester = new LogTester();
+ @RegisterExtension
+ LogTesterJUnit5 logTester = new LogTesterJUnit5();
+
private Server jetty;
- private PluginRepository pluginRepository = mock(PluginRepository.class);
- private CoreExtensionRepository coreExtensionRepository = mock(CoreExtensionRepository.class);
- private TestSystem system = new TestSystem(pluginRepository, coreExtensionRepository);
+ private final PluginRepository pluginRepository = mock(PluginRepository.class);
+ private final CoreExtensionRepository coreExtensionRepository = mock(CoreExtensionRepository.class);
+ private final TestSystem system = new TestSystem(pluginRepository, coreExtensionRepository);
- @Before
- public void setUp() throws Exception {
+ @BeforeEach
+ void setUp() throws Exception {
logTester.setLevel(Level.TRACE);
- jetty = new Server(0);
+ jetty = new Server(InetSocketAddress.createUnresolved("localhost", 0));
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
ServletHolder servletHolder = new ServletHolder(new StaticResourcesServlet(system));
@@ -69,83 +74,86 @@ public class StaticResourcesServletTest {
jetty.start();
}
- @After
+ @AfterEach
public void tearDown() throws Exception {
if (jetty != null) {
jetty.stop();
}
}
- private Response callAndStop(String path) throws Exception {
- OkHttpClient client = new OkHttpClient();
- Request request = new Request.Builder()
- .url(jetty.getURI().resolve(path).toString())
+ private HttpResponse<String> callAndStop(String path) throws Exception {
+ HttpClient client = HttpClient.newHttpClient();
+ HttpRequest request = HttpRequest.newBuilder()
+ .uri(jetty.getURI().resolve(URI.create(path)))
.build();
- Response response = client.newCall(request).execute();
+
+ HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
+
jetty.stop();
return response;
}
@Test
public void return_content_if_exists_in_installed_plugin() throws Exception {
- system.pluginStream = IOUtils.toInputStream("bar");
+ system.pluginStream = IOUtils.toInputStream("bar", Charset.defaultCharset());
when(pluginRepository.hasPlugin("myplugin")).thenReturn(true);
- Response response = callAndStop("/static/myplugin/foo.txt");
+ HttpResponse<String> response = callAndStop("/static/myplugin/foo.txt");
- assertThat(response.isSuccessful()).isTrue();
- assertThat(response.body().string()).isEqualTo("bar");
+ assertThat(response.statusCode()).isEqualTo(200);
+ assertThat(response.body()).isEqualTo("bar");
assertThat(system.pluginResource).isEqualTo("static/foo.txt");
}
@Test
public void return_content_of_folder_of_installed_plugin() throws Exception {
- system.pluginStream = IOUtils.toInputStream("bar");
+ system.pluginStream = IOUtils.toInputStream("bar", Charset.defaultCharset());
when(pluginRepository.hasPlugin("myplugin")).thenReturn(true);
- Response response = callAndStop("/static/myplugin/foo/bar.txt");
+ HttpResponse<String> response = callAndStop("/static/myplugin/foo/bar.txt");
- assertThat(response.isSuccessful()).isTrue();
- assertThat(response.body().string()).isEqualTo("bar");
+ assertThat(response.statusCode()).isEqualTo(200);
+ assertThat(response.body()).isEqualTo("bar");
assertThat(system.pluginResource).isEqualTo("static/foo/bar.txt");
}
@Test
public void return_content_of_folder_of_installed_core_extension() throws Exception {
- system.coreExtensionStream = IOUtils.toInputStream("bar");
+ system.coreExtensionStream = IOUtils.toInputStream("bar", Charset.defaultCharset());
when(coreExtensionRepository.isInstalled("coreext")).thenReturn(true);
- Response response = callAndStop("/static/coreext/foo/bar.txt");
+ HttpResponse<String> response = callAndStop("/static/coreext/foo/bar.txt");
- assertThat(response.isSuccessful()).isTrue();
- assertThat(response.body().string()).isEqualTo("bar");
+ assertThat(response.statusCode()).isEqualTo(200);
+ assertThat(response.body()).isEqualTo("bar");
assertThat(system.coreExtensionResource).isEqualTo("static/foo/bar.txt");
}
@Test
public void return_content_of_folder_of_installed_core_extension_over_installed_plugin_in_case_of_key_conflict() throws Exception {
- system.coreExtensionStream = IOUtils.toInputStream("bar of plugin");
+ system.coreExtensionStream = IOUtils.toInputStream("bar of plugin", Charset.defaultCharset());
when(coreExtensionRepository.isInstalled("samekey")).thenReturn(true);
- system.coreExtensionStream = IOUtils.toInputStream("bar of core extension");
+ system.coreExtensionStream = IOUtils.toInputStream("bar of core extension", Charset.defaultCharset());
when(coreExtensionRepository.isInstalled("samekey")).thenReturn(true);
- Response response = callAndStop("/static/samekey/foo/bar.txt");
+ HttpResponse<String> response = callAndStop("/static/samekey/foo/bar.txt");
- assertThat(response.isSuccessful()).isTrue();
- assertThat(response.body().string()).isEqualTo("bar of core extension");
+ assertThat(response.statusCode()).isEqualTo(200);
+ assertThat(response.body()).isEqualTo("bar of core extension");
assertThat(system.pluginResource).isNull();
assertThat(system.coreExtensionResource).isEqualTo("static/foo/bar.txt");
}
@Test
public void mime_type_is_set_on_response() throws Exception {
- system.pluginStream = IOUtils.toInputStream("bar");
+ system.pluginStream = IOUtils.toInputStream("bar", Charset.defaultCharset());
when(pluginRepository.hasPlugin("myplugin")).thenReturn(true);
- Response response = callAndStop("/static/myplugin/foo.css");
+ HttpResponse<String> response = callAndStop("/static/myplugin/foo.css");
- assertThat(response.header("Content-Type")).isEqualTo("text/css");
- assertThat(response.body().string()).isEqualTo("bar");
+ Optional<String> header = response.headers().firstValue("Content-Type");
+ assertThat(header).hasValue("text/css");
+ assertThat(response.body()).isEqualTo("bar");
}
@Test
@@ -153,9 +161,9 @@ public class StaticResourcesServletTest {
system.pluginStream = null;
when(pluginRepository.hasPlugin("myplugin")).thenReturn(true);
- Response response = callAndStop("/static/myplugin/foo.css");
+ HttpResponse<String> response = callAndStop("/static/myplugin/foo.css");
- assertThat(response.code()).isEqualTo(404);
+ assertThat(response.statusCode()).isEqualTo(404);
assertThat(logTester.logs(Level.ERROR)).isEmpty();
}
@@ -164,22 +172,22 @@ public class StaticResourcesServletTest {
system.pluginStream = null;
when(pluginRepository.hasPlugin("myplugin")).thenReturn(false);
- Response response = callAndStop("/static/myplugin/foo.css");
+ HttpResponse<String> response = callAndStop("/static/myplugin/foo.css");
- assertThat(response.code()).isEqualTo(404);
+ assertThat(response.statusCode()).isEqualTo(404);
assertThat(logTester.logs(Level.ERROR)).isEmpty();
}
@Test
public void return_resource_if_exists_in_requested_plugin() throws Exception {
- system.pluginStream = IOUtils.toInputStream("bar");
+ system.pluginStream = IOUtils.toInputStream("bar", Charset.defaultCharset());
when(pluginRepository.hasPlugin("myplugin")).thenReturn(true);
when(pluginRepository.getPluginInfo("myplugin")).thenReturn(new PluginInfo("myplugin"));
- Response response = callAndStop("/static/myplugin/foo.css");
+ HttpResponse<String> response = callAndStop("/static/myplugin/foo.css");
- assertThat(response.isSuccessful()).isTrue();
- assertThat(response.body().string()).isEqualTo("bar");
+ assertThat(response.statusCode()).isEqualTo(200);
+ assertThat(response.body()).isEqualTo("bar");
assertThat(logTester.logs(Level.ERROR)).isEmpty();
}
@@ -189,9 +197,9 @@ public class StaticResourcesServletTest {
system.isCommitted = true;
when(pluginRepository.hasPlugin("myplugin")).thenReturn(false);
- Response response = callAndStop("/static/myplugin/foo.css");
+ HttpResponse<String> response = callAndStop("/static/myplugin/foo.css");
- assertThat(response.code()).isEqualTo(200);
+ assertThat(response.statusCode()).isEqualTo(200);
assertThat(logTester.logs(Level.ERROR)).isEmpty();
assertThat(logTester.logs(Level.TRACE)).contains("Response is committed. Cannot send error response code 404");
}
@@ -201,9 +209,9 @@ public class StaticResourcesServletTest {
system.sendErrorException = new IOException("Simulating sendError throwing IOException");
when(pluginRepository.hasPlugin("myplugin")).thenReturn(false);
- Response response = callAndStop("/static/myplugin/foo.css");
+ HttpResponse<String> response = callAndStop("/static/myplugin/foo.css");
- assertThat(response.code()).isEqualTo(200);
+ assertThat(response.statusCode()).isEqualTo(200);
assertThat(logTester.logs(Level.ERROR)).isEmpty();
assertThat(logTester.logs(Level.TRACE)).contains("Failed to send error code 404: {}");
}
@@ -214,9 +222,9 @@ public class StaticResourcesServletTest {
system.pluginStream = null;
when(pluginRepository.hasPlugin("myplugin")).thenReturn(true);
- Response response = callAndStop("/static/myplugin/foo.css");
+ HttpResponse<String> response = callAndStop("/static/myplugin/foo.css");
- assertThat(response.code()).isEqualTo(200);
+ assertThat(response.statusCode()).isEqualTo(200);
assertThat(logTester.logs(Level.ERROR)).isEmpty();
assertThat(logTester.logs(Level.TRACE)).contains("Response is committed. Cannot send error response code 404");
}
@@ -226,9 +234,9 @@ public class StaticResourcesServletTest {
system.pluginStreamException = new ClientAbortException("Simulating ClientAbortException");
when(pluginRepository.hasPlugin("myplugin")).thenReturn(true);
- Response response = callAndStop("/static/myplugin/foo.css");
+ HttpResponse<String> response = callAndStop("/static/myplugin/foo.css");
- assertThat(response.code()).isEqualTo(200);
+ assertThat(response.statusCode()).isEqualTo(200);
assertThat(logTester.logs(Level.ERROR)).isEmpty();
assertThat(logTester.getLogs(Level.TRACE)).extracting(LogAndArguments::getFormattedMsg).contains(
"Client canceled loading resource [static/foo.css] from plugin [myplugin]: {}");
@@ -240,9 +248,9 @@ public class StaticResourcesServletTest {
system.pluginStreamException = new RuntimeException("Simulating a error");
when(pluginRepository.hasPlugin("myplugin")).thenReturn(true);
- Response response = callAndStop("/static/myplugin/foo.css");
+ HttpResponse<String> response = callAndStop("/static/myplugin/foo.css");
- assertThat(response.code()).isEqualTo(200);
+ assertThat(response.statusCode()).isEqualTo(200);
assertThat(logTester.logs(Level.ERROR)).contains("Unable to load resource [static/foo.css] from plugin [myplugin]");
}