Skip to content

Commit

Permalink
#96 MkContainer extends Closeable
Browse files Browse the repository at this point in the history
  • Loading branch information
longtimeago committed Sep 9, 2015
1 parent 5ccd57d commit 918e085
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 140 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,13 @@
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/jcabi/http/mock/MkContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/
package com.jcabi.http.mock;

import java.io.Closeable;
import java.io.IOException;
import java.net.URI;
import java.util.Collection;
Expand Down Expand Up @@ -58,14 +59,17 @@
* and works until JVM is shut down. The only way to stop it is to call
* {@link #stop()}.
*
* <p>Since version 0.11 container implements {@link Closeable} and can be
* used in try-with-resource block.
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @since 0.10
* @see <a href="http://www.rexsl.com/rexsl-test/example-mock-servlet.html">Examples</a>
* @checkstyle TooManyMethods (200 lines)
*/
@SuppressWarnings("PMD.TooManyMethods")
public interface MkContainer {
public interface MkContainer extends Closeable {

/**
* Give this answer on the next request.
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/com/jcabi/http/mock/MkGrizzlyContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,21 @@ public URI home() {
);
}

@Override
public void close() {
this.stop();
}

/**
* Reserve port.
* @return Reserved TCP port
* @throws IOException If fails
*/
private static int reserve() throws IOException {
int reserved;
final ServerSocket socket = new ServerSocket(0);
try {
final int reserved;
try (final ServerSocket socket = new ServerSocket(0)) {
reserved = socket.getLocalPort();
} finally {
socket.close();
}
return reserved;
}

}
Loading

0 comments on commit 918e085

Please sign in to comment.