ProtocolLib/ProtocolLib/src/test/java/com/comphenix/protocol/BukkitInitialization.java

70 lines
2.2 KiB
Java
Raw Normal View History

package com.comphenix.protocol;
2015-04-06 23:30:01 +02:00
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
2015-05-22 20:35:58 +02:00
import net.minecraft.server.v1_8_R3.DispenserRegistry;
2015-04-06 23:30:01 +02:00
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;
2014-11-29 04:08:46 +01:00
import com.comphenix.protocol.utility.Constants;
import com.comphenix.protocol.utility.MinecraftReflection;
import com.comphenix.protocol.utility.MinecraftVersion;
2015-04-06 23:30:01 +02:00
import com.comphenix.protocol.wrappers.ItemFactoryDelegate;
/**
* Used to ensure that ProtocolLib and Bukkit is prepared to be tested.
*
* @author Kristian
*/
public class BukkitInitialization {
2015-04-06 23:30:01 +02:00
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();
2015-04-06 23:30:01 +02:00
DispenserRegistry.c(); // Basically registers everything
// Mock the server object
Server mockedServer = mock(Server.class);
ItemMeta mockedMeta = mock(ItemMeta.class);
2015-04-06 23:30:01 +02:00
ItemFactory mockedFactory = new ItemFactoryDelegate(mockedMeta);
when(mockedServer.getItemFactory()).thenReturn(mockedFactory);
when(mockedServer.isPrimaryThread()).thenReturn(true);
2015-04-06 23:30:01 +02:00
// when(mockedFactory.getItemMeta(any(Material.class))).thenReturn(mockedMeta);
// Inject this fake server
FieldUtils.writeStaticField(Bukkit.class, "server", mockedServer, true);
2015-04-06 23:30:01 +02:00
// 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();
} */
}
}
/**
* Ensure that package names are correctly set up.
*/
public static void initializePackage() {
// Initialize reflection
2014-11-29 04:08:46 +01:00
MinecraftReflection.setMinecraftPackage(Constants.NMS, Constants.OBC);
MinecraftVersion.setCurrentVersion(MinecraftVersion.BOUNTIFUL_UPDATE);
}
}