Skip to content

Commit

Permalink
Added MainGeneratorListener test class.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Nov 4, 2019
1 parent 46256c6 commit f197571
Show file tree
Hide file tree
Showing 2 changed files with 272 additions and 4 deletions.
9 changes: 5 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<powermock.version>1.7.4</powermock.version>
<powermock.version>2.0.2</powermock.version>
<!-- More visible way how to change dependency versions -->
<spigot.version>1.14.4-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>1.7.0</bentobox.version>
Expand Down Expand Up @@ -143,10 +143,11 @@
<version>${spigot.version}</version>
<scope>provided</scope>
</dependency>
<!-- Mockito (Unit testing) -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<artifactId>mockito-core</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -157,7 +158,7 @@
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
package world.bentobox.magiccobblestonegenerator.listeners;

import static org.junit.Assert.*;
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.event.block.BlockFromToEvent;
import org.bukkit.scheduler.BukkitScheduler;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.magiccobblestonegenerator.StoneGeneratorAddon;
import world.bentobox.magiccobblestonegenerator.StoneGeneratorManager;
import world.bentobox.magiccobblestonegenerator.tasks.MagicGenerator;

/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class})
public class MainGeneratorListenerTest {

@Mock
private BentoBox plugin;
@Mock
private StoneGeneratorAddon addon;

private MainGeneratorListener mgl;
@Mock
private Block block;
@Mock
private Block block2;
@Mock
private StoneGeneratorManager sgm;
@Mock
private World world;
@Mock
private Location location;
@Mock
private Location location2;
@Mock
private MagicGenerator mg;
@Mock
private BukkitScheduler scheduler;
@Mock
private Block block3;

/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
when(addon.getPlugin()).thenReturn(plugin);

// Bukkit
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getScheduler()).thenReturn(scheduler);

// Stone Generator Manager
when(addon.getManager()).thenReturn(sgm);
when(sgm.canOperateInWorld(any())).thenReturn(true);
when(sgm.isMembersOnline(any())).thenReturn(true);

// Blocks
when(block.getWorld()).thenReturn(world);
when(block2.getWorld()).thenReturn(world);
when(block.getLocation()).thenReturn(location);
when(block2.getLocation()).thenReturn(location2);
when(location.getWorld()).thenReturn(world);
when(location2.getWorld()).thenReturn(world);
when(block.getType()).thenReturn(Material.LAVA);
when(block2.getType()).thenReturn(Material.WATER);
when(block2.getRelative(any())).thenReturn(block3);
when(block3.getType()).thenReturn(Material.LAVA);

// Generator
when(addon.getGenerator()).thenReturn(mg);
when(mg.isReplacementGenerated(any())).thenReturn(true);
when(mg.isReplacementGenerated(any(), anyBoolean())).thenReturn(true);

mgl = new MainGeneratorListener(addon);
}

/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}

/**
* Test method for {@link world.bentobox.magiccobblestonegenerator.listeners.MainGeneratorListener#onBlockFromToEvent(org.bukkit.event.block.BlockFromToEvent)}.
*/
@Test
public void testOnBlockFromToEventSuccess() {
BlockFromToEvent event = new BlockFromToEvent(block, block2);
mgl.onBlockFromToEvent(event);
assertTrue(event.isCancelled());
verify(scheduler).runTask(eq(plugin), any(Runnable.class));
}

/**
* Test method for {@link world.bentobox.magiccobblestonegenerator.listeners.MainGeneratorListener#onBlockFromToEvent(org.bukkit.event.block.BlockFromToEvent)}.
*/
@Test
public void testOnBlockFromToEventSuccessAir() {
when(block2.getType()).thenReturn(Material.AIR);
BlockFromToEvent event = new BlockFromToEvent(block, block2);
mgl.onBlockFromToEvent(event);
assertFalse(event.isCancelled());
verify(scheduler, never()).runTask(eq(plugin), any(Runnable.class));
}

/**
* Test method for {@link world.bentobox.magiccobblestonegenerator.listeners.MainGeneratorListener#onBlockFromToEvent(org.bukkit.event.block.BlockFromToEvent)}.
*/
@Test
public void testOnBlockFromToEventSuccessVoidAir() {
when(block2.getType()).thenReturn(Material.VOID_AIR);
BlockFromToEvent event = new BlockFromToEvent(block, block2);
mgl.onBlockFromToEvent(event);
assertFalse(event.isCancelled());
verify(scheduler, never()).runTask(eq(plugin), any(Runnable.class));
}

/**
* Test method for {@link world.bentobox.magiccobblestonegenerator.listeners.MainGeneratorListener#onBlockFromToEvent(org.bukkit.event.block.BlockFromToEvent)}.
*/
@Test
public void testOnBlockFromToEventSuccessCaveAir() {
when(block2.getType()).thenReturn(Material.CAVE_AIR);
BlockFromToEvent event = new BlockFromToEvent(block, block2);
mgl.onBlockFromToEvent(event);
assertFalse(event.isCancelled());
verify(scheduler, never()).runTask(eq(plugin), any(Runnable.class));
}

