Skip to content

Commit

Permalink
Add testCreateSpawnMessage
Browse files Browse the repository at this point in the history
Updates PowerMock because otherwise affected by
powermock/powermock#731
  • Loading branch information
Pr0methean committed Jan 31, 2018
1 parent f5acf96 commit aee504d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 22 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.6</version>
<version>1.7.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.6</version>
<version>1.7.3</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
26 changes: 22 additions & 4 deletions src/test/java/net/glowstone/entity/EntityTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package net.glowstone.entity;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Answers.RETURNS_SMART_NULLS;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.when;

import com.flowpowered.network.Message;
import java.io.IOException;
import java.util.Collections;
import java.util.function.Supplier;
import java.util.List;
import java.util.function.Function;
import java.util.logging.Logger;
import net.glowstone.GlowServer;
import net.glowstone.GlowWorld;
Expand All @@ -33,7 +37,7 @@
*/
@PrepareForTest({GlowWorld.class, GlowServer.class})
@RunWith(PowerMockRunner.class)
public abstract class EntityTest {
public abstract class EntityTest<T extends GlowEntity> {

// Mocks
protected final Logger log = Logger.getLogger(getClass().getSimpleName());
Expand All @@ -45,9 +49,14 @@ public abstract class EntityTest {
protected GlowChunk chunk;

// Real objects
protected Location origin;
protected Location location;
protected final EntityIdManager idManager = new EntityIdManager();
protected final EntityManager entityManager = new EntityManager();
protected final Function<Location, ? extends T> entityCreator;

protected EntityTest(Function<Location, ? extends T> entityCreator) {
this.entityCreator = entityCreator;
}

@Before
public void setUp() throws IOException {
Expand All @@ -56,7 +65,7 @@ public void setUp() throws IOException {
Bukkit.setServer(server);
}
MockitoAnnotations.initMocks(this);
origin = new Location(world, 0, 0, 0);
location = new Location(world, 0, 0, 0);
when(world.getServer()).thenReturn(server);
when(world.getEntityManager()).thenReturn(entityManager);
when(world.getChunkAt(any(Location.class))).thenReturn(chunk);
Expand All @@ -70,4 +79,13 @@ public void setUp() throws IOException {
when(itemFactory.ensureServerConversions(any(ItemStack.class)))
.thenAnswer(invocation -> invocation.getArguments()[0]);
}

@Test
public void testCreateSpawnMessage() {
T entity = entityCreator.apply(location);
List<Message> messages = entity.createSpawnMessage();
assertFalse(messages.isEmpty());
// Should start with an instance of one of the Spawn*Message classes
assertTrue(messages.get(0).getClass().getSimpleName().startsWith("Spawn"));
}
}
31 changes: 22 additions & 9 deletions src/test/java/net/glowstone/entity/GlowPlayerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import java.util.function.Supplier;
import net.glowstone.GlowServer;
import net.glowstone.GlowWorld;
import net.glowstone.block.GlowBlock;
Expand Down Expand Up @@ -39,16 +40,16 @@
import org.powermock.core.classloader.annotations.PrepareForTest;

@PrepareForTest({GlowServer.class, GlowWorld.class, ChunkManager.class})
public class GlowPlayerTest extends EntityTest {
public class GlowPlayerTest extends EntityTest<GlowPlayer> {

private final ChunkManager chunkManager
= PowerMockito.mock(ChunkManager.class, Mockito.RETURNS_MOCKS);

// Mockito mocks
@Mock(answer = RETURNS_SMART_NULLS)
private GlowSession session;
private static GlowSession session;
@Mock(answer = RETURNS_SMART_NULLS)
private PlayerReader reader;
private static PlayerReader reader;
@Mock(answer = RETURNS_SMART_NULLS)
private GlowBlock block;
@Mock(answer = RETURNS_SMART_NULLS)
Expand All @@ -62,7 +63,7 @@ public class GlowPlayerTest extends EntityTest {

// Real objects

private final GlowPlayerProfile profile
private static final GlowPlayerProfile profile
= new GlowPlayerProfile("TestPlayer", UUID.randomUUID());
private GlowScheduler scheduler;
private final SessionRegistry sessionRegistry = new SessionRegistry();
Expand All @@ -72,6 +73,10 @@ public class GlowPlayerTest extends EntityTest {
// Finally, the star of the show
private GlowPlayer player;

public GlowPlayerTest() {
super(ignoredLocation -> new GlowPlayer(session, profile, reader));
}

@Before
@Override
public void setUp() throws IOException {
Expand All @@ -85,29 +90,29 @@ public void setUp() throws IOException {
when(server.getScheduler()).thenReturn(scheduler);
when(server.getOpsList()).thenReturn(opsList);
when(server.getPlayerStatisticIoService()).thenReturn(statisticIoService);
when(world.getSpawnLocation()).thenReturn(origin);
when(world.getSpawnLocation()).thenReturn(location);
when(world.getBlockAt(anyInt(), anyInt(), anyInt())).thenReturn(block);
when(world.getBlockAt(any(Location.class))).thenReturn(block);
when(world.getChunkManager()).thenReturn(chunkManager);
when(world.newChunkLock(anyString())).thenReturn(chunkLock);
when(block.getType()).thenReturn(Material.AIR);
when(block.getRelative(any(BlockFace.class))).thenReturn(block);
player = new GlowPlayer(session, profile, reader);
player = entityCreator.apply(location);
player.setItemInHand(new ItemStack(Material.FISHING_ROD));
when(session.getPlayer()).thenReturn(player);
}

@Test
public void testFishingContinues() {
final GlowFishingHook fishingHook = new GlowFishingHook(origin);
final GlowFishingHook fishingHook = new GlowFishingHook(location);
player.setCurrentFishingHook(fishingHook);
player.pulse();
assertSame(fishingHook, player.getCurrentFishingHook());
}

@Test
public void testFishingStopsAtDistance() {
player.setCurrentFishingHook(new GlowFishingHook(origin));
player.setCurrentFishingHook(new GlowFishingHook(location));
player.teleport(new Location(world, 33, 0, 0));
player.endTeleport();
player.pulse();
Expand All @@ -116,9 +121,17 @@ public void testFishingStopsAtDistance() {

@Test
public void testFishingStopsWhenNoPoleHeld() {
player.setCurrentFishingHook(new GlowFishingHook(origin));
player.setCurrentFishingHook(new GlowFishingHook(location));
player.setItemInHand(InventoryUtil.createEmptyStack());
player.pulse();
assertNull(player.getCurrentFishingHook());
}

private static class PlayerSupplier implements Supplier<GlowPlayer> {

@Override
public GlowPlayer get() {
return new GlowPlayer(session, profile, reader);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.mockito.Mock;
import org.mockito.internal.matchers.GreaterThan;

public class GlowFishingHookTest extends EntityTest {
public class GlowFishingHookTest extends EntityTest<GlowFishingHook> {

private static final BaseMatcher<ItemStack> IS_NON_EMPTY_ITEM_STACK
= new BaseMatcher<ItemStack>() {
Expand All @@ -49,6 +49,10 @@ public void describeTo(Description description) {

private FishingRewardManager fishingRewardManager;

public GlowFishingHookTest() {
super(GlowFishingHook::new);
}

@Before
@Override
public void setUp() throws IOException {
Expand All @@ -58,33 +62,33 @@ public void setUp() throws IOException {
when(block.getType()).thenReturn(Material.WATER);
fishingRewardManager = new FishingRewardManager();
when(server.getFishingRewardManager()).thenReturn(fishingRewardManager);
when(player.getLocation()).thenReturn(origin);
when(player.getLocation()).thenReturn(location);
}

@Test
public void testReelInNotPlayer() {
GlowFishingHook hook = new GlowFishingHook(origin);
hook.setShooter(new GlowCreeper(origin));
GlowFishingHook hook = new GlowFishingHook(location);
hook.setShooter(new GlowCreeper(location));
hook.reelIn();
assertTrue(hook.isRemoved());
verify(world, never()).dropItemNaturally(any(Location.class), any(ItemStack.class));
}

@Test
public void testReelIn() {
GlowFishingHook hook = new GlowFishingHook(origin);
GlowFishingHook hook = new GlowFishingHook(location);
hook.setShooter(player);
hook.reelIn();
assertTrue(hook.isRemoved());
verify(player).giveExp(intThat(new GreaterThan<>(0)));
verify(world).dropItemNaturally(eq(origin),
verify(world).dropItemNaturally(eq(location),
argThat(IS_NON_EMPTY_ITEM_STACK));
}

@Test
public void testReelInNotInWater() {
when(block.getType()).thenReturn(Material.DIRT);
GlowFishingHook hook = new GlowFishingHook(origin);
GlowFishingHook hook = new GlowFishingHook(location);
hook.setShooter(player);
hook.reelIn();
assertTrue(hook.isRemoved());
Expand Down

0 comments on commit aee504d

Please sign in to comment.