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.
This commit is contained in:
TheBusyBiscuit 2021-06-21 18:21:55 +02:00 committed by GitHub
parent fffbacd239
commit e35bfe758c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 234 additions and 217 deletions

48
pom.xml
View File

@ -72,11 +72,21 @@
<plugins> <plugins>
<plugin> <plugin>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version> <version>3.0.0-M5</version>
<configuration>
<junitArtifactName>org.junit.jupiter:junit-jupiter</junitArtifactName>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin> </plugin>
<plugin> <plugin>
<artifactId>maven-failsafe-plugin</artifactId> <artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version> <version>3.0.0-M5</version>
<configuration>
<junitArtifactName>org.junit.jupiter:junit-jupiter</junitArtifactName>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin> </plugin>
<plugin> <plugin>
@ -324,38 +334,20 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.junit.jupiter</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId> <artifactId>junit-jupiter</artifactId>
<version>5.8.0-M1</version> <version>5.8.0-M1</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.0-M1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId> <artifactId>mockito-core</artifactId>
<version>3.9.0</version> <version>3.11.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>3.11.2</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -1,23 +1,26 @@
package com.gmail.nossr50.database; 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") @SuppressWarnings("ResultOfMethodCallIgnored")
@Test @Test
public void testGetExpectedValueType() { void testGetExpectedValueType() {
for (int i = 0; i < FlatFileDatabaseManager.DATA_ENTRY_COUNT; i++) { for (int i = 0; i < FlatFileDatabaseManager.DATA_ENTRY_COUNT; i++) {
FlatFileDataProcessor.getExpectedValueType(i); FlatFileDataProcessor.getExpectedValueType(i);
} }
} }
@SuppressWarnings("ResultOfMethodCallIgnored") @SuppressWarnings("ResultOfMethodCallIgnored")
@Test(expected = IndexOutOfBoundsException.class) @Test
public void testGetExpectedValueTypeException() { void testGetExpectedValueTypeException() {
Assertions.assertThrows(IndexOutOfBoundsException.class, () -> {
for (int i = 0; i < FlatFileDatabaseManager.DATA_ENTRY_COUNT + 1; i++) { for (int i = 0; i < FlatFileDatabaseManager.DATA_ENTRY_COUNT + 1; i++) {
FlatFileDataProcessor.getExpectedValueType(i); FlatFileDataProcessor.getExpectedValueType(i);
} }
});
} }
} }

View File

