Fix the test failing.

This commit is contained in:
graywolf336 2013-12-07 12:53:25 -06:00
parent b664428142
commit e2dc7de842
3 changed files with 316 additions and 1 deletions

View File

@ -43,7 +43,14 @@
<dependency> <dependency>
<groupId>org.bukkit</groupId> <groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId> <artifactId>bukkit</artifactId>
<version>1.6.2-R0.1-SNAPSHOT</version> <version>1.7.2-R0.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>craftbukkit</artifactId>
<version>1.7.2-R0.1-SNAPSHOT</version>
<type>jar</type> <type>jar</type>
</dependency> </dependency>

View File

@ -0,0 +1,300 @@
package test.java.com.graywolf336.jail.util;
import org.bukkit.Material;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
public class MockPlayerInventory implements PlayerInventory {
private int armorSize = 4, inventorySize = 36;
ItemStack[] armorContents = new ItemStack[armorSize];
ItemStack[] inventoryContents = new ItemStack[inventorySize];
@Override
public ItemStack[] getArmorContents() {
return armorContents;
}
@Override
public ItemStack getHelmet() {
return armorContents[0];
}
@Override
public ItemStack getChestplate() {
return armorContents[1];
}
@Override
public ItemStack getLeggings() {
return armorContents[2];
}
@Override
public ItemStack getBoots() {
return armorContents[3];
}
@Override
public void setArmorContents(ItemStack[] itemStacks) {
this.armorContents = itemStacks;
}
@Override
public void setHelmet(ItemStack itemStack) {
this.armorContents[0] = itemStack;
}
@Override
public void setChestplate(ItemStack itemStack) {
this.armorContents[1] = itemStack;
}
@Override
public void setLeggings(ItemStack itemStack) {
this.armorContents[2] = itemStack;
}
@Override
public void setBoots(ItemStack itemStack) {
this.armorContents[3] = itemStack;
}
@Override
public ItemStack getItemInHand() {
return null;
}
@Override
public void setItemInHand(ItemStack itemStack) {
}
@Override
public int getHeldItemSlot() {
return 0;
}
@Override
public int clear(int i, int i2) {
return 0;
}
@Override
public HumanEntity getHolder() {
return null;
}
@Override
public int getSize() {
return inventoryContents.length + armorContents.length;
}
@Override
public String getName() {
return null;
}
@Override
public ItemStack getItem(int i) {
if (i >= 0 && i < inventorySize) {
return inventoryContents[i];
} else if (i >= inventorySize
&& i < inventorySize + armorSize) {
return armorContents[i - inventorySize];
} else {
throw new ArrayIndexOutOfBoundsException();
}
}
@Override
public void setItem(int i, ItemStack itemStack) {
if (i >= 0 && i < inventorySize) {
inventoryContents[i] = itemStack;
} else if (i >= inventorySize
&& i < inventorySize + armorSize) {
armorContents[i - inventorySize] = itemStack;
} else {
throw new ArrayIndexOutOfBoundsException();
}
}
@Override
public HashMap<Integer, ItemStack> addItem(ItemStack... itemStacks) {
return null;
}
@Override
public HashMap<Integer, ItemStack> removeItem(ItemStack... itemStacks) {
return null;
}
@Override
public ItemStack[] getContents() {
return this.inventoryContents;
}
@Override
public void setContents(ItemStack[] itemStacks) {
this.inventoryContents = itemStacks;
}
@Override
public boolean contains(int i) {
return false;
}
@Override
public boolean contains(Material material) {
return false;
}
@Override
public boolean contains(ItemStack itemStack) {
return false;
}
@Override
public boolean contains(int i, int i1) {
return false;
}
@Override
public boolean contains(Material material, int i) {
return false;
}
@Override
public boolean contains(ItemStack itemStack, int i) {
return false;
}
@Override
public HashMap<Integer, ? extends ItemStack> all(int i) {
return null;
}
@Override
public HashMap<Integer, ? extends ItemStack> all(Material material) {
return null;
}
@Override
public HashMap<Integer, ? extends ItemStack> all(ItemStack itemStack) {
return null;
}
@Override
public int first(int i) {
return 0;
}
@Override
public int first(Material material) {
return 0;
}
@Override
public int first(ItemStack itemStack) {
return 0;
}
@Override
public int firstEmpty() {
return 0;
}
@Override
public void remove(int i) {
}
@Override
public void remove(Material material) {
}
@Override
public void remove(ItemStack itemStack) {
}
@Override
public void clear(int i) {
inventoryContents[i] = null;
}
@Override
public void clear() {
}
@Override
public List<HumanEntity> getViewers() {
return null;
}
@Override
public String getTitle() {
return null;
}
@Override
public InventoryType getType() {
return null;
}
@Override
public ListIterator<ItemStack> iterator() {
return null;
}
@Override
public int getMaxStackSize() {
return 0;
}
@Override
public void setMaxStackSize(int i) {
}
@Override
public ListIterator<ItemStack> iterator(int i) {
return null;
}
@Override
public boolean containsAtLeast(final ItemStack itemStack, final int i) {
return false;
}
private static Map<String, Object> makeMap(ItemStack[] items) {
Map<String, Object> contents = new LinkedHashMap<String, Object>(
items.length);
for (int i = 0; i < items.length; i++) {
if (items[i] != null && items[i].getTypeId() != 0) {
contents.put(Integer.valueOf(i).toString(), items[i]);
}
}
return contents;
}
public String toString() {
return "{\"inventoryContents\":" + makeMap(getContents())
+ ",\"armorContents\":" + makeMap(getArmorContents()) + "}";
}
@Override
public void setHeldItemSlot(int slot) {
}
}

