Java 11 migraiton: monostate

This commit is contained in:
Anurag Agarwal 2020-04-12 22:49:00 +00:00
parent a142c06048
commit 109d33c710
No known key found for this signature in database
GPG Key ID: CF5E14552DA23F13
4 changed files with 13 additions and 14 deletions

View File

@ -30,7 +30,7 @@ package com.iluwatar.monostate;
* *
* <p>In the following example, The {@link LoadBalancer} class represents the app's logic. It * <p>In the following example, The {@link LoadBalancer} class represents the app's logic. It
* contains a series of Servers, which can handle requests of type {@link Request}. Two instances of * contains a series of Servers, which can handle requests of type {@link Request}. Two instances of
* LoadBalacer are created. When a request is made to a server via the first LoadBalancer the state * LoadBalancer are created. When a request is made to a server via the first LoadBalancer the state
* change in the first load balancer affects the second. So if the first LoadBalancer selects the * change in the first load balancer affects the second. So if the first LoadBalancer selects the
* Server 1, the second LoadBalancer on a new request will select the Second server. If a third * Server 1, the second LoadBalancer on a new request will select the Second server. If a third
* LoadBalancer is created and a new request is made to it, then it will select the third server as * LoadBalancer is created and a new request is made to it, then it will select the third server as
@ -43,8 +43,8 @@ public class App {
* @param args command line args * @param args command line args
*/ */
public static void main(String[] args) { public static void main(String[] args) {
LoadBalancer loadBalancer1 = new LoadBalancer(); var loadBalancer1 = new LoadBalancer();
LoadBalancer loadBalancer2 = new LoadBalancer(); var loadBalancer2 = new LoadBalancer();
loadBalancer1.serverRequest(new Request("Hello")); loadBalancer1.serverRequest(new Request("Hello"));
loadBalancer2.serverRequest(new Request("Hello World")); loadBalancer2.serverRequest(new Request("Hello World"));
} }

View File

@ -38,8 +38,8 @@ public class LoadBalancer {
private static int lastServedId; private static int lastServedId;
static { static {
int id = 0; var id = 0;
for (int port : new int[]{8080, 8081, 8082, 8083, 8084}) { for (var port : new int[]{8080, 8081, 8082, 8083, 8084}) {
SERVERS.add(new Server("localhost", port, ++id)); SERVERS.add(new Server("localhost", port, ++id));
} }
} }
@ -69,7 +69,7 @@ public class LoadBalancer {
if (lastServedId >= SERVERS.size()) { if (lastServedId >= SERVERS.size()) {
lastServedId = 0; lastServedId = 0;
} }
Server server = SERVERS.get(lastServedId++); var server = SERVERS.get(lastServedId++);
server.serve(request); server.serve(request);
} }

View File

@ -32,8 +32,7 @@ public class AppTest {
@Test @Test
public void testMain() { public void testMain() {
String[] args = {}; App.main(new String[]{});
App.main(args);
} }
} }

View File

@ -44,8 +44,8 @@ public class LoadBalancerTest {
@Test @Test
public void testSameStateAmongstAllInstances() { public void testSameStateAmongstAllInstances() {
final LoadBalancer firstBalancer = new LoadBalancer(); final var firstBalancer = new LoadBalancer();
final LoadBalancer secondBalancer = new LoadBalancer(); final var secondBalancer = new LoadBalancer();
firstBalancer.addServer(new Server("localhost", 8085, 6)); firstBalancer.addServer(new Server("localhost", 8085, 6));
// Both should have the same number of servers. // Both should have the same number of servers.
assertEquals(firstBalancer.getNoOfServers(), secondBalancer.getNoOfServers()); assertEquals(firstBalancer.getNoOfServers(), secondBalancer.getNoOfServers());
@ -55,18 +55,18 @@ public class LoadBalancerTest {
@Test @Test
public void testServe() { public void testServe() {
final Server server = mock(Server.class); final var server = mock(Server.class);
when(server.getHost()).thenReturn("testhost"); when(server.getHost()).thenReturn("testhost");
when(server.getPort()).thenReturn(1234); when(server.getPort()).thenReturn(1234);
doNothing().when(server).serve(any(Request.class)); doNothing().when(server).serve(any(Request.class));
final LoadBalancer loadBalancer = new LoadBalancer(); final var loadBalancer = new LoadBalancer();
loadBalancer.addServer(server); loadBalancer.addServer(server);
verifyZeroInteractions(server); verifyZeroInteractions(server);
final Request request = new Request("test"); final var request = new Request("test");
for (int i = 0; i < loadBalancer.getNoOfServers() * 2; i++) { for (var i = 0; i < loadBalancer.getNoOfServers() * 2; i++) {
loadBalancer.serverRequest(request); loadBalancer.serverRequest(request);
} }