Skip to content

Commit

Permalink
More unit tests; using try-with-resources
Browse files Browse the repository at this point in the history
  • Loading branch information
avojak committed Sep 24, 2018
1 parent a46ec78 commit e3ab259
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import java.io.IOException;
import java.net.ServerSocket;

import com.avojak.plugin.hydrogen.core.logging.HydrogenLoggerFactory;
import com.avojak.plugin.hydrogen.core.logging.IHydrogenLogger;

/**
* <p>
* Class to handle last-resort checking of port availability by attempting to
Expand All @@ -21,16 +18,13 @@
*/
public class LaunchDelegatePortAvailabilityChecker {

private static final IHydrogenLogger LOGGER = HydrogenLoggerFactory
.getForClass(LaunchDelegatePortAvailabilityChecker.class);

private final ServerSocketFactory serverSocketFactory;

/**
* Constructor.
*
* @param serverSocketFactory The {@link ServerSocketFactory}. Cannot be
* null.
* @param serverSocketFactory
* The {@link ServerSocketFactory}. Cannot be null.
*/
public LaunchDelegatePortAvailabilityChecker(final ServerSocketFactory serverSocketFactory) {
if (serverSocketFactory == null) {
Expand All @@ -40,30 +34,21 @@ public LaunchDelegatePortAvailabilityChecker(final ServerSocketFactory serverSoc
}

/**
* Returns whether or not the given port number represents a valid, free,
* and unused port.
* Returns whether or not the given port number represents a valid, free, and
* unused port.
*
* @param port The port number.
* @param port
* The port number.
* @return {@code true} if the port is free, otherwise {@code false}.
*/
public boolean isPortFree(final int port) {
if (!PortValidator.isValid(port)) {
return false;
}
ServerSocket serverSocket = null;
try {
serverSocket = serverSocketFactory.create(port);
try (final ServerSocket serverSocket = serverSocketFactory.create(port)) {
return true;
} catch (final IOException e) {
return false;
} finally {
try {
if (serverSocket != null) {
serverSocket.close();
}
} catch (final IOException e) {
LOGGER.debug("Failed to close socket during port availability test"); //$NON-NLS-1$
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public class LaunchDelegatePortPool {
/**
* Constructor.
*
* @param availabilityChecker The
* {@link LaunchDelegatePortAvailabilityChecker}. Cannot be null.
* @param serverSocketFactory The {@link ServerSocketFactory}. Cannot be
* null.
* @param availabilityChecker
* The {@link LaunchDelegatePortAvailabilityChecker}. Cannot be null.
* @param serverSocketFactory
* The {@link ServerSocketFactory}. Cannot be null.
*/
public LaunchDelegatePortPool(final LaunchDelegatePortAvailabilityChecker availabilityChecker,
final ServerSocketFactory serverSocketFactory) {
Expand All @@ -50,10 +50,11 @@ public LaunchDelegatePortPool(final LaunchDelegatePortAvailabilityChecker availa
}

/**
* Returns whether or not the given port number represents a valid, free,
* and unused port.
* Returns whether or not the given port number represents a valid, free, and
* unused port.
*
* @param port The port number.
* @param port
* The port number.
* @return {@code true} if the port is free, otherwise {@code false}.
*/
public boolean isPortFree(final int port) {
Expand Down Expand Up @@ -90,42 +91,32 @@ public boolean isPortFree(final int port) {
* Finds and returns an unused port number.
*
* @return The port number.
* @throws IOException If an I/O error occurs when checking the socket.
* @throws IOException
* If an I/O error occurs when checking the socket.
*/
public int getFreePort() throws IOException {
ServerSocket socket = null;
try {
socket = serverSocketFactory.create(0);
try (final ServerSocket socket = serverSocketFactory.create(0)) {
final int port = socket.getLocalPort();
distributedPorts.add(port);
return port;
} finally {
try {
if (socket != null) {
socket.close();
}
} catch (final IOException e) {
LOGGER.debug("Failed to close socket while attempting to find a free port"); //$NON-NLS-1$
}
}
}

/**
* Returns all ports which have been distributed by the pool.
*
* @return A non-null, possibly empty {@link Collection} containing the
* ports numbers.
* @return A non-null, possibly empty {@link Collection} containing the ports
* numbers.
*/
public Collection<Integer> getDistributedPorts() {
return new HashSet<Integer>(distributedPorts);
}

/**
* Returns all ports which have been discovered to be in use by other
* services.
* Returns all ports which have been discovered to be in use by other services.
*
* @return A non-null, possibly empty {@link Collection} containing the
* ports numbers.
* @return A non-null, possibly empty {@link Collection} containing the ports
* numbers.
*/
public Collection<Integer> getDiscoveredUsedPorts() {
return new HashSet<Integer>(discoveredUsedPorts);
Expand All @@ -134,8 +125,8 @@ public Collection<Integer> getDiscoveredUsedPorts() {
/**
* Returns all distributed ports back to the pool for re-distribution.
*
* @return A non-null, possibly empty {@link Collection} containing the
* ports which have been successfully added back to the pool.
* @return A non-null, possibly empty {@link Collection} containing the ports
* which have been successfully added back to the pool.
*/
public Collection<Integer> returnAllDistributedPorts() {
final Set<Integer> returnSet = new HashSet<Integer>(distributedPorts);
Expand All @@ -146,7 +137,8 @@ public Collection<Integer> returnAllDistributedPorts() {
/**
* Returns the specified ports back to the pool for re-distribution.
*
* @param ports The {@link List} of port numbers to be returned to the pool.
* @param ports
* The {@link List} of port numbers to be returned to the pool.
* Cannot be null.
*/
public void returnPorts(final List<Integer> ports) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
*/
public class PortValidator {

private PortValidator() {
}

/**
* Returns whether or not the given port String is a valid port number.
*
* @param portString The port String.
* @param portString
* The port String.
* @return {@code true} if the given port String is valid, otherwise
* {@code false}. A {@code null} or empty String will return
* {@code false}.
Expand All @@ -29,7 +33,8 @@ public static boolean isValid(final String portString) {
/**
* Returns whether or not the given port is a valid port number.
*
* @param portNumber The port number.
* @param portNumber
* The port number.
* @return {@code true} if the given port is valid, otherwise {@code false}.
*/
public static boolean isValid(final int portNumber) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,31 @@ public abstract class LaunchesAdapter implements ILaunchesListener2 {
*/
@Override
public void launchesRemoved(final ILaunch[] launches) {
// Empty default implementation
}

/**
* {@inheritDoc}
*/
@Override
public void launchesAdded(final ILaunch[] launches) {
// Empty default implementation
}

/**
* {@inheritDoc}
*/
@Override
public void launchesChanged(final ILaunch[] launches) {
// Empty default implementation
}

/**
* {@inheritDoc}
*/
@Override
public void launchesTerminated(final ILaunch[] launches) {
// Empty default implementation
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ public int hashCode() {
result = prime * result + port.hashCode();
result = prime * result + startPg.hashCode();
result = prime * result + useDaemonThread.hashCode();
result = prime * result + arguments.hashCode();
return result;
}

Expand Down Expand Up @@ -154,9 +153,6 @@ public boolean equals(final Object obj) {
if (!useDaemonThread.equals(other.useDaemonThread)) {
return false;
}
if (!arguments.equals(other.arguments)) {
return false;
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,23 @@ public class TcpServerArguments {
/**
* Constructor.
*
* @param startTcp The argument for starting the TCP server. Cannot be null
* or empty.
* @param allowOthers The argument for allowing connections to the server
* outside of the host machine.
* @param useDaemonThread The argument for running the server on a daemon
* thread.
* @param port The argument for specifying a port number.
* @param useSsl The argument for enabling SSL.
* @param shutdownUrl The argument for specifying a shutdown URL.
* @param shutdownPassword The argument for specifying a shutdown password.
* @param forceShutdown The argument for allowing a forced shutdown.
* @param startTcp
* The argument for starting the TCP server. Cannot be null or empty.
* @param allowOthers
* The argument for allowing connections to the server outside of the
* host machine.
* @param useDaemonThread
* The argument for running the server on a daemon thread.
* @param port
* The argument for specifying a port number.
* @param useSsl
* The argument for enabling SSL.
* @param shutdownUrl
* The argument for specifying a shutdown URL.
* @param shutdownPassword
* The argument for specifying a shutdown password.
* @param forceShutdown
* The argument for allowing a forced shutdown.
*/
public TcpServerArguments(final String startTcp, final String allowOthers, final String useDaemonThread,
final String port, final String useSsl, final String shutdownUrl, final String shutdownPassword,
Expand Down Expand Up @@ -181,7 +187,6 @@ public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + allowOthers.hashCode();
result = prime * result + arguments.hashCode();
result = prime * result + forceShutdown.hashCode();
result = prime * result + port.hashCode();
result = prime * result + shutdownPassword.hashCode();
Expand Down Expand Up @@ -210,9 +215,6 @@ public boolean equals(final Object obj) {
if (!allowOthers.equals(other.allowOthers)) {
return false;
}
if (!arguments.equals(other.arguments)) {
return false;
}
if (!forceShutdown.equals(other.forceShutdown)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ public class WebServerArguments {
/**
* Constructor.
*
* @param startWeb The argument for starting the web server. Cannot be null
* or empty.
* @param allowOthers The argument for allowing connections to the server
* outside of the host machine.
* @param useDaemonThread The argument for running the server on a daemon
* thread.
* @param port The argument for specifying a port number.
* @param useSsl The argument for enabling SSL.
* @param openBrowser The argument for opening the web browser by default to
* connect to the web interface.
* @param startWeb
* The argument for starting the web server. Cannot be null or empty.
* @param allowOthers
* The argument for allowing connections to the server outside of the
* host machine.
* @param useDaemonThread
* The argument for running the server on a daemon thread.
* @param port
* The argument for specifying a port number.
* @param useSsl
* The argument for enabling SSL.
* @param openBrowser
* The argument for opening the web browser by default to connect to
* the web interface.
*/
public WebServerArguments(final String startWeb, final String allowOthers, final String useDaemonThread,
final String port, final String useSsl, final String openBrowser) {
Expand Down Expand Up @@ -149,7 +153,6 @@ public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + allowOthers.hashCode();
result = prime * result + arguments.hashCode();
result = prime * result + openBrowser.hashCode();
result = prime * result + port.hashCode();
result = prime * result + startWeb.hashCode();
Expand All @@ -176,9 +179,6 @@ public boolean equals(final Object obj) {
if (!allowOthers.equals(other.allowOthers)) {
return false;
}
if (!arguments.equals(other.arguments)) {
return false;
}
if (!openBrowser.equals(other.openBrowser)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ public void testNoArgsConstructor() {
assertNull(builder.getPort());
}

/**
* Tests that the deep copy constructor throws an exception when the old
* arguments are null.
*/
@Test(expected = IllegalArgumentException.class)
public void testDeepCopyConstructor_NullArguments() {
new PgServerArgumentsBuilder(null);
}

/**
* Tests the deep copy constructor.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ public void testNoArgsConstructor() {
assertNull(builder.getForceShutdown());
}

/**
* Tests that the deep copy constructor throws an exception when the old
* arguments are null.
*/
@Test(expected = IllegalArgumentException.class)
public void testDeepCopyConstructor_NullArguments() {
new TcpServerArgumentsBuilder(null);
}

/**
* Tests the deep copy constructor.
*/
Expand Down Expand Up @@ -118,17 +127,17 @@ public void testUseSsl() {
}

/**
* Tests that {@link TcpServerArgumentsBuilder#withShutdownUrl(String)}
* throws an exception when the given URL is {@code null}.
* Tests that {@link TcpServerArgumentsBuilder#withShutdownUrl(String)} throws
* an exception when the given URL is {@code null}.
*/
@Test(expected = IllegalArgumentException.class)
public void testWithShutdownUrl_NullUrl() {
new TcpServerArgumentsBuilder().withShutdownUrl((String) null);
}

/**
* Tests that {@link TcpServerArgumentsBuilder#withShutdownUrl(String)}
* throws an exception when the given URL is empty.
* Tests that {@link TcpServerArgumentsBuilder#withShutdownUrl(String)} throws
* an exception when the given URL is empty.
*/
@Test(expected = IllegalArgumentException.class)
public void testWithShutdownUrl_EmptyUrl() {
Expand Down

0 comments on commit e3ab259

Please sign in to comment.