View File

@ -14,6 +14,7 @@ import org.bukkit.Server;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.WorldCreator; import org.bukkit.WorldCreator;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.v1_7_R1.inventory.CraftItemFactory;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission; import org.bukkit.permissions.Permission;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
@ -22,18 +23,23 @@ import org.bukkit.plugin.PluginLogger;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import org.junit.Assert; import org.junit.Assert;
import org.mockito.Matchers; import org.mockito.Matchers;
import org.mockito.invocation.InvocationOnMock; import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito; import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.MockGateway; import org.powermock.core.MockGateway;
import org.powermock.core.classloader.annotations.PrepareForTest;
import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import com.graywolf336.jail.JailMain; import com.graywolf336.jail.JailMain;
@PrepareForTest({ CraftItemFactory.class })
public class TestInstanceCreator { public class TestInstanceCreator {
private JailMain main; private JailMain main;
private Server mockServer; private Server mockServer;
@ -59,6 +65,7 @@ public class TestInstanceCreator {
Logger.getLogger("Minecraft").setParent(Util.logger); Logger.getLogger("Minecraft").setParent(Util.logger);
when(mockServer.getLogger()).thenReturn(Util.logger); when(mockServer.getLogger()).thenReturn(Util.logger);
when(mockServer.getWorldContainer()).thenReturn(worldsDirectory); when(mockServer.getWorldContainer()).thenReturn(worldsDirectory);
when(mockServer.getItemFactory()).thenReturn(CraftItemFactory.instance());
main = PowerMockito.spy(new JailMain()); main = PowerMockito.spy(new JailMain());
@ -208,6 +215,7 @@ public class TestInstanceCreator {
when(mockPlayer.hasPermission(anyString())).thenReturn(true); when(mockPlayer.hasPermission(anyString())).thenReturn(true);
when(mockPlayer.hasPermission(Matchers.isA(Permission.class))).thenReturn(true); when(mockPlayer.hasPermission(Matchers.isA(Permission.class))).thenReturn(true);
when(mockPlayer.isOp()).thenReturn(true); when(mockPlayer.isOp()).thenReturn(true);
when(mockPlayer.getInventory()).thenReturn(new MockPlayerInventory());
// Init our second command sender, but this time is an instance of a player // Init our second command sender, but this time is an instance of a player
mockPlayerSender = (CommandSender) mockPlayer; mockPlayerSender = (CommandSender) mockPlayer;