Reimplement a bunch more tests

This commit is contained in:
Dan Mulloy 2015-04-06 17:30:01 -04:00
parent 0958c042c9
commit a2c9240011
8 changed files with 176 additions and 63 deletions

View File

@ -394,6 +394,22 @@ public class FieldUtils {
writeStaticField(field, value);
}
public static void writeStaticFinalField(Class<?> clazz, String fieldName, Object value, boolean forceAccess) throws Exception {
Field field = getField(clazz, fieldName, forceAccess);
if (field == null) {
throw new IllegalArgumentException("Cannot locate field " + fieldName + " in " + clazz);
}
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.setAccessible(true);
field.set(null, value);
}
/**
* Write an accessible field.
*

View File

@ -726,6 +726,7 @@ public class BukkitConverters {
requireModifier(Modifier.STATIC).
build();
FuzzyMethodContract getBlockContract = FuzzyMethodContract.newBuilder().
returnTypeExact(block).
parameterExactArray(int.class).
requireModifier(Modifier.STATIC).
build();

View File

@ -1,8 +1,19 @@
package com.comphenix.protocol;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import net.minecraft.server.v1_8_R2.DispenserRegistry;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.meta.ItemMeta;
import com.comphenix.protocol.reflect.FieldUtils;
import com.comphenix.protocol.utility.Constants;
import com.comphenix.protocol.utility.MinecraftReflection;
import com.comphenix.protocol.utility.MinecraftVersion;
import com.comphenix.protocol.wrappers.ItemFactoryDelegate;
/**
* Used to ensure that ProtocolLib and Bukkit is prepared to be tested.
@ -10,54 +21,42 @@ import com.comphenix.protocol.utility.MinecraftVersion;
* @author Kristian
*/
public class BukkitInitialization {
// private static boolean initialized;
private static boolean initialized;
/**
* Initialize Bukkit and ProtocolLib such that we can perfrom unit testing.
* @throws IllegalAccessException If we are unable to initialize Bukkit.
*/
public static void initializeItemMeta() throws IllegalAccessException {
/* None of this works in 1.8
// None of this works in 1.8
if (!initialized) {
// Denote that we're done
initialized = true;
initializePackage();
// "Accessed X before bootstrap!
try {
Block.S(); // Block.register()
} catch (Throwable ex) {
System.err.println("Failed to register blocks: " + ex);
}
try {
Item.t(); // Item.register()
} catch (Throwable ex) {
System.err.println("Failed to register items: " + ex);
}
try {
StatisticList.a(); // StatisticList.register()
} catch (Throwable ex) {
System.err.println("Failed to register statistics: " + ex);
}
DispenserRegistry.c(); // Basically registers everything
// Mock the server object
Server mockedServer = mock(Server.class);
ItemFactory mockedFactory = mock(CraftItemFactory.class);
ItemMeta mockedMeta = mock(ItemMeta.class);
ItemFactory mockedFactory = new ItemFactoryDelegate(mockedMeta);
when(mockedServer.getItemFactory()).thenReturn(mockedFactory);
when(mockedServer.isPrimaryThread()).thenReturn(true);
when(mockedFactory.getItemMeta(any(Material.class))).thenReturn(mockedMeta);
// when(mockedFactory.getItemMeta(any(Material.class))).thenReturn(mockedMeta);
// Inject this fake server
FieldUtils.writeStaticField(Bukkit.class, "server", mockedServer, true);
// And the fake item factory
FieldUtils.writeStaticField(CraftItemFactory.class, "instance", mockedFactory, true);
} */
// TODO Figure this out
/* try {
FieldUtils.writeStaticFinalField(CraftItemFactory.class, "instance", mockedFactory, true);
} catch (Exception ex) {
System.err.println("Failed to inject fake item factory: ");
ex.printStackTrace();
} */
}
}
/**

View File

@ -18,14 +18,20 @@ package com.comphenix.protocol.events;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.lang.reflect.Array;
import java.util.List;
import java.util.UUID;
import org.apache.commons.lang.SerializationUtils;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.WorldType;
import org.bukkit.inventory.ItemStack;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -34,12 +40,18 @@ import org.powermock.core.classloader.annotations.PowerMockIgnore;
import com.comphenix.protocol.BukkitInitialization;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.PacketType.Sender;
import com.comphenix.protocol.reflect.EquivalentConverter;
import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.utility.MinecraftReflection;
import com.comphenix.protocol.utility.Util;
import com.comphenix.protocol.wrappers.BukkitConverters;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import com.comphenix.protocol.wrappers.WrappedGameProfile;
import com.comphenix.protocol.wrappers.WrappedWatchableObject;
import com.comphenix.protocol.wrappers.nbt.NbtCompound;
import com.comphenix.protocol.wrappers.nbt.NbtFactory;
import com.google.common.base.Objects;
// Ensure that the CraftItemFactory is mockable
@RunWith(org.powermock.modules.junit4.PowerMockRunner.class)
@ -47,8 +59,8 @@ import com.comphenix.protocol.wrappers.WrappedWatchableObject;
//@PrepareForTest(CraftItemFactory.class)
public class PacketContainerTest {
// Helper converters
// private EquivalentConverter<WrappedDataWatcher> watchConvert = BukkitConverters.getDataWatcherConverter();
// private EquivalentConverter<ItemStack> itemConvert = BukkitConverters.getItemStackConverter();
private EquivalentConverter<WrappedDataWatcher> watchConvert = BukkitConverters.getDataWatcherConverter();
private EquivalentConverter<ItemStack> itemConvert = BukkitConverters.getItemStackConverter();
@BeforeClass
public static void initializeBukkit() throws IllegalAccessException {
@ -139,6 +151,7 @@ public class PacketContainerTest {
testPrimitive(explosion.getStrings(), 0, null, "hello");
}
// TODO Rewrite with chat components
/* @Test
public void testGetStringArrays() {
PacketContainer explosion = new PacketContainer(PacketType.Play.Server.UPDATE_SIGN);
@ -161,7 +174,7 @@ public class PacketContainerTest {
assertArrayEquals(testArray, integers.read(0));
}
/* @Test
@Test
public void testGetItemModifier() {
PacketContainer windowClick = new PacketContainer(PacketType.Play.Client.WINDOW_CLICK);
@ -174,9 +187,9 @@ public class PacketContainerTest {
// Insert the goldaxe and check if it's there
items.write(0, goldAxe);
assertTrue("Item " + goldAxe + " != " + items.read(0), equivalentItem(goldAxe, items.read(0)));
} */
}
/* @Test
@Test
public void testGetItemArrayModifier() {
PacketContainer windowItems = new PacketContainer(PacketType.Play.Server.WINDOW_ITEMS);
StructureModifier<ItemStack[]> itemAccess = windowItems.getItemArrayModifier();
@ -197,13 +210,10 @@ public class PacketContainerTest {
// Check that it is equivalent
for (int i = 0; i < itemArray.length; i++) {
System.out.println(i);
ItemStack element = itemArray[i];
System.out.println(element);
ItemStack original = comparison[i];
System.out.println(original);
ItemStack original = itemArray[i];
ItemStack written = comparison[i];
assertTrue(String.format("Array element %s is not the same: %s != %s", i, element, original), equivalentItem(element, original));
assertTrue(String.format("Array element %s is not the same: %s != %s", i, original, written), equivalentItem(original, written));
}
}
@ -215,7 +225,7 @@ public class PacketContainerTest {
} else {
return first.getType().equals(second.getType());
}
} */
}
@Test
public void testGetWorldTypeModifier() {
@ -235,7 +245,7 @@ public class PacketContainerTest {
assertEquals(testValue, worldAccess.read(0));
}
/* @Test
@Test
public void testGetNbtModifier() {
PacketContainer updateTileEntity = new PacketContainer(PacketType.Play.Server.TILE_ENTITY_DATA);
@ -249,7 +259,7 @@ public class PacketContainerTest {
assertEquals(compound.getString("test"), result.getString("test"));
assertEquals(compound.getList("ages"), result.getList("ages"));
} */
}
@Test
public void testGetDataWatcherModifier() {
@ -345,6 +355,7 @@ public class PacketContainerTest {
assertEquals("Test", copy.getStrings().read(0));
}
// TODO Deal with inner classes
/* @Test
public void testAttributeList() {
PacketContainer attribute = new PacketContainer(PacketType.Play.Server.UPDATE_ATTRIBUTES);
@ -361,6 +372,7 @@ public class PacketContainerTest {
public AttributeSnapshot attributeSnapshot(String string, double d, Collection<AttributeModifier> coll) {
return new AttributeSnapshot(string, d, coll);
}
};
AttributeSnapshot snapshot = new PacketAccessor().attributeSnapshot("generic.Maxhealth", 20.0, modifiers);
@ -372,7 +384,7 @@ public class PacketContainerTest {
assertEquals(
ToStringBuilder.reflectionToString(snapshot, ToStringStyle.SHORT_PREFIX_STYLE),
ToStringBuilder.reflectionToString(clonedSnapshot, ToStringStyle.SHORT_PREFIX_STYLE));
}
} */
@Test
public void testBlocks() {
@ -380,8 +392,9 @@ public class PacketContainerTest {
blockAction.getBlocks().write(0, Material.STONE);
assertEquals(Material.STONE, blockAction.getBlocks().read(0));
} */
}
// TODO Rewrite with MobEffects
/* @Test
@SuppressWarnings("deprecation")
public void testPotionEffect() {
@ -398,13 +411,15 @@ public class PacketContainerTest {
assertEquals(effect.getDuration(), (short) packet.getShorts().read(0));
} */
/* Fails due to weird logger configuration stuff
private static final List<PacketType> BLACKLISTED = Util.asList(
PacketType.Play.Client.CUSTOM_PAYLOAD, PacketType.Play.Server.CUSTOM_PAYLOAD, PacketType.Play.Server.MAP_CHUNK
);
@Test
public void testDeepClone() {
// Try constructing all the packets
for (PacketType type : PacketType.values()) {
if (type == PacketType.Play.Client.CUSTOM_PAYLOAD) {
// TODO Handle data serializers
if (BLACKLISTED.contains(type)) {
continue;
}
@ -452,7 +467,7 @@ public class PacketContainerTest {
throw new RuntimeException("Failed to serialize packet " + type, e);
}
}
} */
}
@Test
public void testPacketType() {
@ -460,7 +475,7 @@ public class PacketContainerTest {
}
// Convert to objects that support equals()
/* private void testEquality(Object a, Object b) {
private void testEquality(Object a, Object b) {
if (a != null && b != null) {
if (MinecraftReflection.isDataWatcher(a)) {
a = watchConvert.getSpecific(a);
@ -482,14 +497,14 @@ public class PacketContainerTest {
}
assertEquals(a, b);
} */
}
/**
* Get the underlying array as an object array.
* @param val - array wrapped as an Object.
* @return An object array.
*/
/* private Object[] getArray(Object val) {
private Object[] getArray(Object val) {
if (val instanceof Object[])
return (Object[]) val;
if (val == null)
@ -501,5 +516,5 @@ public class PacketContainerTest {
for (int i = 0; i < arrlength; ++i)
outputArray[i] = Array.get(val, i);
return outputArray;
} */
}
}

