From e35bfe758c9cc82f899401d5cfacf3d26737d6c7 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Mon, 21 Jun 2021 18:21:55 +0200 Subject: [PATCH] Update to Junit5 and allow for mcMMO to be compiled on Java 16 (#4552) * Updated all unit tests to Junit5 and removed Powermock * Some formatting * I did not mean to commit this file * Fixed file ending * We don't need these extra dependencies * Replaced delegate with Junit5 assertThrows * Revert this change * Re-add mockito-core * A bit more refactoring * Update surefire and remove failsafe * Updated Mockito * Add failsafe back * Bump Mockito to 3.11.2 They literally just released that. --- pom.xml | 48 ++-- .../database/FlatFileDataProcessorTest.java | 23 +- .../database/FlatFileDatabaseManagerTest.java | 52 ++-- .../flatfile/FlatFileDataUtilTest.java | 31 +- .../datatypes/BlockLocationHistoryTest.java | 22 +- .../util/blockmeta/ChunkStoreTest.java | 268 +++++++++--------- .../nossr50/util/text/TextUtilsTest.java | 7 +- 7 files changed, 234 insertions(+), 217 deletions(-) diff --git a/pom.xml b/pom.xml index bdd226ad2..5ca833020 100755 --- a/pom.xml +++ b/pom.xml @@ -72,11 +72,21 @@ maven-surefire-plugin - 2.22.2 + 3.0.0-M5 + + + org.junit.jupiter:junit-jupiter + false + maven-failsafe-plugin - 2.22.2 + 3.0.0-M5 + + + org.junit.jupiter:junit-jupiter + false + @@ -324,38 +334,20 @@ org.junit.jupiter - junit-jupiter-api + junit-jupiter 5.8.0-M1 test - - org.junit.jupiter - junit-jupiter-engine - 5.8.0-M1 - test - - - org.junit.vintage - junit-vintage-engine - 5.6.2 - test - - - org.powermock - powermock-module-junit4 - 2.0.9 - test - - - org.powermock - powermock-api-mockito2 - 2.0.9 - test - org.mockito mockito-core - 3.9.0 + 3.11.2 + test + + + org.mockito + mockito-inline + 3.11.2 test diff --git a/src/test/java/com/gmail/nossr50/database/FlatFileDataProcessorTest.java b/src/test/java/com/gmail/nossr50/database/FlatFileDataProcessorTest.java index 2acbd41f8..6f1847fae 100644 --- a/src/test/java/com/gmail/nossr50/database/FlatFileDataProcessorTest.java +++ b/src/test/java/com/gmail/nossr50/database/FlatFileDataProcessorTest.java @@ -1,23 +1,26 @@ package com.gmail.nossr50.database; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; -public class FlatFileDataProcessorTest { +class FlatFileDataProcessorTest { @SuppressWarnings("ResultOfMethodCallIgnored") @Test - public void testGetExpectedValueType() { - for(int i = 0; i < FlatFileDatabaseManager.DATA_ENTRY_COUNT; i++) { + void testGetExpectedValueType() { + for (int i = 0; i < FlatFileDatabaseManager.DATA_ENTRY_COUNT; i++) { FlatFileDataProcessor.getExpectedValueType(i); } } @SuppressWarnings("ResultOfMethodCallIgnored") - @Test(expected = IndexOutOfBoundsException.class) - public void testGetExpectedValueTypeException() { - for(int i = 0; i < FlatFileDatabaseManager.DATA_ENTRY_COUNT+1; i++) { - FlatFileDataProcessor.getExpectedValueType(i); - } + @Test + void testGetExpectedValueTypeException() { + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { + for (int i = 0; i < FlatFileDatabaseManager.DATA_ENTRY_COUNT + 1; i++) { + FlatFileDataProcessor.getExpectedValueType(i); + } + }); } -} \ No newline at end of file +} diff --git a/src/test/java/com/gmail/nossr50/database/FlatFileDatabaseManagerTest.java b/src/test/java/com/gmail/nossr50/database/FlatFileDatabaseManagerTest.java index cd1cd6f54..f626002c1 100644 --- a/src/test/java/com/gmail/nossr50/database/FlatFileDatabaseManagerTest.java +++ b/src/test/java/com/gmail/nossr50/database/FlatFileDatabaseManagerTest.java @@ -32,7 +32,7 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.mock; //This class uses JUnit5/Jupiter -public class FlatFileDatabaseManagerTest { +class FlatFileDatabaseManagerTest { public static final @NotNull String TEST_FILE_NAME = "test.mcmmo.users"; public static final @NotNull String BAD_FILE_LINE_ONE = "mrfloris:2420:::0:2452:0:1983:1937:1790:3042:1138:3102:2408:3411:0:0:0:0:0:0:0:0::642:0:1617583171:0:1617165043:0:1617583004:1617563189:1616785408::2184:0:0:1617852413:HEARTS:415:0:631e3896-da2a-4077-974b-d047859d76bc:5:1600906906:"; @@ -72,7 +72,7 @@ public class FlatFileDatabaseManagerTest { } @BeforeEach - public void init() { + void init() { assertNull(db); //noinspection UnstableApiUsage tempDir = Files.createTempDir(); @@ -84,7 +84,7 @@ public class FlatFileDatabaseManagerTest { } @AfterEach - public void tearDown() { + void tearDown() { recursiveDelete(tempDir); db = null; } @@ -147,18 +147,18 @@ public class FlatFileDatabaseManagerTest { }; @Test - public void testDefaultInit() { + void testDefaultInit() { db = new FlatFileDatabaseManager(getTemporaryUserFilePath(), logger, PURGE_TIME, 0); } @Test - public void testUpdateLeaderboards() { + void testUpdateLeaderboards() { assertNotNull(db); assertEquals(LeaderboardStatus.UPDATED, db.updateLeaderboards()); } @Test - public void testSaveUser() { + void testSaveUser() { //Make a Profile to save and check to see if it worked UUID uuid = UUID.fromString("588fe472-1c82-4c4e-9aa1-7eefccb277e3"); String playerName = "nossr50"; @@ -194,7 +194,7 @@ public class FlatFileDatabaseManagerTest { } @Test - public void testAddedMissingLastLoginValues() { + void testAddedMissingLastLoginValues() { File dbFile = prepareDatabaseTestResource(DB_MISSING_LAST_LOGIN); //This makes sure our private method is working before the tests run afterwards @@ -214,7 +214,7 @@ public class FlatFileDatabaseManagerTest { } @Test - public void testLoadByName() { + void testLoadByName() { File healthyDB = prepareDatabaseTestResource(DB_HEALTHY); /* @@ -244,7 +244,7 @@ public class FlatFileDatabaseManagerTest { } @Test - public void testNewUser() { + void testNewUser() { //We will test that new user values line up with our expectations UUID uuid = new UUID(0, 1); String playerName = "nossr50"; @@ -277,7 +277,7 @@ public class FlatFileDatabaseManagerTest { } @Test - public void testAddingUsersToEndOfExistingDB() { + void testAddingUsersToEndOfExistingDB() { //We will test that new user values line up with our expectations UUID uuid = new UUID(0, 80); String playerName = "the_kitty_man"; @@ -335,7 +335,7 @@ public class FlatFileDatabaseManagerTest { } @Test - public void testLoadByUUID() { + void testLoadByUUID() { File dbFile = prepareDatabaseTestResource(DB_HEALTHY); /* @@ -367,7 +367,7 @@ public class FlatFileDatabaseManagerTest { } @Test - public void testLoadByUUIDAndName() { + void testLoadByUUIDAndName() { File dbFile = prepareDatabaseTestResource(DB_HEALTHY); /* @@ -568,14 +568,14 @@ public class FlatFileDatabaseManagerTest { } @Test - public void testOverwriteName() { + void testOverwriteName() { overwriteDataAndCheckForFlag(db, duplicateNameDatabaseData, FlatFileDataFlag.DUPLICATE_NAME); ArrayList splitDataLines = getSplitDataFromFile(db.getUsersFile()); assertNotEquals(splitDataLines.get(1)[0], splitDataLines.get(0)[0]); //Name comparison } @Test - public void testDataNotFound() { + void testDataNotFound() { //Save the zero version and see if it looks correct assertNotNull(db); assertTrue(db.getUsersFile().exists()); @@ -587,14 +587,14 @@ public class FlatFileDatabaseManagerTest { } @Test - public void testPurgePowerlessUsers() { + void testPurgePowerlessUsers() { replaceDataInFile(db, normalDatabaseData); int purgeCount = db.purgePowerlessUsers(); assertEquals(purgeCount, 1); //1 User should have been purged } @Test - public void testCheckFileHealthAndStructure() { + void testCheckFileHealthAndStructure() { replaceDataInFile(db, badDatabaseData); List dataFlags = db.checkFileHealthAndStructure(); @@ -603,48 +603,48 @@ public class FlatFileDatabaseManagerTest { } @Test - public void testFindFixableDuplicateNames() { + void testFindFixableDuplicateNames() { overwriteDataAndCheckForFlag(db, duplicateNameDatabaseData, FlatFileDataFlag.DUPLICATE_NAME); } @Test - public void testFindDuplicateUUIDs() { + void testFindDuplicateUUIDs() { overwriteDataAndCheckForFlag(db, duplicateUUIDDatabaseData, FlatFileDataFlag.DUPLICATE_UUID); } @Test() - public void findBadUUIDData() { + void findBadUUIDData() { overwriteDataAndCheckForFlag(db, badUUIDDatabaseData, FlatFileDataFlag.BAD_UUID_DATA); } @Test - public void testFindCorruptData() { + void testFindCorruptData() { overwriteDataAndCheckForFlag(db, corruptDatabaseData, FlatFileDataFlag.CORRUPTED_OR_UNRECOGNIZABLE); } @Test - public void testFindEmptyNames() { + void testFindEmptyNames() { overwriteDataAndCheckForFlag(db, emptyNameDatabaseData, FlatFileDataFlag.MISSING_NAME); } @Test - public void testFindBadValues() { + void testFindBadValues() { overwriteDataAndCheckForFlag(db, badDatabaseData, FlatFileDataFlag.BAD_VALUES); } @Test - public void testFindOutdatedData() { + void testFindOutdatedData() { overwriteDataAndCheckForFlag(db, outdatedDatabaseData, FlatFileDataFlag.INCOMPLETE); } @Test - public void testGetDatabaseType() { + void testGetDatabaseType() { assertNotNull(db); assertEquals(db.getDatabaseType(), DatabaseType.FLATFILE); } @Test - public void testReadRank() { + void testReadRank() { //This is an empty DB assertNotNull(db); String rankBoyName = "rankBoy"; @@ -674,7 +674,7 @@ public class FlatFileDatabaseManagerTest { } @Test - public void testLoadFromFile() { + void testLoadFromFile() { ClassLoader classLoader = getClass().getClassLoader(); URI resourceFileURI = null; diff --git a/src/test/java/com/gmail/nossr50/database/flatfile/FlatFileDataUtilTest.java b/src/test/java/com/gmail/nossr50/database/flatfile/FlatFileDataUtilTest.java index ee6e71ffb..b8e43ccbe 100644 --- a/src/test/java/com/gmail/nossr50/database/flatfile/FlatFileDataUtilTest.java +++ b/src/test/java/com/gmail/nossr50/database/flatfile/FlatFileDataUtilTest.java @@ -1,27 +1,34 @@ package com.gmail.nossr50.database.flatfile; -import com.gmail.nossr50.database.FlatFileDatabaseManager; -import org.junit.Test; - import java.util.HashSet; -public class FlatFileDataUtilTest { +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import com.gmail.nossr50.database.FlatFileDatabaseManager; + +class FlatFileDataUtilTest { @Test - public void getPreparedSaveDataLine() { + void getPreparedSaveDataLine() { + } @Test - public void repairBadData() { + void repairBadData() { + } @Test - public void getZeroInitialisedData() { + void getZeroInitialisedData() { + } - @Test(expected = AssertionError.class) - public void testTooManyDataEntriesSplitString() { - FlatFileDataContainer dataContainer = new CategorizedFlatFileData(0, new HashSet<>(), new String[FlatFileDatabaseManager.DATA_ENTRY_COUNT + 1]); - FlatFileDataUtil.getPreparedSaveDataLine(dataContainer); + @Test + void testTooManyDataEntriesSplitString() { + Assertions.assertThrows(AssertionError.class, () -> { + FlatFileDataContainer dataContainer = new CategorizedFlatFileData(0, new HashSet<>(), new String[FlatFileDatabaseManager.DATA_ENTRY_COUNT + 1]); + FlatFileDataUtil.getPreparedSaveDataLine(dataContainer); + }); } -} \ No newline at end of file +} diff --git a/src/test/java/com/gmail/nossr50/datatypes/BlockLocationHistoryTest.java b/src/test/java/com/gmail/nossr50/datatypes/BlockLocationHistoryTest.java index 5e9499a17..aa1ce3afa 100644 --- a/src/test/java/com/gmail/nossr50/datatypes/BlockLocationHistoryTest.java +++ b/src/test/java/com/gmail/nossr50/datatypes/BlockLocationHistoryTest.java @@ -1,12 +1,12 @@ package com.gmail.nossr50.datatypes; import org.bukkit.Location; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; -public class BlockLocationHistoryTest { +class BlockLocationHistoryTest { @Test - public void testRemovesOldestElement() { + void testRemovesOldestElement() { BlockLocationHistory history = new BlockLocationHistory(2); Location locationA = new Location(null, 0, 1, 2); Location locationB = new Location(null, 1, 2, 3); @@ -15,13 +15,13 @@ public class BlockLocationHistoryTest { history.add(locationA); history.add(locationB); history.add(locationC); - Assert.assertFalse(history.contains(locationA)); - Assert.assertTrue(history.contains(locationB)); - Assert.assertTrue(history.contains(locationC)); + Assertions.assertFalse(history.contains(locationA)); + Assertions.assertTrue(history.contains(locationB)); + Assertions.assertTrue(history.contains(locationC)); } @Test - public void testSupportsDuplicateElement() { + void testSupportsDuplicateElement() { BlockLocationHistory history = new BlockLocationHistory(2); Location locationA = new Location(null, 0, 1, 2); Location locationB = new Location(null, 1, 2, 3); @@ -29,9 +29,9 @@ public class BlockLocationHistoryTest { history.add(locationA); history.add(locationA); history.add(locationB); - Assert.assertTrue(history.contains(locationA)); - Assert.assertTrue(history.contains(locationB)); + Assertions.assertTrue(history.contains(locationA)); + Assertions.assertTrue(history.contains(locationB)); history.add(locationB); - Assert.assertFalse(history.contains(locationA)); + Assertions.assertFalse(history.contains(locationA)); } } diff --git a/src/test/java/com/gmail/nossr50/util/blockmeta/ChunkStoreTest.java b/src/test/java/com/gmail/nossr50/util/blockmeta/ChunkStoreTest.java index 774b55f8d..f7f5a5f75 100644 --- a/src/test/java/com/gmail/nossr50/util/blockmeta/ChunkStoreTest.java +++ b/src/test/java/com/gmail/nossr50/util/blockmeta/ChunkStoreTest.java @@ -1,43 +1,54 @@ package com.gmail.nossr50.util.blockmeta; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.OutputStream; +import java.io.Serializable; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; + import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.util.BlockUtils; import com.gmail.nossr50.util.compat.CompatibilityManager; import com.gmail.nossr50.util.compat.layers.world.WorldCompatibilityLayer; import com.gmail.nossr50.util.platform.PlatformManager; import com.google.common.io.Files; -import org.bukkit.Bukkit; -import org.bukkit.World; -import org.bukkit.block.Block; -import org.jetbrains.annotations.NotNull; -import org.junit.*; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; - -import java.io.*; -import java.util.UUID; - -import static org.mockito.Mockito.mock; /** - * Could be a lot better. But some tests are better than none! Tests the major things, still kinda unit-testy. Verifies that the serialization isn't completely broken. + * Could be a lot better. But some tests are better than none! Tests the major things, still kinda unit-testy. Verifies + * that the serialization isn't completely broken. */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({ Bukkit.class, mcMMO.class}) -public class ChunkStoreTest { +class ChunkStoreTest { + public static final int LEGACY_WORLD_HEIGHT_MAX = 256; public static final int LEGACY_WORLD_HEIGHT_MIN = 0; private static File tempDir; - @BeforeClass + + @BeforeAll public static void setUpClass() { tempDir = Files.createTempDir(); } - @AfterClass + @AfterAll public static void tearDownClass() { recursiveDelete(tempDir); } @@ -46,99 +57,114 @@ public class ChunkStoreTest { private CompatibilityManager compatibilityManager; private WorldCompatibilityLayer worldCompatibilityLayer; private PlatformManager platformManager; + + private MockedStatic bukkitMock; + private MockedStatic mcMMOMock; - @Before - public void setUpMock(){ + @BeforeEach + void setUpMock() { UUID worldUUID = UUID.randomUUID(); - mockWorld = mock(World.class); + mockWorld = Mockito.mock(World.class); Mockito.when(mockWorld.getUID()).thenReturn(worldUUID); Mockito.when(mockWorld.getMaxHeight()).thenReturn(256); Mockito.when(mockWorld.getWorldFolder()).thenReturn(tempDir); - PowerMockito.mockStatic(Bukkit.class); - Mockito.when(Bukkit.getWorld(worldUUID)).thenReturn(mockWorld); - platformManager = mock(PlatformManager.class); - compatibilityManager = mock(CompatibilityManager.class); - worldCompatibilityLayer = mock(WorldCompatibilityLayer.class); + bukkitMock = Mockito.mockStatic(Bukkit.class); + bukkitMock.when(() -> Bukkit.getWorld(worldUUID)).thenReturn(mockWorld); - Whitebox.setInternalState(mcMMO.class, "platformManager", platformManager); - Mockito.when(mcMMO.getCompatibilityManager()).thenReturn(compatibilityManager); + platformManager = Mockito.mock(PlatformManager.class); + compatibilityManager = Mockito.mock(CompatibilityManager.class); + worldCompatibilityLayer = Mockito.mock(WorldCompatibilityLayer.class); + + mcMMOMock = Mockito.mockStatic(mcMMO.class); + + mcMMOMock.when(() -> mcMMO.getPlatformManager()).thenReturn(platformManager); + Assertions.assertNotNull(mcMMO.getPlatformManager()); + + mcMMOMock.when(() -> mcMMO.getCompatibilityManager()).thenReturn(compatibilityManager); + Assertions.assertNotNull(mcMMO.getCompatibilityManager()); - Assert.assertNotNull(mcMMO.getCompatibilityManager()); Mockito.when(platformManager.getCompatibilityManager()).thenReturn(compatibilityManager); Mockito.when(platformManager.getCompatibilityManager().getWorldCompatibilityLayer()).thenReturn(worldCompatibilityLayer); - Assert.assertNotNull(mcMMO.getCompatibilityManager().getWorldCompatibilityLayer()); + Assertions.assertNotNull(mcMMO.getCompatibilityManager().getWorldCompatibilityLayer()); Mockito.when(worldCompatibilityLayer.getMinWorldHeight(mockWorld)).thenReturn(LEGACY_WORLD_HEIGHT_MIN); Mockito.when(worldCompatibilityLayer.getMaxWorldHeight(mockWorld)).thenReturn(LEGACY_WORLD_HEIGHT_MAX); - } - @Test(expected = IndexOutOfBoundsException.class) - public void testIndexOutOfBounds() { - Mockito.when(mcMMO.getCompatibilityManager().getWorldCompatibilityLayer().getMinWorldHeight(mockWorld)).thenReturn(-64); - HashChunkManager hashChunkManager = new HashChunkManager(); - - //Top Block - Block illegalHeightBlock = initMockBlock(1337, 256, -1337); - Assert.assertFalse(hashChunkManager.isTrue(illegalHeightBlock)); - hashChunkManager.setTrue(illegalHeightBlock); + } + + @AfterEach + void teardownMock() { + bukkitMock.close(); + mcMMOMock.close(); } @Test - public void testSetTrue() { + void testIndexOutOfBounds() { Mockito.when(mcMMO.getCompatibilityManager().getWorldCompatibilityLayer().getMinWorldHeight(mockWorld)).thenReturn(-64); HashChunkManager hashChunkManager = new HashChunkManager(); - int radius = 2; //Could be anything but drastically changes test time - for(int x = -radius; x <= radius; x++) { - for(int y = mockWorld.getMinHeight(); y < mockWorld.getMaxHeight(); y++) { - for(int z = -radius; z <= radius; z++) { + // Top Block + Block illegalHeightBlock = initMockBlock(1337, 256, -1337); + Assertions.assertFalse(hashChunkManager.isTrue(illegalHeightBlock)); + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> hashChunkManager.setTrue(illegalHeightBlock)); + } + + @Test + void testSetTrue() { + Mockito.when(mcMMO.getCompatibilityManager().getWorldCompatibilityLayer().getMinWorldHeight(mockWorld)).thenReturn(-64); + HashChunkManager hashChunkManager = new HashChunkManager(); + int radius = 2; // Could be anything but drastically changes test time + + for (int x = -radius; x <= radius; x++) { + for (int y = mockWorld.getMinHeight(); y < mockWorld.getMaxHeight(); y++) { + for (int z = -radius; z <= radius; z++) { Block testBlock = initMockBlock(x, y, z); hashChunkManager.setTrue(testBlock); - Assert.assertTrue(hashChunkManager.isTrue(testBlock)); + Assertions.assertTrue(hashChunkManager.isTrue(testBlock)); hashChunkManager.setFalse(testBlock); - Assert.assertFalse(hashChunkManager.isTrue(testBlock)); + Assertions.assertFalse(hashChunkManager.isTrue(testBlock)); } } } - //Bot Block + // Bot Block Block bottomBlock = initMockBlock(1337, 0, -1337); - Assert.assertFalse(hashChunkManager.isTrue(bottomBlock)); + Assertions.assertFalse(hashChunkManager.isTrue(bottomBlock)); - Assert.assertTrue(BlockUtils.isWithinWorldBounds(worldCompatibilityLayer, bottomBlock)); + Assertions.assertTrue(BlockUtils.isWithinWorldBounds(worldCompatibilityLayer, bottomBlock)); hashChunkManager.setTrue(bottomBlock); - Assert.assertTrue(hashChunkManager.isTrue(bottomBlock)); + Assertions.assertTrue(hashChunkManager.isTrue(bottomBlock)); - //Top Block + // Top Block Block topBlock = initMockBlock(1337, 255, -1337); - Assert.assertFalse(hashChunkManager.isTrue(topBlock)); + Assertions.assertFalse(hashChunkManager.isTrue(topBlock)); - Assert.assertTrue(BlockUtils.isWithinWorldBounds(worldCompatibilityLayer, topBlock)); + Assertions.assertTrue(BlockUtils.isWithinWorldBounds(worldCompatibilityLayer, topBlock)); hashChunkManager.setTrue(topBlock); - Assert.assertTrue(hashChunkManager.isTrue(topBlock)); + Assertions.assertTrue(hashChunkManager.isTrue(topBlock)); } @Test - public void testSetValue() { + void testSetValue() { BitSetChunkStore original = new BitSetChunkStore(mockWorld, 0, 0); original.setTrue(0, 0, 0); - Assert.assertTrue(original.isTrue(0, 0, 0)); + Assertions.assertTrue(original.isTrue(0, 0, 0)); original.setFalse(0, 0, 0); - Assert.assertFalse(original.isTrue(0, 0, 0)); + Assertions.assertFalse(original.isTrue(0, 0, 0)); } @Test - public void testIsEmpty() { + void testIsEmpty() { BitSetChunkStore original = new BitSetChunkStore(mockWorld, 0, 0); - Assert.assertTrue(original.isEmpty()); + Assertions.assertTrue(original.isEmpty()); original.setTrue(0, 0, 0); original.setFalse(0, 0, 0); - Assert.assertTrue(original.isEmpty()); + Assertions.assertTrue(original.isEmpty()); } @Test - public void testRoundTrip() throws IOException { + void testRoundTrip() throws IOException { BitSetChunkStore original = new BitSetChunkStore(mockWorld, 1, 2); original.setTrue(14, 89, 12); original.setTrue(14, 90, 12); @@ -149,7 +175,7 @@ public class ChunkStoreTest { } @Test - public void testNegativeWorldMin() throws IOException { + void testNegativeWorldMin() throws IOException { Mockito.when(mcMMO.getCompatibilityManager().getWorldCompatibilityLayer().getMinWorldHeight(mockWorld)).thenReturn(-64); BitSetChunkStore original = new BitSetChunkStore(mockWorld, 1, 2); @@ -162,7 +188,7 @@ public class ChunkStoreTest { } @Test - public void testNegativeWorldMinUpgrade() throws IOException { + void testNegativeWorldMinUpgrade() throws IOException { BitSetChunkStore original = new BitSetChunkStore(mockWorld, 1, 2); original.setTrue(14, 1, 12); original.setTrue(14, 2, 12); @@ -175,16 +201,16 @@ public class ChunkStoreTest { } @Test - public void testChunkCoords() throws IOException { + void testChunkCoords() throws IOException { for (int x = -96; x < 0; x++) { - int cx = x >> 4; - int ix = Math.abs(x) % 16; - System.out.print(cx + ":" + ix + " "); + int cx = x >> 4; + int ix = Math.abs(x) % 16; + System.out.print(cx + ":" + ix + " "); } } @Test - public void testUpgrade() throws IOException { + void testUpgrade() throws IOException { LegacyChunkStore original = new LegacyChunkStore(mockWorld, 12, 32); original.setTrue(14, 89, 12); original.setTrue(14, 90, 12); @@ -195,21 +221,20 @@ public class ChunkStoreTest { } @Test - public void testSimpleRegionRoundtrip() throws IOException { + void testSimpleRegionRoundtrip() throws IOException { LegacyChunkStore original = new LegacyChunkStore(mockWorld, 12, 12); original.setTrue(14, 89, 12); original.setTrue(14, 90, 12); original.setTrue(13, 89, 12); File file = new File(tempDir, "SimpleRegionRoundTrip.region"); McMMOSimpleRegionFile region = new McMMOSimpleRegionFile(file, 0, 0); - try (DataOutputStream outputStream = region.getOutputStream(12, 12)){ + try (DataOutputStream outputStream = region.getOutputStream(12, 12)) { outputStream.write(serializeChunkstore(original)); } region.close(); region = new McMMOSimpleRegionFile(file, 0, 0); - try (DataInputStream is = region.getInputStream(original.getChunkX(), original.getChunkZ())) - { - Assert.assertNotNull(is); + try (DataInputStream is = region.getInputStream(original.getChunkX(), original.getChunkZ())) { + Assertions.assertNotNull(is); ChunkStore deserialized = BitSetChunkStore.Serialization.readChunkStore(is); assertEqual(original, deserialized); } @@ -218,36 +243,36 @@ public class ChunkStoreTest { } @Test - public void testSimpleRegionRejectsOutOfBounds() { + void testSimpleRegionRejectsOutOfBounds() { File file = new File(tempDir, "SimpleRegionRoundTrip.region"); McMMOSimpleRegionFile region = new McMMOSimpleRegionFile(file, 0, 0); - assertThrows(() -> region.getOutputStream(-1, 0), IndexOutOfBoundsException.class); - assertThrows(() -> region.getOutputStream(0, -1), IndexOutOfBoundsException.class); - assertThrows(() -> region.getOutputStream(32, 0), IndexOutOfBoundsException.class); - assertThrows(() -> region.getOutputStream(0, 32), IndexOutOfBoundsException.class); + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> region.getOutputStream(-1, 0)); + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> region.getOutputStream(0, -1)); + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> region.getOutputStream(32, 0)); + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> region.getOutputStream(0, 32)); region.close(); } @Test - public void testChunkStoreRejectsOutOfBounds() { + void testChunkStoreRejectsOutOfBounds() { ChunkStore chunkStore = new BitSetChunkStore(mockWorld, 0, 0); - assertThrows(() -> chunkStore.setTrue(-1, 0, 0), IndexOutOfBoundsException.class); - assertThrows(() -> chunkStore.setTrue(0, -1, 0), IndexOutOfBoundsException.class); - assertThrows(() -> chunkStore.setTrue(0, 0, -1), IndexOutOfBoundsException.class); - assertThrows(() -> chunkStore.setTrue(16, 0, 0), IndexOutOfBoundsException.class); - assertThrows(() -> chunkStore.setTrue(0, mockWorld.getMaxHeight(), 0), IndexOutOfBoundsException.class); - assertThrows(() -> chunkStore.setTrue(0, 0, 16), IndexOutOfBoundsException.class); + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> chunkStore.setTrue(-1, 0, 0)); + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> chunkStore.setTrue(0, -1, 0)); + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> chunkStore.setTrue(0, 0, -1)); + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> chunkStore.setTrue(16, 0, 0)); + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> chunkStore.setTrue(0, mockWorld.getMaxHeight(), 0)); + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> chunkStore.setTrue(0, 0, 16)); } @Test - public void testRegressionChunkMirrorBug() { + void testRegressionChunkMirrorBug() { ChunkManager chunkManager = new HashChunkManager(); - Block mockBlockA = mock(Block.class); + Block mockBlockA = Mockito.mock(Block.class); Mockito.when(mockBlockA.getX()).thenReturn(15); Mockito.when(mockBlockA.getZ()).thenReturn(15); Mockito.when(mockBlockA.getY()).thenReturn(0); Mockito.when(mockBlockA.getWorld()).thenReturn(mockWorld); - Block mockBlockB = mock(Block.class); + Block mockBlockB = Mockito.mock(Block.class); Mockito.when(mockBlockB.getX()).thenReturn(-15); Mockito.when(mockBlockB.getZ()).thenReturn(-15); Mockito.when(mockBlockB.getY()).thenReturn(0); @@ -255,42 +280,25 @@ public class ChunkStoreTest { chunkManager.setTrue(mockBlockA); chunkManager.setFalse(mockBlockB); - Assert.assertTrue(chunkManager.isTrue(mockBlockA)); + Assertions.assertTrue(chunkManager.isTrue(mockBlockA)); } - private interface Delegate { - - void run(); - } - private void assertThrows(@NotNull Delegate delegate, @NotNull Class clazz) { - try { - delegate.run(); - Assert.fail(); // We didn't throw - } - catch (Throwable t) { - Assert.assertTrue(t.getClass().equals(clazz)); - } - } - - private void assertEqual(ChunkStore expected, ChunkStore actual) - { - Assert.assertEquals(expected.getChunkMin(), actual.getChunkMin()); - Assert.assertEquals(expected.getChunkMax(), actual.getChunkMax()); + private void assertEqual(ChunkStore expected, ChunkStore actual) { + Assertions.assertEquals(expected.getChunkMin(), actual.getChunkMin()); + Assertions.assertEquals(expected.getChunkMax(), actual.getChunkMax()); assertEqualIgnoreMinMax(expected, actual); } - private void assertEqualIgnoreMinMax(ChunkStore expected, ChunkStore actual) - { - Assert.assertEquals(expected.getChunkX(), actual.getChunkX()); - Assert.assertEquals(expected.getChunkZ(), actual.getChunkZ()); - Assert.assertEquals(expected.getWorldId(), actual.getWorldId()); - for (int y = Math.min(actual.getChunkMin(), expected.getChunkMin()); y < Math.max(actual.getChunkMax(), expected.getChunkMax()); y++) - { + private void assertEqualIgnoreMinMax(ChunkStore expected, ChunkStore actual) { + Assertions.assertEquals(expected.getChunkX(), actual.getChunkX()); + Assertions.assertEquals(expected.getChunkZ(), actual.getChunkZ()); + Assertions.assertEquals(expected.getWorldId(), actual.getWorldId()); + for (int y = Math.min(actual.getChunkMin(), expected.getChunkMin()); y < Math.max(actual.getChunkMax(), expected.getChunkMax()); y++) { if (expected.getChunkMin() > y || actual.getChunkMin() > y || expected.getChunkMax() <= y || actual.getChunkMax() <= y) continue; // Ignore for (int x = 0; x < 16; x++) for (int z = 0; z < 16; z++) - Assert.assertTrue(expected.isTrue(x, y, z) == actual.isTrue(x, y, z)); + Assertions.assertEquals(expected.isTrue(x, y, z), actual.isTrue(x, y, z)); } } @@ -299,7 +307,9 @@ public class ChunkStoreTest { if (chunkStore instanceof BitSetChunkStore) BitSetChunkStore.Serialization.writeChunkStore(new DataOutputStream(byteArrayOutputStream), chunkStore); else - new UnitTestObjectOutputStream(byteArrayOutputStream).writeObject(chunkStore); // Serializes the class as if it were the old PrimitiveChunkStore + new UnitTestObjectOutputStream(byteArrayOutputStream).writeObject(chunkStore); // Serializes the class as if + // it were the old + // PrimitiveChunkStore return byteArrayOutputStream.toByteArray(); } @@ -313,6 +323,7 @@ public class ChunkStoreTest { private final int cx; private final int cz; private final @NotNull UUID worldUid; + public LegacyChunkStore(@NotNull World world, int cx, int cz) { this.cx = cx; this.cz = cz; @@ -416,11 +427,13 @@ public class ChunkStoreTest { } } + private static class UnitTestObjectOutputStream extends ObjectOutputStream { public UnitTestObjectOutputStream(@NotNull OutputStream outputStream) throws IOException { super(outputStream); } + @Override public void writeUTF(@NotNull String str) throws IOException { // Pretend to be the old class @@ -430,14 +443,15 @@ public class ChunkStoreTest { } } + @NotNull private Block initMockBlock(int x, int y, int z) { - Block testBlock = mock(Block.class); - Mockito.when(testBlock.getX()).thenReturn(x); - Mockito.when(testBlock.getY()).thenReturn(y); - Mockito.when(testBlock.getZ()).thenReturn(z); - Mockito.when(testBlock.getWorld()).thenReturn(mockWorld); - return testBlock; + Block mockBlock = Mockito.mock(Block.class); + Mockito.when(mockBlock.getX()).thenReturn(x); + Mockito.when(mockBlock.getY()).thenReturn(y); + Mockito.when(mockBlock.getZ()).thenReturn(z); + Mockito.when(mockBlock.getWorld()).thenReturn(mockWorld); + return mockBlock; } public static void recursiveDelete(@NotNull File directoryToBeDeleted) { diff --git a/src/test/java/com/gmail/nossr50/util/text/TextUtilsTest.java b/src/test/java/com/gmail/nossr50/util/text/TextUtilsTest.java index da1e021db..cefbe7010 100644 --- a/src/test/java/com/gmail/nossr50/util/text/TextUtilsTest.java +++ b/src/test/java/com/gmail/nossr50/util/text/TextUtilsTest.java @@ -14,10 +14,10 @@ import org.junit.jupiter.api.Test; * See https://github.com/mcMMO-Dev/mcMMO/pull/4446 * */ -public class TextUtilsTest { +class TextUtilsTest { @Test - public void testColorizeText() { + void testColorizeText() { String inputText = "&4This text should be red."; /* @@ -26,6 +26,7 @@ public class TextUtilsTest { */ TextComponent component = TextUtils.colorizeText(inputText); - Assertions.assertEquals(NamedTextColor.DARK_RED, component.color(), "Looks like Adventure is not working correctly."); + String message = "Looks like Adventure is not working correctly. We likely need to update our dependency!"; + Assertions.assertEquals(NamedTextColor.DARK_RED, component.color(), message); } }