JAMES-1958 Define a configuration object for WebAdmin server
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/6ae498a7 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/6ae498a7 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/6ae498a7 Branch: refs/heads/master Commit: 6ae498a7c98ae9f61a2e51cf56c71f8fe6ee571b Parents: 9d99b73 Author: benwa <btell...@linagora.com> Authored: Wed Mar 8 14:09:26 2017 +0700 Committer: benwa <btell...@linagora.com> Committed: Wed Mar 15 09:01:52 2017 +0700 ---------------------------------------------------------------------- server/protocols/webadmin/pom.xml | 5 + .../org/apache/james/webadmin/FixedPort.java | 16 ++++ .../james/webadmin/WebAdminConfiguration.java | 96 ++++++++++++++++++++ .../apache/james/webadmin/FixedPortTest.java | 7 ++ .../webadmin/WebAdminConfigurationTest.java | 74 +++++++++++++++ 5 files changed, 198 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/6ae498a7/server/protocols/webadmin/pom.xml ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/pom.xml b/server/protocols/webadmin/pom.xml index 02cba3e..8ae6689 100644 --- a/server/protocols/webadmin/pom.xml +++ b/server/protocols/webadmin/pom.xml @@ -231,6 +231,11 @@ <artifactId>slf4j-api</artifactId> </dependency> <dependency> + <groupId>nl.jqno.equalsverifier</groupId> + <artifactId>equalsverifier</artifactId> + <version>1.7.6</version> + </dependency> + <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <scope>test</scope> http://git-wip-us.apache.org/repos/asf/james-project/blob/6ae498a7/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/FixedPort.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/FixedPort.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/FixedPort.java index e977f09..7ada8a1 100644 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/FixedPort.java +++ b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/FixedPort.java @@ -19,6 +19,8 @@ package org.apache.james.webadmin; +import java.util.Objects; + import com.google.common.base.Preconditions; public class FixedPort implements Port { @@ -35,4 +37,18 @@ public class FixedPort implements Port { return port; } + @Override + public final boolean equals(Object o) { + if (o instanceof FixedPort) { + FixedPort that = (FixedPort) o; + + return Objects.equals(this.port, that.port); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(port); + } } http://git-wip-us.apache.org/repos/asf/james-project/blob/6ae498a7/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/WebAdminConfiguration.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/WebAdminConfiguration.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/WebAdminConfiguration.java new file mode 100644 index 0000000..b21749c --- /dev/null +++ b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/WebAdminConfiguration.java @@ -0,0 +1,96 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + + +package org.apache.james.webadmin; + +import java.util.Objects; +import java.util.Optional; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; + +public class WebAdminConfiguration { + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private Optional<Boolean> enabled = Optional.empty(); + private Port port; + + public Builder port(Port port) { + this.port = port; + return this; + } + + public Builder enable(boolean isEnabled) { + this.enabled = Optional.of(isEnabled); + return this; + } + + public Builder enabled() { + return enable(true); + } + + public Builder disabled() { + return enable(false); + } + + public WebAdminConfiguration build() { + Preconditions.checkState(enabled.isPresent(), "You need to explicitly enable or disable WebAdmin server"); + Preconditions.checkState(!enabled.get() || port != null, "You need to specify a port for WebAdminConfiguration"); + return new WebAdminConfiguration(enabled.get(), port); + } + } + + private final boolean enabled; + private final Port port; + + @VisibleForTesting + WebAdminConfiguration(boolean enabled, Port port) { + this.enabled = enabled; + this.port = port; + } + + public boolean isEnabled() { + return enabled; + } + + public Port getPort() { + return port; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof WebAdminConfiguration) { + WebAdminConfiguration that = (WebAdminConfiguration) o; + + return Objects.equals(this.enabled, that.enabled) + && Objects.equals(this.port, that.port); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(enabled, port); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/6ae498a7/server/protocols/webadmin/src/test/java/org/apache/james/webadmin/FixedPortTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/test/java/org/apache/james/webadmin/FixedPortTest.java b/server/protocols/webadmin/src/test/java/org/apache/james/webadmin/FixedPortTest.java index eb6abdb..2712daa 100644 --- a/server/protocols/webadmin/src/test/java/org/apache/james/webadmin/FixedPortTest.java +++ b/server/protocols/webadmin/src/test/java/org/apache/james/webadmin/FixedPortTest.java @@ -24,6 +24,8 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import org.junit.Test; +import nl.jqno.equalsverifier.EqualsVerifier; + public class FixedPortTest { @Test @@ -47,4 +49,9 @@ public class FixedPortTest { assertThat(new FixedPort(expectedPort).toInt()).isEqualTo(expectedPort); } + @Test + public void shouldMatchBeanContract() { + EqualsVerifier.forClass(FixedPort.class).verify(); + } + } http://git-wip-us.apache.org/repos/asf/james-project/blob/6ae498a7/server/protocols/webadmin/src/test/java/org/apache/james/webadmin/WebAdminConfigurationTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/test/java/org/apache/james/webadmin/WebAdminConfigurationTest.java b/server/protocols/webadmin/src/test/java/org/apache/james/webadmin/WebAdminConfigurationTest.java new file mode 100644 index 0000000..01f3c2a --- /dev/null +++ b/server/protocols/webadmin/src/test/java/org/apache/james/webadmin/WebAdminConfigurationTest.java @@ -0,0 +1,74 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.webadmin; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import nl.jqno.equalsverifier.EqualsVerifier; + +public class WebAdminConfigurationTest { + + public static final FixedPort PORT = new FixedPort(80); + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void buildShouldThrowWhenNoPortButEnabled() { + expectedException.expect(IllegalStateException.class); + + WebAdminConfiguration.builder().enabled().build(); + } + + @Test + public void buildShouldWorkWithoutPortWhenDisabled() { + assertThat(WebAdminConfiguration.builder() + .disabled() + .build()) + .isEqualTo(new WebAdminConfiguration(false, null)); + } + + @Test + public void buildShouldFailOnNoEnable() { + expectedException.expect(IllegalStateException.class); + + WebAdminConfiguration.builder().port(PORT).build(); + } + + @Test + public void builderShouldBuildRightObject() { + assertThat( + WebAdminConfiguration.builder() + .enabled() + .port(PORT) + .build()) + .isEqualTo(new WebAdminConfiguration(true, PORT)); + } + + @Test + public void shouldMatchBeanContract() { + EqualsVerifier.forClass(WebAdminConfiguration.class).verify(); + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org