Skip to content

Commit 0ccb00c

Browse files
committed
Allow a single test to run multiple apps
1 parent 799bcf8 commit 0ccb00c

File tree

6 files changed

+61
-19
lines changed

6 files changed

+61
-19
lines changed

tests/src/test/java/io/jooby/FeaturedTest.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,7 @@ public void methodNotAllowed(ServerTestRunner runner) {
13151315

13161316
@ServerTest
13171317
public void silentFavicon(ServerTestRunner runner) {
1318-
runner.define(Jooby::new).ready(client -> {
1318+
runner.use(Jooby::new).ready(client -> {
13191319
client.get("/favicon.ico", rsp -> {
13201320
assertEquals(StatusCode.NOT_FOUND.value(), rsp.code());
13211321
});
@@ -2734,6 +2734,47 @@ public void requestUrlWithContextPath(ServerTestRunner runner) {
27342734
});
27352735
}
27362736

2737+
@ServerTest
2738+
public void shouldAccessToWebVariables(ServerTestRunner runner) {
2739+
runner.define(app -> {
2740+
app.decorator(new WebVariables());
2741+
2742+
app.get("/webvars", ctx ->
2743+
Arrays.asList(ctx.attribute("contextPath"), ctx.attribute("path"), ctx.attribute("user"))
2744+
);
2745+
}).ready(client -> {
2746+
client.get("/webvars", rsp -> {
2747+
assertEquals("[, /webvars, null]", rsp.body().string().trim());
2748+
});
2749+
});
2750+
2751+
runner.define(app -> {
2752+
app.setContextPath("/app");
2753+
2754+
app.decorator(new WebVariables());
2755+
2756+
app.get("/webvars", ctx ->
2757+
Arrays.asList(ctx.attribute("contextPath"), ctx.attribute("path"), ctx.attribute("user"))
2758+
);
2759+
}).ready(client -> {
2760+
client.get("/app/webvars", rsp -> {
2761+
assertEquals("[/app, /app/webvars, null]", rsp.body().string().trim());
2762+
});
2763+
});
2764+
2765+
runner.define(app -> {
2766+
app.decorator(new WebVariables("scope"));
2767+
2768+
app.get("/webvars", ctx ->
2769+
Arrays.asList(ctx.attribute("scope.contextPath"), ctx.attribute("scope.path"), ctx.attribute("scope.user"))
2770+
);
2771+
}).ready(client -> {
2772+
client.get("/webvars", rsp -> {
2773+
assertEquals("[, /webvars, null]", rsp.body().string().trim());
2774+
});
2775+
});
2776+
}
2777+
27372778
private byte[][] partition(byte[] bytes, int size) {
27382779
List<byte[]> result = new ArrayList<>();
27392780
int offset = 0;

tests/src/test/java/io/jooby/Issue1349.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public String something() {
3333

3434
@ServerTest
3535
public void issue1349(ServerTestRunner runner) {
36-
runner.define(App1349::new)
36+
runner.use(App1349::new)
3737
.ready(client -> {
3838
client.get("/1349", rsp -> {
3939
assertEquals(401, rsp.code());

tests/src/test/java/io/jooby/junit/ServerExtensionImpl.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.List;
1717
import java.util.ServiceLoader;
1818
import java.util.Set;
19+
import java.util.function.Supplier;
1920
import java.util.stream.IntStream;
2021
import java.util.stream.Stream;
2122

@@ -26,13 +27,13 @@ public class ServerExtensionImpl implements TestTemplateInvocationContextProvide
2627

2728
private static class ServerInfo implements Comparable<ServerInfo> {
2829
private final int index;
29-
private Server server;
30+
private Supplier<Server> server;
3031

3132
private ExecutionMode mode;
3233

3334
private String description;
3435

35-
public ServerInfo(Server server, ExecutionMode mode, int index, String description) {
36+
public ServerInfo(Supplier<Server> server, ExecutionMode mode, int index, String description) {
3637
this.server = server;
3738
this.mode = mode;
3839
this.description = description;
@@ -98,8 +99,8 @@ public ServerInfo(Server server, ExecutionMode mode, int index, String descripti
9899
.map(info -> invocationContext(info));
99100
}
100101

101-
private Server newServer(Class serverClass) {
102-
return stream(ServiceLoader.load(Server.class).spliterator(), false)
102+
private Supplier<Server> newServer(Class serverClass) {
103+
return () -> stream(ServiceLoader.load(Server.class).spliterator(), false)
103104
.filter(s -> serverClass.isInstance(s))
104105
.findFirst()
105106
.orElseThrow(() -> new IllegalArgumentException("Server not found: " + serverClass));

tests/src/test/java/io/jooby/junit/ServerParameterResolver.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
import org.junit.jupiter.api.extension.ParameterResolver;
99

1010
import java.lang.reflect.Method;
11+
import java.util.function.Supplier;
1112

1213
public class ServerParameterResolver implements ParameterResolver {
1314

14-
private final Server server;
15+
private final Supplier<Server> server;
1516

1617
private final ExecutionMode executionMode;
1718

1819

19-
public ServerParameterResolver(Server server, ExecutionMode executionMode) {
20+
public ServerParameterResolver(Supplier<Server> server, ExecutionMode executionMode) {
2021
this.server = server;
2122
this.executionMode = executionMode;
2223
}

tests/src/test/java/io/jooby/junit/ServerTestRunner.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,30 @@ public class ServerTestRunner {
1919

2020
private final String testName;
2121

22-
private final Server server;
22+
private final Supplier<Server> server;
2323

2424
private final ExecutionMode executionMode;
2525

2626
private Supplier<Jooby> provider;
2727

2828
private boolean followRedirects = true;
2929

30-
public ServerTestRunner(String testName, Server server, ExecutionMode executionMode) {
30+
public ServerTestRunner(String testName, Supplier<Server> server, ExecutionMode executionMode) {
3131
this.testName = testName;
3232
this.server = server;
3333
this.executionMode = executionMode;
3434
}
3535

3636
public ServerTestRunner define(Consumer<Jooby> consumer) {
37-
define(() -> {
37+
use(() -> {
3838
Jooby app = new Jooby();
3939
consumer.accept(app);
4040
return app;
4141
});
4242
return this;
4343
}
4444

45-
public ServerTestRunner define(Supplier<Jooby> provider) {
45+
public ServerTestRunner use(Supplier<Jooby> provider) {
4646
this.provider = provider;
4747
return this;
4848
}
@@ -52,6 +52,7 @@ public void ready(SneakyThrows.Consumer<WebClient> onReady) {
5252
}
5353

5454
public void ready(SneakyThrows.Consumer2<WebClient, WebClient> onReady) {
55+
Server server = this.server.get();
5556
try {
5657
System.setProperty("___app_name__", testName);
5758
Jooby app = provider.get();

tests/src/test/kotlin/io/jooby/FeaturedKotlinTest.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ package io.jooby
33
import io.jooby.internal.mvc.KotlinMvc
44
import io.jooby.junit.ServerTest
55
import io.jooby.junit.ServerTestRunner
6-
import io.jooby.netty.Netty
76
import io.reactivex.Flowable
87
import io.reactivex.schedulers.Schedulers
98
import kotlinx.coroutines.delay
109
import org.junit.jupiter.api.Assertions.assertEquals
11-
import org.junit.jupiter.api.Test
1210

1311
class FeaturedKotlinTest {
1412

@@ -27,7 +25,7 @@ class FeaturedKotlinTest {
2725

2826
@ServerTest
2927
fun implicitContext(runner: ServerTestRunner) {
30-
runner.define { ->
28+
runner.use { ->
3129
Kooby {
3230
get("/") {
3331
ctx.send("Hello World!")
@@ -42,7 +40,7 @@ class FeaturedKotlinTest {
4240

4341
@ServerTest
4442
fun coroutineNoSuspend(runner: ServerTestRunner) {
45-
runner.define { ->
43+
runner.use { ->
4644
Kooby {
4745
coroutine {
4846
get {
@@ -59,7 +57,7 @@ class FeaturedKotlinTest {
5957

6058
@ServerTest
6159
fun coroutineSuspend(runner: ServerTestRunner) {
62-
runner.define { ->
60+
runner.use { ->
6361
Kooby {
6462
coroutine {
6563
get("/") {
@@ -77,7 +75,7 @@ class FeaturedKotlinTest {
7775

7876
@ServerTest
7977
fun javaApiWithReactiveType(runner: ServerTestRunner) {
80-
runner.define { ->
78+
runner.use { ->
8179
Jooby().apply {
8280
get("/") {
8381
println(Thread.currentThread())
@@ -127,7 +125,7 @@ class FeaturedKotlinTest {
127125

128126
@ServerTest
129127
fun suspendMvc(runner: ServerTestRunner) {
130-
runner.define { ->
128+
runner.use { ->
131129
Kooby {
132130
coroutine {
133131
mvc(SuspendMvc())

0 commit comments

Comments
 (0)