Skip to content

Commit

Permalink
JAMES-1842: JMAP port should be Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Quynh Nguyen committed Nov 18, 2016
1 parent ff57437 commit 978e722
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 14 deletions.
Expand Up @@ -47,7 +47,7 @@ public class GuiceJamesServer {
private final Module module;
private Stager<PreDestroy> preDestroy;
private GuiceServerProbe serverProbe;
private int jmapPort;
private Optional<Integer> jmapPort;
private Optional<Port> webadminPort;

public GuiceJamesServer() {
Expand All @@ -57,7 +57,7 @@ public GuiceJamesServer() {
new MailetProcessingModule()));
}

private GuiceJamesServer(Module module) {
public GuiceJamesServer(Module module) {
this.module = module;
}

Expand All @@ -74,10 +74,18 @@ public void start() throws Exception {
preDestroy = injector.getInstance(Key.get(new TypeLiteral<Stager<PreDestroy>>() {}));
injector.getInstance(ConfigurationsPerformer.class).initModules();
serverProbe = injector.getInstance(GuiceServerProbe.class);
jmapPort = injector.getInstance(JMAPServer.class).getPort();
jmapPort = locateJMAPPort(injector);
webadminPort =locateWebAdminPort(injector);
}

private Optional<Integer> locateJMAPPort(Injector injector) {
try {
return Optional.of(injector.getInstance(JMAPServer.class).getPort());
} catch(Exception e) {
return Optional.empty();
}
}

private Optional<Port> locateWebAdminPort(Injector injector) {
try {
return Optional.of(injector.getInstance(WebAdminServer.class).getPort());
Expand All @@ -96,7 +104,7 @@ public ExtendedServerProbe serverProbe() {
return serverProbe;
}

public int getJmapPort() {
public Optional<Integer> getJmapPort() {
return jmapPort;
}

Expand Down
Expand Up @@ -31,6 +31,7 @@
import java.nio.charset.Charset;

import org.junit.After;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -46,6 +47,7 @@ public abstract class AbstractJamesServerTest {
private static final int POP3_PORT = 1110;
private static final int SMTP_PORT = 1025;
private static final int LMTP_PORT = 1024;
private static final int JMAP_PORT = 1080;

private GuiceJamesServer server;
private SocketChannel socketChannel;
Expand All @@ -60,7 +62,8 @@ public void setup() throws Exception {
.setContentType(ContentType.JSON)
.setAccept(ContentType.JSON)
.setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8)))
.setPort(server.getJmapPort())
.setPort(server.getJmapPort()
.orElse(JMAP_PORT))
.build();
}

Expand Down Expand Up @@ -122,6 +125,7 @@ public void connectLMTPServerShouldSendShabangOnConnect() throws Exception {

@Test
public void connectJMAPServerShouldRespondBadRequest() throws Exception {
Assume.assumeTrue(server.getJmapPort().isPresent());
given()
.body("{\"badAttributeName\": \"value\"}")
.when()
Expand Down
Expand Up @@ -63,7 +63,8 @@ public void setup() throws Throwable {
jmapServer.start();
RestAssured.requestSpecification = new RequestSpecBuilder()
.setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8)))
.setPort(jmapServer.getJmapPort())
.setPort(jmapServer.getJmapPort()
.orElseThrow(() -> new RuntimeException("Unable to locate JMAP port")))
.build();

userCredentials = UserCredentials.builder()
Expand Down
Expand Up @@ -87,7 +87,8 @@ public void setUp() throws Exception {
.setContentType(ContentType.JSON)
.setAccept(ContentType.JSON)
.setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8)))
.setPort(guiceJamesServer.getJmapPort())
.setPort(guiceJamesServer.getJmapPort()
.orElseThrow(() -> new RuntimeException("Unable to locate JMAP port")))
.build();

Duration slowPacedPollInterval = Duration.FIVE_HUNDRED_MILLISECONDS;
Expand Down
Expand Up @@ -69,7 +69,8 @@ public void setup() throws Throwable {
.setContentType(ContentType.JSON)
.setAccept(ContentType.JSON)
.setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8)))
.setPort(jmapServer.getJmapPort())
.setPort(jmapServer.getJmapPort()
.orElseThrow(() -> new RuntimeException("Unable to locate JMAP port")))
.build();

String domain = "domain.tld";
Expand Down
Expand Up @@ -73,7 +73,8 @@ public void setup() throws Throwable {
.setContentType(ContentType.JSON)
.setAccept(ContentType.JSON)
.setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8)))
.setPort(jmapServer.getJmapPort())
.setPort(jmapServer.getJmapPort()
.orElseThrow(() -> new RuntimeException("Unable to locate JMAP port")))
.build();

this.domain = "domain.tld";
Expand Down
Expand Up @@ -76,7 +76,8 @@ public void setup() throws Throwable {
.setContentType(ContentType.JSON)
.setAccept(ContentType.JSON)
.setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8)))
.setPort(jmapServer.getJmapPort())
.setPort(jmapServer.getJmapPort()
.orElseThrow(() -> new RuntimeException("Unable to locate JMAP port")))
.build();


Expand Down
Expand Up @@ -75,7 +75,8 @@ public void setup() throws Throwable {
.setContentType(ContentType.JSON)
.setAccept(ContentType.JSON)
.setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8)))
.setPort(jmapServer.getJmapPort())
.setPort(jmapServer.getJmapPort()
.orElseThrow(() -> new RuntimeException("Unable to locate JMAP port")))
.build();
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();

Expand Down
Expand Up @@ -106,7 +106,8 @@ public void setup() throws Throwable {
.setContentType(ContentType.JSON)
.setAccept(ContentType.JSON)
.setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8)))
.setPort(jmapServer.getJmapPort())
.setPort(jmapServer.getJmapPort()
.orElseThrow(() -> new RuntimeException("Unable to locate JMAP port")))
.build();

username = "username@" + USERS_DOMAIN;
Expand Down
Expand Up @@ -68,7 +68,8 @@ public void setup() throws Throwable {
.setContentType(ContentType.JSON)
.setAccept(ContentType.JSON)
.setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8)))
.setPort(jmapServer.getJmapPort())
.setPort(jmapServer.getJmapPort()
.orElseThrow(() -> new RuntimeException("Unable to locate JMAP port")))
.build();

jmapServer.serverProbe().addDomain(USERS_DOMAIN);
Expand Down
Expand Up @@ -41,7 +41,8 @@ public URIBuilder baseUri() {
return new URIBuilder()
.setScheme("http")
.setHost("localhost")
.setPort(jmapServer.getJmapPort())
.setPort(jmapServer.getJmapPort()
.orElseThrow(() -> new RuntimeException("Unable to locate JMAP port")))
.setCharset(Charsets.UTF_8);
}

Expand Down

0 comments on commit 978e722

Please sign in to comment.