mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-11-03 01:10:17 +01:00
Moved Bukkit tests to JUnit 5
This commit is contained in:
parent
b5fb947724
commit
c3f9d77698
@ -25,49 +25,42 @@ import com.djrapitops.plan.system.settings.ConfigSettingKeyTest;
|
||||
import com.djrapitops.plan.system.settings.config.PlanConfig;
|
||||
import com.djrapitops.plan.system.settings.paths.WebserverSettings;
|
||||
import com.djrapitops.plan.system.settings.paths.key.Setting;
|
||||
import org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.platform.runner.JUnitPlatform;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import rules.BukkitComponentMocker;
|
||||
import rules.ComponentMocker;
|
||||
import utilities.OptionalAssert;
|
||||
import utilities.RandomData;
|
||||
import utilities.mocks.BukkitMockComponent;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
|
||||
/**
|
||||
* Test for Bukkit PlanSystem.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.Silent.class)
|
||||
@RunWith(JUnitPlatform.class)
|
||||
public class BukkitSystemTest {
|
||||
|
||||
@ClassRule
|
||||
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@Rule
|
||||
public ComponentMocker component = new BukkitComponentMocker(temporaryFolder);
|
||||
|
||||
private final int TEST_PORT_NUMBER = RandomData.randomInt(9005, 9500);
|
||||
private PlanSystem system;
|
||||
|
||||
@Before
|
||||
public void prepareSystem() {
|
||||
system = component.getPlanSystem();
|
||||
@BeforeEach
|
||||
void prepareSystem(@TempDir Path temp) throws Exception {
|
||||
system = new BukkitMockComponent(temp).getPlanSystem();
|
||||
system.getConfigSystem().getConfig()
|
||||
.set(WebserverSettings.PORT, TEST_PORT_NUMBER);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bukkitSystemEnables() throws EnableException {
|
||||
void bukkitSystemEnables() throws EnableException {
|
||||
try {
|
||||
system.enable();
|
||||
assertTrue(system.isEnabled());
|
||||
@ -77,7 +70,7 @@ public class BukkitSystemTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void correctWebAddressInDatabaseAfterEnable() throws EnableException {
|
||||
void correctWebAddressInDatabaseAfterEnable() throws EnableException {
|
||||
try {
|
||||
system.enable();
|
||||
Database database = system.getDatabaseSystem().getDatabase();
|
||||
@ -92,7 +85,7 @@ public class BukkitSystemTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bukkitSystemHasDefaultConfigValuesAfterEnable() throws EnableException, IllegalAccessException {
|
||||
void bukkitSystemHasDefaultConfigValuesAfterEnable() throws EnableException, IllegalAccessException {
|
||||
try {
|
||||
system.enable();
|
||||
PlanConfig config = system.getConfigSystem().getConfig();
|
||||
|
@ -23,9 +23,13 @@ import com.djrapitops.plugin.logging.console.TestPluginLogger;
|
||||
import com.djrapitops.plugin.logging.error.ConsoleErrorLogger;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.player.PlayerMoveEvent;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.junit.platform.runner.JUnitPlatform;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import utilities.TestConstants;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@ -37,20 +41,33 @@ import static org.mockito.Mockito.*;
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@RunWith(JUnitPlatform.class)
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class AFKListenerTest {
|
||||
|
||||
private AFKListener underTest;
|
||||
private static AFKListener underTest;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
@BeforeAll
|
||||
static void setUp() {
|
||||
PlanConfig config = Mockito.mock(PlanConfig.class);
|
||||
when(config.get(TimeSettings.AFK_THRESHOLD)).thenReturn(TimeUnit.MINUTES.toMillis(3));
|
||||
underTest = new AFKListener(config, new ConsoleErrorLogger(new TestPluginLogger()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void afkPermissionIsNotCalledMoreThanOnce() {
|
||||
Player player = mockPlayer();
|
||||
void afkPermissionIsNotCalledMoreThanOnceWhenIgnored() {
|
||||
Player player = mockPlayerWithPermissions();
|
||||
PlayerMoveEvent event = mockMoveEvent(player);
|
||||
|
||||
underTest.onMove(event);
|
||||
underTest.onMove(event);
|
||||
|
||||
verify(player, times(1)).hasPermission(anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void afkPermissionIsNotCalledMoreThanOnceWhenNotIgnored() {
|
||||
Player player = mockPlayerWithoutPermissions();
|
||||
PlayerMoveEvent event = mockMoveEvent(player);
|
||||
|
||||
underTest.onMove(event);
|
||||
@ -65,11 +82,18 @@ public class AFKListenerTest {
|
||||
return event;
|
||||
}
|
||||
|
||||
private Player mockPlayer() {
|
||||
private Player mockPlayerWithPermissions() {
|
||||
Player player = Mockito.mock(Player.class);
|
||||
when(player.getUniqueId()).thenReturn(TestConstants.PLAYER_ONE_UUID);
|
||||
when(player.hasPermission(anyString())).thenReturn(true);
|
||||
return player;
|
||||
}
|
||||
|
||||
private Player mockPlayerWithoutPermissions() {
|
||||
Player player = Mockito.mock(Player.class);
|
||||
when(player.getUniqueId()).thenReturn(TestConstants.PLAYER_TWO_UUID);
|
||||
when(player.hasPermission(anyString())).thenReturn(false);
|
||||
return player;
|
||||
}
|
||||
|
||||
}
|
@ -14,44 +14,47 @@
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package rules;
|
||||
package utilities.mocks;
|
||||
|
||||
import com.djrapitops.plan.DaggerPlanBukkitComponent;
|
||||
import com.djrapitops.plan.Plan;
|
||||
import com.djrapitops.plan.PlanBukkitComponent;
|
||||
import com.djrapitops.plan.PlanPlugin;
|
||||
import com.djrapitops.plan.system.PlanSystem;
|
||||
import org.junit.rules.ExternalResource;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import utilities.mocks.PlanBukkitMocker;
|
||||
|
||||
public class BukkitComponentMocker extends ExternalResource implements ComponentMocker {
|
||||
import java.nio.file.Path;
|
||||
|
||||
private final TemporaryFolder testFolder;
|
||||
/**
|
||||
* Test utility for creating a dagger PlanComponent using a mocked Plan.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class BukkitMockComponent {
|
||||
|
||||
private final Path tempDir;
|
||||
|
||||
private Plan planMock;
|
||||
private PlanBukkitComponent component;
|
||||
|
||||
public BukkitComponentMocker(TemporaryFolder testFolder) {
|
||||
this.testFolder = testFolder;
|
||||
public BukkitMockComponent(Path tempDir) {
|
||||
this.tempDir = tempDir;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void before() throws Throwable {
|
||||
PlanBukkitMocker mocker = PlanBukkitMocker.setUp()
|
||||
.withDataFolder(testFolder.newFolder())
|
||||
public Plan getPlanMock() throws Exception {
|
||||
if (planMock == null) {
|
||||
planMock = PlanBukkitMocker.setUp()
|
||||
.withDataFolder(tempDir.toFile())
|
||||
.withPluginDescription()
|
||||
.withResourceFetchingFromJar()
|
||||
.withServer();
|
||||
planMock = mocker.getPlanMock();
|
||||
component = DaggerPlanBukkitComponent.builder().plan(planMock).build();
|
||||
.withServer()
|
||||
.getPlanMock();
|
||||
}
|
||||
|
||||
public PlanPlugin getPlanMock() {
|
||||
return planMock;
|
||||
}
|
||||
|
||||
public PlanSystem getPlanSystem() {
|
||||
public PlanSystem getPlanSystem() throws Exception {
|
||||
if (component == null) {
|
||||
component = DaggerPlanBukkitComponent.builder().plan(getPlanMock()).build();
|
||||
}
|
||||
return component.system();
|
||||
}
|
||||
}
|
@ -84,17 +84,12 @@ public class PlanBukkitMocker extends Mocker {
|
||||
return this;
|
||||
}
|
||||
|
||||
public PlanBukkitMocker withDataFolder(File tempFolder) {
|
||||
PlanBukkitMocker withDataFolder(File tempFolder) {
|
||||
doReturn(tempFolder).when(planMock).getDataFolder();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public PlanBukkitMocker withLogging() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public PlanBukkitMocker withPluginDescription() {
|
||||
PlanBukkitMocker withPluginDescription() {
|
||||
try {
|
||||
File pluginYml = getFile("/plugin.yml");
|
||||
PluginDescriptionFile description = new PluginDescriptionFile(new FileInputStream(pluginYml));
|
||||
@ -105,12 +100,12 @@ public class PlanBukkitMocker extends Mocker {
|
||||
return this;
|
||||
}
|
||||
|
||||
public PlanBukkitMocker withResourceFetchingFromJar() throws IOException {
|
||||
PlanBukkitMocker withResourceFetchingFromJar() throws IOException {
|
||||
withPluginFiles();
|
||||
return this;
|
||||
}
|
||||
|
||||
public PlanBukkitMocker withServer() {
|
||||
PlanBukkitMocker withServer() {
|
||||
Server serverMock = Mockito.mock(Server.class);
|
||||
doReturn("").when(serverMock).getIp();
|
||||
doReturn("Bukkit").when(serverMock).getName();
|
||||
@ -129,7 +124,7 @@ public class PlanBukkitMocker extends Mocker {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Plan getPlanMock() {
|
||||
Plan getPlanMock() {
|
||||
return planMock;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user