ChanceSD / PvPManager

Test files should contain tests JAVA-W1088
Anti-pattern
Minor
2 months ago6 months old
 24import org.mockito.ArgumentMatchers;
 25import org.mockito.Mockito;
 26
 27public class PluginTest { 28 29	private PvPManager plugin; 30	private Server server; 31	private String filePath; 32	private static PluginTest instance; 33	private Player attacker; 34	private Player defender; 35 36	public final void setup() throws Exception { 37		filePath = URLDecoder.decode(PluginTest.class.getClassLoader().getResource("").getPath(), "UTF-8"); 38		final String decoded = filePath + "TestServer/plugins/PvPManager"; 39		final File pluginDirectory = new File(decoded); 40		pluginDirectory.mkdirs(); 41		server = mock(Server.class, Mockito.RETURNS_MOCKS); 42		Mockito.when(server.getPluginManager()).thenReturn(mock(PluginManager.class)); 43		Mockito.when(server.getUpdateFolderFile()).thenReturn(new File(filePath + "TestServer/plugins/update")); 44		System.setProperty("java.util.logging.SimpleFormatter.format", "[%1$tT] [%4$-7s] %5$s %n"); 45		Mockito.when(server.getLogger()).thenReturn(Logger.getLogger("Minecraft")); 46		final World world = mock(World.class); 47		Mockito.when(world.getName()).thenReturn(""); 48		Mockito.when(server.getWorlds()).thenReturn(Arrays.asList(world)); 49		Bukkit.setServer(server); 50		plugin = Mockito.mock(PvPManager.class, Mockito.CALLS_REAL_METHODS); 51		final PluginDescriptionFile pdf = new PluginDescriptionFile(PluginTest.class.getClassLoader().getResource("plugin.yml").openStream()); 52		final Method method = JavaPlugin.class.getDeclaredMethod("init", PluginLoader.class, Server.class, PluginDescriptionFile.class, File.class, File.class, 53		        ClassLoader.class); 54		method.setAccessible(true); 55		method.invoke(plugin, (Object) null, server, pdf, pluginDirectory, new File(filePath), PluginTest.class.getClassLoader()); 56		Mockito.doReturn(mock(PluginCommand.class)).when(plugin).getCommand(ArgumentMatchers.anyString()); 57		plugin.onLoad(); 58		plugin.onEnable(); 59		setupPlayers(); 60	} 61 62	public final void setupPlayers() { 63		attacker = mock(Player.class, Mockito.RETURNS_MOCKS); 64		defender = mock(Player.class, Mockito.RETURNS_MOCKS); 65		when(attacker.hasPlayedBefore()).thenReturn(true); 66		when(defender.hasPlayedBefore()).thenReturn(true); 67		when(attacker.getName()).thenReturn("Attacker"); 68		when(defender.getName()).thenReturn("Defender"); 69		when(attacker.getUniqueId()).thenReturn(UUID.randomUUID()); 70		when(defender.getUniqueId()).thenReturn(UUID.randomUUID()); 71		when(attacker.getGameMode()).thenReturn(GameMode.SURVIVAL); 72		when(defender.getKiller()).thenReturn(attacker); 73	} 74 75	@NotNull 76	public final Player createPlayer(final String name) { 77		final Player player = mock(Player.class, Mockito.RETURNS_MOCKS); 78		when(player.hasPlayedBefore()).thenReturn(true); 79		when(player.getName()).thenReturn(name); 80		when(player.getUniqueId()).thenReturn(UUID.randomUUID()); 81		when(player.getGameMode()).thenReturn(GameMode.SURVIVAL); 82		return player; 83	} 84 85	@NotNull 86	public final Player createPlayer(final String name, final Player killer) { 87		final Player player = createPlayer(name); 88		when(player.getKiller()).thenReturn(killer); 89		return player; 90	} 91 92	public final void tearDown() { 93		plugin.onDisable(); 94		deleteDir(new File(filePath + "TestServer")); 95	} 96 97	private boolean deleteDir(final File file) { 98		if (file.isDirectory()) { 99			final String[] children = file.list();100			if (children != null) {101				for (final String aChildren : children) {102					final boolean success = deleteDir(new File(file, aChildren));103					if (!success)104						return false;105				}106			}107		}108		return file.delete();109	}110111	public static PluginTest getInstance() {112		return instance;113	}114115	public Server getServer() {116		return server;117	}118119	public final PvPManager getPlugin() {120		return plugin;121	}122123	public Player getAttacker() {124		return attacker;125	}126127	public Player getDefender() {128		return defender;129	}130131}