/**
* Test method for {@link world.bentobox.magiccobblestonegenerator.listeners.MainGeneratorListener#onBlockFromToEvent(org.bukkit.event.block.BlockFromToEvent)}.
*/
@Test
public void testOnBlockFromToEventSuccessWaterAirLava() {
when(block.getType()).thenReturn(Material.WATER);
when(block2.getType()).thenReturn(Material.AIR);
BlockFromToEvent event = new BlockFromToEvent(block, block2);
mgl.onBlockFromToEvent(event);
assertTrue(event.isCancelled());
verify(scheduler).runTask(eq(plugin), any(Runnable.class));
}

/**
* Test method for {@link world.bentobox.magiccobblestonegenerator.listeners.MainGeneratorListener#onBlockFromToEvent(org.bukkit.event.block.BlockFromToEvent)}.
*/
@Test
public void testOnBlockFromToEventSuccessWaterVoidAirLava() {
when(block.getType()).thenReturn(Material.WATER);
when(block2.getType()).thenReturn(Material.VOID_AIR);
BlockFromToEvent event = new BlockFromToEvent(block, block2);
mgl.onBlockFromToEvent(event);
assertTrue(event.isCancelled());
verify(scheduler).runTask(eq(plugin), any(Runnable.class));
}

/**
* Test method for {@link world.bentobox.magiccobblestonegenerator.listeners.MainGeneratorListener#onBlockFromToEvent(org.bukkit.event.block.BlockFromToEvent)}.
*/
@Test
public void testOnBlockFromToEventSuccessWaterCaveAirLava() {
when(block.getType()).thenReturn(Material.WATER);
when(block2.getType()).thenReturn(Material.CAVE_AIR);
BlockFromToEvent event = new BlockFromToEvent(block, block2);
mgl.onBlockFromToEvent(event);
assertTrue(event.isCancelled());
verify(scheduler).runTask(eq(plugin), any(Runnable.class));
}

/**
* Test method for {@link world.bentobox.magiccobblestonegenerator.listeners.MainGeneratorListener#onBlockFromToEvent(org.bukkit.event.block.BlockFromToEvent)}.
*/
@Test
public void testOnBlockFromToEventNotInWorld() {
when(sgm.canOperateInWorld(any())).thenReturn(false);
BlockFromToEvent event = new BlockFromToEvent(block, block2);
mgl.onBlockFromToEvent(event);
assertFalse(event.isCancelled());
verify(scheduler, never()).runTask(eq(plugin), any(Runnable.class));
}

/**
* Test method for {@link world.bentobox.magiccobblestonegenerator.listeners.MainGeneratorListener#onBlockFromToEvent(org.bukkit.event.block.BlockFromToEvent)}.
*/
@Test
public void testOnBlockFromToEventNotOnline() {
when(sgm.isMembersOnline(any())).thenReturn(false);
BlockFromToEvent event = new BlockFromToEvent(block, block2);
mgl.onBlockFromToEvent(event);
assertFalse(event.isCancelled());
verify(scheduler, never()).runTask(eq(plugin), any(Runnable.class));
}

/**
* Test method for {@link world.bentobox.magiccobblestonegenerator.listeners.MainGeneratorListener#onBlockFromToEvent(org.bukkit.event.block.BlockFromToEvent)}.
*/
@Test
public void testOnBlockFromToEventSameBlock() {
BlockFromToEvent event = new BlockFromToEvent(block, block);
mgl.onBlockFromToEvent(event);
assertFalse(event.isCancelled());
verify(scheduler, never()).runTask(eq(plugin), any(Runnable.class));
}

/**
* Test method for {@link world.bentobox.magiccobblestonegenerator.listeners.MainGeneratorListener#onBlockFromToEvent(org.bukkit.event.block.BlockFromToEvent)}.
*/
@Test
public void testOnBlockFromToEventWrongType() {
when(block.getType()).thenReturn(Material.AIR);
BlockFromToEvent event = new BlockFromToEvent(block, block2);
mgl.onBlockFromToEvent(event);
assertFalse(event.isCancelled());
verify(scheduler, never()).runTask(eq(plugin), any(Runnable.class));
}

/**
* Test method for {@link world.bentobox.magiccobblestonegenerator.listeners.MainGeneratorListener#onBlockFromToEvent(org.bukkit.event.block.BlockFromToEvent)}.
*/
@Test
public void testOnBlockFromToEventSameType() {
when(block2.getType()).thenReturn(Material.LAVA);
BlockFromToEvent event = new BlockFromToEvent(block, block2);
mgl.onBlockFromToEvent(event);
assertFalse(event.isCancelled());
verify(scheduler, never()).runTask(eq(plugin), any(Runnable.class));
}

/**
* Test method for {@link world.bentobox.magiccobblestonegenerator.listeners.MainGeneratorListener#onBlockFromToEvent(org.bukkit.event.block.BlockFromToEvent)}.
*/
@Test
public void testOnBlockFromToEventLava2AirAboveAir() {
when(block2.getType()).thenReturn(Material.AIR);
when(block3.getType()).thenReturn(Material.AIR);
BlockFromToEvent event = new BlockFromToEvent(block, block2);
mgl.onBlockFromToEvent(event);
assertFalse(event.isCancelled());
verify(scheduler, never()).runTask(eq(plugin), any(Runnable.class));
}


}

0 comments on commit f197571

Please sign in to comment.