Archiviert
13
0

Improve server mocking, test lore and material data

Dieser Commit ist enthalten in:
Dan Mulloy 2015-07-29 12:29:59 -04:00
Ursprung 8576b7a057
Commit 3166241397
4 geänderte Dateien mit 30 neuen und 91 gelöschten Zeilen

Datei anzeigen

@ -93,9 +93,9 @@ public class ProtocolLibrary extends JavaPlugin {
public static final String MAXIMUM_MINECRAFT_VERSION = "1.8.8"; public static final String MAXIMUM_MINECRAFT_VERSION = "1.8.8";
/** /**
* The date (with ISO 8601 or YYYY-MM-DD) when the most recent version (1.8.7) was released. * The date (with ISO 8601 or YYYY-MM-DD) when the most recent version (1.8.8) was released.
*/ */
public static final String MINECRAFT_LAST_RELEASE_DATE = "2015-06-05"; public static final String MINECRAFT_LAST_RELEASE_DATE = "2015-07-27";
// Different commands // Different commands
private enum ProtocolCommand { private enum ProtocolCommand {

Datei anzeigen

@ -2,19 +2,20 @@ package com.comphenix.protocol;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.util.logging.Logger;
import net.minecraft.server.v1_8_R3.DispenserRegistry; import net.minecraft.server.v1_8_R3.DispenserRegistry;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.craftbukkit.v1_8_R3.CraftServer; import org.bukkit.craftbukkit.v1_8_R3.CraftServer;
import org.bukkit.inventory.ItemFactory; import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemFactory;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.craftbukkit.v1_8_R3.util.Versioning;
import com.comphenix.protocol.reflect.FieldUtils;
import com.comphenix.protocol.utility.Constants; import com.comphenix.protocol.utility.Constants;
import com.comphenix.protocol.utility.MinecraftReflection; import com.comphenix.protocol.utility.MinecraftReflection;
import com.comphenix.protocol.utility.MinecraftVersion; import com.comphenix.protocol.utility.MinecraftVersion;
import com.comphenix.protocol.wrappers.ItemFactoryDelegate;
/** /**
* Used to ensure that ProtocolLib and Bukkit is prepared to be tested. * Used to ensure that ProtocolLib and Bukkit is prepared to be tested.
@ -37,15 +38,17 @@ public class BukkitInitialization {
// Mock the server object // Mock the server object
Server mockedServer = mock(Server.class); Server mockedServer = mock(Server.class);
ItemMeta mockedMeta = mock(ItemMeta.class);
ItemFactory mockedFactory = new ItemFactoryDelegate(mockedMeta);
when(mockedServer.getLogger()).thenReturn(Logger.getLogger("Minecraft"));
when(mockedServer.getName()).thenReturn("Mock Server");
when(mockedServer.getVersion()).thenReturn(CraftServer.class.getPackage().getImplementationVersion()); when(mockedServer.getVersion()).thenReturn(CraftServer.class.getPackage().getImplementationVersion());
when(mockedServer.getItemFactory()).thenReturn(mockedFactory); when(mockedServer.getBukkitVersion()).thenReturn(Versioning.getBukkitVersion());
when(mockedServer.getItemFactory()).thenReturn(CraftItemFactory.instance());
when(mockedServer.isPrimaryThread()).thenReturn(true); when(mockedServer.isPrimaryThread()).thenReturn(true);
// Inject this fake server // Inject this fake server
FieldUtils.writeStaticField(Bukkit.class, "server", mockedServer, true); Bukkit.setServer(mockedServer);
initializePackage(); initializePackage();
} }

Datei anzeigen

@ -18,7 +18,6 @@ package com.comphenix.protocol.events;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@ -35,9 +34,11 @@ import net.minecraft.server.v1_8_R3.PacketPlayOutUpdateAttributes.AttributeSnaps
import org.apache.commons.lang.SerializationUtils; import org.apache.commons.lang.SerializationUtils;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.DyeColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.WorldType; import org.bukkit.WorldType;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import org.junit.BeforeClass; import org.junit.BeforeClass;
@ -196,15 +197,23 @@ public class PacketContainerTest {
public void testGetItemModifier() { public void testGetItemModifier() {
PacketContainer windowClick = new PacketContainer(PacketType.Play.Client.WINDOW_CLICK); PacketContainer windowClick = new PacketContainer(PacketType.Play.Client.WINDOW_CLICK);
StructureModifier<ItemStack> items = windowClick.getItemModifier(); ItemStack item = itemWithData();
ItemStack goldAxe = new ItemStack(Material.GOLD_AXE);
assertNotNull(goldAxe.getType()); StructureModifier<ItemStack> items = windowClick.getItemModifier();
assertNull(items.read(0)); assertNull(items.read(0));
// Insert the goldaxe and check if it's there // Insert the item and check if it's there
items.write(0, goldAxe); items.write(0, item);
assertTrue("Item " + goldAxe + " != " + items.read(0), equivalentItem(goldAxe, items.read(0))); assertTrue("Item " + item + " != " + items.read(0), equivalentItem(item, items.read(0)));
}
private ItemStack itemWithData() {
ItemStack item = new ItemStack(Material.WOOL, 1, DyeColor.GREEN.getWoolData());
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(ChatColor.GREEN + "Green Wool");
meta.setLore(Util.asList(ChatColor.WHITE + "This is lore."));
item.setItemMeta(meta);
return item;
} }
@Test @Test
@ -213,7 +222,7 @@ public class PacketContainerTest {
StructureModifier<ItemStack[]> itemAccess = windowItems.getItemArrayModifier(); StructureModifier<ItemStack[]> itemAccess = windowItems.getItemArrayModifier();
ItemStack[] itemArray = new ItemStack[] { ItemStack[] itemArray = new ItemStack[] {
new ItemStack(Material.GOLD_AXE), itemWithData(),
new ItemStack(Material.DIAMOND_AXE) new ItemStack(Material.DIAMOND_AXE)
}; };

Datei anzeigen

@ -1,73 +0,0 @@
/**
* ProtocolLib - Bukkit server library that allows access to the Minecraft protocol.
* Copyright (C) 2015 dmulloy2
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*/
package com.comphenix.protocol.wrappers;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemFactory;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
/**
* @author dmulloy2
*/
public class ItemFactoryDelegate implements ItemFactory {
private final CraftItemFactory factory;
private final ItemMeta mocked;
public ItemFactoryDelegate(ItemMeta mocked) {
this.factory = CraftItemFactory.instance();
this.mocked = mocked;
}
@Override
public ItemMeta asMetaFor(ItemMeta meta, ItemStack stack) throws IllegalArgumentException {
return factory.asMetaFor(meta, stack);
}
@Override
public ItemMeta asMetaFor(ItemMeta meta, Material material) throws IllegalArgumentException {
return factory.asMetaFor(meta, material);
}
@Override
public boolean equals(ItemMeta meta1, ItemMeta meta2) throws IllegalArgumentException {
return factory.equals(meta1, meta2);
}
@Override
public Color getDefaultLeatherColor() {
return factory.getDefaultLeatherColor();
}
@Override
public ItemMeta getItemMeta(Material arg0) {
return mocked;
}
@Override
public boolean isApplicable(ItemMeta meta, ItemStack itemstack) throws IllegalArgumentException {
return factory.isApplicable(meta, itemstack);
}
@Override
public boolean isApplicable(ItemMeta meta, Material material) throws IllegalArgumentException {
return factory.isApplicable(meta, material);
}
}