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>
<plugin>
<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>
<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>
@ -324,38 +334,20 @@
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.0-M1</version>
<scope>test</scope>
</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>
<groupId>org.mockito</groupId>
<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>
</dependency>
<dependency>

View File

@ -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);
}
});
}
}
}

View File

@ -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<String[]> 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<FlatFileDataFlag> 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;

View File

@ -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);
});
}
}
}

View File

@ -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));
}
}

View File

@ -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<Bukkit> bukkitMock;
private MockedStatic<mcMMO> 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) {

View File

@ -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);
}
}