Skip to content

Commit

Permalink
test: apply OpenRewrite JUnit migration
Browse files Browse the repository at this point in the history
  • Loading branch information
NiccoMlt committed Oct 27, 2024
1 parent 079ffaf commit ba2f839
Show file tree
Hide file tree
Showing 53 changed files with 1,332 additions and 897 deletions.
10 changes: 5 additions & 5 deletions carapace-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,14 @@

<!-- test dependecies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>pl.pragmatists</groupId>
<artifactId>JUnitParams</artifactId>
<version>${libs.junitparams}</version>
<groupId>org.junit.platform</groupId>
<!-- Only needed to run tests in a version of IntelliJ IDEA that bundles older versions -->
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
Expand All @@ -41,16 +48,10 @@
import org.carapaceproxy.user.UserRealm;
import org.carapaceproxy.utils.TestEndpointMapper;
import org.carapaceproxy.utils.TestUserRealm;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

/**
*
Expand All @@ -59,9 +60,9 @@
public class ApplyConfigurationTest {

@ClassRule
public static WireMockRule wireMockRule = new WireMockRule(0);
public static WireMockExtension wireMockRule = WireMockExtension.newInstance().options(WireMockConfiguration.options().port(0)).build();

@BeforeClass
@BeforeAll
public static void setupWireMock() {
stubFor(get(urlEqualTo("/index.html?redir"))
.willReturn(aResponse()
Expand All @@ -73,24 +74,33 @@ public static void setupWireMock() {

}

@Rule
public TemporaryFolder tmpDir = new TemporaryFolder();
@TempDir
File tmpDir;

/**
* Static mapper, so that it can be references by configuration
*/
public static final class StaticEndpointMapper extends TestEndpointMapper {

public StaticEndpointMapper() {
super("localhost", wireMockRule.port());
super("localhost", wireMockRule.getPort());
}

private static File newFolder(File root, String... subDirs) throws IOException {
String subFolder = String.join("/", subDirs);
File result = new File(root, subFolder);
if (!result.mkdirs()) {
throw new IOException("Couldn't create folders " + root);
}
return result;
}

}

