Changed approach to support test concurrency

This commit is contained in:
tastybento 2019-03-12 20:35:33 -07:00
parent cb09ec2e3a
commit fd7e3e4154

View File

@ -5,7 +5,6 @@ package world.bentobox.bentobox.database.yaml;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -14,6 +13,8 @@ import java.beans.IntrospectionException;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@ -27,7 +28,7 @@ import org.bukkit.World;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scheduler.BukkitTask;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -52,7 +53,6 @@ import world.bentobox.bentobox.managers.FlagsManager;
@PrepareForTest( {BentoBox.class, Bukkit.class} )
public class YamlDatabaseHandlerTest {
private static final String UNIQUE_ID = "BSkyBlock6fb2d177-b186-4cac-84b9-27203ce5c6e1";
@Mock
private BentoBox plugin;
@Mock
@ -71,11 +71,11 @@ public class YamlDatabaseHandlerTest {
private YamlDatabaseHandler<Island> handler;
// File system
private File database;
private static File database;
private File islandTable;
private File record;
private File record2;
private UUID uuid;
/**
@ -99,10 +99,12 @@ public class YamlDatabaseHandlerTest {
when(Bukkit.getServer()).thenReturn(server);
// A YAML file representing island
uuid = UUID.randomUUID();
UUID uuid2 = UUID.randomUUID();
YamlConfiguration config = new YamlConfiguration();
config.loadFromString(YAML);
config.loadFromString(getYaml(uuid));
YamlConfiguration config2 = new YamlConfiguration();
config2.loadFromString(YAML2);
config2.loadFromString(getYaml2(uuid2));
when(dbConnector.loadYamlFile(Mockito.anyString(), Mockito.anyString())).thenReturn(config, config2);
// Flags Manager
@ -111,20 +113,16 @@ public class YamlDatabaseHandlerTest {
when(plugin.getFlagsManager()).thenReturn(fm);
// Island
when(island.getUniqueId()).thenReturn(UNIQUE_ID);
when(island.getUniqueId()).thenReturn(uuid.toString());
// File system
database = new File("Database");
database = new File("database");
islandTable = new File(database, "Island");
islandTable.mkdirs();
record = new File(islandTable, UNIQUE_ID + ".yml");
record2 = new File(islandTable, "BSkyBlock56ffb3d6-67bf-4951-8b7b-67b91cfec40d.yml");
YamlConfiguration c = new YamlConfiguration();
c.loadFromString(YAML);
c.save(record);
c = new YamlConfiguration();
c.loadFromString(YAML2);
c.save(record2);
record = new File(islandTable, uuid.toString() + ".yml");
record2 = new File(islandTable, uuid2.toString() + ".yml");
config.save(record);
config2.save(record2);
// Handler
handler = new YamlDatabaseHandler<Island>(plugin, Island.class, dbConnector);
@ -133,21 +131,13 @@ public class YamlDatabaseHandlerTest {
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
@AfterClass
public static void tearDown() throws Exception {
// Clean up file system
if (record.exists()) {
Files.delete(record.toPath());
}
if (record2.exists()) {
Files.delete(record2.toPath());
}
if (islandTable.exists()) {
Files.delete(islandTable.toPath());
}
if (database.exists()) {
Files.delete(database.toPath());
}
Files.walk(database.toPath())
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
}
/**
@ -156,18 +146,12 @@ public class YamlDatabaseHandlerTest {
@Test
public void testLoadObjects() throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, NoSuchMethodException, IntrospectionException {
List<Island> list = handler.loadObjects();
assertTrue(list.size() == 2);
Island is = list.get(0);
assertEquals(UNIQUE_ID, is.getUniqueId());
assertEquals("5988eecd-1dcd-4080-a843-785b62419abb", is.getOwner().toString());
assertEquals(1552264678424L, is.getCreatedDate());
assertEquals((Integer)1000, is.getMembers().get(UUID.fromString("5988eecd-1dcd-4080-a843-785b62419abb")));
is = list.get(1);
assertEquals("BSkyBlock56ffb3d6-67bf-4951-8b7b-67b91cfec40d", is.getUniqueId());
assertNull(is.getOwner());
assertEquals(1552264640164L, is.getCreatedDate());
assertTrue(is.getMembers().isEmpty());
assertFalse(list.isEmpty());
// Check at least one contains correct info
assertTrue(list.stream().anyMatch(i -> i.getOwner().toString().equals("5988eecd-1dcd-4080-a843-785b62419abb")));
assertTrue(list.stream().anyMatch(i -> i.getUniqueId().equals(uuid.toString())));
assertTrue(list.stream().anyMatch(i -> i.getCreatedDate() == 1552264678424L));
assertTrue(list.stream().anyMatch(i -> i.getMembers().get(UUID.fromString("5988eecd-1dcd-4080-a843-785b62419abb")) == 1000));
}
/**
@ -183,7 +167,7 @@ public class YamlDatabaseHandlerTest {
public void testLoadObject() throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, NoSuchMethodException, IntrospectionException {
String name = UUID.randomUUID().toString();
Island is = handler.loadObject(name);
assertEquals(UNIQUE_ID, is.getUniqueId());
assertEquals(uuid.toString(), is.getUniqueId());
assertEquals("5988eecd-1dcd-4080-a843-785b62419abb", is.getOwner().toString());
assertEquals(1552264678424L, is.getCreatedDate());
assertEquals((Integer)1000, is.getMembers().get(UUID.fromString("5988eecd-1dcd-4080-a843-785b62419abb")));
@ -276,8 +260,8 @@ public class YamlDatabaseHandlerTest {
*/
@Test
public void testObjectExists() {
when(dbConnector.uniqueIdExists(Mockito.eq(Island.class.getSimpleName()), Mockito.eq(UNIQUE_ID))).thenReturn(true);
assertTrue(handler.objectExists(UNIQUE_ID));
when(dbConnector.uniqueIdExists(Mockito.eq(Island.class.getSimpleName()), Mockito.eq(uuid.toString()))).thenReturn(true);
assertTrue(handler.objectExists(uuid.toString()));
assertFalse(handler.objectExists("nope"));
}
@ -286,7 +270,7 @@ public class YamlDatabaseHandlerTest {
*/
@Test
public void testDeleteID() {
handler.deleteID(UNIQUE_ID);
handler.deleteID(uuid.toString());
// Handled by queue
assertTrue(record.exists());
}
@ -297,7 +281,7 @@ public class YamlDatabaseHandlerTest {
@Test
public void testDeleteIDPluginNotEnabled() {
when(plugin.isEnabled()).thenReturn(false);
handler.deleteID(UNIQUE_ID);
handler.deleteID(uuid.toString());
assertFalse(record.exists());
}
@ -307,7 +291,7 @@ public class YamlDatabaseHandlerTest {
@Test
public void testDeleteIDNotEnabledWithYML() {
when(plugin.isEnabled()).thenReturn(false);
handler.deleteID(UNIQUE_ID + ".yml");
handler.deleteID(uuid.toString() + ".yml");
assertFalse(record.exists());
}
@ -326,8 +310,9 @@ public class YamlDatabaseHandlerTest {
}
// YAML
private final static String YAML = "deleted: false\n" +
"uniqueId: BSkyBlock6fb2d177-b186-4cac-84b9-27203ce5c6e1\n" +
private String getYaml(UUID uuid) {
return "deleted: false\n" +
"uniqueId: " + uuid.toString() + "\n" +
"center: cleanroom:384:100:-768:0:0\n" +
"range: 192\n" +
"protectionRange: 100\n" +
@ -419,9 +404,11 @@ public class YamlDatabaseHandlerTest {
" THE_END: cleanroom_the_end:383:106:-769:1134395392:1106247680\n" +
" NORMAL: cleanroom:384:105:-766:0:1106247680\n" +
"doNotLoad: false\n";
}
private final static String YAML2 = "deleted: false\n" +
"uniqueId: BSkyBlock56ffb3d6-67bf-4951-8b7b-67b91cfec40d\n" +
private String getYaml2(UUID uuid) {
return "deleted: false\n" +
"uniqueId: " + uuid.toString() + "\n" +
"center: cleanroom:0:100:0:0:0\n" +
"range: 192\n" +
"protectionRange: 100\n" +
@ -440,3 +427,4 @@ public class YamlDatabaseHandlerTest {
"spawnPoint: {}\n" +
"doNotLoad: false\n";
}
}