Use java 11 (#1050)

This commit is contained in:
Leon Mak 2019-10-28 23:02:17 +08:00 committed by Ilkka Seppälä
parent 6bb3438965
commit dd971d8c19
12 changed files with 102 additions and 141 deletions

View File

@ -31,7 +31,6 @@ import com.iluwatar.hexagonal.domain.LotteryTicket;
import com.iluwatar.hexagonal.domain.LotteryTicketId; import com.iluwatar.hexagonal.domain.LotteryTicketId;
import com.iluwatar.hexagonal.domain.PlayerDetails; import com.iluwatar.hexagonal.domain.PlayerDetails;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
@ -44,46 +43,47 @@ public class SampleData {
private static final Random RANDOM = new Random(); private static final Random RANDOM = new Random();
static { static {
PLAYERS = new ArrayList<>(); PLAYERS = List.of(
PLAYERS.add(new PlayerDetails("john@google.com", "312-342", "+3242434242")); new PlayerDetails("john@google.com", "312-342", "+3242434242"),
PLAYERS.add(new PlayerDetails("mary@google.com", "234-987", "+23452346")); new PlayerDetails("mary@google.com", "234-987", "+23452346"),
PLAYERS.add(new PlayerDetails("steve@google.com", "833-836", "+63457543")); new PlayerDetails("steve@google.com", "833-836", "+63457543"),
PLAYERS.add(new PlayerDetails("wayne@google.com", "319-826", "+24626")); new PlayerDetails("wayne@google.com", "319-826", "+24626"),
PLAYERS.add(new PlayerDetails("johnie@google.com", "983-322", "+3635635")); new PlayerDetails("johnie@google.com", "983-322", "+3635635"),
PLAYERS.add(new PlayerDetails("andy@google.com", "934-734", "+0898245")); new PlayerDetails("andy@google.com", "934-734", "+0898245"),
PLAYERS.add(new PlayerDetails("richard@google.com", "536-738", "+09845325")); new PlayerDetails("richard@google.com", "536-738", "+09845325"),
PLAYERS.add(new PlayerDetails("kevin@google.com", "453-936", "+2423532")); new PlayerDetails("kevin@google.com", "453-936", "+2423532"),
PLAYERS.add(new PlayerDetails("arnold@google.com", "114-988", "+5646346524")); new PlayerDetails("arnold@google.com", "114-988", "+5646346524"),
PLAYERS.add(new PlayerDetails("ian@google.com", "663-765", "+928394235")); new PlayerDetails("ian@google.com", "663-765", "+928394235"),
PLAYERS.add(new PlayerDetails("robin@google.com", "334-763", "+35448")); new PlayerDetails("robin@google.com", "334-763", "+35448"),
PLAYERS.add(new PlayerDetails("ted@google.com", "735-964", "+98752345")); new PlayerDetails("ted@google.com", "735-964", "+98752345"),
PLAYERS.add(new PlayerDetails("larry@google.com", "734-853", "+043842423")); new PlayerDetails("larry@google.com", "734-853", "+043842423"),
PLAYERS.add(new PlayerDetails("calvin@google.com", "334-746", "+73294135")); new PlayerDetails("calvin@google.com", "334-746", "+73294135"),
PLAYERS.add(new PlayerDetails("jacob@google.com", "444-766", "+358042354")); new PlayerDetails("jacob@google.com", "444-766", "+358042354"),
PLAYERS.add(new PlayerDetails("edwin@google.com", "895-345", "+9752435")); new PlayerDetails("edwin@google.com", "895-345", "+9752435"),
PLAYERS.add(new PlayerDetails("mary@google.com", "760-009", "+34203542")); new PlayerDetails("mary@google.com", "760-009", "+34203542"),
PLAYERS.add(new PlayerDetails("lolita@google.com", "425-907", "+9872342")); new PlayerDetails("lolita@google.com", "425-907", "+9872342"),
PLAYERS.add(new PlayerDetails("bruno@google.com", "023-638", "+673824122")); new PlayerDetails("bruno@google.com", "023-638", "+673824122"),
PLAYERS.add(new PlayerDetails("peter@google.com", "335-886", "+5432503945")); new PlayerDetails("peter@google.com", "335-886", "+5432503945"),
PLAYERS.add(new PlayerDetails("warren@google.com", "225-946", "+9872341324")); new PlayerDetails("warren@google.com", "225-946", "+9872341324"),
PLAYERS.add(new PlayerDetails("monica@google.com", "265-748", "+134124")); new PlayerDetails("monica@google.com", "265-748", "+134124"),
PLAYERS.add(new PlayerDetails("ollie@google.com", "190-045", "+34453452")); new PlayerDetails("ollie@google.com", "190-045", "+34453452"),
PLAYERS.add(new PlayerDetails("yngwie@google.com", "241-465", "+9897641231")); new PlayerDetails("yngwie@google.com", "241-465", "+9897641231"),
PLAYERS.add(new PlayerDetails("lars@google.com", "746-936", "+42345298345")); new PlayerDetails("lars@google.com", "746-936", "+42345298345"),
PLAYERS.add(new PlayerDetails("bobbie@google.com", "946-384", "+79831742")); new PlayerDetails("bobbie@google.com", "946-384", "+79831742"),
PLAYERS.add(new PlayerDetails("tyron@google.com", "310-992", "+0498837412")); new PlayerDetails("tyron@google.com", "310-992", "+0498837412"),
PLAYERS.add(new PlayerDetails("tyrell@google.com", "032-045", "+67834134")); new PlayerDetails("tyrell@google.com", "032-045", "+67834134"),
PLAYERS.add(new PlayerDetails("nadja@google.com", "000-346", "+498723")); new PlayerDetails("nadja@google.com", "000-346", "+498723"),
PLAYERS.add(new PlayerDetails("wendy@google.com", "994-989", "+987324454")); new PlayerDetails("wendy@google.com", "994-989", "+987324454"),
PLAYERS.add(new PlayerDetails("luke@google.com", "546-634", "+987642435")); new PlayerDetails("luke@google.com", "546-634", "+987642435"),
PLAYERS.add(new PlayerDetails("bjorn@google.com", "342-874", "+7834325")); new PlayerDetails("bjorn@google.com", "342-874", "+7834325"),
PLAYERS.add(new PlayerDetails("lisa@google.com", "024-653", "+980742154")); new PlayerDetails("lisa@google.com", "024-653", "+980742154"),
PLAYERS.add(new PlayerDetails("anton@google.com", "834-935", "+876423145")); new PlayerDetails("anton@google.com", "834-935", "+876423145"),
PLAYERS.add(new PlayerDetails("bruce@google.com", "284-936", "+09843212345")); new PlayerDetails("bruce@google.com", "284-936", "+09843212345"),
PLAYERS.add(new PlayerDetails("ray@google.com", "843-073", "+678324123")); new PlayerDetails("ray@google.com", "843-073", "+678324123"),
PLAYERS.add(new PlayerDetails("ron@google.com", "637-738", "+09842354")); new PlayerDetails("ron@google.com", "637-738", "+09842354"),
PLAYERS.add(new PlayerDetails("xavier@google.com", "143-947", "+375245")); new PlayerDetails("xavier@google.com", "143-947", "+375245"),
PLAYERS.add(new PlayerDetails("harriet@google.com", "842-404", "+131243252")); new PlayerDetails("harriet@google.com", "842-404", "+131243252")
);
InMemoryBank wireTransfers = new InMemoryBank(); InMemoryBank wireTransfers = new InMemoryBank();
for (PlayerDetails player : PLAYERS) { for (PlayerDetails player : PLAYERS) {
wireTransfers.setFunds(player.getBankAccount(), wireTransfers.setFunds(player.getBankAccount(),

View File

@ -27,10 +27,9 @@ import com.iluwatar.hexagonal.banking.WireTransfers;
import com.iluwatar.hexagonal.domain.*; import com.iluwatar.hexagonal.domain.*;
import org.slf4j.Logger; import org.slf4j.Logger;
import java.util.HashSet; import java.util.*;
import java.util.Optional; import java.util.stream.Collectors;
import java.util.Scanner; import java.util.stream.IntStream;
import java.util.Set;
/** /**
* Console implementation for lottery console service * Console implementation for lottery console service
@ -88,10 +87,7 @@ public class LotteryConsoleServiceImpl implements LotteryConsoleService {
String numbers = readString( scanner ); String numbers = readString( scanner );
try { try {
String[] parts = numbers.split( "," ); String[] parts = numbers.split( "," );
Set<Integer> chosen = new HashSet<>(); Set<Integer> chosen = Arrays.stream(parts).map(Integer::parseInt).collect(Collectors.toSet());
for (int i = 0; i < 4; i++) {
chosen.add( Integer.parseInt( parts[i] ) );
}
LotteryNumbers lotteryNumbers = LotteryNumbers.create( chosen ); LotteryNumbers lotteryNumbers = LotteryNumbers.create( chosen );
LotteryTicket lotteryTicket = new LotteryTicket( new LotteryTicketId(), details, lotteryNumbers ); LotteryTicket lotteryTicket = new LotteryTicket( new LotteryTicketId(), details, lotteryNumbers );
Optional<LotteryTicketId> id = service.submitTicket( lotteryTicket ); Optional<LotteryTicketId> id = service.submitTicket( lotteryTicket );

View File

@ -27,6 +27,7 @@ import org.junit.jupiter.api.Test;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
@ -43,7 +44,7 @@ class LotteryNumbersTest {
@Test @Test
void testGivenNumbers() { void testGivenNumbers() {
LotteryNumbers numbers = LotteryNumbers.create( LotteryNumbers numbers = LotteryNumbers.create(
new HashSet<>(Arrays.asList(1, 2, 3, 4))); Set.of(1, 2, 3, 4));
assertEquals(numbers.getNumbers().size(), 4); assertEquals(numbers.getNumbers().size(), 4);
assertTrue(numbers.getNumbers().contains(1)); assertTrue(numbers.getNumbers().contains(1));
assertTrue(numbers.getNumbers().contains(2)); assertTrue(numbers.getNumbers().contains(2));
@ -54,7 +55,7 @@ class LotteryNumbersTest {
@Test @Test
void testNumbersCantBeModified() { void testNumbersCantBeModified() {
LotteryNumbers numbers = LotteryNumbers.create( LotteryNumbers numbers = LotteryNumbers.create(
new HashSet<>(Arrays.asList(1, 2, 3, 4))); Set.of(1, 2, 3, 4));
assertThrows(UnsupportedOperationException.class, () -> numbers.getNumbers().add(5)); assertThrows(UnsupportedOperationException.class, () -> numbers.getNumbers().add(5));
} }
@ -67,12 +68,12 @@ class LotteryNumbersTest {
@Test @Test
void testEquals() { void testEquals() {
LotteryNumbers numbers1 = LotteryNumbers.create( LotteryNumbers numbers1 = LotteryNumbers.create(
new HashSet<>(Arrays.asList(1, 2, 3, 4))); Set.of(1, 2, 3, 4));
LotteryNumbers numbers2 = LotteryNumbers.create( LotteryNumbers numbers2 = LotteryNumbers.create(
new HashSet<>(Arrays.asList(1, 2, 3, 4))); Set.of(1, 2, 3, 4));
assertEquals(numbers1, numbers2); assertEquals(numbers1, numbers2);
LotteryNumbers numbers3 = LotteryNumbers.create( LotteryNumbers numbers3 = LotteryNumbers.create(
new HashSet<>(Arrays.asList(11, 12, 13, 14))); Set.of(11, 12, 13, 14));
assertNotEquals(numbers1, numbers3); assertNotEquals(numbers1, numbers3);
} }
} }

View File

@ -33,10 +33,7 @@ import com.iluwatar.hexagonal.test.LotteryTestUtils;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.Arrays; import java.util.*;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
@ -76,13 +73,13 @@ class LotteryTest {
// players submit the lottery tickets // players submit the lottery tickets
Optional<LotteryTicketId> ticket1 = service.submitTicket(LotteryTestUtils.createLotteryTicket("cvt@bbb.com", Optional<LotteryTicketId> ticket1 = service.submitTicket(LotteryTestUtils.createLotteryTicket("cvt@bbb.com",
"123-12312", "+32425255", new HashSet<>(Arrays.asList(1, 2, 3, 4)))); "123-12312", "+32425255", Set.of(1, 2, 3, 4)));
assertTrue(ticket1.isPresent()); assertTrue(ticket1.isPresent());
Optional<LotteryTicketId> ticket2 = service.submitTicket(LotteryTestUtils.createLotteryTicket("ant@bac.com", Optional<LotteryTicketId> ticket2 = service.submitTicket(LotteryTestUtils.createLotteryTicket("ant@bac.com",
"123-12312", "+32423455", new HashSet<>(Arrays.asList(11, 12, 13, 14)))); "123-12312", "+32423455", Set.of(11, 12, 13, 14)));
assertTrue(ticket2.isPresent()); assertTrue(ticket2.isPresent());
Optional<LotteryTicketId> ticket3 = service.submitTicket(LotteryTestUtils.createLotteryTicket("arg@boo.com", Optional<LotteryTicketId> ticket3 = service.submitTicket(LotteryTestUtils.createLotteryTicket("arg@boo.com",
"123-12312", "+32421255", new HashSet<>(Arrays.asList(6, 8, 13, 19)))); "123-12312", "+32421255", Set.of(6, 8, 13, 19)));
assertTrue(ticket3.isPresent()); assertTrue(ticket3.isPresent());
assertEquals(3, administration.getAllSubmittedTickets().size()); assertEquals(3, administration.getAllSubmittedTickets().size());

View File

@ -27,6 +27,7 @@ import org.junit.jupiter.api.Test;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
@ -39,14 +40,14 @@ class LotteryTicketTest {
@Test @Test
void testEquals() { void testEquals() {
PlayerDetails details1 = new PlayerDetails("bob@foo.bar", "1212-121212", "+34332322"); PlayerDetails details1 = new PlayerDetails("bob@foo.bar", "1212-121212", "+34332322");
LotteryNumbers numbers1 = LotteryNumbers.create(new HashSet<>(Arrays.asList(1, 2, 3, 4))); LotteryNumbers numbers1 = LotteryNumbers.create(Set.of(1, 2, 3, 4));
LotteryTicket ticket1 = new LotteryTicket(new LotteryTicketId(), details1, numbers1); LotteryTicket ticket1 = new LotteryTicket(new LotteryTicketId(), details1, numbers1);
PlayerDetails details2 = new PlayerDetails("bob@foo.bar", "1212-121212", "+34332322"); PlayerDetails details2 = new PlayerDetails("bob@foo.bar", "1212-121212", "+34332322");
LotteryNumbers numbers2 = LotteryNumbers.create(new HashSet<>(Arrays.asList(1, 2, 3, 4))); LotteryNumbers numbers2 = LotteryNumbers.create(Set.of(1, 2, 3, 4));
LotteryTicket ticket2 = new LotteryTicket(new LotteryTicketId(), details2, numbers2); LotteryTicket ticket2 = new LotteryTicket(new LotteryTicketId(), details2, numbers2);
assertEquals(ticket1, ticket2); assertEquals(ticket1, ticket2);
PlayerDetails details3 = new PlayerDetails("elsa@foo.bar", "1223-121212", "+49332322"); PlayerDetails details3 = new PlayerDetails("elsa@foo.bar", "1223-121212", "+49332322");
LotteryNumbers numbers3 = LotteryNumbers.create(new HashSet<>(Arrays.asList(1, 2, 3, 8))); LotteryNumbers numbers3 = LotteryNumbers.create(Set.of(1, 2, 3, 8));
LotteryTicket ticket3 = new LotteryTicket(new LotteryTicketId(), details3, numbers3); LotteryTicket ticket3 = new LotteryTicket(new LotteryTicketId(), details3, numbers3);
assertNotEquals(ticket1, ticket3); assertNotEquals(ticket1, ticket3);
} }

View File

@ -43,7 +43,7 @@ public class LotteryTestUtils {
* @return lottery ticket * @return lottery ticket
*/ */
public static LotteryTicket createLotteryTicket() { public static LotteryTicket createLotteryTicket() {
return createLotteryTicket("foo@bar.com", "12231-213132", "+99324554", new HashSet<>(Arrays.asList(1, 2, 3, 4))); return createLotteryTicket("foo@bar.com", "12231-213132", "+99324554", Set.of(1, 2, 3, 4));
} }
/** /**

View File

@ -40,17 +40,17 @@ public class TreasureChest {
* Constructor * Constructor
*/ */
public TreasureChest() { public TreasureChest() {
items = new ArrayList<>(); items = List.of(
items.add(new Item(ItemType.POTION, "Potion of courage")); new Item(ItemType.POTION, "Potion of courage"),
items.add(new Item(ItemType.RING, "Ring of shadows")); new Item(ItemType.RING, "Ring of shadows"),
items.add(new Item(ItemType.POTION, "Potion of wisdom")); new Item(ItemType.POTION, "Potion of wisdom"),
items.add(new Item(ItemType.POTION, "Potion of blood")); new Item(ItemType.POTION, "Potion of blood"),
items.add(new Item(ItemType.WEAPON, "Sword of silver +1")); new Item(ItemType.WEAPON, "Sword of silver +1"),
items.add(new Item(ItemType.POTION, "Potion of rust")); new Item(ItemType.POTION, "Potion of rust"),
items.add(new Item(ItemType.POTION, "Potion of healing")); new Item(ItemType.POTION, "Potion of healing"),
items.add(new Item(ItemType.RING, "Ring of armor")); new Item(ItemType.RING, "Ring of armor"),
items.add(new Item(ItemType.WEAPON, "Steel halberd")); new Item(ItemType.WEAPON, "Steel halberd"),
items.add(new Item(ItemType.WEAPON, "Dagger of poison")); new Item(ItemType.WEAPON, "Dagger of poison"));
} }
public Iterator<Item> iterator(ItemType itemType) { public Iterator<Item> iterator(ItemType itemType) {

View File

@ -28,7 +28,6 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
import com.iluwatar.iterator.Iterator; import com.iluwatar.iterator.Iterator;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
@ -46,18 +45,18 @@ public class TreasureChestTest {
* @return The set of all expected items in the chest * @return The set of all expected items in the chest
*/ */
public static List<Object[]> dataProvider() { public static List<Object[]> dataProvider() {
final List<Object[]> parameters = new ArrayList<>(); return List.of(
parameters.add(new Object[]{new Item(ItemType.POTION, "Potion of courage")}); new Object[]{new Item(ItemType.POTION, "Potion of courage")},
parameters.add(new Object[]{new Item(ItemType.RING, "Ring of shadows")}); new Object[]{new Item(ItemType.RING, "Ring of shadows")},
parameters.add(new Object[]{new Item(ItemType.POTION, "Potion of wisdom")}); new Object[]{new Item(ItemType.POTION, "Potion of wisdom")},
parameters.add(new Object[]{new Item(ItemType.POTION, "Potion of blood")}); new Object[]{new Item(ItemType.POTION, "Potion of blood")},
parameters.add(new Object[]{new Item(ItemType.WEAPON, "Sword of silver +1")}); new Object[]{new Item(ItemType.WEAPON, "Sword of silver +1")},
parameters.add(new Object[]{new Item(ItemType.POTION, "Potion of rust")}); new Object[]{new Item(ItemType.POTION, "Potion of rust")},
parameters.add(new Object[]{new Item(ItemType.POTION, "Potion of healing")}); new Object[]{new Item(ItemType.POTION, "Potion of healing")},
parameters.add(new Object[]{new Item(ItemType.RING, "Ring of armor")}); new Object[]{new Item(ItemType.RING, "Ring of armor")},
parameters.add(new Object[]{new Item(ItemType.WEAPON, "Steel halberd")}); new Object[]{new Item(ItemType.WEAPON, "Steel halberd")},
parameters.add(new Object[]{new Item(ItemType.WEAPON, "Dagger of poison")}); new Object[]{new Item(ItemType.WEAPON, "Dagger of poison")}
return parameters; );
} }
/** /**

View File

@ -66,11 +66,10 @@ public class CakeTest {
assertNotNull(cake.getLayers()); assertNotNull(cake.getLayers());
assertTrue(cake.getLayers().isEmpty()); assertTrue(cake.getLayers().isEmpty());
final Set<CakeLayer> expectedLayers = new HashSet<>(); final Set<CakeLayer> expectedLayers = Set.of(
expectedLayers.add(new CakeLayer("layer1", 1000)); new CakeLayer("layer1", 1000),
expectedLayers.add(new CakeLayer("layer2", 2000)); new CakeLayer("layer2", 2000),
expectedLayers.add(new CakeLayer("layer3", 3000)); new CakeLayer("layer3", 3000));
cake.setLayers(expectedLayers); cake.setLayers(expectedLayers);
assertEquals(expectedLayers, cake.getLayers()); assertEquals(expectedLayers, cake.getLayers());
} }
@ -111,7 +110,7 @@ public class CakeTest {
cake.setTopping(topping); cake.setTopping(topping);
cake.addLayer(layer); cake.addLayer(layer);
final String expected = "id=1234 topping=id=2345 name=topping calories=20 " final String expected = "id=1234 topping=id=2345 name=topping calories=20 "
+ "layers=[id=3456 name=layer calories=100]"; + "layers=[id=3456 name=layer calories=100]";
assertEquals(expected, cake.toString()); assertEquals(expected, cake.toString());

View File

@ -31,7 +31,6 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -64,14 +63,13 @@ public class CakeViewImplTest {
@Test @Test
public void testRender() { public void testRender() {
final List<CakeLayerInfo> layers = new ArrayList<>(); final List<CakeLayerInfo> layers = List.of(
layers.add(new CakeLayerInfo("layer1", 1000)); new CakeLayerInfo("layer1", 1000),
layers.add(new CakeLayerInfo("layer2", 2000)); new CakeLayerInfo("layer2", 2000),
layers.add(new CakeLayerInfo("layer3", 3000)); new CakeLayerInfo("layer3", 3000));
final List<CakeInfo> cakes = new ArrayList<>();
final CakeInfo cake = new CakeInfo(new CakeToppingInfo("topping", 1000), layers); final CakeInfo cake = new CakeInfo(new CakeToppingInfo("topping", 1000), layers);
cakes.add(cake); final List<CakeInfo> cakes = List.of(cake);
final CakeBakingService bakingService = mock(CakeBakingService.class); final CakeBakingService bakingService = mock(CakeBakingService.class);
when(bakingService.getAllCakes()).thenReturn(cakes); when(bakingService.getAllCakes()).thenReturn(cakes);

View File

@ -24,12 +24,9 @@
package com.iluwatar.leaderelection.bully; package com.iluwatar.leaderelection.bully;
import com.iluwatar.leaderelection.*; import com.iluwatar.leaderelection.*;
import com.iluwatar.leaderelection.ring.RingInstance;
import com.iluwatar.leaderelection.ring.RingMessageManager;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Queue; import java.util.Queue;
@ -43,8 +40,7 @@ public class BullyMessageManagerTest {
@Test @Test
public void testSendHeartbeatMessage() { public void testSendHeartbeatMessage() {
Instance instance1 = new BullyInstance(null, 1, 1); Instance instance1 = new BullyInstance(null, 1, 1);
Map<Integer, Instance> instanceMap = new HashMap<>(); Map<Integer, Instance> instanceMap = Map.of(1, instance1);
instanceMap.put(1, instance1);
MessageManager messageManager = new BullyMessageManager(instanceMap); MessageManager messageManager = new BullyMessageManager(instanceMap);
assertTrue(messageManager.sendHeartbeatMessage(1)); assertTrue(messageManager.sendHeartbeatMessage(1));
} }
@ -56,11 +52,7 @@ public class BullyMessageManagerTest {
Instance instance2 = new BullyInstance(null, 1, 2); Instance instance2 = new BullyInstance(null, 1, 2);
Instance instance3 = new BullyInstance(null, 1, 3); Instance instance3 = new BullyInstance(null, 1, 3);
Instance instance4 = new BullyInstance(null, 1, 4); Instance instance4 = new BullyInstance(null, 1, 4);
Map<Integer, Instance> instanceMap = new HashMap<>(); Map<Integer, Instance> instanceMap = Map.of(1, instance1, 2, instance2, 3, instance3, 4, instance4);
instanceMap.put(1, instance1);
instanceMap.put(2, instance2);
instanceMap.put(3, instance3);
instanceMap.put(4, instance4);
instance1.setAlive(false); instance1.setAlive(false);
MessageManager messageManager = new BullyMessageManager(instanceMap); MessageManager messageManager = new BullyMessageManager(instanceMap);
boolean result = messageManager.sendElectionMessage(3, "3"); boolean result = messageManager.sendElectionMessage(3, "3");
@ -84,11 +76,7 @@ public class BullyMessageManagerTest {
Instance instance2 = new BullyInstance(null, 1, 2); Instance instance2 = new BullyInstance(null, 1, 2);
Instance instance3 = new BullyInstance(null, 1, 3); Instance instance3 = new BullyInstance(null, 1, 3);
Instance instance4 = new BullyInstance(null, 1, 4); Instance instance4 = new BullyInstance(null, 1, 4);
Map<Integer, Instance> instanceMap = new HashMap<>(); Map<Integer, Instance> instanceMap = Map.of(1, instance1, 2, instance2, 3, instance3, 4, instance4);
instanceMap.put(1, instance1);
instanceMap.put(2, instance2);
instanceMap.put(3, instance3);
instanceMap.put(4, instance4);
instance1.setAlive(false); instance1.setAlive(false);
MessageManager messageManager = new BullyMessageManager(instanceMap); MessageManager messageManager = new BullyMessageManager(instanceMap);
boolean result = messageManager.sendElectionMessage(2, "2"); boolean result = messageManager.sendElectionMessage(2, "2");
@ -102,11 +90,7 @@ public class BullyMessageManagerTest {
Instance instance2 = new BullyInstance(null, 1, 2); Instance instance2 = new BullyInstance(null, 1, 2);
Instance instance3 = new BullyInstance(null, 1, 3); Instance instance3 = new BullyInstance(null, 1, 3);
Instance instance4 = new BullyInstance(null, 1, 4); Instance instance4 = new BullyInstance(null, 1, 4);
Map<Integer, Instance> instanceMap = new HashMap<>(); Map<Integer, Instance> instanceMap = Map.of(1, instance1, 2, instance2, 3, instance3, 4, instance4);
instanceMap.put(1, instance1);
instanceMap.put(2, instance2);
instanceMap.put(3, instance3);
instanceMap.put(4, instance4);
instance1.setAlive(false); instance1.setAlive(false);
MessageManager messageManager = new BullyMessageManager(instanceMap); MessageManager messageManager = new BullyMessageManager(instanceMap);
messageManager.sendLeaderMessage(2, 2); messageManager.sendLeaderMessage(2, 2);
@ -129,10 +113,7 @@ public class BullyMessageManagerTest {
Instance instance1 = new BullyInstance(null, 1, 1); Instance instance1 = new BullyInstance(null, 1, 1);
Instance instance2 = new BullyInstance(null, 1, 2); Instance instance2 = new BullyInstance(null, 1, 2);
Instance instance3 = new BullyInstance(null, 1, 3); Instance instance3 = new BullyInstance(null, 1, 3);
Map<Integer, Instance> instanceMap = new HashMap<>(); Map<Integer, Instance> instanceMap = Map.of(1, instance1, 2, instance2, 3, instance3);
instanceMap.put(1, instance1);
instanceMap.put(2, instance2);
instanceMap.put(3, instance3);
MessageManager messageManager = new BullyMessageManager(instanceMap); MessageManager messageManager = new BullyMessageManager(instanceMap);
messageManager.sendHeartbeatInvokeMessage(2); messageManager.sendHeartbeatInvokeMessage(2);
Message message = new Message(MessageType.HEARTBEAT_INVOKE, ""); Message message = new Message(MessageType.HEARTBEAT_INVOKE, "");

View File

@ -27,7 +27,6 @@ import com.iluwatar.leaderelection.*;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Queue; import java.util.Queue;
@ -41,8 +40,7 @@ public class RingMessageManagerTest {
@Test @Test
public void testSendHeartbeatMessage() { public void testSendHeartbeatMessage() {
Instance instance1 = new RingInstance(null, 1, 1); Instance instance1 = new RingInstance(null, 1, 1);
Map<Integer, Instance> instanceMap = new HashMap<>(); Map<Integer, Instance> instanceMap = Map.of(1, instance1);
instanceMap.put(1, instance1);
MessageManager messageManager = new RingMessageManager(instanceMap); MessageManager messageManager = new RingMessageManager(instanceMap);
assertTrue(messageManager.sendHeartbeatMessage(1)); assertTrue(messageManager.sendHeartbeatMessage(1));
} }
@ -53,10 +51,7 @@ public class RingMessageManagerTest {
Instance instance1 = new RingInstance(null, 1, 1); Instance instance1 = new RingInstance(null, 1, 1);
Instance instance2 = new RingInstance(null, 1, 2); Instance instance2 = new RingInstance(null, 1, 2);
Instance instance3 = new RingInstance(null, 1, 3); Instance instance3 = new RingInstance(null, 1, 3);
Map<Integer, Instance> instanceMap = new HashMap<>(); Map<Integer, Instance> instanceMap = Map.of(1, instance1, 2, instance2, 3, instance3);
instanceMap.put(1, instance1);
instanceMap.put(2, instance2);
instanceMap.put(3, instance3);
MessageManager messageManager = new RingMessageManager(instanceMap); MessageManager messageManager = new RingMessageManager(instanceMap);
String messageContent = "2"; String messageContent = "2";
messageManager.sendElectionMessage(2, messageContent); messageManager.sendElectionMessage(2, messageContent);
@ -78,10 +73,7 @@ public class RingMessageManagerTest {
Instance instance1 = new RingInstance(null, 1, 1); Instance instance1 = new RingInstance(null, 1, 1);
Instance instance2 = new RingInstance(null, 1, 2); Instance instance2 = new RingInstance(null, 1, 2);
Instance instance3 = new RingInstance(null, 1, 3); Instance instance3 = new RingInstance(null, 1, 3);
Map<Integer, Instance> instanceMap = new HashMap<>(); Map<Integer, Instance> instanceMap = Map.of(1, instance1, 2, instance2, 3, instance3);
instanceMap.put(1, instance1);
instanceMap.put(2, instance2);
instanceMap.put(3, instance3);
MessageManager messageManager = new RingMessageManager(instanceMap); MessageManager messageManager = new RingMessageManager(instanceMap);
String messageContent = "3"; String messageContent = "3";
messageManager.sendLeaderMessage(2, 3); messageManager.sendLeaderMessage(2, 3);
@ -102,10 +94,7 @@ public class RingMessageManagerTest {
Instance instance1 = new RingInstance(null, 1, 1); Instance instance1 = new RingInstance(null, 1, 1);
Instance instance2 = new RingInstance(null, 1, 2); Instance instance2 = new RingInstance(null, 1, 2);
Instance instance3 = new RingInstance(null, 1, 3); Instance instance3 = new RingInstance(null, 1, 3);
Map<Integer, Instance> instanceMap = new HashMap<>(); Map<Integer, Instance> instanceMap = Map.of(1, instance1, 2, instance2, 3, instance3);
instanceMap.put(1, instance1);
instanceMap.put(2, instance2);
instanceMap.put(3, instance3);
MessageManager messageManager = new RingMessageManager(instanceMap); MessageManager messageManager = new RingMessageManager(instanceMap);
messageManager.sendHeartbeatInvokeMessage(2); messageManager.sendHeartbeatInvokeMessage(2);
Message ringMessage = new Message(MessageType.HEARTBEAT_INVOKE, ""); Message ringMessage = new Message(MessageType.HEARTBEAT_INVOKE, "");