From b138332608f78abe21e6586c2f18c2dad22b9230 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Fri, 18 Jan 2013 15:36:42 +0100 Subject: [PATCH] Compile PL for 1.4.7 - though earlier versions will still work. --- ProtocolLib/.project | 4 +- ProtocolLib/pom.xml | 2 +- .../comphenix/protocol/ProtocolLibrary.java | 2 +- .../protocol/BukkitInitialization.java | 60 +++++++++++++++++++ .../protocol/events/PacketContainerTest.java | 29 +-------- .../injector/SortedCopyOnWriteArrayTest.java | 1 - .../wrappers/nbt/NbtCompoundTest.java | 7 +-- .../protocol/wrappers/nbt/NbtFactoryTest.java | 8 +-- .../io/NbtConfigurationSerializerTest.java | 7 +-- 9 files changed, 76 insertions(+), 44 deletions(-) create mode 100644 ProtocolLib/src/test/java/com/comphenix/protocol/BukkitInitialization.java diff --git a/ProtocolLib/.project b/ProtocolLib/.project index a1960c9e..d36fcbe8 100644 --- a/ProtocolLib/.project +++ b/ProtocolLib/.project @@ -11,12 +11,12 @@ - org.eclipse.m2e.core.maven2Builder + net.sourceforge.metrics.builder - net.sourceforge.metrics.builder + org.eclipse.m2e.core.maven2Builder diff --git a/ProtocolLib/pom.xml b/ProtocolLib/pom.xml index ac5b3cd1..9ef4c1aa 100644 --- a/ProtocolLib/pom.xml +++ b/ProtocolLib/pom.xml @@ -203,7 +203,7 @@ org.bukkit craftbukkit - 1.4.6-R0.1 + 1.4.7-R0.1 provided diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java b/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java index 1a64565d..4717e674 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java @@ -56,7 +56,7 @@ public class ProtocolLibrary extends JavaPlugin { /** * The maximum version ProtocolLib has been tested with, */ - private static final String MAXIMUM_MINECRAFT_VERSION = "1.4.6"; + private static final String MAXIMUM_MINECRAFT_VERSION = "1.4.7"; /** * The number of milliseconds per second. diff --git a/ProtocolLib/src/test/java/com/comphenix/protocol/BukkitInitialization.java b/ProtocolLib/src/test/java/com/comphenix/protocol/BukkitInitialization.java new file mode 100644 index 00000000..9fac9cac --- /dev/null +++ b/ProtocolLib/src/test/java/com/comphenix/protocol/BukkitInitialization.java @@ -0,0 +1,60 @@ +package com.comphenix.protocol; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +// Will have to be updated for every version though +import org.bukkit.craftbukkit.v1_4_R1.inventory.CraftItemFactory; + +import org.bukkit.Bukkit; +import org.bukkit.Material; +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.MinecraftReflection; + +/** + * Used to ensure that ProtocolLib and Bukkit is prepared to be tested. + * + * @author Kristian + */ +public class BukkitInitialization { + 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 { + if (!initialized) { + // Denote that we're done + initialized = true; + + initializePackage(); + + // Mock the server object + Server mockedServer = mock(Server.class); + ItemFactory mockedFactory = mock(CraftItemFactory.class); + ItemMeta mockedMeta = mock(ItemMeta.class); + + when(mockedServer.getItemFactory()).thenReturn(mockedFactory); + 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); + } + } + + /** + * Ensure that package names are correctly set up. + */ + public static void initializePackage() { + // Initialize reflection + MinecraftReflection.setMinecraftPackage("net.minecraft.server.v1_4_R1", "org.bukkit.craftbukkit.v1_4_R1"); + } +} diff --git a/ProtocolLib/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java b/ProtocolLib/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java index 8359c753..1b66511e 100644 --- a/ProtocolLib/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java +++ b/ProtocolLib/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java @@ -18,31 +18,23 @@ package com.comphenix.protocol.events; import static org.junit.Assert.*; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - import java.lang.reflect.Array; import java.util.List; // Will have to be updated for every version though -import org.bukkit.craftbukkit.v1_4_6.inventory.CraftItemFactory; +import org.bukkit.craftbukkit.v1_4_R1.inventory.CraftItemFactory; -import org.bukkit.Bukkit; import org.bukkit.Material; -import org.bukkit.Server; import org.bukkit.WorldType; -import org.bukkit.inventory.ItemFactory; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PrepareForTest; +import com.comphenix.protocol.BukkitInitialization; import com.comphenix.protocol.Packets; import com.comphenix.protocol.reflect.EquivalentConverter; -import com.comphenix.protocol.reflect.FieldUtils; import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.utility.MinecraftReflection; import com.comphenix.protocol.wrappers.BukkitConverters; @@ -65,22 +57,7 @@ public class PacketContainerTest { @BeforeClass public static void initializeBukkit() throws IllegalAccessException { - // Initialize reflection - MinecraftReflection.setMinecraftPackage("net.minecraft.server.v1_4_6", "org.bukkit.craftbukkit.v1_4_6"); - - // Mock the server object - Server mockedServer = mock(Server.class); - ItemFactory mockedFactory = mock(CraftItemFactory.class); - ItemMeta mockedMeta = mock(ItemMeta.class); - - when(mockedServer.getItemFactory()).thenReturn(mockedFactory); - 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); + BukkitInitialization.initializeItemMeta(); } private void testPrimitive(StructureModifier modifier, int index, T initialValue, T testValue) { diff --git a/ProtocolLib/src/test/java/com/comphenix/protocol/injector/SortedCopyOnWriteArrayTest.java b/ProtocolLib/src/test/java/com/comphenix/protocol/injector/SortedCopyOnWriteArrayTest.java index 57280526..6bc2cce1 100644 --- a/ProtocolLib/src/test/java/com/comphenix/protocol/injector/SortedCopyOnWriteArrayTest.java +++ b/ProtocolLib/src/test/java/com/comphenix/protocol/injector/SortedCopyOnWriteArrayTest.java @@ -30,7 +30,6 @@ import com.comphenix.protocol.events.ListenerPriority; import com.google.common.primitives.Ints; public class SortedCopyOnWriteArrayTest { - @Test public void testInsertion() { diff --git a/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/nbt/NbtCompoundTest.java b/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/nbt/NbtCompoundTest.java index bfeea39d..f512944e 100644 --- a/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/nbt/NbtCompoundTest.java +++ b/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/nbt/NbtCompoundTest.java @@ -21,13 +21,12 @@ import static org.junit.Assert.*; import org.junit.BeforeClass; import org.junit.Test; - -import com.comphenix.protocol.utility.MinecraftReflection; +import com.comphenix.protocol.BukkitInitialization; public class NbtCompoundTest { @BeforeClass - public static void setupBukkit() { - MinecraftReflection.setMinecraftPackage("net.minecraft.server.v1_4_6", "org.bukkit.craftbukkit.v1_4_6"); + public static void initializeBukkit() throws IllegalAccessException { + BukkitInitialization.initializePackage(); } @Test diff --git a/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/nbt/NbtFactoryTest.java b/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/nbt/NbtFactoryTest.java index c2504df0..cd85bf7d 100644 --- a/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/nbt/NbtFactoryTest.java +++ b/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/nbt/NbtFactoryTest.java @@ -28,15 +28,13 @@ import java.io.DataOutputStream; import org.junit.BeforeClass; import org.junit.Test; - -import com.comphenix.protocol.utility.MinecraftReflection; +import com.comphenix.protocol.BukkitInitialization; import com.comphenix.protocol.wrappers.nbt.io.NbtBinarySerializer; public class NbtFactoryTest { @BeforeClass - public static void initializeBukkit() { - // Initialize reflection - MinecraftReflection.setMinecraftPackage("net.minecraft.server.v1_4_6", "org.bukkit.craftbukkit.v1_4_6"); + public static void initializeBukkit() throws IllegalAccessException { + BukkitInitialization.initializePackage(); } @Test diff --git a/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/nbt/io/NbtConfigurationSerializerTest.java b/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/nbt/io/NbtConfigurationSerializerTest.java index 916875ab..3cc4f675 100644 --- a/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/nbt/io/NbtConfigurationSerializerTest.java +++ b/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/nbt/io/NbtConfigurationSerializerTest.java @@ -6,15 +6,14 @@ import org.bukkit.configuration.file.YamlConfiguration; import org.junit.BeforeClass; import org.junit.Test; -import com.comphenix.protocol.utility.MinecraftReflection; +import com.comphenix.protocol.BukkitInitialization; import com.comphenix.protocol.wrappers.nbt.NbtCompound; import com.comphenix.protocol.wrappers.nbt.NbtFactory; public class NbtConfigurationSerializerTest { @BeforeClass - public static void initializeBukkit() { - // Initialize reflection - MinecraftReflection.setMinecraftPackage("net.minecraft.server.v1_4_6", "org.bukkit.craftbukkit.v1_4_6"); + public static void initializeBukkit() throws IllegalAccessException { + BukkitInitialization.initializePackage(); } @SuppressWarnings("unchecked")