Author: matthieu
Date: Fri Dec 11 10:04:07 2015
New Revision: 1719298
URL: http://svn.apache.org/viewvc?rev=1719298&view=rev
Log:
JAMES-1639 Cover Configuration with tests
Added:
james/project/trunk/server/container/jetty/src/test/java/org/apache/james/http/jetty/ConfigurationTest.java
Modified:
james/project/trunk/server/container/jetty/src/main/java/org/apache/james/http/jetty/Configuration.java
james/project/trunk/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java
Modified:
james/project/trunk/server/container/jetty/src/main/java/org/apache/james/http/jetty/Configuration.java
URL:
http://svn.apache.org/viewvc/james/project/trunk/server/container/jetty/src/main/java/org/apache/james/http/jetty/Configuration.java?rev=1719298&r1=1719297&r2=1719298&view=diff
==============================================================================
---
james/project/trunk/server/container/jetty/src/main/java/org/apache/james/http/jetty/Configuration.java
(original)
+++
james/project/trunk/server/container/jetty/src/main/java/org/apache/james/http/jetty/Configuration.java
Fri Dec 11 10:04:07 2015
@@ -18,13 +18,17 @@
****************************************************************/
package org.apache.james.http.jetty;
+import java.util.Optional;
+
import javax.servlet.Servlet;
+import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Range;
public class Configuration {
- public static Configuration empty() {
+ public static Configuration defaultConfiguration() {
return builder().build();
}
@@ -34,7 +38,10 @@ public class Configuration {
public static class Builder {
+ private static final Range<Integer> VALID_PORT_RANGE = Range.closed(1,
65535);
+
private ImmutableMap.Builder<String, Servlet> mappings;
+ private Optional<Integer> port;
public class ServletBinder {
private String mappingUrl;
@@ -52,24 +59,44 @@ public class Configuration {
private Builder() {
mappings = ImmutableMap.builder();
+ port = Optional.empty();
}
public ServletBinder serve(String mappingUrl) {
+ Preconditions.checkNotNull(mappingUrl);
+ Preconditions.checkArgument(!mappingUrl.trim().isEmpty());
return new ServletBinder(mappingUrl);
}
+ public Builder port(int port) {
+ Preconditions.checkArgument(VALID_PORT_RANGE.contains(port));
+ this.port = Optional.of(port);
+ return this;
+ }
+
+ public Builder randomPort() {
+ this.port = Optional.empty();
+ return this;
+ }
+
public Configuration build() {
- return new Configuration(mappings.build());
+ return new Configuration(mappings.build(), port);
}
}
private final ImmutableMap<String, Servlet> mappings;
+ private final Optional<Integer> port;
- public Configuration(ImmutableMap<String, Servlet> mappings) {
+ private Configuration(ImmutableMap<String, Servlet> mappings,
Optional<Integer> port) {
this.mappings = mappings;
+ this.port = port;
}
public ImmutableMap<String, Servlet> getMappings() {
return mappings;
}
+
+ public Optional<Integer> getPort() {
+ return port;
+ }
}
\ No newline at end of file
Added:
james/project/trunk/server/container/jetty/src/test/java/org/apache/james/http/jetty/ConfigurationTest.java
URL:
http://svn.apache.org/viewvc/james/project/trunk/server/container/jetty/src/test/java/org/apache/james/http/jetty/ConfigurationTest.java?rev=1719298&view=auto
==============================================================================
---
james/project/trunk/server/container/jetty/src/test/java/org/apache/james/http/jetty/ConfigurationTest.java
(added)
+++
james/project/trunk/server/container/jetty/src/test/java/org/apache/james/http/jetty/ConfigurationTest.java
Fri Dec 11 10:04:07 2015
@@ -0,0 +1,102 @@
+/****************************************************************
+ * 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.http.jetty;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import org.junit.Test;
+
+public class ConfigurationTest {
+
+ @Test
+ public void defaultConfigurationDefinition() {
+ Configuration defaultConfiguration =
Configuration.defaultConfiguration();
+ assertThat(defaultConfiguration.getPort()).isEmpty();
+ assertThat(defaultConfiguration.getMappings()).isEmpty();
+ }
+
+ @Test
+ public void shouldAllowWorkingDefinition() {
+ Bad400 bad400 = new Bad400();
+ Configuration testee = Configuration
+ .builder()
+ .port(2000)
+ .serve("/abc")
+ .with(Ok200.class)
+ .serve("/def")
+ .with(bad400)
+ .build();
+ assertThat(testee.getPort()).isPresent().contains(2000);
+ assertThat(testee.getMappings())
+ .hasSize(2)
+ .containsEntry("/abc", Ok200.class)
+ .containsEntry("/def", bad400);
+ }
+
+ @Test
+ public void shouldAllowRandomPort() {
+ Configuration testee = Configuration.builder().randomPort().build();
+ assertThat(testee.getPort()).isEmpty();
+ }
+
+ @Test
+ public void shouldNotAllowNegativePort() {
+ assertThatThrownBy(() ->
Configuration.builder().port(-1)).isInstanceOf(IllegalArgumentException.class);
+ }
+
+ @Test
+ public void shouldNotAllowZeroPort() {
+ assertThatThrownBy(() ->
Configuration.builder().port(0)).isInstanceOf(IllegalArgumentException.class);
+ }
+
+
+ @Test
+ public void shouldNotAllowTooLargePort() {
+ assertThatThrownBy(() ->
Configuration.builder().port(65536)).isInstanceOf(IllegalArgumentException.class);
+ }
+
+ @Test
+ public void shouldNotAllowOverridingPortWithRandom() {
+ Configuration configuration =
Configuration.builder().port(143).randomPort().build();
+ assertThat(configuration.getPort()).isEmpty();
+ }
+
+ @Test
+ public void shouldNotAllowNullMappingUrl() {
+ assertThatThrownBy(() ->
Configuration.builder().serve(null)).isInstanceOf(NullPointerException.class);
+ }
+
+ @Test
+ public void shouldNotAllowEmptyMappingUrl() {
+ assertThatThrownBy(() ->
Configuration.builder().serve("")).isInstanceOf(IllegalArgumentException.class);
+ }
+
+
+ @Test
+ public void shouldNotAllowWhitespaceOnlyMappingUrl() {
+ assertThatThrownBy(() -> Configuration.builder().serve("
")).isInstanceOf(IllegalArgumentException.class);
+ }
+
+
+ @Test
+ public void shouldNotAllowNullServlet() {
+ assertThatThrownBy(() ->
Configuration.builder().serve("/").with(null)).isInstanceOf(NullPointerException.class);
+ }
+}
Modified:
james/project/trunk/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java
URL:
http://svn.apache.org/viewvc/james/project/trunk/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java?rev=1719298&r1=1719297&r2=1719298&view=diff
==============================================================================
---
james/project/trunk/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java
(original)
+++
james/project/trunk/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java
Fri Dec 11 10:04:07 2015
@@ -65,7 +65,7 @@ public class JettyHttpServerTest {
@Test
public void shouldReturn404WhenNoServletConfigured() throws Exception {
- testee = JettyHttpServer.start(Configuration.empty());
+ testee = JettyHttpServer.start(Configuration.defaultConfiguration());
RestAssured.port = testee.getPort();
when()
.get("/")
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]