View File

@ -5,7 +5,11 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import net.minecraft.server.v1_8_R2.ChatComponentText;
import net.minecraft.server.v1_8_R2.ChunkCoordIntPair;
import net.minecraft.server.v1_8_R2.IChatBaseComponent;
import net.minecraft.server.v1_8_R2.ServerPing;
import net.minecraft.server.v1_8_R2.ServerPing.ServerData;
import net.minecraft.server.v1_8_R2.ServerPing.ServerPingPlayerSample;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
@ -78,7 +82,7 @@ public class MinecraftReflectionTest {
/* @Test
public void testChatSerializer() {
assertEquals(ChatSerializer.class, MinecraftReflection.getChatSerializerClass());
}
} */
@Test
public void testServerPing() {
@ -100,7 +104,7 @@ public class MinecraftReflectionTest {
assertEquals(ChunkCoordIntPair.class, MinecraftReflection.getChunkCoordIntPair());
}
@Test
/* @Test
public void testWatchableObject() {
assertEquals(WatchableObject.class, MinecraftReflection.getWatchableObjectClass());
} */

View File

@ -10,6 +10,8 @@ import java.io.IOException;
import net.minecraft.server.v1_8_R2.IntHashMap;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -31,7 +33,7 @@ public class StreamSerializerTest {
assertEquals(IntHashMap.class, MinecraftReflection.getIntHashMapClass());
}
/* @Test
@Test
public void testSerializer() throws IOException {
ItemStack before = new ItemStack(Material.GOLD_AXE);
@ -41,7 +43,7 @@ public class StreamSerializerTest {
assertEquals(before.getType(), after.getType());
assertEquals(before.getAmount(), after.getAmount());
} */
}
@Test
public void testStrings() throws IOException {
@ -59,8 +61,8 @@ public class StreamSerializerTest {
assertEquals(initial, deserialized);
}
/* TODO This is actually an issue
@Test
// TODO This is an actual issue
/* @Test
public void testCompound() throws IOException {
StreamSerializer serializer = new StreamSerializer();
NbtCompound initial = NbtFactory.ofCompound("tag");

View File

@ -0,0 +1,73 @@
/**
* 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_R2.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);
}
}

View File

@ -1,28 +1,31 @@
package com.comphenix.protocol.wrappers;
import org.bukkit.craftbukkit.v1_8_R2.inventory.CraftItemFactory;
import static org.junit.Assert.assertEquals;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import com.comphenix.protocol.BukkitInitialization;
@RunWith(org.powermock.modules.junit4.PowerMockRunner.class)
@PowerMockIgnore({ "org.apache.log4j.*", "org.apache.logging.*", "org.bukkit.craftbukkit.libs.jline.*" })
@PrepareForTest(CraftItemFactory.class)
//@PrepareForTest(CraftItemFactory.class)
public class WrappedWatchableObjectTest {
@BeforeClass
public static void initializeBukkit() throws IllegalAccessException {
BukkitInitialization.initializeItemMeta();
}
/* @Test
@Test
public void testItemStack() {
final ItemStack stack = new ItemStack(Material.GOLD_AXE);
final WrappedWatchableObject test = new WrappedWatchableObject(0, stack);
ItemStack value = (ItemStack) test.getValue();
assertEquals(value.getType(), stack.getType());
} */
}
}