@ -32,7 +32,7 @@ import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
//This class uses JUnit5/Jupiter //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 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:"; 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 @BeforeEach
public void init() { void init() {
assertNull(db); assertNull(db);
//noinspection UnstableApiUsage //noinspection UnstableApiUsage
tempDir = Files.createTempDir(); tempDir = Files.createTempDir();
@ -84,7 +84,7 @@ public class FlatFileDatabaseManagerTest {
} }
@AfterEach @AfterEach
public void tearDown() { void tearDown() {
recursiveDelete(tempDir); recursiveDelete(tempDir);
db = null; db = null;
} }
@ -147,18 +147,18 @@ public class FlatFileDatabaseManagerTest {
}; };
@Test @Test
public void testDefaultInit() { void testDefaultInit() {
db = new FlatFileDatabaseManager(getTemporaryUserFilePath(), logger, PURGE_TIME, 0); db = new FlatFileDatabaseManager(getTemporaryUserFilePath(), logger, PURGE_TIME, 0);
} }
@Test @Test
public void testUpdateLeaderboards() { void testUpdateLeaderboards() {
assertNotNull(db); assertNotNull(db);
assertEquals(LeaderboardStatus.UPDATED, db.updateLeaderboards()); assertEquals(LeaderboardStatus.UPDATED, db.updateLeaderboards());
} }
@Test @Test
public void testSaveUser() { void testSaveUser() {
//Make a Profile to save and check to see if it worked //Make a Profile to save and check to see if it worked
UUID uuid = UUID.fromString("588fe472-1c82-4c4e-9aa1-7eefccb277e3"); UUID uuid = UUID.fromString("588fe472-1c82-4c4e-9aa1-7eefccb277e3");
String playerName = "nossr50"; String playerName = "nossr50";
@ -194,7 +194,7 @@ public class FlatFileDatabaseManagerTest {
} }
@Test @Test
public void testAddedMissingLastLoginValues() { void testAddedMissingLastLoginValues() {
File dbFile = prepareDatabaseTestResource(DB_MISSING_LAST_LOGIN); File dbFile = prepareDatabaseTestResource(DB_MISSING_LAST_LOGIN);
//This makes sure our private method is working before the tests run afterwards //This makes sure our private method is working before the tests run afterwards
@ -214,7 +214,7 @@ public class FlatFileDatabaseManagerTest {
} }
@Test @Test
public void testLoadByName() { void testLoadByName() {
File healthyDB = prepareDatabaseTestResource(DB_HEALTHY); File healthyDB = prepareDatabaseTestResource(DB_HEALTHY);
/* /*
@ -244,7 +244,7 @@ public class FlatFileDatabaseManagerTest {
} }
@Test @Test
public void testNewUser() { void testNewUser() {
//We will test that new user values line up with our expectations //We will test that new user values line up with our expectations
UUID uuid = new UUID(0, 1); UUID uuid = new UUID(0, 1);
String playerName = "nossr50"; String playerName = "nossr50";
@ -277,7 +277,7 @@ public class FlatFileDatabaseManagerTest {
} }
@Test @Test
public void testAddingUsersToEndOfExistingDB() { void testAddingUsersToEndOfExistingDB() {
//We will test that new user values line up with our expectations //We will test that new user values line up with our expectations
UUID uuid = new UUID(0, 80); UUID uuid = new UUID(0, 80);
String playerName = "the_kitty_man"; String playerName = "the_kitty_man";
@ -335,7 +335,7 @@ public class FlatFileDatabaseManagerTest {
} }
@Test @Test
public void testLoadByUUID() { void testLoadByUUID() {
File dbFile = prepareDatabaseTestResource(DB_HEALTHY); File dbFile = prepareDatabaseTestResource(DB_HEALTHY);
/* /*
@ -367,7 +367,7 @@ public class FlatFileDatabaseManagerTest {
} }
@Test @Test
public void testLoadByUUIDAndName() { void testLoadByUUIDAndName() {
File dbFile = prepareDatabaseTestResource(DB_HEALTHY); File dbFile = prepareDatabaseTestResource(DB_HEALTHY);
/* /*
@ -568,14 +568,14 @@ public class FlatFileDatabaseManagerTest {
} }
@Test @Test
public void testOverwriteName() { void testOverwriteName() {
overwriteDataAndCheckForFlag(db, duplicateNameDatabaseData, FlatFileDataFlag.DUPLICATE_NAME); overwriteDataAndCheckForFlag(db, duplicateNameDatabaseData, FlatFileDataFlag.DUPLICATE_NAME);
ArrayList<String[]> splitDataLines = getSplitDataFromFile(db.getUsersFile()); ArrayList<String[]> splitDataLines = getSplitDataFromFile(db.getUsersFile());
assertNotEquals(splitDataLines.get(1)[0], splitDataLines.get(0)[0]); //Name comparison assertNotEquals(splitDataLines.get(1)[0], splitDataLines.get(0)[0]); //Name comparison
} }
@Test @Test
public void testDataNotFound() { void testDataNotFound() {
//Save the zero version and see if it looks correct //Save the zero version and see if it looks correct
assertNotNull(db); assertNotNull(db);
assertTrue(db.getUsersFile().exists()); assertTrue(db.getUsersFile().exists());
@ -587,14 +587,14 @@ public class FlatFileDatabaseManagerTest {
} }
@Test @Test
public void testPurgePowerlessUsers() { void testPurgePowerlessUsers() {
replaceDataInFile(db, normalDatabaseData); replaceDataInFile(db, normalDatabaseData);
int purgeCount = db.purgePowerlessUsers(); int purgeCount = db.purgePowerlessUsers();
assertEquals(purgeCount, 1); //1 User should have been purged assertEquals(purgeCount, 1); //1 User should have been purged
} }
@Test @Test
public void testCheckFileHealthAndStructure() { void testCheckFileHealthAndStructure() {
replaceDataInFile(db, badDatabaseData); replaceDataInFile(db, badDatabaseData);
List<FlatFileDataFlag> dataFlags = db.checkFileHealthAndStructure(); List<FlatFileDataFlag> dataFlags = db.checkFileHealthAndStructure();
@ -603,48 +603,48 @@ public class FlatFileDatabaseManagerTest {
} }
@Test @Test
public void testFindFixableDuplicateNames() { void testFindFixableDuplicateNames() {
overwriteDataAndCheckForFlag(db, duplicateNameDatabaseData, FlatFileDataFlag.DUPLICATE_NAME); overwriteDataAndCheckForFlag(db, duplicateNameDatabaseData, FlatFileDataFlag.DUPLICATE_NAME);
} }
@Test @Test
public void testFindDuplicateUUIDs() { void testFindDuplicateUUIDs() {
overwriteDataAndCheckForFlag(db, duplicateUUIDDatabaseData, FlatFileDataFlag.DUPLICATE_UUID); overwriteDataAndCheckForFlag(db, duplicateUUIDDatabaseData, FlatFileDataFlag.DUPLICATE_UUID);
} }
@Test() @Test()
public void findBadUUIDData() { void findBadUUIDData() {
overwriteDataAndCheckForFlag(db, badUUIDDatabaseData, FlatFileDataFlag.BAD_UUID_DATA); overwriteDataAndCheckForFlag(db, badUUIDDatabaseData, FlatFileDataFlag.BAD_UUID_DATA);
} }
@Test @Test
public void testFindCorruptData() { void testFindCorruptData() {
overwriteDataAndCheckForFlag(db, corruptDatabaseData, FlatFileDataFlag.CORRUPTED_OR_UNRECOGNIZABLE); overwriteDataAndCheckForFlag(db, corruptDatabaseData, FlatFileDataFlag.CORRUPTED_OR_UNRECOGNIZABLE);
} }
@Test @Test
public void testFindEmptyNames() { void testFindEmptyNames() {
overwriteDataAndCheckForFlag(db, emptyNameDatabaseData, FlatFileDataFlag.MISSING_NAME); overwriteDataAndCheckForFlag(db, emptyNameDatabaseData, FlatFileDataFlag.MISSING_NAME);
} }
@Test @Test
public void testFindBadValues() { void testFindBadValues() {
overwriteDataAndCheckForFlag(db, badDatabaseData, FlatFileDataFlag.BAD_VALUES); overwriteDataAndCheckForFlag(db, badDatabaseData, FlatFileDataFlag.BAD_VALUES);
} }
@Test @Test
public void testFindOutdatedData() { void testFindOutdatedData() {
overwriteDataAndCheckForFlag(db, outdatedDatabaseData, FlatFileDataFlag.INCOMPLETE); overwriteDataAndCheckForFlag(db, outdatedDatabaseData, FlatFileDataFlag.INCOMPLETE);
} }
@Test @Test
public void testGetDatabaseType() { void testGetDatabaseType() {
assertNotNull(db); assertNotNull(db);
assertEquals(db.getDatabaseType(), DatabaseType.FLATFILE); assertEquals(db.getDatabaseType(), DatabaseType.FLATFILE);
} }
@Test @Test
public void testReadRank() { void testReadRank() {
//This is an empty DB //This is an empty DB
assertNotNull(db); assertNotNull(db);
String rankBoyName = "rankBoy"; String rankBoyName = "rankBoy";
@ -674,7 +674,7 @@ public class FlatFileDatabaseManagerTest {
} }
@Test @Test
public void testLoadFromFile() { void testLoadFromFile() {
ClassLoader classLoader = getClass().getClassLoader(); ClassLoader classLoader = getClass().getClassLoader();
URI resourceFileURI = null; URI resourceFileURI = null;

View File

@ -1,27 +1,34 @@
package com.gmail.nossr50.database.flatfile; package com.gmail.nossr50.database.flatfile;
import com.gmail.nossr50.database.FlatFileDatabaseManager;
import org.junit.Test;
import java.util.HashSet; 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 @Test
public void getPreparedSaveDataLine() { void getPreparedSaveDataLine() {
} }
@Test @Test
public void repairBadData() { void repairBadData() {
} }
@Test @Test
public void getZeroInitialisedData() { void getZeroInitialisedData() {
} }
@Test(expected = AssertionError.class) @Test
public void testTooManyDataEntriesSplitString() { void testTooManyDataEntriesSplitString() {
Assertions.assertThrows(AssertionError.class, () -> {
FlatFileDataContainer dataContainer = new CategorizedFlatFileData(0, new HashSet<>(), new String[FlatFileDatabaseManager.DATA_ENTRY_COUNT + 1]); FlatFileDataContainer dataContainer = new CategorizedFlatFileData(0, new HashSet<>(), new String[FlatFileDatabaseManager.DATA_ENTRY_COUNT + 1]);
FlatFileDataUtil.getPreparedSaveDataLine(dataContainer); FlatFileDataUtil.getPreparedSaveDataLine(dataContainer);
});
} }
} }

View File

@ -1,12 +1,12 @@
package com.gmail.nossr50.datatypes; package com.gmail.nossr50.datatypes;
import org.bukkit.Location; import org.bukkit.Location;
import org.junit.Assert; import org.junit.jupiter.api.Assertions;
import org.junit.Test; import org.junit.jupiter.api.Test;
public class BlockLocationHistoryTest { class BlockLocationHistoryTest {
@Test @Test
public void testRemovesOldestElement() { void testRemovesOldestElement() {
BlockLocationHistory history = new BlockLocationHistory(2); BlockLocationHistory history = new BlockLocationHistory(2);
Location locationA = new Location(null, 0, 1, 2); Location locationA = new Location(null, 0, 1, 2);
Location locationB = new Location(null, 1, 2, 3); Location locationB = new Location(null, 1, 2, 3);
@ -15,13 +15,13 @@ public class BlockLocationHistoryTest {
history.add(locationA); history.add(locationA);
history.add(locationB); history.add(locationB);
history.add(locationC); history.add(locationC);
Assert.assertFalse(history.contains(locationA)); Assertions.assertFalse(history.contains(locationA));
Assert.assertTrue(history.contains(locationB)); Assertions.assertTrue(history.contains(locationB));
Assert.assertTrue(history.contains(locationC)); Assertions.assertTrue(history.contains(locationC));
} }
@Test @Test
public void testSupportsDuplicateElement() { void testSupportsDuplicateElement() {
BlockLocationHistory history = new BlockLocationHistory(2); BlockLocationHistory history = new BlockLocationHistory(2);
Location locationA = new Location(null, 0, 1, 2); Location locationA = new Location(null, 0, 1, 2);
Location locationB = new Location(null, 1, 2, 3); Location locationB = new Location(null, 1, 2, 3);
@ -29,9 +29,9 @@ public class BlockLocationHistoryTest {
history.add(locationA); history.add(locationA);
history.add(locationA); history.add(locationA);
history.add(locationB); history.add(locationB);
Assert.assertTrue(history.contains(locationA)); Assertions.assertTrue(history.contains(locationA));
Assert.assertTrue(history.contains(locationB)); Assertions.assertTrue(history.contains(locationB));
history.add(locationB); history.add(locationB);
Assert.assertFalse(history.contains(locationA)); Assertions.assertFalse(history.contains(locationA));
} }
} }

View File

@ -1,43 +1,54 @@
package com.gmail.nossr50.util.blockmeta; 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.mcMMO;
import com.gmail.nossr50.util.BlockUtils; import com.gmail.nossr50.util.BlockUtils;
import com.gmail.nossr50.util.compat.CompatibilityManager; import com.gmail.nossr50.util.compat.CompatibilityManager;
import com.gmail.nossr50.util.compat.layers.world.WorldCompatibilityLayer; import com.gmail.nossr50.util.compat.layers.world.WorldCompatibilityLayer;
import com.gmail.nossr50.util.platform.PlatformManager; import com.gmail.nossr50.util.platform.PlatformManager;
import com.google.common.io.Files; 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) class ChunkStoreTest {
@PrepareForTest({ Bukkit.class, mcMMO.class})
public class ChunkStoreTest {
public static final int LEGACY_WORLD_HEIGHT_MAX = 256; public static final int LEGACY_WORLD_HEIGHT_MAX = 256;
public static final int LEGACY_WORLD_HEIGHT_MIN = 0; public static final int LEGACY_WORLD_HEIGHT_MIN = 0;
private static File tempDir; private static File tempDir;
@BeforeClass
@BeforeAll
public static void setUpClass() { public static void setUpClass() {
tempDir = Files.createTempDir(); tempDir = Files.createTempDir();
} }
@AfterClass @AfterAll
public static void tearDownClass() { public static void tearDownClass() {
recursiveDelete(tempDir); recursiveDelete(tempDir);
} }
@ -47,44 +58,59 @@ public class ChunkStoreTest {
private WorldCompatibilityLayer worldCompatibilityLayer; private WorldCompatibilityLayer worldCompatibilityLayer;
private PlatformManager platformManager; private PlatformManager platformManager;
@Before private MockedStatic<Bukkit> bukkitMock;
public void setUpMock(){ private MockedStatic<mcMMO> mcMMOMock;
@BeforeEach
void setUpMock() {
UUID worldUUID = UUID.randomUUID(); UUID worldUUID = UUID.randomUUID();
mockWorld = mock(World.class); mockWorld = Mockito.mock(World.class);
Mockito.when(mockWorld.getUID()).thenReturn(worldUUID); Mockito.when(mockWorld.getUID()).thenReturn(worldUUID);
Mockito.when(mockWorld.getMaxHeight()).thenReturn(256); Mockito.when(mockWorld.getMaxHeight()).thenReturn(256);
Mockito.when(mockWorld.getWorldFolder()).thenReturn(tempDir); Mockito.when(mockWorld.getWorldFolder()).thenReturn(tempDir);
PowerMockito.mockStatic(Bukkit.class);
Mockito.when(Bukkit.getWorld(worldUUID)).thenReturn(mockWorld);
platformManager = mock(PlatformManager.class); bukkitMock = Mockito.mockStatic(Bukkit.class);
compatibilityManager = mock(CompatibilityManager.class); bukkitMock.when(() -> Bukkit.getWorld(worldUUID)).thenReturn(mockWorld);
worldCompatibilityLayer = mock(WorldCompatibilityLayer.class);
Whitebox.setInternalState(mcMMO.class, "platformManager", platformManager); platformManager = Mockito.mock(PlatformManager.class);
Mockito.when(mcMMO.getCompatibilityManager()).thenReturn(compatibilityManager); 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()).thenReturn(compatibilityManager);
Mockito.when(platformManager.getCompatibilityManager().getWorldCompatibilityLayer()).thenReturn(worldCompatibilityLayer); 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.getMinWorldHeight(mockWorld)).thenReturn(LEGACY_WORLD_HEIGHT_MIN);
Mockito.when(worldCompatibilityLayer.getMaxWorldHeight(mockWorld)).thenReturn(LEGACY_WORLD_HEIGHT_MAX); Mockito.when(worldCompatibilityLayer.getMaxWorldHeight(mockWorld)).thenReturn(LEGACY_WORLD_HEIGHT_MAX);
} }
@Test(expected = IndexOutOfBoundsException.class) @AfterEach
public void testIndexOutOfBounds() { void teardownMock() {
bukkitMock.close();
mcMMOMock.close();
}
@Test
void testIndexOutOfBounds() {
Mockito.when(mcMMO.getCompatibilityManager().getWorldCompatibilityLayer().getMinWorldHeight(mockWorld)).thenReturn(-64); Mockito.when(mcMMO.getCompatibilityManager().getWorldCompatibilityLayer().getMinWorldHeight(mockWorld)).thenReturn(-64);
HashChunkManager hashChunkManager = new HashChunkManager(); HashChunkManager hashChunkManager = new HashChunkManager();
// Top Block // Top Block
Block illegalHeightBlock = initMockBlock(1337, 256, -1337); Block illegalHeightBlock = initMockBlock(1337, 256, -1337);
Assert.assertFalse(hashChunkManager.isTrue(illegalHeightBlock)); Assertions.assertFalse(hashChunkManager.isTrue(illegalHeightBlock));
hashChunkManager.setTrue(illegalHeightBlock); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> hashChunkManager.setTrue(illegalHeightBlock));
} }
@Test @Test
public void testSetTrue() { void testSetTrue() {
Mockito.when(mcMMO.getCompatibilityManager().getWorldCompatibilityLayer().getMinWorldHeight(mockWorld)).thenReturn(-64); Mockito.when(mcMMO.getCompatibilityManager().getWorldCompatibilityLayer().getMinWorldHeight(mockWorld)).thenReturn(-64);
HashChunkManager hashChunkManager = new HashChunkManager(); HashChunkManager hashChunkManager = new HashChunkManager();
int radius = 2; // Could be anything but drastically changes test time int radius = 2; // Could be anything but drastically changes test time
@ -95,50 +121,50 @@ public class ChunkStoreTest {
Block testBlock = initMockBlock(x, y, z); Block testBlock = initMockBlock(x, y, z);
hashChunkManager.setTrue(testBlock); hashChunkManager.setTrue(testBlock);
Assert.assertTrue(hashChunkManager.isTrue(testBlock)); Assertions.assertTrue(hashChunkManager.isTrue(testBlock));
hashChunkManager.setFalse(testBlock); hashChunkManager.setFalse(testBlock);
Assert.assertFalse(hashChunkManager.isTrue(testBlock)); Assertions.assertFalse(hashChunkManager.isTrue(testBlock));
} }
} }
} }
// Bot Block // Bot Block
Block bottomBlock = initMockBlock(1337, 0, -1337); 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); hashChunkManager.setTrue(bottomBlock);
Assert.assertTrue(hashChunkManager.isTrue(bottomBlock)); Assertions.assertTrue(hashChunkManager.isTrue(bottomBlock));
// Top Block // Top Block
Block topBlock = initMockBlock(1337, 255, -1337); 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); hashChunkManager.setTrue(topBlock);
Assert.assertTrue(hashChunkManager.isTrue(topBlock)); Assertions.assertTrue(hashChunkManager.isTrue(topBlock));
} }
@Test @Test
public void testSetValue() { void testSetValue() {
BitSetChunkStore original = new BitSetChunkStore(mockWorld, 0, 0); BitSetChunkStore original = new BitSetChunkStore(mockWorld, 0, 0);
original.setTrue(0, 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); original.setFalse(0, 0, 0);
Assert.assertFalse(original.isTrue(0, 0, 0)); Assertions.assertFalse(original.isTrue(0, 0, 0));
} }
@Test @Test
public void testIsEmpty() { void testIsEmpty() {
BitSetChunkStore original = new BitSetChunkStore(mockWorld, 0, 0); BitSetChunkStore original = new BitSetChunkStore(mockWorld, 0, 0);
Assert.assertTrue(original.isEmpty()); Assertions.assertTrue(original.isEmpty());
original.setTrue(0, 0, 0); original.setTrue(0, 0, 0);
original.setFalse(0, 0, 0); original.setFalse(0, 0, 0);
Assert.assertTrue(original.isEmpty()); Assertions.assertTrue(original.isEmpty());
} }
@Test @Test
public void testRoundTrip() throws IOException { void testRoundTrip() throws IOException {
BitSetChunkStore original = new BitSetChunkStore(mockWorld, 1, 2); BitSetChunkStore original = new BitSetChunkStore(mockWorld, 1, 2);
original.setTrue(14, 89, 12); original.setTrue(14, 89, 12);
original.setTrue(14, 90, 12); original.setTrue(14, 90, 12);
@ -149,7 +175,7 @@ public class ChunkStoreTest {
} }
@Test @Test
public void testNegativeWorldMin() throws IOException { void testNegativeWorldMin() throws IOException {
Mockito.when(mcMMO.getCompatibilityManager().getWorldCompatibilityLayer().getMinWorldHeight(mockWorld)).thenReturn(-64); Mockito.when(mcMMO.getCompatibilityManager().getWorldCompatibilityLayer().getMinWorldHeight(mockWorld)).thenReturn(-64);
BitSetChunkStore original = new BitSetChunkStore(mockWorld, 1, 2); BitSetChunkStore original = new BitSetChunkStore(mockWorld, 1, 2);
@ -162,7 +188,7 @@ public class ChunkStoreTest {
} }
@Test @Test
public void testNegativeWorldMinUpgrade() throws IOException { void testNegativeWorldMinUpgrade() throws IOException {
BitSetChunkStore original = new BitSetChunkStore(mockWorld, 1, 2); BitSetChunkStore original = new BitSetChunkStore(mockWorld, 1, 2);
original.setTrue(14, 1, 12); original.setTrue(14, 1, 12);
original.setTrue(14, 2, 12); original.setTrue(14, 2, 12);
@ -175,7 +201,7 @@ public class ChunkStoreTest {
} }
@Test @Test
public void testChunkCoords() throws IOException { void testChunkCoords() throws IOException {
for (int x = -96; x < 0; x++) { for (int x = -96; x < 0; x++) {
int cx = x >> 4; int cx = x >> 4;
int ix = Math.abs(x) % 16; int ix = Math.abs(x) % 16;
@ -184,7 +210,7 @@ public class ChunkStoreTest {
} }
@Test @Test
public void testUpgrade() throws IOException { void testUpgrade() throws IOException {
LegacyChunkStore original = new LegacyChunkStore(mockWorld, 12, 32); LegacyChunkStore original = new LegacyChunkStore(mockWorld, 12, 32);
original.setTrue(14, 89, 12); original.setTrue(14, 89, 12);
original.setTrue(14, 90, 12); original.setTrue(14, 90, 12);
@ -195,7 +221,7 @@ public class ChunkStoreTest {
} }
@Test @Test
public void testSimpleRegionRoundtrip() throws IOException { void testSimpleRegionRoundtrip() throws IOException {
LegacyChunkStore original = new LegacyChunkStore(mockWorld, 12, 12); LegacyChunkStore original = new LegacyChunkStore(mockWorld, 12, 12);
original.setTrue(14, 89, 12); original.setTrue(14, 89, 12);
original.setTrue(14, 90, 12); original.setTrue(14, 90, 12);
@ -207,9 +233,8 @@ public class ChunkStoreTest {
} }
region.close(); region.close();
region = new McMMOSimpleRegionFile(file, 0, 0); region = new McMMOSimpleRegionFile(file, 0, 0);
try (DataInputStream is = region.getInputStream(original.getChunkX(), original.getChunkZ())) try (DataInputStream is = region.getInputStream(original.getChunkX(), original.getChunkZ())) {
{ Assertions.assertNotNull(is);
Assert.assertNotNull(is);
ChunkStore deserialized = BitSetChunkStore.Serialization.readChunkStore(is); ChunkStore deserialized = BitSetChunkStore.Serialization.readChunkStore(is);
assertEqual(original, deserialized); assertEqual(original, deserialized);
} }
@ -218,36 +243,36 @@ public class ChunkStoreTest {
} }
@Test @Test
public void testSimpleRegionRejectsOutOfBounds() { void testSimpleRegionRejectsOutOfBounds() {
File file = new File(tempDir, "SimpleRegionRoundTrip.region"); File file = new File(tempDir, "SimpleRegionRoundTrip.region");
McMMOSimpleRegionFile region = new McMMOSimpleRegionFile(file, 0, 0); McMMOSimpleRegionFile region = new McMMOSimpleRegionFile(file, 0, 0);
assertThrows(() -> region.getOutputStream(-1, 0), IndexOutOfBoundsException.class); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> region.getOutputStream(-1, 0));
assertThrows(() -> region.getOutputStream(0, -1), IndexOutOfBoundsException.class); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> region.getOutputStream(0, -1));
assertThrows(() -> region.getOutputStream(32, 0), IndexOutOfBoundsException.class); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> region.getOutputStream(32, 0));
assertThrows(() -> region.getOutputStream(0, 32), IndexOutOfBoundsException.class); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> region.getOutputStream(0, 32));
region.close(); region.close();
} }
@Test @Test
public void testChunkStoreRejectsOutOfBounds() { void testChunkStoreRejectsOutOfBounds() {
ChunkStore chunkStore = new BitSetChunkStore(mockWorld, 0, 0); ChunkStore chunkStore = new BitSetChunkStore(mockWorld, 0, 0);
assertThrows(() -> chunkStore.setTrue(-1, 0, 0), IndexOutOfBoundsException.class); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> chunkStore.setTrue(-1, 0, 0));
assertThrows(() -> chunkStore.setTrue(0, -1, 0), IndexOutOfBoundsException.class); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> chunkStore.setTrue(0, -1, 0));
assertThrows(() -> chunkStore.setTrue(0, 0, -1), IndexOutOfBoundsException.class); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> chunkStore.setTrue(0, 0, -1));
assertThrows(() -> chunkStore.setTrue(16, 0, 0), IndexOutOfBoundsException.class); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> chunkStore.setTrue(16, 0, 0));
assertThrows(() -> chunkStore.setTrue(0, mockWorld.getMaxHeight(), 0), IndexOutOfBoundsException.class); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> chunkStore.setTrue(0, mockWorld.getMaxHeight(), 0));
assertThrows(() -> chunkStore.setTrue(0, 0, 16), IndexOutOfBoundsException.class); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> chunkStore.setTrue(0, 0, 16));
} }
@Test @Test
public void testRegressionChunkMirrorBug() { void testRegressionChunkMirrorBug() {
ChunkManager chunkManager = new HashChunkManager(); ChunkManager chunkManager = new HashChunkManager();
Block mockBlockA = mock(Block.class); Block mockBlockA = Mockito.mock(Block.class);
Mockito.when(mockBlockA.getX()).thenReturn(15); Mockito.when(mockBlockA.getX()).thenReturn(15);
Mockito.when(mockBlockA.getZ()).thenReturn(15); Mockito.when(mockBlockA.getZ()).thenReturn(15);
Mockito.when(mockBlockA.getY()).thenReturn(0); Mockito.when(mockBlockA.getY()).thenReturn(0);
Mockito.when(mockBlockA.getWorld()).thenReturn(mockWorld); 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.getX()).thenReturn(-15);
Mockito.when(mockBlockB.getZ()).thenReturn(-15); Mockito.when(mockBlockB.getZ()).thenReturn(-15);
Mockito.when(mockBlockB.getY()).thenReturn(0); Mockito.when(mockBlockB.getY()).thenReturn(0);
@ -255,42 +280,25 @@ public class ChunkStoreTest {
chunkManager.setTrue(mockBlockA); chunkManager.setTrue(mockBlockA);
chunkManager.setFalse(mockBlockB); chunkManager.setFalse(mockBlockB);
Assert.assertTrue(chunkManager.isTrue(mockBlockA)); Assertions.assertTrue(chunkManager.isTrue(mockBlockA));
} }
private interface Delegate { private void assertEqual(ChunkStore expected, ChunkStore actual) {
Assertions.assertEquals(expected.getChunkMin(), actual.getChunkMin());
void run(); Assertions.assertEquals(expected.getChunkMax(), actual.getChunkMax());
}
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());
assertEqualIgnoreMinMax(expected, actual); assertEqualIgnoreMinMax(expected, actual);
} }
private void assertEqualIgnoreMinMax(ChunkStore expected, ChunkStore actual) private void assertEqualIgnoreMinMax(ChunkStore expected, ChunkStore actual) {
{ Assertions.assertEquals(expected.getChunkX(), actual.getChunkX());
Assert.assertEquals(expected.getChunkX(), actual.getChunkX()); Assertions.assertEquals(expected.getChunkZ(), actual.getChunkZ());
Assert.assertEquals(expected.getChunkZ(), actual.getChunkZ()); Assertions.assertEquals(expected.getWorldId(), actual.getWorldId());
Assert.assertEquals(expected.getWorldId(), actual.getWorldId()); for (int y = Math.min(actual.getChunkMin(), expected.getChunkMin()); y < Math.max(actual.getChunkMax(), expected.getChunkMax()); y++) {
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) if (expected.getChunkMin() > y || actual.getChunkMin() > y || expected.getChunkMax() <= y || actual.getChunkMax() <= y)
continue; // Ignore continue; // Ignore
for (int x = 0; x < 16; x++) for (int x = 0; x < 16; x++)
for (int z = 0; z < 16; z++) 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) if (chunkStore instanceof BitSetChunkStore)
BitSetChunkStore.Serialization.writeChunkStore(new DataOutputStream(byteArrayOutputStream), chunkStore); BitSetChunkStore.Serialization.writeChunkStore(new DataOutputStream(byteArrayOutputStream), chunkStore);
else 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(); return byteArrayOutputStream.toByteArray();
} }
@ -313,6 +323,7 @@ public class ChunkStoreTest {
private final int cx; private final int cx;
private final int cz; private final int cz;
private final @NotNull UUID worldUid; private final @NotNull UUID worldUid;
public LegacyChunkStore(@NotNull World world, int cx, int cz) { public LegacyChunkStore(@NotNull World world, int cx, int cz) {
this.cx = cx; this.cx = cx;
this.cz = cz; this.cz = cz;
@ -416,11 +427,13 @@ public class ChunkStoreTest {
} }
} }
private static class UnitTestObjectOutputStream extends ObjectOutputStream { private static class UnitTestObjectOutputStream extends ObjectOutputStream {
public UnitTestObjectOutputStream(@NotNull OutputStream outputStream) throws IOException { public UnitTestObjectOutputStream(@NotNull OutputStream outputStream) throws IOException {
super(outputStream); super(outputStream);
} }
@Override @Override
public void writeUTF(@NotNull String str) throws IOException { public void writeUTF(@NotNull String str) throws IOException {
// Pretend to be the old class // Pretend to be the old class
@ -430,14 +443,15 @@ public class ChunkStoreTest {
} }
} }
@NotNull @NotNull
private Block initMockBlock(int x, int y, int z) { private Block initMockBlock(int x, int y, int z) {
Block testBlock = mock(Block.class); Block mockBlock = Mockito.mock(Block.class);
Mockito.when(testBlock.getX()).thenReturn(x); Mockito.when(mockBlock.getX()).thenReturn(x);
Mockito.when(testBlock.getY()).thenReturn(y); Mockito.when(mockBlock.getY()).thenReturn(y);
Mockito.when(testBlock.getZ()).thenReturn(z); Mockito.when(mockBlock.getZ()).thenReturn(z);
Mockito.when(testBlock.getWorld()).thenReturn(mockWorld); Mockito.when(mockBlock.getWorld()).thenReturn(mockWorld);
return testBlock; return mockBlock;
} }
public static void recursiveDelete(@NotNull File directoryToBeDeleted) { public static void recursiveDelete(@NotNull File directoryToBeDeleted) {

View File

@ -14,10 +14,10 @@ import org.junit.jupiter.api.Test;
* See https://github.com/mcMMO-Dev/mcMMO/pull/4446 * See https://github.com/mcMMO-Dev/mcMMO/pull/4446
* *
*/ */
public class TextUtilsTest { class TextUtilsTest {
@Test @Test
public void testColorizeText() { void testColorizeText() {
String inputText = "&4This text should be red."; String inputText = "&4This text should be red.";
/* /*
@ -26,6 +26,7 @@ public class TextUtilsTest {
*/ */
TextComponent component = TextUtils.colorizeText(inputText); 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);
} }
} }