From 7dc45c0281a6bcb468356e8ee7861d43bada412a Mon Sep 17 00:00:00 2001 From: Gary Tierney Date: Sun, 13 Sep 2015 01:07:19 +0100 Subject: [PATCH 01/10] Add PowerMock JUnit module and Mockito API to pom --- pom.xml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pom.xml b/pom.xml index 29a33b97..7ad94bdf 100644 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,18 @@ + + org.powermock + powermock-module-junit4 + 1.6.2 + test + + + org.powermock + powermock-api-mockito + 1.6.2 + test + org.apache.commons commons-compress From a5f8b1f9fd5ca90669005969515ee6fafdd5e137 Mon Sep 17 00:00:00 2001 From: Gary Tierney Date: Sun, 13 Sep 2015 01:08:11 +0100 Subject: [PATCH 02/10] Test messages with an invalid source are terminated --- .../ItemOnItemVerificationHandlerTest.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java diff --git a/game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java b/game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java new file mode 100644 index 00000000..7a087783 --- /dev/null +++ b/game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java @@ -0,0 +1,47 @@ +package org.apollo.game.message.handler; + +import org.apollo.cache.def.ItemDefinition; +import org.apollo.game.message.impl.ItemOnItemMessage; +import org.apollo.game.model.Item; +import org.apollo.game.model.World; +import org.apollo.game.model.entity.Player; +import org.apollo.game.model.inter.bank.BankConstants; +import org.apollo.game.model.inv.Inventory; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import static org.junit.Assert.assertTrue; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({Player.class, ItemDefinition.class}) +public class ItemOnItemVerificationHandlerTest { + + private ItemOnItemVerificationHandler verificationHandler = new ItemOnItemVerificationHandler(new World()); + + @BeforeClass + public static void setupTestItemDefinitions() { + PowerMockito.mockStatic(ItemDefinition.class); + Mockito.when(ItemDefinition.lookup(4151)).thenReturn(new ItemDefinition(4151)); + } + + @Test + public void testTerminateWithNoSourceItem() throws Exception { + Player player = PowerMockito.mock(Player.class); + Inventory inventory = new Inventory(28); + inventory.set(1, new Item(4151, 1)); + + Mockito.when(player.getInventory()).thenReturn(inventory); + + ItemOnItemMessage itemOnItemMessage = new ItemOnItemMessage(BankConstants.SIDEBAR_INVENTORY_ID, 500, 1, + BankConstants.SIDEBAR_INVENTORY_ID, 4151, 1); + + verificationHandler.handle(player, itemOnItemMessage); + + assertTrue("ItemOnItemVerificationHandler: failed checking source item / slot exists", itemOnItemMessage.terminated()); + } +} \ No newline at end of file From c9b702bcfb294f22f192bd8e169a8b1424336637 Mon Sep 17 00:00:00 2001 From: Gary Tierney Date: Sun, 13 Sep 2015 01:14:04 +0100 Subject: [PATCH 03/10] Get ItemOnItemVerificationHandlerTest passing --- .../handler/ItemOnItemVerificationHandlerTest.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java b/game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java index 7a087783..aa496f2f 100644 --- a/game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java +++ b/game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java @@ -21,7 +21,10 @@ import static org.junit.Assert.assertTrue; @PrepareForTest({Player.class, ItemDefinition.class}) public class ItemOnItemVerificationHandlerTest { - private ItemOnItemVerificationHandler verificationHandler = new ItemOnItemVerificationHandler(new World()); + private World world = new World(); + + private ItemOnItemVerificationHandler itemOnItemVerificationHandler = new ItemOnItemVerificationHandler(world); + private ItemVerificationHandler itemVerificationHandler = new ItemVerificationHandler(world); @BeforeClass public static void setupTestItemDefinitions() { @@ -40,7 +43,8 @@ public class ItemOnItemVerificationHandlerTest { ItemOnItemMessage itemOnItemMessage = new ItemOnItemMessage(BankConstants.SIDEBAR_INVENTORY_ID, 500, 1, BankConstants.SIDEBAR_INVENTORY_ID, 4151, 1); - verificationHandler.handle(player, itemOnItemMessage); + itemVerificationHandler.handle(player, itemOnItemMessage); + itemOnItemVerificationHandler.handle(player, itemOnItemMessage); assertTrue("ItemOnItemVerificationHandler: failed checking source item / slot exists", itemOnItemMessage.terminated()); } From 82801df4364259b4d70d92e850bfe879fdf1681b Mon Sep 17 00:00:00 2001 From: Gary Tierney Date: Sun, 13 Sep 2015 01:17:10 +0100 Subject: [PATCH 04/10] Add a test around the target of ItemOnItemMessage Check that the message is terminated() if an ItemOnItemMessage is received with an invalid target item. --- .../ItemOnItemVerificationHandlerTest.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java b/game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java index aa496f2f..8098ca94 100644 --- a/game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java +++ b/game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java @@ -46,6 +46,23 @@ public class ItemOnItemVerificationHandlerTest { itemVerificationHandler.handle(player, itemOnItemMessage); itemOnItemVerificationHandler.handle(player, itemOnItemMessage); - assertTrue("ItemOnItemVerificationHandler: failed checking source item / slot exists", itemOnItemMessage.terminated()); + assertTrue("ItemOnItemVerificationHandler: failed terminating message with invalid source item", itemOnItemMessage.terminated()); + } + + @Test + public void testTerminateWithNoTargetItem() throws Exception { + Player player = PowerMockito.mock(Player.class); + Inventory inventory = new Inventory(28); + inventory.set(1, new Item(4151, 1)); + + Mockito.when(player.getInventory()).thenReturn(inventory); + + ItemOnItemMessage itemOnItemMessage = new ItemOnItemMessage(BankConstants.SIDEBAR_INVENTORY_ID, 4151, 1, + BankConstants.SIDEBAR_INVENTORY_ID, 4152, 2); + + itemVerificationHandler.handle(player, itemOnItemMessage); + itemOnItemVerificationHandler.handle(player, itemOnItemMessage); + + assertTrue("ItemOnItemVerificationHandler: failed terminating message with invalid target item", itemOnItemMessage.terminated()); } } \ No newline at end of file From ef398dc098f4e2e26de6c65affd9d182abe65eca Mon Sep 17 00:00:00 2001 From: Gary Tierney Date: Sun, 13 Sep 2015 01:25:00 +0100 Subject: [PATCH 05/10] Add a test around ChatMessageHandler, update imports Update the previous ItemOnItemVerificationHandler test to use static imports of PowerMockito and Assert. --- .../handler/ChatMessageHandlerTest.java | 34 +++++++++++++++++++ .../ItemOnItemVerificationHandlerTest.java | 17 +++++----- 2 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 game/src/test/org/apollo/game/message/handler/ChatMessageHandlerTest.java diff --git a/game/src/test/org/apollo/game/message/handler/ChatMessageHandlerTest.java b/game/src/test/org/apollo/game/message/handler/ChatMessageHandlerTest.java new file mode 100644 index 00000000..2cb25f47 --- /dev/null +++ b/game/src/test/org/apollo/game/message/handler/ChatMessageHandlerTest.java @@ -0,0 +1,34 @@ +package org.apollo.game.message.handler; + +import org.apollo.game.message.impl.ChatMessage; +import org.apollo.game.model.World; +import org.apollo.game.model.entity.Player; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import static org.junit.Assert.*; +import static org.powermock.api.mockito.PowerMockito.*; + + +@RunWith(PowerMockRunner.class) +@PrepareForTest({Player.class}) +public class ChatMessageHandlerTest { + + private final World world = new World(); + private final ChatMessageHandler chatMessageHandler = new ChatMessageHandler(world); + + @Test + public void testTerminatedIfMuted() throws Exception { + Player player = PowerMockito.mock(Player.class); + + when(player.isMuted()).thenReturn(true); + + ChatMessage chatMessage = new ChatMessage("Test", "Test".getBytes(), 0, 0); + chatMessageHandler.handle(player, chatMessage); + + assertTrue("ChatMessageHandler: player can send messages when muted", chatMessage.terminated()); + } +} \ No newline at end of file diff --git a/game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java b/game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java index 8098ca94..3542c858 100644 --- a/game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java +++ b/game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java @@ -10,12 +10,11 @@ import org.apollo.game.model.inv.Inventory; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; +import static org.powermock.api.mockito.PowerMockito.*; @RunWith(PowerMockRunner.class) @PrepareForTest({Player.class, ItemDefinition.class}) @@ -28,17 +27,17 @@ public class ItemOnItemVerificationHandlerTest { @BeforeClass public static void setupTestItemDefinitions() { - PowerMockito.mockStatic(ItemDefinition.class); - Mockito.when(ItemDefinition.lookup(4151)).thenReturn(new ItemDefinition(4151)); + mockStatic(ItemDefinition.class); + when(ItemDefinition.lookup(4151)).thenReturn(new ItemDefinition(4151)); } @Test public void testTerminateWithNoSourceItem() throws Exception { - Player player = PowerMockito.mock(Player.class); + Player player = mock(Player.class); Inventory inventory = new Inventory(28); inventory.set(1, new Item(4151, 1)); - Mockito.when(player.getInventory()).thenReturn(inventory); + when(player.getInventory()).thenReturn(inventory); ItemOnItemMessage itemOnItemMessage = new ItemOnItemMessage(BankConstants.SIDEBAR_INVENTORY_ID, 500, 1, BankConstants.SIDEBAR_INVENTORY_ID, 4151, 1); @@ -51,11 +50,11 @@ public class ItemOnItemVerificationHandlerTest { @Test public void testTerminateWithNoTargetItem() throws Exception { - Player player = PowerMockito.mock(Player.class); + Player player = mock(Player.class); Inventory inventory = new Inventory(28); inventory.set(1, new Item(4151, 1)); - Mockito.when(player.getInventory()).thenReturn(inventory); + when(player.getInventory()).thenReturn(inventory); ItemOnItemMessage itemOnItemMessage = new ItemOnItemMessage(BankConstants.SIDEBAR_INVENTORY_ID, 4151, 1, BankConstants.SIDEBAR_INVENTORY_ID, 4152, 2); From 0c074bd1237cf6b39e6b0b2ff26d75f2fc6468d8 Mon Sep 17 00:00:00 2001 From: Gary Tierney Date: Sun, 13 Sep 2015 01:42:12 +0100 Subject: [PATCH 06/10] Add tests around ObjectActionVerificationHandler Add a test which verifies an ObjectActionMessage is terminated if its not within the specified distance of the player. Additionally add a test which verifies that an ObjectActionMessage is terminated if there is no object with the given id nearby. --- .../ObjectActionVerificationHandlerTest.java | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 game/src/test/org/apollo/game/message/handler/ObjectActionVerificationHandlerTest.java diff --git a/game/src/test/org/apollo/game/message/handler/ObjectActionVerificationHandlerTest.java b/game/src/test/org/apollo/game/message/handler/ObjectActionVerificationHandlerTest.java new file mode 100644 index 00000000..21344672 --- /dev/null +++ b/game/src/test/org/apollo/game/message/handler/ObjectActionVerificationHandlerTest.java @@ -0,0 +1,91 @@ +package org.apollo.game.message.handler; + +import org.apollo.cache.def.ItemDefinition; +import org.apollo.cache.def.ObjectDefinition; +import org.apollo.game.message.impl.ObjectActionMessage; +import org.apollo.game.model.Position; +import org.apollo.game.model.World; +import org.apollo.game.model.area.Region; +import org.apollo.game.model.area.RegionRepository; +import org.apollo.game.model.entity.Entity; +import org.apollo.game.model.entity.EntityType; +import org.apollo.game.model.entity.Player; +import org.apollo.game.model.entity.obj.StaticGameObject; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.util.HashSet; +import java.util.Set; + +import static org.junit.Assert.*; +import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.when; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({World.class, Player.class, ObjectDefinition.class, RegionRepository.class, Region.class}) +public class ObjectActionVerificationHandlerTest { + + @BeforeClass + public static void setupTestObjectDefinitions() { + mockStatic(ObjectDefinition.class); + when(ObjectDefinition.count()).thenReturn(4152); + } + + @Test + public void testTerminateIfOutOfRange() throws Exception { + Position playerPosition = new Position(3200, 3200); + Position objectPosition = new Position(3200, 3216); + + World world = mock(World.class); + Region region = mock(Region.class); + RegionRepository regionRepository = mock(RegionRepository.class); + Player player = mock(Player.class); + + Set entitySet = new HashSet<>(); + entitySet.add(new StaticGameObject(world, 4151, objectPosition, 0, 0)); + + when(world.getRegionRepository()).thenReturn(regionRepository); + when(regionRepository.fromPosition(objectPosition)).thenReturn(region); + when(player.getPosition()).thenReturn(playerPosition); + when(region.getEntities(objectPosition, EntityType.STATIC_OBJECT, EntityType.DYNAMIC_OBJECT)) + .thenReturn(entitySet); + + ObjectActionMessage objectActionMessage = new ObjectActionMessage(1, 4151, objectPosition); + ObjectActionVerificationHandler objectActionVerificationHandler = new ObjectActionVerificationHandler(world); + + objectActionVerificationHandler.handle(player, objectActionMessage); + + assertTrue("ObjectVerificationHandler: message not terminated when out of range!", objectActionMessage.terminated()); + } + + @Test + + public void testTerminateIfNoObject() throws Exception { + Position playerPosition = new Position(3200, 3200); + Position objectPosition = new Position(3200, 3201); + + World world = mock(World.class); + Region region = mock(Region.class); + RegionRepository regionRepository = mock(RegionRepository.class); + Player player = mock(Player.class); + + Set entitySet = new HashSet<>(); + + when(world.getRegionRepository()).thenReturn(regionRepository); + when(regionRepository.fromPosition(objectPosition)).thenReturn(region); + when(player.getPosition()).thenReturn(playerPosition); + when(region.getEntities(objectPosition, EntityType.STATIC_OBJECT, EntityType.DYNAMIC_OBJECT)) + .thenReturn(entitySet); + + ObjectActionMessage objectActionMessage = new ObjectActionMessage(1, 4151, objectPosition); + ObjectActionVerificationHandler objectActionVerificationHandler = new ObjectActionVerificationHandler(world); + + objectActionVerificationHandler.handle(player, objectActionMessage); + + assertTrue("ObjectVerificationHandler: message not terminated when no object exists!", objectActionMessage.terminated()); + } +} \ No newline at end of file From a21b6581d66f89129638ccb4223fd86d32735563 Mon Sep 17 00:00:00 2001 From: Gary Tierney Date: Sun, 13 Sep 2015 01:46:41 +0100 Subject: [PATCH 07/10] Move powermock to bottom of pom, change junit scope to test --- pom.xml | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/pom.xml b/pom.xml index 7ad94bdf..905cd3c5 100644 --- a/pom.xml +++ b/pom.xml @@ -44,18 +44,6 @@ - - org.powermock - powermock-module-junit4 - 1.6.2 - test - - - org.powermock - powermock-api-mockito - 1.6.2 - test - org.apache.commons commons-compress @@ -97,6 +85,21 @@ junit junit 4.12 + test + + + + org.powermock + powermock-module-junit4 + 1.6.2 + test + + + + org.powermock + powermock-api-mockito + 1.6.2 + test From 15f5d4a36e9df8d54f859688a2129bfa93389d83 Mon Sep 17 00:00:00 2001 From: Gary Tierney Date: Sun, 13 Sep 2015 01:46:59 +0100 Subject: [PATCH 08/10] Change @BeforeClass to @Before to prevent tests failing --- .../message/handler/ItemOnItemVerificationHandlerTest.java | 5 +++-- .../message/handler/ObjectActionVerificationHandlerTest.java | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java b/game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java index 3542c858..31389266 100644 --- a/game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java +++ b/game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java @@ -7,6 +7,7 @@ import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; import org.apollo.game.model.inter.bank.BankConstants; import org.apollo.game.model.inv.Inventory; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; @@ -25,8 +26,8 @@ public class ItemOnItemVerificationHandlerTest { private ItemOnItemVerificationHandler itemOnItemVerificationHandler = new ItemOnItemVerificationHandler(world); private ItemVerificationHandler itemVerificationHandler = new ItemVerificationHandler(world); - @BeforeClass - public static void setupTestItemDefinitions() { + @Before + public void setupTestItemDefinitions() { mockStatic(ItemDefinition.class); when(ItemDefinition.lookup(4151)).thenReturn(new ItemDefinition(4151)); } diff --git a/game/src/test/org/apollo/game/message/handler/ObjectActionVerificationHandlerTest.java b/game/src/test/org/apollo/game/message/handler/ObjectActionVerificationHandlerTest.java index 21344672..40cf72db 100644 --- a/game/src/test/org/apollo/game/message/handler/ObjectActionVerificationHandlerTest.java +++ b/game/src/test/org/apollo/game/message/handler/ObjectActionVerificationHandlerTest.java @@ -11,6 +11,7 @@ import org.apollo.game.model.entity.Entity; import org.apollo.game.model.entity.EntityType; import org.apollo.game.model.entity.Player; import org.apollo.game.model.entity.obj.StaticGameObject; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; @@ -29,8 +30,8 @@ import static org.powermock.api.mockito.PowerMockito.when; @PrepareForTest({World.class, Player.class, ObjectDefinition.class, RegionRepository.class, Region.class}) public class ObjectActionVerificationHandlerTest { - @BeforeClass - public static void setupTestObjectDefinitions() { + @Before + public void setupTestObjectDefinitions() { mockStatic(ObjectDefinition.class); when(ObjectDefinition.count()).thenReturn(4152); } From c65d5332880267a0812505ed5c77f0ad0569bd5c Mon Sep 17 00:00:00 2001 From: Gary Tierney Date: Sun, 13 Sep 2015 02:20:37 +0100 Subject: [PATCH 09/10] Add tests around ItemOnObjectVerificationHandler Add various tests around ItemOnObject messages to verify that a valid object needs to be given as well as a valid item in order for the message to be passed down the MessageHandlerChain. --- .../ItemOnObjectVerificationHandler.java | 22 +++ .../ObjectActionVerificationHandler.java | 2 +- .../ItemOnObjectVerificationHandlerTest.java | 135 ++++++++++++++++++ 3 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 game/src/test/org/apollo/game/message/handler/ItemOnObjectVerificationHandlerTest.java diff --git a/game/src/main/org/apollo/game/message/handler/ItemOnObjectVerificationHandler.java b/game/src/main/org/apollo/game/message/handler/ItemOnObjectVerificationHandler.java index 9d17cc08..89a9260c 100644 --- a/game/src/main/org/apollo/game/message/handler/ItemOnObjectVerificationHandler.java +++ b/game/src/main/org/apollo/game/message/handler/ItemOnObjectVerificationHandler.java @@ -1,13 +1,20 @@ package org.apollo.game.message.handler; +import org.apollo.cache.def.ObjectDefinition; import org.apollo.game.message.impl.ItemOnObjectMessage; import org.apollo.game.model.Item; +import org.apollo.game.model.Position; import org.apollo.game.model.World; +import org.apollo.game.model.area.Region; +import org.apollo.game.model.entity.EntityType; import org.apollo.game.model.entity.Player; +import org.apollo.game.model.entity.obj.GameObject; import org.apollo.game.model.inter.bank.BankConstants; import org.apollo.game.model.inv.Inventory; import org.apollo.game.model.inv.SynchronizationInventoryListener; +import java.util.Set; + /** * A {@link MessageHandler} that verifies {@link ItemOnObjectMessage}s. * @@ -44,6 +51,21 @@ public final class ItemOnObjectVerificationHandler extends MessageHandler= ObjectDefinition.count()) { + message.terminate(); + return; + } + + Position position = message.getPosition(); + Region region = world.getRegionRepository().fromPosition(position); + Set objects = region.getEntities(position, EntityType.STATIC_OBJECT, EntityType.DYNAMIC_OBJECT); + + if (!player.getPosition().isWithinDistance(position, 15) || !ObjectActionVerificationHandler.containsObject(objectId, objects)) { + message.terminate(); + return; + } } } \ No newline at end of file diff --git a/game/src/main/org/apollo/game/message/handler/ObjectActionVerificationHandler.java b/game/src/main/org/apollo/game/message/handler/ObjectActionVerificationHandler.java index 862f59a9..8983cee0 100644 --- a/game/src/main/org/apollo/game/message/handler/ObjectActionVerificationHandler.java +++ b/game/src/main/org/apollo/game/message/handler/ObjectActionVerificationHandler.java @@ -26,7 +26,7 @@ public final class ObjectActionVerificationHandler extends MessageHandler objects) { + public static boolean containsObject(int id, Set objects) { return objects.stream().anyMatch(object -> object.getId() == id); } diff --git a/game/src/test/org/apollo/game/message/handler/ItemOnObjectVerificationHandlerTest.java b/game/src/test/org/apollo/game/message/handler/ItemOnObjectVerificationHandlerTest.java new file mode 100644 index 00000000..0f391776 --- /dev/null +++ b/game/src/test/org/apollo/game/message/handler/ItemOnObjectVerificationHandlerTest.java @@ -0,0 +1,135 @@ +package org.apollo.game.message.handler; + +import org.apollo.cache.def.ItemDefinition; +import org.apollo.cache.def.ObjectDefinition; +import org.apollo.game.message.impl.ItemOnObjectMessage; +import org.apollo.game.model.Item; +import org.apollo.game.model.Position; +import org.apollo.game.model.World; +import org.apollo.game.model.area.Region; +import org.apollo.game.model.area.RegionRepository; +import org.apollo.game.model.entity.Entity; +import org.apollo.game.model.entity.EntityType; +import org.apollo.game.model.entity.Player; +import org.apollo.game.model.entity.obj.StaticGameObject; +import org.apollo.game.model.inter.bank.BankConstants; +import org.apollo.game.model.inv.Inventory; +import org.apollo.game.model.inv.SynchronizationInventoryListener; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.util.HashSet; +import java.util.Set; + +import static org.junit.Assert.assertTrue; +import static org.powermock.api.mockito.PowerMockito.*; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({Player.class, World.class, Region.class, RegionRepository.class, ObjectDefinition.class, + ItemDefinition.class}) +public class ItemOnObjectVerificationHandlerTest { + + @Before + public void setupTestItemDefinitions() { + mockStatic(ItemDefinition.class); + when(ItemDefinition.lookup(4151)).thenReturn(new ItemDefinition(4151)); + + mockStatic(ObjectDefinition.class); + when(ObjectDefinition.count()).thenReturn(2); + } + + + @Test + public void testTerminateIfInvalidItem() throws Exception { + Position playerPosition = new Position(3200, 3200); + Position objectPosition = new Position(3200, 3216); + + World world = mock(World.class); + Region region = mock(Region.class); + RegionRepository regionRepository = mock(RegionRepository.class); + Player player = mock(Player.class); + + Set entitySet = new HashSet<>(); + entitySet.add(new StaticGameObject(world, 4151, objectPosition, 0, 0)); + Inventory inventory = new Inventory(28); + + when(player.getInventory()).thenReturn(inventory); + when(world.getRegionRepository()).thenReturn(regionRepository); + when(regionRepository.fromPosition(objectPosition)).thenReturn(region); + when(player.getPosition()).thenReturn(playerPosition); + when(region.getEntities(objectPosition, EntityType.STATIC_OBJECT, EntityType.DYNAMIC_OBJECT)) + .thenReturn(entitySet); + + ItemOnObjectMessage itemOnObjectMessage = new ItemOnObjectMessage(SynchronizationInventoryListener.INVENTORY_ID, 4151, 1, + 1, objectPosition.getX(), objectPosition.getY()); + ItemOnObjectVerificationHandler itemOnObjectVerificationHandler = new ItemOnObjectVerificationHandler(world); + + itemOnObjectVerificationHandler.handle(player, itemOnObjectMessage); + + assertTrue("ObjectVerificationHandler: message not terminated valid item given!", itemOnObjectMessage.terminated()); + } + + @Test + public void testTerminateIfInvalidSlot() throws Exception { + Position playerPosition = new Position(3200, 3200); + Position objectPosition = new Position(3200, 3200); + + World world = mock(World.class); + Region region = mock(Region.class); + RegionRepository regionRepository = mock(RegionRepository.class); + Player player = mock(Player.class); + + Set entitySet = new HashSet<>(); + entitySet.add(new StaticGameObject(world, 4151, objectPosition, 0, 0)); + Inventory inventory = new Inventory(28); + + when(player.getInventory()).thenReturn(inventory); + when(world.getRegionRepository()).thenReturn(regionRepository); + when(regionRepository.fromPosition(objectPosition)).thenReturn(region); + when(player.getPosition()).thenReturn(playerPosition); + when(region.getEntities(objectPosition, EntityType.STATIC_OBJECT, EntityType.DYNAMIC_OBJECT)) + .thenReturn(entitySet); + + ItemOnObjectMessage itemOnObjectMessage = new ItemOnObjectMessage(SynchronizationInventoryListener.INVENTORY_ID, 4151, 30, + 1, objectPosition.getX(), objectPosition.getY()); + ItemOnObjectVerificationHandler itemOnObjectVerificationHandler = new ItemOnObjectVerificationHandler(world); + + itemOnObjectVerificationHandler.handle(player, itemOnObjectMessage); + + assertTrue("ObjectVerificationHandler: message not terminated when no valid slot given!", itemOnObjectMessage.terminated()); + } + + @Test + public void testTerminateIfObjectOutOfRange() throws Exception { + Position playerPosition = new Position(3200, 3200); + Position objectPosition = new Position(3200, 3200); + + World world = mock(World.class); + Region region = mock(Region.class); + RegionRepository regionRepository = mock(RegionRepository.class); + Player player = mock(Player.class); + + Set entitySet = new HashSet<>(); + entitySet.add(new StaticGameObject(world, 4151, objectPosition, 0, 0)); + Inventory inventory = new Inventory(28); + inventory.set(1, new Item(4151, 1)); + + when(player.getInventory()).thenReturn(inventory); + when(world.getRegionRepository()).thenReturn(regionRepository); + when(regionRepository.fromPosition(objectPosition)).thenReturn(region); + when(player.getPosition()).thenReturn(playerPosition); + when(region.getEntities(objectPosition, EntityType.STATIC_OBJECT, EntityType.DYNAMIC_OBJECT)) + .thenReturn(entitySet); + + ItemOnObjectMessage itemOnObjectMessage = new ItemOnObjectMessage(SynchronizationInventoryListener.INVENTORY_ID, 4151, 1, + 1, objectPosition.getX(), objectPosition.getY()); + ItemOnObjectVerificationHandler itemOnObjectVerificationHandler = new ItemOnObjectVerificationHandler(world); + + itemOnObjectVerificationHandler.handle(player, itemOnObjectMessage); + + assertTrue("ObjectVerificationHandler: message not terminated when object out of range!", itemOnObjectMessage.terminated()); + } +} \ No newline at end of file From 80be2691ca7ac48712748a79a148b991889bc245 Mon Sep 17 00:00:00 2001 From: Gary Tierney Date: Sun, 13 Sep 2015 16:06:20 +0100 Subject: [PATCH 10/10] Remove ItemVerificationHandler test to account for #32 --- .../ItemOnItemVerificationHandlerTest.java | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java b/game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java index 31389266..34d03c35 100644 --- a/game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java +++ b/game/src/test/org/apollo/game/message/handler/ItemOnItemVerificationHandlerTest.java @@ -24,7 +24,6 @@ public class ItemOnItemVerificationHandlerTest { private World world = new World(); private ItemOnItemVerificationHandler itemOnItemVerificationHandler = new ItemOnItemVerificationHandler(world); - private ItemVerificationHandler itemVerificationHandler = new ItemVerificationHandler(world); @Before public void setupTestItemDefinitions() { @@ -32,23 +31,6 @@ public class ItemOnItemVerificationHandlerTest { when(ItemDefinition.lookup(4151)).thenReturn(new ItemDefinition(4151)); } - @Test - public void testTerminateWithNoSourceItem() throws Exception { - Player player = mock(Player.class); - Inventory inventory = new Inventory(28); - inventory.set(1, new Item(4151, 1)); - - when(player.getInventory()).thenReturn(inventory); - - ItemOnItemMessage itemOnItemMessage = new ItemOnItemMessage(BankConstants.SIDEBAR_INVENTORY_ID, 500, 1, - BankConstants.SIDEBAR_INVENTORY_ID, 4151, 1); - - itemVerificationHandler.handle(player, itemOnItemMessage); - itemOnItemVerificationHandler.handle(player, itemOnItemMessage); - - assertTrue("ItemOnItemVerificationHandler: failed terminating message with invalid source item", itemOnItemMessage.terminated()); - } - @Test public void testTerminateWithNoTargetItem() throws Exception { Player player = mock(Player.class); @@ -60,7 +42,6 @@ public class ItemOnItemVerificationHandlerTest { ItemOnItemMessage itemOnItemMessage = new ItemOnItemMessage(BankConstants.SIDEBAR_INVENTORY_ID, 4151, 1, BankConstants.SIDEBAR_INVENTORY_ID, 4152, 2); - itemVerificationHandler.handle(player, itemOnItemMessage); itemOnItemVerificationHandler.handle(player, itemOnItemMessage); assertTrue("ItemOnItemVerificationHandler: failed terminating message with invalid target item", itemOnItemMessage.terminated());