mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-26 02:27:50 +01:00
Write tests for BukkitService
This commit is contained in:
parent
367380265e
commit
7578247085
@ -9,10 +9,14 @@ import org.bukkit.Server;
|
|||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.ConsoleCommandSender;
|
import org.bukkit.command.ConsoleCommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
import org.bukkit.scheduler.BukkitScheduler;
|
||||||
|
import org.bukkit.scheduler.BukkitTask;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.Mockito;
|
||||||
import org.mockito.junit.MockitoJUnitRunner;
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@ -20,9 +24,13 @@ import java.util.Collection;
|
|||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.hasSize;
|
import static org.hamcrest.Matchers.hasSize;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.BDDMockito.given;
|
import static org.mockito.BDDMockito.given;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.only;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for {@link BukkitService}.
|
* Test for {@link BukkitService}.
|
||||||
@ -38,10 +46,13 @@ public class BukkitServiceTest {
|
|||||||
private Settings settings;
|
private Settings settings;
|
||||||
@Mock
|
@Mock
|
||||||
private Server server;
|
private Server server;
|
||||||
|
@Mock
|
||||||
|
private BukkitScheduler scheduler;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void constructBukkitService() {
|
public void constructBukkitService() {
|
||||||
ReflectionTestUtils.setField(Bukkit.class, null, "server", server);
|
ReflectionTestUtils.setField(Bukkit.class, null, "server", server);
|
||||||
|
given(server.getScheduler()).willReturn(scheduler);
|
||||||
given(settings.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(true);
|
given(settings.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(true);
|
||||||
bukkitService = new BukkitService(authMe, settings);
|
bukkitService = new BukkitService(authMe, settings);
|
||||||
}
|
}
|
||||||
@ -101,6 +112,191 @@ public class BukkitServiceTest {
|
|||||||
verify(server).dispatchCommand(consoleSender, command);
|
verify(server).dispatchCommand(consoleSender, command);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldScheduleSyncDelayedTask() {
|
||||||
|
// given
|
||||||
|
Runnable task = () -> {/* noop */};
|
||||||
|
given(scheduler.scheduleSyncDelayedTask(authMe, task)).willReturn(123);
|
||||||
|
|
||||||
|
// when
|
||||||
|
int taskId = bukkitService.scheduleSyncDelayedTask(task);
|
||||||
|
|
||||||
|
// then
|
||||||
|
verify(scheduler, only()).scheduleSyncDelayedTask(authMe, task);
|
||||||
|
assertThat(taskId, equalTo(123));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldScheduleSyncDelayedTaskWithDelay() {
|
||||||
|
// given
|
||||||
|
Runnable task = () -> {/* noop */};
|
||||||
|
int delay = 3;
|
||||||
|
given(scheduler.scheduleSyncDelayedTask(authMe, task, delay)).willReturn(44);
|
||||||
|
|
||||||
|
// when
|
||||||
|
int taskId = bukkitService.scheduleSyncDelayedTask(task, delay);
|
||||||
|
|
||||||
|
// then
|
||||||
|
verify(scheduler, only()).scheduleSyncDelayedTask(authMe, task, delay);
|
||||||
|
assertThat(taskId, equalTo(44));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldScheduleSyncTask() {
|
||||||
|
// given
|
||||||
|
BukkitService spy = Mockito.spy(bukkitService);
|
||||||
|
doReturn(1).when(spy).scheduleSyncDelayedTask(any(Runnable.class));
|
||||||
|
Runnable task = mock(Runnable.class);
|
||||||
|
|
||||||
|
// when
|
||||||
|
spy.scheduleSyncTaskFromOptionallyAsyncTask(task);
|
||||||
|
|
||||||
|
// then
|
||||||
|
verify(spy).scheduleSyncDelayedTask(task);
|
||||||
|
verifyZeroInteractions(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldRunTaskDirectly() {
|
||||||
|
// given
|
||||||
|
given(settings.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(false);
|
||||||
|
bukkitService.reload(settings);
|
||||||
|
BukkitService spy = Mockito.spy(bukkitService);
|
||||||
|
Runnable task = mock(Runnable.class);
|
||||||
|
|
||||||
|
// when
|
||||||
|
spy.scheduleSyncTaskFromOptionallyAsyncTask(task);
|
||||||
|
|
||||||
|
// then
|
||||||
|
verify(task).run();
|
||||||
|
verify(spy, only()).scheduleSyncTaskFromOptionallyAsyncTask(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldRunTask() {
|
||||||
|
// given
|
||||||
|
Runnable task = () -> {/* noop */};
|
||||||
|
BukkitTask bukkitTask = mock(BukkitTask.class);
|
||||||
|
given(scheduler.runTask(authMe, task)).willReturn(bukkitTask);
|
||||||
|
|
||||||
|
// when
|
||||||
|
BukkitTask resultingTask = bukkitService.runTask(task);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(resultingTask, equalTo(bukkitTask));
|
||||||
|
verify(scheduler, only()).runTask(authMe, task);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldRunTaskLater() {
|
||||||
|
// given
|
||||||
|
Runnable task = () -> {/* noop */};
|
||||||
|
BukkitTask bukkitTask = mock(BukkitTask.class);
|
||||||
|
long delay = 400;
|
||||||
|
given(scheduler.runTaskLater(authMe, task, delay)).willReturn(bukkitTask);
|
||||||
|
|
||||||
|
// when
|
||||||
|
BukkitTask resultingTask = bukkitService.runTaskLater(task, delay);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(resultingTask, equalTo(bukkitTask));
|
||||||
|
verify(scheduler, only()).runTaskLater(authMe, task, delay);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldRunTaskInAsync() {
|
||||||
|
// given
|
||||||
|
Runnable task = mock(Runnable.class);
|
||||||
|
BukkitService spy = Mockito.spy(bukkitService);
|
||||||
|
doReturn(null).when(spy).runTaskAsynchronously(task);
|
||||||
|
|
||||||
|
// when
|
||||||
|
spy.runTaskOptionallyAsync(task);
|
||||||
|
|
||||||
|
// then
|
||||||
|
verifyZeroInteractions(task);
|
||||||
|
verify(spy).runTaskAsynchronously(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldRunTaskDirectlyIfConfigured() {
|
||||||
|
// given
|
||||||
|
given(settings.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(false);
|
||||||
|
bukkitService.reload(settings);
|
||||||
|
BukkitService spy = Mockito.spy(bukkitService);
|
||||||
|
Runnable task = mock(Runnable.class);
|
||||||
|
|
||||||
|
// when
|
||||||
|
spy.runTaskOptionallyAsync(task);
|
||||||
|
|
||||||
|
// then
|
||||||
|
verify(task).run();
|
||||||
|
verify(spy, only()).runTaskOptionallyAsync(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldRunTaskAsynchronously() {
|
||||||
|
// given
|
||||||
|
Runnable task = () -> {/* noop */};
|
||||||
|
BukkitTask bukkitTask = mock(BukkitTask.class);
|
||||||
|
given(scheduler.runTaskAsynchronously(authMe, task)).willReturn(bukkitTask);
|
||||||
|
|
||||||
|
// when
|
||||||
|
BukkitTask resultingTask = bukkitService.runTaskAsynchronously(task);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(resultingTask, equalTo(bukkitTask));
|
||||||
|
verify(scheduler, only()).runTaskAsynchronously(authMe, task);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldRunTaskTimerAsynchronously() {
|
||||||
|
// given
|
||||||
|
Runnable task = () -> {/* */};
|
||||||
|
long delay = 20L;
|
||||||
|
long period = 4000L;
|
||||||
|
BukkitTask bukkitTask = mock(BukkitTask.class);
|
||||||
|
given(scheduler.runTaskTimerAsynchronously(authMe, task, delay, period)).willReturn(bukkitTask);
|
||||||
|
|
||||||
|
// when
|
||||||
|
BukkitTask resultingTask = bukkitService.runTaskTimerAsynchronously(task, delay, period);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(resultingTask, equalTo(bukkitTask));
|
||||||
|
verify(scheduler).runTaskTimerAsynchronously(authMe, task, delay, period);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldRunTaskTimer() {
|
||||||
|
// given
|
||||||
|
BukkitRunnable bukkitRunnable = mock(BukkitRunnable.class);
|
||||||
|
long delay = 20;
|
||||||
|
long period = 80;
|
||||||
|
BukkitTask bukkitTask = mock(BukkitTask.class);
|
||||||
|
given(bukkitRunnable.runTaskTimer(authMe, delay, period)).willReturn(bukkitTask);
|
||||||
|
|
||||||
|
// when
|
||||||
|
BukkitTask result = bukkitService.runTaskTimer(bukkitRunnable, delay, period);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result, equalTo(bukkitTask));
|
||||||
|
verify(bukkitRunnable).runTaskTimer(authMe, delay, period);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldBroadcastMessage() {
|
||||||
|
// given
|
||||||
|
String message = "Important message to all";
|
||||||
|
given(server.broadcastMessage(message)).willReturn(24);
|
||||||
|
|
||||||
|
// when
|
||||||
|
int result = bukkitService.broadcastMessage(message);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result, equalTo(24));
|
||||||
|
verify(server).broadcastMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
// Note: This method is used through reflections
|
// Note: This method is used through reflections
|
||||||
public static Player[] onlinePlayersImpl() {
|
public static Player[] onlinePlayersImpl() {
|
||||||
return new Player[]{
|
return new Player[]{
|
||||||
|
Loading…
Reference in New Issue
Block a user