Skip to content

Commit

Permalink
VaadinBootBaseTest: add tests for lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
mvysny committed Sep 12, 2024
1 parent 8a43984 commit 4599675
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,68 @@

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import static org.junit.jupiter.api.Assertions.*;

public class DummyWebServer implements WebServer {
public VaadinBootBase<?> configured;
public boolean started;
@Override
public void configure(@NotNull VaadinBootBase<?> configuration) throws Exception {
this.configured = configuration;
}

@Override
public void start() throws Exception {
assertNotNull(configured, "configure() not called");
assertFalse(started, "start() called repeatedly");
started = true;
}

@Override
public void stop() throws Exception {
assertNotNull(configured, "configure() not called");
assertTrue(started, "start() not called");
started = false;
}

@Override
public void await() throws InterruptedException {
assertNotNull(configured, "configure() not called");
assertTrue(started, "start() not called");
}

@Override
public @NotNull String getName() {
return "Dummy";
}

public static class FailsToStart extends DummyWebServer {
@Override
public void start() throws Exception {
super.start();
started = false;
throw new IOException("Port 8080 is occupied, cannot bind");
}

@Override
public void stop() throws Exception {
super.stop();
fail("Shouldn't be called");
}

@Override
public void await() throws InterruptedException {
fail("Shouldn't be called");
}
}

public static class FailsToStop extends DummyWebServer {
@Override
public void stop() throws Exception {
super.stop();
throw new IOException("Failed to stop");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.github.mvysny.vaadinboot.common;

import org.jetbrains.annotations.NotNull;

public class VaadinBoot extends VaadinBootBase<VaadinBoot> {
protected VaadinBoot() {
super(new DummyWebServer());
public VaadinBoot() {
this(new DummyWebServer());
}
public VaadinBoot(@NotNull DummyWebServer webServer) {
super(webServer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -116,4 +117,23 @@ public void testContextRootParsedCorrectlyFromSystemEnv() {
// manual config takes precedence
assertEquals("", new VaadinBoot().withContextRoot("/").contextRoot);
}

@Test
public void failureToStartPropagatedProperly() throws Exception {
final DummyWebServer.FailsToStart webServer = new DummyWebServer.FailsToStart();
final IOException ex = assertThrows(IOException.class, () ->
new VaadinBoot(webServer).start()
);
assertEquals("Port 8080 is occupied, cannot bind", ex.getMessage());
assertFalse(webServer.started);
}

@Test
public void failureToStopNotPropagated() throws Exception {
final DummyWebServer.FailsToStop webServer = new DummyWebServer.FailsToStop();
final VaadinBoot boot = new VaadinBoot(webServer);
boot.start();
boot.stop("foo");
assertFalse(webServer.started);
}
}

0 comments on commit 4599675

Please sign in to comment.