Skip to content

Commit

Permalink
Reorganize some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spraetz committed Mar 15, 2014
1 parent bd6fffb commit 6469290
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 75 deletions.
53 changes: 53 additions & 0 deletions src/test/java/com/gmail/spraetz/BaseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.gmail.spraetz;

import com.gmail.spraetz.plugin.MineCraftSpells;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.junit.Before;
import org.junit.BeforeClass;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.MockGateway;
import org.powermock.core.classloader.annotations.PrepareForTest;

import java.io.File;

import static org.powermock.api.mockito.PowerMockito.when;

/**
* Created by spraetz on 3/15/14.
*/
@PrepareForTest({MineCraftSpells.class})
public abstract class BaseTest {

protected static FileConfiguration config;
protected MineCraftSpells plugin;
protected World world;
protected ItemStack spellbook;
protected Player player;
protected ItemMeta spellbookMeta;

@BeforeClass
public static void testClassSetup(){
File configFile = new File(new File("").getAbsolutePath() + "/src/main/resources/config.yml");
config = YamlConfiguration.loadConfiguration(configFile);
MockGateway.MOCK_STANDARD_METHODS = false;
}

@Before
public void testSetup(){
plugin = PowerMockito.mock(MineCraftSpells.class);
when(plugin.getConfig()).thenReturn(config);

world = PowerMockito.mock(World.class);
player = PowerMockito.mock(Player.class);
spellbook = PowerMockito.mock(ItemStack.class);
spellbookMeta = PowerMockito.mock(ItemMeta.class);
when(spellbook.getItemMeta()).thenReturn(spellbookMeta);
when(spellbook.getType()).thenReturn(Material.BOOK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,39 @@
import com.gmail.spraetz.listeners.CastSpellListener;
import com.gmail.spraetz.listeners.CastTouchSpellListener;
import com.gmail.spraetz.plugin.Analytics;
import com.gmail.spraetz.plugin.MineCraftSpells;
import com.gmail.spraetz.spells.Spell;
import com.gmail.spraetz.spells.Spellbook;
import com.gmail.spraetz.spells.TouchSpell;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Fireball;
import org.bukkit.entity.Player;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.MockGateway;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import static org.mockito.Matchers.*;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;

/**
* Created by spraetz on 3/2/14.
*/

@RunWith(PowerMockRunner.class)
@PrepareForTest({PlayerInteractEvent.class, MineCraftSpells.class})
public class CastSpellTests {

FileConfiguration config;

@Before
public void testClassSetup(){
File configFile = new File(new File("").getAbsolutePath() + "/src/main/resources/config.yml");
config = YamlConfiguration.loadConfiguration(configFile);
MockGateway.MOCK_STANDARD_METHODS = false;
}
@PrepareForTest({PlayerInteractEvent.class})
public class CastSpellTest extends BaseTest{

@Test
public void testCastSpell(){
Expand All @@ -67,6 +48,7 @@ public void testCastSpell(){
//Mock the analytics
Analytics analytics = PowerMockito.mock(Analytics.class);
doNothing().when(analytics).trackSpellCast(any(Player.class), anyString());
when(plugin.getAnalytics()).thenReturn(analytics);

//Mock the damn fireball
Fireball fireball = PowerMockito.mock(Fireball.class);
Expand All @@ -75,31 +57,19 @@ public void testCastSpell(){
Block block = PowerMockito.mock(Block.class);

//Mock the world
World world = PowerMockito.mock(World.class);
when(world.spawn(any(Location.class), any(Class.class))).thenReturn(fireball);
when(world.getBlockAt(anyInt(), anyInt(), anyInt())).thenReturn(block);

when(block.getLocation()).thenReturn(new Location(world, 0, 0, 0));
when(block.getType()).thenReturn(Material.STONE);

//Mock the plugin
MineCraftSpells plugin = PowerMockito.mock(MineCraftSpells.class);
when(plugin.getConfig()).thenReturn(config);
when(plugin.getAnalytics()).thenReturn(analytics);

//Mock item meta for spellbook
ItemMeta itemMeta = mock(ItemMeta.class);
when(itemMeta.getDisplayName()).thenReturn(spellName);
when(spellbookMeta.getDisplayName()).thenReturn(spellName);
ArrayList<String> loreList = new ArrayList<String>();
loreList.add(spellName + Spellbook.LORE_STRING_SEPARATOR + "64");
when(itemMeta.getLore()).thenReturn(loreList);

//Mock a spellbook
ItemStack spellbook = mock(ItemStack.class);
when(spellbook.getItemMeta()).thenReturn(itemMeta);
when(spellbookMeta.getLore()).thenReturn(loreList);

//Mock the player
Player player = mock(Player.class);
when(player.getItemInHand()).thenReturn(spellbook);
when(player.getWorld()).thenReturn(world);
when(player.getEyeLocation()).thenReturn(new Location(world, 0, 0, 0));
Expand Down Expand Up @@ -140,7 +110,7 @@ else if(TouchSpell.class.isAssignableFrom(c)){
//Verify that our spell was cast and our spellbook updated.
ArrayList<String> loreListToVerify = new ArrayList<String>();
loreListToVerify.add(spellName + Spellbook.LORE_STRING_SEPARATOR + "63");
verify(itemMeta).setLore(loreListToVerify);
verify(spellbookMeta).setLore(loreListToVerify);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,28 @@
import com.gmail.spraetz.commands.ChargeSpellbook;
import com.gmail.spraetz.plugin.MineCraftSpells;
import com.gmail.spraetz.spells.Spellbook;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.MockGateway;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.File;
import java.util.ArrayList;
import java.util.Set;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;

/**
* Created by spraetz on 3/14/14.
*/

@RunWith(PowerMockRunner.class)
@PrepareForTest({MineCraftSpells.class})
public class ChargeSpellbookTests {
FileConfiguration config;

@Before
public void testClassSetup(){
File configFile = new File(new File("").getAbsolutePath() + "/src/main/resources/config.yml");
config = YamlConfiguration.loadConfiguration(configFile);
MockGateway.MOCK_STANDARD_METHODS = false;
}
public class ChargeSpellbookTest extends BaseTest{

@Test
public void testChargeSpellbook(){
Expand All @@ -54,26 +33,17 @@ public void testChargeSpellbook(){
Integer EXISTING_CHARGES = 10;

//Find the list of all the spells:
assert config != null;
Set<String> spells = config.getConfigurationSection("spells").getKeys(false);

for(String spellName : spells){

MineCraftSpells plugin = PowerMockito.mock(MineCraftSpells.class);
when(plugin.getConfig()).thenReturn(config);
ChargeSpellbook chargeCommand = new ChargeSpellbook(plugin);

//Mock item meta for spellbook
ItemMeta itemMeta = mock(ItemMeta.class);
when(itemMeta.getDisplayName()).thenReturn(spellName);
when(spellbookMeta.getDisplayName()).thenReturn(spellName);
ArrayList<String> loreList = new ArrayList<String>();
loreList.add(spellName + Spellbook.LORE_STRING_SEPARATOR + EXISTING_CHARGES);
when(itemMeta.getLore()).thenReturn(loreList);

//Mock a spellbook
ItemStack spellbook = mock(ItemStack.class);
when(spellbook.getItemMeta()).thenReturn(itemMeta);
when(spellbook.getType()).thenReturn(Material.BOOK);
when(spellbookMeta.getLore()).thenReturn(loreList);

//Mock inventory contents
ItemStack[] items = getReagentsForInventory(spellName, CHARGES_TO_ADD, plugin);
Expand All @@ -83,7 +53,6 @@ public void testChargeSpellbook(){
when(playerInventory.getContents()).thenReturn(items);

//Mock a player
Player player = PowerMockito.mock(Player.class);
when(player.getItemInHand()).thenReturn(spellbook);
when(player.getInventory()).thenReturn(playerInventory);

Expand All @@ -102,7 +71,7 @@ public void testChargeSpellbook(){
//Verify we set the book's lore.
ArrayList<String> lore = new ArrayList<String>();
lore.add(spellName + Spellbook.LORE_STRING_SEPARATOR + (CHARGES_TO_ADD + EXISTING_CHARGES));
verify(itemMeta).setLore(lore);
verify(spellbookMeta).setLore(lore);
}
}

Expand Down
Binary file modified target/MineCraftSpells-2.0.jar
Binary file not shown.
Binary file modified target/original-MineCraftSpells-2.0.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<testsuite failures="0" time="1.421" errors="0" skipped="0" tests="1" name="com.gmail.spraetz.FirstTest">
<testsuite failures="0" time="0.994" errors="0" skipped="0" tests="1" name="com.gmail.spraetz.CastSpellTest">
<properties>
<property name="java.runtime.name" value="Java(TM) SE Runtime Environment"/>
<property name="sun.boot.library.path" value="/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Libraries"/>
Expand All @@ -22,7 +22,7 @@
<property name="basedir" value="/Users/spraetz/dev/MineCraftSpells"/>
<property name="java.endorsed.dirs" value="/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/endorsed"/>
<property name="os.arch" value="x86_64"/>
<property name="surefire.real.class.path" value="/var/folders/j0/jbh3hw_x671gvlwghd7bt25r0000gn/T/surefirebooter1633708850599561581.jar"/>
<property name="surefire.real.class.path" value="/var/folders/j0/jbh3hw_x671gvlwghd7bt25r0000gn/T/surefirebooter1048814891254791445.jar"/>
<property name="java.io.tmpdir" value="/var/folders/j0/jbh3hw_x671gvlwghd7bt25r0000gn/T/"/>
<property name="line.separator" value="
"/>
Expand All @@ -46,7 +46,7 @@
<property name="java.vm.specification.version" value="1.0"/>
<property name="sun.arch.data.model" value="64"/>
<property name="java.home" value="/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home"/>
<property name="sun.java.command" value="/var/folders/j0/jbh3hw_x671gvlwghd7bt25r0000gn/T/surefirebooter1633708850599561581.jar /var/folders/j0/jbh3hw_x671gvlwghd7bt25r0000gn/T/surefire5400758133374179897tmp /var/folders/j0/jbh3hw_x671gvlwghd7bt25r0000gn/T/surefire4500494774051547287tmp"/>
<property name="sun.java.command" value="/var/folders/j0/jbh3hw_x671gvlwghd7bt25r0000gn/T/surefirebooter1048814891254791445.jar /var/folders/j0/jbh3hw_x671gvlwghd7bt25r0000gn/T/surefire1286904475022533031tmp /var/folders/j0/jbh3hw_x671gvlwghd7bt25r0000gn/T/surefire7435075876109043375tmp"/>
<property name="java.specification.vendor" value="Sun Microsystems Inc."/>
<property name="user.language" value="en"/>
<property name="awt.toolkit" value="apple.awt.CToolkit"/>
Expand All @@ -65,5 +65,5 @@
<property name="ftp.nonProxyHosts" value="local|*.local|169.254/16|*.169.254/16"/>
<property name="sun.cpu.isalist" value=""/>
</properties>
<testcase time="0.414" classname="com.gmail.spraetz.FirstTest" name="testCastSpell"/>
<testcase time="0.41" classname="com.gmail.spraetz.CastSpellTest" name="testCastSpell"/>
</testsuite>
Loading

0 comments on commit 6469290

Please sign in to comment.