From 00f12f9cdf132df1c67b73bf7cd0d61458a3d914 Mon Sep 17 00:00:00 2001 From: Thorinwasher Date: Sat, 20 Jan 2024 17:44:03 +0100 Subject: [PATCH] Pass tests --- .../org/sgrewritten/stargate/Stargate.java | 11 +++-------- .../stargate/gate/structure/GateIris.java | 2 +- .../stargate/migration/DataMigration9.java | 6 +++--- .../stargate/network/StargateRegistry.java | 8 ++++---- .../thread/task/StargateRegionTask.java | 15 ++++++++++----- .../stargate/thread/task/StargateTask.java | 10 +++++++--- .../stargate/util/ButtonHelper.java | 4 ++-- .../org/sgrewritten/stargate/StargateTest.java | 7 ++++--- .../api/MaterialHandlerResolverTest.java | 5 ++--- .../stargate/command/CommandParityTest.java | 5 +++-- .../stargate/command/CommandTraceTest.java | 5 +++-- .../stargate/database/DatabaseTester.java | 3 ++- .../stargate/database/MySQLDatabaseTest.java | 3 ++- .../stargate/database/SQLiteDatabaseTest.java | 3 ++- .../property/PropertiesDatabaseTest.java | 5 +++-- .../stargate/gate/GateFormatTest.java | 3 +-- .../sgrewritten/stargate/gate/GateTest.java | 5 +++-- .../listener/BlockEventListenerTest.java | 6 +++--- .../listener/MoveEventListenerTest.java | 5 ++--- .../listener/PlayerEventListenerTest.java | 5 +++-- .../listener/PluginEventListenerTest.java | 5 +++-- .../manager/StargateBungeeManagerTest.java | 5 +++-- .../migration/DataMigration1014Test.java | 5 +++-- .../stargate/migration/DataMigratorTest.java | 5 +++-- .../stargate/network/NetworkManagerTest.java | 5 +++-- .../stargate/network/StargateNetworkTest.java | 3 +-- .../stargate/network/StargateRegistryTest.java | 5 ++--- .../network/portal/NetworkedPortalTest.java | 5 ++--- .../network/portal/TeleporterTest.java | 15 +++++---------- .../proxy/InterServerMessageSenderTest.java | 5 ++--- .../stargate/util/BlockEventHelperTest.java | 4 ++-- .../stargate/util/GateFormatReaderTest.java | 4 ++-- .../stargate/util/StargateTestHelper.java | 18 +++++++++++++++++- .../util/TranslatableMessageFormatterTest.java | 4 ++-- .../util/portal/TeleportationHelperTest.java | 5 +++-- 35 files changed, 116 insertions(+), 93 deletions(-) diff --git a/src/main/java/org/sgrewritten/stargate/Stargate.java b/src/main/java/org/sgrewritten/stargate/Stargate.java index 18ddfcaf8..77daa1af7 100644 --- a/src/main/java/org/sgrewritten/stargate/Stargate.java +++ b/src/main/java/org/sgrewritten/stargate/Stargate.java @@ -81,6 +81,7 @@ import org.sgrewritten.stargate.thread.ThreadHelper; import org.sgrewritten.stargate.thread.task.StargateAsyncTask; import org.sgrewritten.stargate.thread.task.StargateRegionTask; +import org.sgrewritten.stargate.thread.task.StargateTask; import org.sgrewritten.stargate.util.BStatsHelper; import org.sgrewritten.stargate.util.BungeeHelper; import org.sgrewritten.stargate.util.FileHelper; @@ -128,8 +129,6 @@ public class Stargate extends JavaPlugin implements StargateAPI, ConfigurationAP private StorageAPI storageAPI; private LanguageManager languageManager; private BungeeManager bungeeManager; - private final SynchronousPopulator synchronousTickPopulator = new SynchronousPopulator(); - private final SynchronousPopulator syncSecPopulator = new SynchronousPopulator(); private static final int MAX_TEXT_LENGTH = 13; private StargateEconomyAPI economyManager; @@ -566,12 +565,8 @@ public void onDisable() { //Close networked always-on Stargates as they have no destination on next start registry.getNetworkRegistry(StorageType.LOCAL).closeAllPortals(); registry.getNetworkRegistry(StorageType.INTER_SERVER).closeAllPortals(); - /* - * Replacement for legacy, which used: - * methodPortal.closeAllGates(this); Portal.clearGates(); managedWorlds.clear(); - */ - synchronousTickPopulator.clear(); - syncSecPopulator.clear(); + StargateTask.forceRunAllTasks(); + ThreadHelper.setAsyncQueueEnabled(false); if (ConfigurationHelper.getBoolean(ConfigurationOption.USING_BUNGEE)) { Messenger messenger = Bukkit.getMessenger(); diff --git a/src/main/java/org/sgrewritten/stargate/gate/structure/GateIris.java b/src/main/java/org/sgrewritten/stargate/gate/structure/GateIris.java index 787956546..2240709a9 100644 --- a/src/main/java/org/sgrewritten/stargate/gate/structure/GateIris.java +++ b/src/main/java/org/sgrewritten/stargate/gate/structure/GateIris.java @@ -77,7 +77,7 @@ public void generateStructure(VectorOperation converter, Location topLeft) { // (Clear all blocks that are in the portal iris) Material[] irisClosedList = irisClosed.toArray(new Material[0]); for(BlockVector blockVector : this.blocks){ - int target = RANDOM.nextInt(irisClosedList.length)-1; + int target = RANDOM.nextInt(irisClosedList.length); Material chosenType = irisClosedList[target]; Location location = VectorUtils.getLocation(topLeft,converter,blockVector); new StargateRegionTask(location, () -> { diff --git a/src/main/java/org/sgrewritten/stargate/migration/DataMigration9.java b/src/main/java/org/sgrewritten/stargate/migration/DataMigration9.java index a1620fb31..46df6da54 100644 --- a/src/main/java/org/sgrewritten/stargate/migration/DataMigration9.java +++ b/src/main/java/org/sgrewritten/stargate/migration/DataMigration9.java @@ -50,7 +50,7 @@ public int getConfigVersion() { @Override protected TwoTuple getNewConfigPair(TwoTuple oldPair) { - if (!configConversions.contains(oldPair.getFirstValue())) { + if (!configConversions.containsKey(oldPair.getFirstValue())) { return oldPair; } String newKey = configConversions.getProperty(oldPair.getFirstValue()); @@ -91,8 +91,8 @@ private static Properties loadConfigConversions(String file) { private void runChangeWorldNameToUUID(SQLDatabaseAPI database, TableNameConfiguration nameConfiguration, StorageType type) throws SQLException, IOException { String directory = "/migration/database/v-9/"; - String fileNameUpdate = directory + (type == StorageType.LOCAL ? "update_world_local" : "update_world_inter_server"); - String fileNameGet = directory + (type == StorageType.LOCAL ? "get_world_local" : "get_world_inter_server"); + String fileNameUpdate = directory + (type == StorageType.LOCAL ? "update_world_local.sql" : "update_world_inter_server.sql"); + String fileNameGet = directory + (type == StorageType.LOCAL ? "get_world_local.sql" : "get_world_inter_server.sql"); try (Connection connection = database.getConnection()) { Set worldNames = new HashSet<>(); try (InputStream stream = Stargate.class.getResourceAsStream(fileNameGet)) { diff --git a/src/main/java/org/sgrewritten/stargate/network/StargateRegistry.java b/src/main/java/org/sgrewritten/stargate/network/StargateRegistry.java index f57760d48..3b645d18e 100644 --- a/src/main/java/org/sgrewritten/stargate/network/StargateRegistry.java +++ b/src/main/java/org/sgrewritten/stargate/network/StargateRegistry.java @@ -392,8 +392,8 @@ public void renameNetwork(String newId, String oldId, StorageType storageType) t int zMod = corner1Chunk.getZ() < corner2Chunk.getZ() ? 1 : -1; Set chunks = new HashSet<>(); - for (int x = corner1Chunk.getX(); shouldStop(corner2Chunk.getX(), x, xMod); x += xMod) { - for (int z = corner1Chunk.getZ(); shouldStop(corner2Chunk.getZ(), z, zMod); z += zMod) { + for (int x = corner1Chunk.getX(); !shouldStop(corner2Chunk.getX(), x, xMod); x += xMod) { + for (int z = corner1Chunk.getZ(); !shouldStop(corner2Chunk.getZ(), z, zMod); z += zMod) { chunks.add(world.getChunkAt(x, z)); } } @@ -402,9 +402,9 @@ public void renameNetwork(String newId, String oldId, StorageType storageType) t private boolean shouldStop(int target, int currentPosition, int modifier) { if (modifier > 0) { - return currentPosition >= target; + return currentPosition > target; } else { - return currentPosition <= target; + return currentPosition < target; } } diff --git a/src/main/java/org/sgrewritten/stargate/thread/task/StargateRegionTask.java b/src/main/java/org/sgrewritten/stargate/thread/task/StargateRegionTask.java index 8d3981219..6f385876c 100644 --- a/src/main/java/org/sgrewritten/stargate/thread/task/StargateRegionTask.java +++ b/src/main/java/org/sgrewritten/stargate/thread/task/StargateRegionTask.java @@ -21,6 +21,7 @@ public StargateRegionTask(Location location, Runnable runnable, boolean bungee) this.plugin = Stargate.getInstance(); this.bungee = bungee; } + public StargateRegionTask(Location location, Runnable runnable) { this(location, runnable, false); } @@ -41,19 +42,23 @@ public void runDelayed(long delay) { ScheduledTask theTask = Bukkit.getServer().getRegionScheduler().runDelayed(plugin, location, super::runTask, delay); super.registerFoliaTask(theTask); } else { - super.registerBukkitTask(new StargateBukkitRunnable(this::runPopulatorTask )).runTaskLater(plugin,delay); + super.registerBukkitTask(new StargateBukkitRunnable(this::runPopulatorTask)).runTaskLater(plugin, delay); } } - private void runPopulatorTask(){ + private void runPopulatorTask() { populator.addAction(super::runTask, bungee); super.registerTask(); } - public static void startPopulator(Plugin plugin){ - if(USING_FOLIA){ + public static void startPopulator(Plugin plugin) { + if (USING_FOLIA) { return; } - new StargateBukkitRunnable(populator).runTaskTimer(plugin,0,1); + new StargateBukkitRunnable(populator).runTaskTimer(plugin, 0, 1); + } + + public static void clearPopulator() { + populator.clear(); } } diff --git a/src/main/java/org/sgrewritten/stargate/thread/task/StargateTask.java b/src/main/java/org/sgrewritten/stargate/thread/task/StargateTask.java index dbff576cf..b89070d04 100644 --- a/src/main/java/org/sgrewritten/stargate/thread/task/StargateTask.java +++ b/src/main/java/org/sgrewritten/stargate/thread/task/StargateTask.java @@ -4,6 +4,7 @@ import org.bukkit.scheduler.BukkitRunnable; import org.sgrewritten.stargate.property.NonLegacyMethod; +import java.util.LinkedList; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; @@ -61,12 +62,15 @@ private void cancelIfTaskHasBeenScheduled(boolean bukkit) { } /** - * Runns all tasks + * Runs all tasks */ public static void forceRunAllTasks() { int counter = 0; - while(!tasks.isEmpty() && counter > MAXIMUM_SHUTDOWN_CYCLES) { - tasks.forEach(Runnable::run); + while(!tasks.isEmpty() && counter < MAXIMUM_SHUTDOWN_CYCLES) { + Queue scheduledTasks = new LinkedList<>(tasks); + scheduledTasks.forEach(Runnable::run); + tasks.removeAll(scheduledTasks); + counter++; } } diff --git a/src/main/java/org/sgrewritten/stargate/util/ButtonHelper.java b/src/main/java/org/sgrewritten/stargate/util/ButtonHelper.java index eeec65d38..db8b18c6a 100644 --- a/src/main/java/org/sgrewritten/stargate/util/ButtonHelper.java +++ b/src/main/java/org/sgrewritten/stargate/util/ButtonHelper.java @@ -65,11 +65,11 @@ public static Material getButtonMaterial(Location locationToGenerateButton) { } private static Material getOverWaterButtonMaterial(){ - return BUTTONS.get(RANDOM.nextInt(BUTTONS.size())-1); + return BUTTONS.get(RANDOM.nextInt(BUTTONS.size())); } private static Material getUnderWaterButtonMaterial(){ - return CORAL_WALL_FANS.get(RANDOM.nextInt(CORAL_WALL_FANS.size())-1); + return CORAL_WALL_FANS.get(RANDOM.nextInt(CORAL_WALL_FANS.size())); } private static Stream getDeadWallCoralWallFans(){ diff --git a/src/test/java/org/sgrewritten/stargate/StargateTest.java b/src/test/java/org/sgrewritten/stargate/StargateTest.java index a6e0dc3c7..52646b629 100644 --- a/src/test/java/org/sgrewritten/stargate/StargateTest.java +++ b/src/test/java/org/sgrewritten/stargate/StargateTest.java @@ -27,6 +27,7 @@ import org.sgrewritten.stargate.network.portal.PortalBlockGenerator; import org.sgrewritten.stargate.network.portal.PortalFactory; import org.sgrewritten.stargate.thread.ThreadHelper; +import org.sgrewritten.stargate.util.StargateTestHelper; import java.util.HashSet; import java.util.Set; @@ -47,7 +48,7 @@ class StargateTest { @BeforeEach void setup() throws TranslatableException, NoFormatFoundException, GateConflictException { - server = MockBukkit.mock(); + server = StargateTestHelper.setup(); scheduler = server.getScheduler(); WorldMock world = server.addSimpleWorld("world"); System.setProperty("bstats.relocatecheck", "false"); @@ -70,7 +71,7 @@ void setup() throws TranslatableException, NoFormatFoundException, GateConflictE @AfterEach void tearDown() { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); } @Test @@ -119,7 +120,7 @@ void reload_StupidDefaultNetworkNameUUID() { @ParameterizedTest @ValueSource(strings = {"thisNameIsWayTooLong", "", "Test1\nTest2"}) - void reload_StupidDefaultNetworkNameTooLong(String name) { + void reload_StupidDefaultNetworkName(String name) { Stargate.setLogLevel(Level.OFF); plugin.setConfigurationOptionValue(ConfigurationOption.DEFAULT_NETWORK, name); plugin.reload(); diff --git a/src/test/java/org/sgrewritten/stargate/api/MaterialHandlerResolverTest.java b/src/test/java/org/sgrewritten/stargate/api/MaterialHandlerResolverTest.java index bf07435e1..3c800ca08 100644 --- a/src/test/java/org/sgrewritten/stargate/api/MaterialHandlerResolverTest.java +++ b/src/test/java/org/sgrewritten/stargate/api/MaterialHandlerResolverTest.java @@ -49,8 +49,7 @@ public class MaterialHandlerResolverTest { @BeforeEach void setUp() throws InvalidNameException, UnimplementedFlagException, NameLengthException, NameConflictException { - this.server = MockBukkit.mock(); - StargateTestHelper.setup(); + this.server = StargateTestHelper.setup(); this.storage = new StorageMock(); this.blockHandlerResolver = new BlockHandlerResolver(storage); this.registry = new RegistryMock(storage, blockHandlerResolver); @@ -62,7 +61,7 @@ void setUp() throws InvalidNameException, UnimplementedFlagException, NameLength @AfterEach void tearDown() { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); } @ParameterizedTest diff --git a/src/test/java/org/sgrewritten/stargate/command/CommandParityTest.java b/src/test/java/org/sgrewritten/stargate/command/CommandParityTest.java index 9d8996a8f..d638b883f 100644 --- a/src/test/java/org/sgrewritten/stargate/command/CommandParityTest.java +++ b/src/test/java/org/sgrewritten/stargate/command/CommandParityTest.java @@ -15,6 +15,7 @@ import org.sgrewritten.stargate.database.property.PropertiesDatabase; import org.sgrewritten.stargate.database.property.StoredProperty; import org.sgrewritten.stargate.util.LanguageManagerMock; +import org.sgrewritten.stargate.util.StargateTestHelper; import java.io.File; import java.io.IOException; @@ -30,7 +31,7 @@ class CommandParityTest { @BeforeEach void setUp() throws IOException { - @NotNull ServerMock server = MockBukkit.mock(); + @NotNull ServerMock server = StargateTestHelper.setup(); this.plugin = MockBukkit.createMockPlugin(); console = server.getConsoleSender(); player = server.addPlayer(); @@ -42,7 +43,7 @@ void setUp() throws IOException { @AfterEach void tearDown() { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); } @Test diff --git a/src/test/java/org/sgrewritten/stargate/command/CommandTraceTest.java b/src/test/java/org/sgrewritten/stargate/command/CommandTraceTest.java index 827d99096..32a9b74fb 100644 --- a/src/test/java/org/sgrewritten/stargate/command/CommandTraceTest.java +++ b/src/test/java/org/sgrewritten/stargate/command/CommandTraceTest.java @@ -11,6 +11,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.sgrewritten.stargate.Stargate; +import org.sgrewritten.stargate.util.StargateTestHelper; class CommandTraceTest { @@ -21,7 +22,7 @@ class CommandTraceTest { @BeforeEach void setUp() { - @NotNull ServerMock server = MockBukkit.mock(); + @NotNull ServerMock server = StargateTestHelper.setup(); sender = server.addPlayer(); System.setProperty("bstats.relocatecheck", "false"); plugin = MockBukkit.load(Stargate.class); @@ -31,7 +32,7 @@ void setUp() { @AfterEach void tearDown() { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); } @Test diff --git a/src/test/java/org/sgrewritten/stargate/database/DatabaseTester.java b/src/test/java/org/sgrewritten/stargate/database/DatabaseTester.java index 9162fca13..5cbb18046 100644 --- a/src/test/java/org/sgrewritten/stargate/database/DatabaseTester.java +++ b/src/test/java/org/sgrewritten/stargate/database/DatabaseTester.java @@ -28,6 +28,7 @@ import org.sgrewritten.stargate.network.portal.PortalFactory; import org.sgrewritten.stargate.network.portal.portaldata.PortalData; import org.sgrewritten.stargate.util.SQLTestHelper; +import org.sgrewritten.stargate.util.StargateTestHelper; import org.sgrewritten.stargate.util.database.DatabaseHelper; import org.sgrewritten.stargate.util.database.PortalStorageHelper; @@ -90,7 +91,7 @@ public DatabaseTester(SQLDatabaseAPI database, TableNameConfiguration nameConfig DatabaseTester.isMySQL = isMySQL; DatabaseTester.nameConfig = nameConfig; - ServerMock server = MockBukkit.mock(); + ServerMock server = StargateTestHelper.setup(); world = new WorldMock(Material.DIRT, 5); server.addWorld(world); diff --git a/src/test/java/org/sgrewritten/stargate/database/MySQLDatabaseTest.java b/src/test/java/org/sgrewritten/stargate/database/MySQLDatabaseTest.java index f251745e8..34c181fe0 100644 --- a/src/test/java/org/sgrewritten/stargate/database/MySQLDatabaseTest.java +++ b/src/test/java/org/sgrewritten/stargate/database/MySQLDatabaseTest.java @@ -14,6 +14,7 @@ import org.sgrewritten.stargate.exception.TranslatableException; import org.sgrewritten.stargate.exception.database.StorageWriteException; import org.sgrewritten.stargate.network.StorageType; +import org.sgrewritten.stargate.util.StargateTestHelper; import java.sql.Connection; import java.sql.SQLException; @@ -53,7 +54,7 @@ public static void setUp() throws SQLException, InvalidStructureException, Starg @AfterAll public static void tearDown() throws SQLException { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); try (Connection connection = database.getConnection()) { connection.prepareStatement("DROP DATABASE Stargate;").execute(); diff --git a/src/test/java/org/sgrewritten/stargate/database/SQLiteDatabaseTest.java b/src/test/java/org/sgrewritten/stargate/database/SQLiteDatabaseTest.java index acfbacf18..d503067d5 100644 --- a/src/test/java/org/sgrewritten/stargate/database/SQLiteDatabaseTest.java +++ b/src/test/java/org/sgrewritten/stargate/database/SQLiteDatabaseTest.java @@ -13,6 +13,7 @@ import org.sgrewritten.stargate.exception.TranslatableException; import org.sgrewritten.stargate.exception.database.StorageWriteException; import org.sgrewritten.stargate.network.StorageType; +import org.sgrewritten.stargate.util.StargateTestHelper; import java.io.File; import java.sql.SQLException; @@ -37,7 +38,7 @@ public static void setUp() throws SQLException, InvalidStructureException, Trans @AfterAll public static void tearDown() throws SQLException { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); try { DatabaseTester.deleteAllTables(nameConfig); } finally { diff --git a/src/test/java/org/sgrewritten/stargate/database/property/PropertiesDatabaseTest.java b/src/test/java/org/sgrewritten/stargate/database/property/PropertiesDatabaseTest.java index 1da424beb..2fc301ab5 100644 --- a/src/test/java/org/sgrewritten/stargate/database/property/PropertiesDatabaseTest.java +++ b/src/test/java/org/sgrewritten/stargate/database/property/PropertiesDatabaseTest.java @@ -6,6 +6,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.sgrewritten.stargate.util.StargateTestHelper; import java.io.File; import java.io.IOException; @@ -17,7 +18,7 @@ class PropertiesDatabaseTest { @BeforeEach void setUp() throws IOException { - MockBukkit.mock(); + StargateTestHelper.setup(); MockPlugin plugin = MockBukkit.createMockPlugin(); fileLocation = new File(plugin.getDataFolder(), "test.properties"); propertiesDatabase = new PropertiesDatabase(fileLocation); @@ -25,7 +26,7 @@ void setUp() throws IOException { @AfterEach void tearDown() { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); } @Test diff --git a/src/test/java/org/sgrewritten/stargate/gate/GateFormatTest.java b/src/test/java/org/sgrewritten/stargate/gate/GateFormatTest.java index 998076c0c..98afa863f 100644 --- a/src/test/java/org/sgrewritten/stargate/gate/GateFormatTest.java +++ b/src/test/java/org/sgrewritten/stargate/gate/GateFormatTest.java @@ -21,14 +21,13 @@ class GateFormatTest { @BeforeEach void setUp() { - MockBukkit.mock(); StargateTestHelper.setup(); this.gateFormat = GateFormatRegistry.getFormat("nether.gate"); } @AfterEach void tearDown() { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); } @Test diff --git a/src/test/java/org/sgrewritten/stargate/gate/GateTest.java b/src/test/java/org/sgrewritten/stargate/gate/GateTest.java index ba1008edf..96c18661b 100644 --- a/src/test/java/org/sgrewritten/stargate/gate/GateTest.java +++ b/src/test/java/org/sgrewritten/stargate/gate/GateTest.java @@ -23,6 +23,7 @@ import org.sgrewritten.stargate.network.RegistryMock; import org.sgrewritten.stargate.network.portal.PortalBlockGenerator; import org.sgrewritten.stargate.network.portal.portaldata.GateData; +import org.sgrewritten.stargate.util.StargateTestHelper; import java.io.File; import java.util.List; @@ -37,7 +38,7 @@ class GateTest { @BeforeEach void setUp() { - ServerMock server = MockBukkit.mock(); + ServerMock server = StargateTestHelper.setup(); this.world = server.addSimpleWorld("world"); Location topLeft = new Location(world, 0, 6, 0); BlockFace facing = BlockFace.SOUTH; @@ -53,7 +54,7 @@ void setUp() { @AfterEach void tearDown() { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); } @Test diff --git a/src/test/java/org/sgrewritten/stargate/listener/BlockEventListenerTest.java b/src/test/java/org/sgrewritten/stargate/listener/BlockEventListenerTest.java index 985a426da..cd81f22a5 100644 --- a/src/test/java/org/sgrewritten/stargate/listener/BlockEventListenerTest.java +++ b/src/test/java/org/sgrewritten/stargate/listener/BlockEventListenerTest.java @@ -50,6 +50,7 @@ import org.sgrewritten.stargate.network.portal.PortalBlockGenerator; import org.sgrewritten.stargate.network.portal.PortalFactory; import org.sgrewritten.stargate.network.portal.formatting.HighlightingStyle; +import org.sgrewritten.stargate.util.StargateTestHelper; import java.io.File; import java.util.HashSet; @@ -73,14 +74,13 @@ class BlockEventListenerTest { @BeforeEach void setUp() { - server = MockBukkit.mock(); + server = StargateTestHelper.setup(); System.setProperty("bstats.relocatecheck", "false"); plugin = MockBukkit.load(Stargate.class); player = server.addPlayer(PLAYER_NAME); world = new WorldMock(Material.GRASS_BLOCK, 0); server.addWorld(world); - GateFormatRegistry.setFormats(Objects.requireNonNull(GateFormatHandler.loadGateFormats(TEST_GATES_DIR))); this.stargateAPI = plugin; this.registry = stargateAPI.getRegistry(); this.networkManager = stargateAPI.getNetworkManager(); @@ -94,7 +94,7 @@ void setUp() { @AfterEach void tearDown() { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); } @ParameterizedTest diff --git a/src/test/java/org/sgrewritten/stargate/listener/MoveEventListenerTest.java b/src/test/java/org/sgrewritten/stargate/listener/MoveEventListenerTest.java index e3e8ce5ce..86314df55 100644 --- a/src/test/java/org/sgrewritten/stargate/listener/MoveEventListenerTest.java +++ b/src/test/java/org/sgrewritten/stargate/listener/MoveEventListenerTest.java @@ -45,9 +45,8 @@ class MoveEventListenerTest { @BeforeEach void setUp() throws TranslatableException, NoFormatFoundException, GateConflictException { System.setProperty("bstats.relocatecheck", "false"); - server = MockBukkit.mock(); + server = StargateTestHelper.setup(); plugin = MockBukkit.load(Stargate.class); - StargateTestHelper.setup(); @NotNull WorldMock theEnd = server.addSimpleWorld("world"); theEnd.setEnvironment(Environment.THE_END); Location from = new Location(theEnd, 0, 0, 0); @@ -64,7 +63,7 @@ void setUp() throws TranslatableException, NoFormatFoundException, GateConflictE @AfterEach void tearDown() { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); } @Test diff --git a/src/test/java/org/sgrewritten/stargate/listener/PlayerEventListenerTest.java b/src/test/java/org/sgrewritten/stargate/listener/PlayerEventListenerTest.java index 882c0b0e6..a40a56f3d 100644 --- a/src/test/java/org/sgrewritten/stargate/listener/PlayerEventListenerTest.java +++ b/src/test/java/org/sgrewritten/stargate/listener/PlayerEventListenerTest.java @@ -25,6 +25,7 @@ import org.sgrewritten.stargate.network.StargateNetworkManager; import org.sgrewritten.stargate.network.portal.PortalBlockGenerator; import org.sgrewritten.stargate.util.LanguageManagerMock; +import org.sgrewritten.stargate.util.StargateTestHelper; import java.io.File; import java.util.Objects; @@ -38,7 +39,7 @@ class PlayerEventListenerTest { @BeforeEach void setUp() { - ServerMock server = MockBukkit.mock(); + ServerMock server = StargateTestHelper.setup(); WorldMock world = server.addSimpleWorld("world"); player = server.addPlayer(); RegistryAPI registry = new RegistryMock(); @@ -49,7 +50,7 @@ void setUp() { @AfterEach void tearDown() { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); } @ParameterizedTest diff --git a/src/test/java/org/sgrewritten/stargate/listener/PluginEventListenerTest.java b/src/test/java/org/sgrewritten/stargate/listener/PluginEventListenerTest.java index 873dce558..3c015ecff 100644 --- a/src/test/java/org/sgrewritten/stargate/listener/PluginEventListenerTest.java +++ b/src/test/java/org/sgrewritten/stargate/listener/PluginEventListenerTest.java @@ -12,6 +12,7 @@ import org.sgrewritten.stargate.Stargate; import org.sgrewritten.stargate.economy.StargateEconomyManagerMock; import org.sgrewritten.stargate.manager.BlockLoggerMock; +import org.sgrewritten.stargate.util.StargateTestHelper; import java.util.logging.Level; @@ -26,7 +27,7 @@ class PluginEventListenerTest { @BeforeEach void setUp() { Stargate.setLogLevel(Level.SEVERE); - MockBukkit.mock(); + StargateTestHelper.setup(); economy = MockBukkit.createMockPlugin("Vault"); blockLogger = MockBukkit.createMockPlugin("CoreProtect"); economyManager = new StargateEconomyManagerMock(); @@ -36,7 +37,7 @@ void setUp() { @AfterEach void tearDown() { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); Stargate.setLogLevel(Level.INFO); } diff --git a/src/test/java/org/sgrewritten/stargate/manager/StargateBungeeManagerTest.java b/src/test/java/org/sgrewritten/stargate/manager/StargateBungeeManagerTest.java index 4c01b62cc..02e2c3f3b 100644 --- a/src/test/java/org/sgrewritten/stargate/manager/StargateBungeeManagerTest.java +++ b/src/test/java/org/sgrewritten/stargate/manager/StargateBungeeManagerTest.java @@ -33,6 +33,7 @@ import org.sgrewritten.stargate.property.StargateProtocolRequestType; import org.sgrewritten.stargate.util.BungeeHelper; import org.sgrewritten.stargate.util.LanguageManagerMock; +import org.sgrewritten.stargate.util.StargateTestHelper; import java.io.File; import java.util.HashSet; @@ -61,7 +62,7 @@ class StargateBungeeManagerTest { @BeforeEach void setUp() throws TranslatableException, InvalidStructureException { - server = MockBukkit.mock(); + server = StargateTestHelper.setup(); GateFormatRegistry.setFormats( Objects.requireNonNull(GateFormatHandler.loadGateFormats(testGatesDir))); Stargate.setServerName(SERVER); @@ -84,7 +85,7 @@ void setUp() throws TranslatableException, InvalidStructureException { @AfterEach void tearDown() { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); } @Test diff --git a/src/test/java/org/sgrewritten/stargate/migration/DataMigration1014Test.java b/src/test/java/org/sgrewritten/stargate/migration/DataMigration1014Test.java index e591c1d10..7d03b12ea 100644 --- a/src/test/java/org/sgrewritten/stargate/migration/DataMigration1014Test.java +++ b/src/test/java/org/sgrewritten/stargate/migration/DataMigration1014Test.java @@ -13,6 +13,7 @@ import org.sgrewritten.stargate.api.network.portal.PortalFlag; import org.sgrewritten.stargate.database.SQLiteDatabase; import org.sgrewritten.stargate.network.StargateNetwork; +import org.sgrewritten.stargate.util.StargateTestHelper; import java.io.File; import java.io.IOException; @@ -38,7 +39,7 @@ void setUp() throws IOException, SQLException { database = new SQLiteDatabase(sqlDatabaseFile); this.stargateAPI = new StargateAPIMock(); - @NotNull ServerMock server = MockBukkit.mock(); + @NotNull ServerMock server = StargateTestHelper.setup(); PlayerMock player = new PlayerMock(server, "player", UUID.fromString(UUID_STRING)); server.addPlayer(player); } @@ -47,7 +48,7 @@ void setUp() throws IOException, SQLException { void tearDown() { Assertions.assertTrue(sqlDatabaseFile.delete()); Assertions.assertTrue(oldSqlDatabaseFile.renameTo(sqlDatabaseFile)); - MockBukkit.unmock(); + StargateTestHelper.tearDown(); } @Test diff --git a/src/test/java/org/sgrewritten/stargate/migration/DataMigratorTest.java b/src/test/java/org/sgrewritten/stargate/migration/DataMigratorTest.java index 30fed9663..39e144489 100644 --- a/src/test/java/org/sgrewritten/stargate/migration/DataMigratorTest.java +++ b/src/test/java/org/sgrewritten/stargate/migration/DataMigratorTest.java @@ -41,6 +41,7 @@ import org.sgrewritten.stargate.network.StargateRegistry; import org.sgrewritten.stargate.network.StorageType; import org.sgrewritten.stargate.thread.ThreadHelper; +import org.sgrewritten.stargate.util.StargateTestHelper; import java.io.File; import java.io.IOException; @@ -95,7 +96,7 @@ public static void setUp() throws IOException, InvalidConfigurationException, SQ networkManager = stargateAPI.getNetworkManager(); defaultConfigFile = new File("src/main/resources", "config.yml"); - server = MockBukkit.mock(); + server = StargateTestHelper.setup(); server.addSimpleWorld("epicknarvik"); server.addSimpleWorld("lclo"); server.addSimpleWorld("pseudoknigth"); @@ -146,7 +147,7 @@ private static Map, Map>> g @AfterAll public static void tearDown() throws IOException, InvalidConfigurationException { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); for (File configFile : configFiles) { File oldConfigFile = new File(configFile.getAbsolutePath() + ".old"); diff --git a/src/test/java/org/sgrewritten/stargate/network/NetworkManagerTest.java b/src/test/java/org/sgrewritten/stargate/network/NetworkManagerTest.java index caf9d7b70..9e6c6cd81 100644 --- a/src/test/java/org/sgrewritten/stargate/network/NetworkManagerTest.java +++ b/src/test/java/org/sgrewritten/stargate/network/NetworkManagerTest.java @@ -27,6 +27,7 @@ import org.sgrewritten.stargate.network.portal.formatting.HighlightingStyle; import org.sgrewritten.stargate.util.LanguageManagerMock; import org.sgrewritten.stargate.util.NetworkCreationHelper; +import org.sgrewritten.stargate.util.StargateTestHelper; import java.util.HashSet; import java.util.logging.Level; @@ -51,7 +52,7 @@ class NetworkManagerTest { @BeforeEach void setup() { - server = MockBukkit.mock(); + server = StargateTestHelper.setup(); player = new PlayerMock(server, PLAYER_NAME); plugin = MockBukkit.createMockPlugin(); permissionManager = new StargatePermissionManager(player, new LanguageManagerMock()); @@ -64,7 +65,7 @@ void setup() { @AfterEach void teardown() { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); } @Test diff --git a/src/test/java/org/sgrewritten/stargate/network/StargateNetworkTest.java b/src/test/java/org/sgrewritten/stargate/network/StargateNetworkTest.java index 2ea4b06de..ff5662da7 100644 --- a/src/test/java/org/sgrewritten/stargate/network/StargateNetworkTest.java +++ b/src/test/java/org/sgrewritten/stargate/network/StargateNetworkTest.java @@ -22,13 +22,12 @@ public class StargateNetworkTest { @BeforeEach void setup() { - MockBukkit.mock(); StargateTestHelper.setup(); } @AfterEach void teardown() { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); } @ParameterizedTest diff --git a/src/test/java/org/sgrewritten/stargate/network/StargateRegistryTest.java b/src/test/java/org/sgrewritten/stargate/network/StargateRegistryTest.java index c5dbeca85..6be9854f9 100644 --- a/src/test/java/org/sgrewritten/stargate/network/StargateRegistryTest.java +++ b/src/test/java/org/sgrewritten/stargate/network/StargateRegistryTest.java @@ -39,8 +39,7 @@ class StargateRegistryTest { @BeforeEach void setUp() throws NameLengthException, NameConflictException, InvalidNameException, UnimplementedFlagException { - ServerMock server = MockBukkit.mock(); - StargateTestHelper.setup(); + ServerMock server = StargateTestHelper.setup(); this.world = server.addSimpleWorld("world"); this.player = server.addPlayer(); this.storageMock = new StorageMock(); @@ -52,7 +51,7 @@ void setUp() throws NameLengthException, NameConflictException, InvalidNameExcep @AfterEach void tearDown() { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); } @Test diff --git a/src/test/java/org/sgrewritten/stargate/network/portal/NetworkedPortalTest.java b/src/test/java/org/sgrewritten/stargate/network/portal/NetworkedPortalTest.java index 94e64c600..49484d146 100644 --- a/src/test/java/org/sgrewritten/stargate/network/portal/NetworkedPortalTest.java +++ b/src/test/java/org/sgrewritten/stargate/network/portal/NetworkedPortalTest.java @@ -44,9 +44,8 @@ class NetworkedPortalTest { @BeforeEach void setUp() throws TranslatableException, NoFormatFoundException, GateConflictException { - @NotNull ServerMock server = MockBukkit.mock(); + @NotNull ServerMock server = StargateTestHelper.setup(); plugin = MockBukkit.createMockPlugin("Stargate"); - StargateTestHelper.setup(); world = server.addSimpleWorld("world"); this.stargateAPI = new StargateAPIMock(); registry = stargateAPI.getRegistry(); @@ -61,7 +60,7 @@ void setUp() throws TranslatableException, NoFormatFoundException, GateConflictE @AfterEach void tearDown() { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); } @ParameterizedTest diff --git a/src/test/java/org/sgrewritten/stargate/network/portal/TeleporterTest.java b/src/test/java/org/sgrewritten/stargate/network/portal/TeleporterTest.java index 3bfe77866..5eb7de627 100644 --- a/src/test/java/org/sgrewritten/stargate/network/portal/TeleporterTest.java +++ b/src/test/java/org/sgrewritten/stargate/network/portal/TeleporterTest.java @@ -29,6 +29,7 @@ import org.sgrewritten.stargate.network.StorageType; import org.sgrewritten.stargate.thread.SynchronousPopulator; import org.sgrewritten.stargate.util.LanguageManagerMock; +import org.sgrewritten.stargate.util.StargateTestHelper; import java.io.File; import java.util.Objects; @@ -39,15 +40,13 @@ class TeleporterTest { private static Teleporter teleporter; private static SynchronousPopulator populator; private static PoweredMinecartMock furnaceMinecart; - private static final File testGatesDir = new File("src/test/resources/gates"); private ServerMock server; private BukkitSchedulerMock scheduler; @BeforeEach public void setup() throws TranslatableException, InvalidStructureException { - this.server = MockBukkit.mock(); + this.server = StargateTestHelper.setup(); this.scheduler = server.getScheduler(); - GateFormatRegistry.setFormats(Objects.requireNonNull(GateFormatHandler.loadGateFormats(testGatesDir))); WorldMock world = server.addSimpleWorld("world"); PlayerMock player = server.addPlayer(); PortalFactory fakePortalGenerator = new PortalFactory("Portal", "iPortal"); @@ -67,24 +66,20 @@ public void setup() throws TranslatableException, InvalidStructureException { @AfterEach public void tearDown() { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); } @Test public void teleport() { teleporter.teleport(horse); - while(!scheduler.getPendingTasks().isEmpty()){ - scheduler.performOneTick(); - } + StargateTestHelper.runAllTasks(); Assertions.assertTrue(horse.hasTeleported()); } @Test public void teleport_FurnaceMinecart() { teleporter.teleport(furnaceMinecart); - while(!scheduler.getPendingTasks().isEmpty()){ - scheduler.performOneTick(); - } + StargateTestHelper.runAllTasks(); Assertions.assertTrue(furnaceMinecart.hasTeleported()); } } diff --git a/src/test/java/org/sgrewritten/stargate/network/proxy/InterServerMessageSenderTest.java b/src/test/java/org/sgrewritten/stargate/network/proxy/InterServerMessageSenderTest.java index ef04f6c79..c17b1eec7 100644 --- a/src/test/java/org/sgrewritten/stargate/network/proxy/InterServerMessageSenderTest.java +++ b/src/test/java/org/sgrewritten/stargate/network/proxy/InterServerMessageSenderTest.java @@ -36,8 +36,7 @@ class InterServerMessageSenderTest { @BeforeEach void setUp() throws TranslatableException, InvalidStructureException { - this.server = MockBukkit.mock(); - StargateTestHelper.setup(); + this.server = StargateTestHelper.setup(); this.pluginMessageInterface = new TestPluginMessageInterface(); this.messageSender = new InterServerMessageSender(pluginMessageInterface); this.network = new StargateNetwork(NETWORK_ID, NetworkType.CUSTOM, StorageType.INTER_SERVER); @@ -47,7 +46,7 @@ void setUp() throws TranslatableException, InvalidStructureException { @AfterEach void tearDown() { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); } @Test diff --git a/src/test/java/org/sgrewritten/stargate/util/BlockEventHelperTest.java b/src/test/java/org/sgrewritten/stargate/util/BlockEventHelperTest.java index b495b7389..13f23fe25 100644 --- a/src/test/java/org/sgrewritten/stargate/util/BlockEventHelperTest.java +++ b/src/test/java/org/sgrewritten/stargate/util/BlockEventHelperTest.java @@ -45,7 +45,7 @@ class BlockEventHelperTest { @BeforeAll static void setUp() throws TranslatableException, NoFormatFoundException, GateConflictException { - ServerMock server = MockBukkit.mock(); + ServerMock server = StargateTestHelper.setup(); player = server.addPlayer(); WorldMock world = server.addSimpleWorld("world"); @@ -61,7 +61,7 @@ static void setUp() throws TranslatableException, NoFormatFoundException, GateCo @AfterAll static void tearDown() { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); } @ParameterizedTest diff --git a/src/test/java/org/sgrewritten/stargate/util/GateFormatReaderTest.java b/src/test/java/org/sgrewritten/stargate/util/GateFormatReaderTest.java index cdf856ff2..32de28d48 100644 --- a/src/test/java/org/sgrewritten/stargate/util/GateFormatReaderTest.java +++ b/src/test/java/org/sgrewritten/stargate/util/GateFormatReaderTest.java @@ -21,13 +21,13 @@ class GateFormatReaderTest { @BeforeAll static void setUp() { - MockBukkit.mock(); + StargateTestHelper.setup(); Stargate.setLogLevel(Level.FINEST); } @AfterAll static void tearDown() { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); Stargate.setLogLevel(Level.INFO); } diff --git a/src/test/java/org/sgrewritten/stargate/util/StargateTestHelper.java b/src/test/java/org/sgrewritten/stargate/util/StargateTestHelper.java index eb71af23a..b7cd01503 100644 --- a/src/test/java/org/sgrewritten/stargate/util/StargateTestHelper.java +++ b/src/test/java/org/sgrewritten/stargate/util/StargateTestHelper.java @@ -1,8 +1,12 @@ package org.sgrewritten.stargate.util; +import be.seeseemelk.mockbukkit.MockBukkit; +import be.seeseemelk.mockbukkit.ServerMock; import org.sgrewritten.stargate.Stargate; import org.sgrewritten.stargate.api.gate.GateFormatRegistry; import org.sgrewritten.stargate.gate.GateFormatHandler; +import org.sgrewritten.stargate.thread.task.StargateRegionTask; +import org.sgrewritten.stargate.thread.task.StargateTask; import java.io.File; import java.util.Objects; @@ -12,9 +16,21 @@ public class StargateTestHelper { private static final String SERVER_NAME = "test_server"; private static final File TEST_GATES_DIR = new File("src/test/resources/gates"); - public static void setup() { + public static ServerMock setup() { + ServerMock server = MockBukkit.mock(); GateFormatRegistry.setFormats(Objects.requireNonNull(GateFormatHandler.loadGateFormats(TEST_GATES_DIR))); Stargate.setServerName(SERVER_NAME); + return server; + } + + public static void tearDown(){ + runAllTasks(); + MockBukkit.unmock(); + } + + public static void runAllTasks(){ + StargateTask.forceRunAllTasks(); + StargateRegionTask.clearPopulator(); } } diff --git a/src/test/java/org/sgrewritten/stargate/util/TranslatableMessageFormatterTest.java b/src/test/java/org/sgrewritten/stargate/util/TranslatableMessageFormatterTest.java index 82de56b0d..7d9e1c91d 100644 --- a/src/test/java/org/sgrewritten/stargate/util/TranslatableMessageFormatterTest.java +++ b/src/test/java/org/sgrewritten/stargate/util/TranslatableMessageFormatterTest.java @@ -21,12 +21,12 @@ class TranslatableMessageFormatterTest { @BeforeEach void setUp() { - server = MockBukkit.mock(); + server = StargateTestHelper.setup(); } @AfterEach void tearDown() { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); } @Test diff --git a/src/test/java/org/sgrewritten/stargate/util/portal/TeleportationHelperTest.java b/src/test/java/org/sgrewritten/stargate/util/portal/TeleportationHelperTest.java index 3a16408cc..a7c901c0d 100644 --- a/src/test/java/org/sgrewritten/stargate/util/portal/TeleportationHelperTest.java +++ b/src/test/java/org/sgrewritten/stargate/util/portal/TeleportationHelperTest.java @@ -25,6 +25,7 @@ import org.sgrewritten.stargate.network.StargateNetwork; import org.sgrewritten.stargate.network.StorageType; import org.sgrewritten.stargate.network.portal.PortalFactory; +import org.sgrewritten.stargate.util.StargateTestHelper; import java.io.File; import java.util.ArrayList; @@ -43,7 +44,7 @@ class TeleportationHelperTest { @BeforeAll public static void setUp() throws NameLengthException, InvalidNameException, UnimplementedFlagException { - ServerMock server = MockBukkit.mock(); + ServerMock server = StargateTestHelper.setup(); world = server.addSimpleWorld("world"); network = new StargateNetwork("network", NetworkType.CUSTOM, StorageType.LOCAL); GateFormatRegistry.setFormats(Objects.requireNonNull(GateFormatHandler.loadGateFormats(TEST_GATES_DIR))); @@ -51,7 +52,7 @@ public static void setUp() throws NameLengthException, InvalidNameException, Uni @AfterAll public static void tearDown() { - MockBukkit.unmock(); + StargateTestHelper.tearDown(); } @Test