@Test
public void testChangeListenersConfig() throws Exception {

try (HttpProxyServer server = new HttpProxyServer(null, tmpDir.newFolder());) {
try (HttpProxyServer server = new HttpProxyServer(null, newFolder(tmpDir, "junit"));) {

{
Properties configuration = new Properties();
Expand Down Expand Up @@ -217,7 +227,7 @@ public void testChangeListenersConfig() throws Exception {
@Test
public void testReloadMapper() throws Exception {

try (HttpProxyServer server = new HttpProxyServer(null, tmpDir.newFolder());) {
try (HttpProxyServer server = new HttpProxyServer(null, newFolder(tmpDir, "junit"));) {

{
Properties configuration = new Properties();
Expand Down Expand Up @@ -289,7 +299,7 @@ public void testReloadMapper() throws Exception {
public void testUserRealm() throws Exception {

// Default UserRealm
try (HttpProxyServer server = new HttpProxyServer(null, tmpDir.newFolder())) {
try (HttpProxyServer server = new HttpProxyServer(null, newFolder(tmpDir, "junit"))) {
Properties configuration = new Properties();
server.configureAtBoot(new PropertiesConfigurationStore(configuration));
server.start();
Expand All @@ -307,7 +317,7 @@ public void testUserRealm() throws Exception {
}

// TestUserRealm
try (HttpProxyServer server = new HttpProxyServer(null, tmpDir.newFolder())) {
try (HttpProxyServer server = new HttpProxyServer(null, newFolder(tmpDir, "junit"))) {
Properties configuration = new Properties();
configuration.put("userrealm.class", "org.carapaceproxy.utils.TestUserRealm");
configuration.put("user.test1", "pass1");
Expand Down Expand Up @@ -336,7 +346,7 @@ public void testUserRealm() throws Exception {
@Test
public void testChangeFiltersConfiguration() throws Exception {

try (HttpProxyServer server = new HttpProxyServer(null, tmpDir.newFolder());) {
try (HttpProxyServer server = new HttpProxyServer(null, newFolder(tmpDir, "junit"));) {

{
Properties configuration = new Properties();
Expand Down Expand Up @@ -375,7 +385,7 @@ public void testChangeFiltersConfiguration() throws Exception {
@Test
public void testChangeBackendHealthManagerConfiguration() throws Exception {

try (HttpProxyServer server = new HttpProxyServer(null, tmpDir.newFolder());) {
try (HttpProxyServer server = new HttpProxyServer(null, newFolder(tmpDir, "junit"));) {

{
Properties configuration = new Properties();
Expand Down Expand Up @@ -418,4 +428,13 @@ private void testIt(int port, boolean ok) throws URISyntaxException, IOException
}
}

private static File newFolder(File root, String... subDirs) throws IOException {
String subFolder = String.join("/", subDirs);
File result = new File(root, subFolder);
if (!result.mkdirs()) {
throw new IOException("Couldn't create folders " + root);
}
return result;
}

}
69 changes: 57 additions & 12 deletions carapace-server/src/test/java/org/carapaceproxy/BigUploadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.carapaceproxy;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand All @@ -37,11 +38,12 @@
import org.carapaceproxy.client.EndpointKey;
import org.carapaceproxy.core.HttpProxyServer;
import org.carapaceproxy.utils.TestEndpointMapper;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.io.TempDir;

/**
* The clients sends a big upload, and the server is very slow at draining the contents
Expand All @@ -52,12 +54,21 @@ public class BigUploadTest {

private static final Logger LOG = Logger.getLogger(BigUploadTest.class.getName());

@Rule
public TemporaryFolder tmpDir = new TemporaryFolder();
@TempDir
public File tmpDir;

public interface ClientHandler {

public void handle(Socket client) throws Exception;

private static File newFolder(File root, String... subDirs) throws IOException {
String subFolder = String.join("/", subDirs);
File result = new File(root, subFolder);
if (!result.mkdirs()) {
throw new IOException("Couldn't create folders " + root);
}
return result;
}
}

public static class ConnectionResetByPeerHandler implements ClientHandler {
Expand All @@ -81,6 +92,15 @@ public void handle(Socket client) {
LOG.log(Level.SEVERE, "error", ii);
}
}

private static File newFolder(File root, String... subDirs) throws IOException {
String subFolder = String.join("/", subDirs);
File result = new File(root, subFolder);
if (!result.mkdirs()) {
throw new IOException("Couldn't create folders " + root);
}
return result;
}
}

public static class StaticResponseHandler implements ClientHandler {
Expand All @@ -101,6 +121,15 @@ public void handle(Socket client) {
LOG.log(Level.SEVERE, "error", ii);
}
}

private static File newFolder(File root, String... subDirs) throws IOException {
String subFolder = String.join("/", subDirs);
File result = new File(root, subFolder);
if (!result.mkdirs()) {
throw new IOException("Couldn't create folders " + root);
}
return result;
}
}

public static final class SimpleBlockingTcpServer implements AutoCloseable {
Expand Down Expand Up @@ -160,6 +189,15 @@ public void close() throws IOException {
socket.close();
}
}

private static File newFolder(File root, String... subDirs) throws IOException {
String subFolder = String.join("/", subDirs);
File result = new File(root, subFolder);
if (!result.mkdirs()) {
throw new IOException("Couldn't create folders " + root);
}
return result;
}
}

@Test
Expand All @@ -175,7 +213,7 @@ public void testConnectionResetByPeerDuringWriteToEndpoint() throws Exception {

int size = 20_000_000;

try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder());) {
try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, newFolder(tmpDir, "junit"));) {
server.start();
int port = server.getLocalPort();
URL url = new URL("http://localhost:" + port + "/index.html");
Expand Down Expand Up @@ -204,16 +242,14 @@ public void testConnectionResetByPeerDuringWriteToEndpoint() throws Exception {
public void testBlockingServerWorks() throws Exception {

try (SimpleBlockingTcpServer mockServer =
new SimpleBlockingTcpServer(() -> {
return new StaticResponseHandler("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nit works!\r\n".getBytes(StandardCharsets.US_ASCII));
})) {
new SimpleBlockingTcpServer(() -> new StaticResponseHandler("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nit works!\r\n".getBytes(StandardCharsets.US_ASCII)))) {

mockServer.start();

TestEndpointMapper mapper = new TestEndpointMapper("localhost", mockServer.getPort());
EndpointKey key = new EndpointKey("localhost", mockServer.getPort());

try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder());) {
try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, newFolder(tmpDir, "junit"));) {
server.start();
int port = server.getLocalPort();
URL url = new URL("http://localhost:" + port + "/index.html");
Expand All @@ -229,4 +265,13 @@ public void testBlockingServerWorks() throws Exception {
}
}

private static File newFolder(File root, String... subDirs) throws IOException {
String subFolder = String.join("/", subDirs);
File result = new File(root, subFolder);
if (!result.mkdirs()) {
throw new IOException("Couldn't create folders " + root);
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
Expand All @@ -34,24 +40,21 @@
import org.carapaceproxy.client.EndpointKey;
import org.carapaceproxy.core.HttpProxyServer;
import org.carapaceproxy.utils.TestEndpointMapper;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;

/**
*
* @author enrico.olivelli
*/
public class ConcurrentClientsTest {

@Rule
public WireMockRule wireMockRule = new WireMockRule(0);
@RegisterExtension
public static WireMockExtension wireMockRule = WireMockExtension.newInstance().options(WireMockConfiguration.options().port(0)).build();

@Rule
public TemporaryFolder tmpDir = new TemporaryFolder();
@TempDir
File tmpDir;

@Test
public void test() throws Exception {
Expand All @@ -62,12 +65,12 @@ public void test() throws Exception {
.withHeader("Content-Type", "text/html")
.withBody("it <b>works</b> !!")));

TestEndpointMapper mapper = new TestEndpointMapper("localhost", wireMockRule.port());
EndpointKey key = new EndpointKey("localhost", wireMockRule.port());
TestEndpointMapper mapper = new TestEndpointMapper("localhost", wireMockRule.getPort());
EndpointKey key = new EndpointKey("localhost", wireMockRule.getPort());

int size = 100;
int concurrentClients = 4;
try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder());) {
try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, newFolder(tmpDir, "junit"));) {
server.start();
int port = server.getLocalPort();

Expand Down Expand Up @@ -99,4 +102,13 @@ public void run() {
}
}

private static File newFolder(File root, String... subDirs) throws IOException {
String subFolder = String.join("/", subDirs);
File result = new File(root, subFolder);
if (!result.mkdirs()) {
throw new IOException("Couldn't create folders " + root);
}
return result;
}

}
Loading

0 comments on commit ba2f839

Please sign in to comment.