Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorinwasher committed Nov 17, 2023
1 parent 26dc36b commit fdf393f
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/sgrewritten/stargate/Stargate.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ public void onEnable() {
storageAPI = new SQLDatabase(database);
blockHandlerResolver = new BlockHandlerResolver(storageAPI);
registry = new StargateRegistry(storageAPI, blockHandlerResolver);
networkManager = new StargateNetworkManager(registry, storageAPI);
bungeeManager = new StargateBungeeManager(this.getRegistry(), this.getLanguageManager(), this.getNetworkManager());
blockLogger = new CoreProtectManager();
storedProperties = new PropertiesDatabase(FileHelper.createHiddenFileIfNotExists(DATA_FOLDER, INTERNAL_FOLDER, INTERNAL_PROPERTIES_FILE));
networkManager = new StargateNetworkManager(registry, storageAPI);
try {
this.migrateConfigurationAndData();
} catch (IOException | InvalidConfigurationException | SQLException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.sgrewritten.stargate.api.StargateAPI;
import org.sgrewritten.stargate.api.network.portal.Portal;
import org.sgrewritten.stargate.api.network.portal.PortalFlag;
import org.sgrewritten.stargate.api.network.portal.RealPortal;
import org.sgrewritten.stargate.api.permission.PermissionManager;
import org.sgrewritten.stargate.exception.TranslatableException;
import org.sgrewritten.stargate.exception.UnimplementedFlagException;
Expand Down Expand Up @@ -92,4 +93,13 @@ public interface NetworkManager {
* @param newName <p> The new name of the portal </p>
*/
void rename(Portal portal, String newName) throws NameConflictException;

/**
* Add portal to network and save to storage
* @param portal
* @param network
*/
void savePortal(RealPortal portal, Network network) throws NameConflictException;

void destroyPortal(RealPortal portal);
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void onBlockBreak(BlockBreakEvent event) {
String msg = languageManager.getErrorMessage(TranslatableMessage.DESTROY);
MessageUtils.sendMessageFromPortal(portal,event.getPlayer(),msg,StargateSendMessagePortalEvent.MessageType.DESTROY);

portal.destroy();
stargateAPI.getNetworkManager().destroyPortal(portal);
try {
stargateAPI.getStorageAPI().removePortalFromStorage(portal);
} catch (StorageWriteException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public NetworkType getType() {

@Override
public StorageType getStorageType() {
return StorageType.LOCAL;
return this.storageType;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.sgrewritten.stargate.api.network.RegistryAPI;
import org.sgrewritten.stargate.api.network.portal.Portal;
import org.sgrewritten.stargate.api.network.portal.PortalFlag;
import org.sgrewritten.stargate.api.network.portal.RealPortal;
import org.sgrewritten.stargate.api.permission.PermissionManager;
import org.sgrewritten.stargate.config.ConfigurationHelper;
import org.sgrewritten.stargate.container.TwoTuple;
Expand Down Expand Up @@ -153,7 +154,7 @@ public Network createNetwork(String name, NetworkType type, boolean isInterServe
Network network = storageAPI.createNetwork(name, type, isInterServer);
registry.registerNetwork(network);
Stargate.log(
Level.FINEST, String.format("Adding networkid %s to interServer = %b", network.getId(), isInterServer));
Level.FINEST, String.format("Adding network id %s to interServer = %b", network.getId(), isInterServer));
return network;
}

Expand Down Expand Up @@ -209,4 +210,26 @@ public void rename(Portal portal, String newName) throws NameConflictException {
portal.getNetwork().updatePortals();
}

@Override
public void savePortal(RealPortal portal, Network network) throws NameConflictException {
network.addPortal(portal);
try {
storageAPI.savePortalToStorage(portal);
} catch (StorageWriteException e) {
Stargate.log(e);
}
}

@Override
public void destroyPortal(RealPortal portal) {
portal.getNetwork().removePortal(portal);
registry.unregisterPortal(portal);
portal.destroy();
try {
storageAPI.removePortalFromStorage(portal);
} catch (StorageWriteException e) {
Stargate.log(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import org.bukkit.World;
import org.sgrewritten.stargate.Stargate;
import org.sgrewritten.stargate.api.StargateAPI;
import org.sgrewritten.stargate.api.network.portal.RealPortal;
import org.sgrewritten.stargate.exception.InvalidStructureException;
import org.sgrewritten.stargate.exception.TranslatableException;
import org.sgrewritten.stargate.exception.database.StorageWriteException;
import org.sgrewritten.stargate.exception.name.InvalidNameException;
import org.sgrewritten.stargate.exception.name.NameConflictException;
import org.sgrewritten.stargate.exception.name.NameLengthException;
Expand Down Expand Up @@ -62,9 +64,7 @@ public static List<Portal> loadPortalsFromStorage(String portalSaveLocation, Ser
String worldName = file.getName().replaceAll("\\.db$", "");

BufferedReader reader = FileHelper.getBufferedReader(file);
// Fix encoding issue / can't convert properly from ansi to utf8
String line = reader.readLine();
// Convert to utf-8 if ansi is used
while (line != null) {
if (line.startsWith("#") || line.trim().isEmpty()) {
continue;
Expand Down Expand Up @@ -120,13 +120,11 @@ private static Portal readPortal(String line, World world, StargateAPI stargateA
gate.addPortalPosition(buttonLocation, PositionType.BUTTON, "Stargate");
}

Portal portal = PortalCreationHelper.createPortal(network, portalData, gate, stargateAPI);
RealPortal portal = PortalCreationHelper.createPortal(network, portalData, gate, stargateAPI);

// Add the portal to its network and store it to the database
Stargate.log(Level.FINE, String.format("Saving portal %s in network %s from old storage... ",
portalData.name(), portalData.networkName()));
network.addPortal(portal);

stargateAPI.getNetworkManager().savePortal(portal, network);
return portal;
}

Expand Down
3 changes: 3 additions & 0 deletions src/test/java/org/sgrewritten/stargate/StargateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ public void restart() {
server.getPluginManager().enablePlugin(plugin);
Assertions.assertTrue(plugin.isEnabled());
Network network = plugin.getRegistry().getNetwork(BungeePortal.getLegacyNetworkName(), false);
for(String networkName : plugin.getRegistry().getNetworkMap().keySet()){
Stargate.log(Level.INFO,networkName);
}
assertNotNull(network);
assertNotNull(network.getPortal(PORTAL2));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void updateNetwork() throws TranslatableException, InvalidStructureException {
RealPortal portal2 = PortalFactory.generateFakePortal(world, network, PORTAL2, true);

bungeeManager.updateNetwork(BungeeHelper.generateJsonMessage(portal, StargateProtocolRequestType.PORTAL_ADD));
bungeeManager.updateNetwork(BungeeHelper.generateJsonMessage(portal2, StargateProtocolRequestType.PORTAL_ADD));
bungeeManager.updateNetwork(BungeeHelper.generateJsonMessage(portal2,StargateProtocolRequestType.PORTAL_ADD));
Network network1 = registry.getNetwork(NETWORK, true);
Assertions.assertNotNull(network1);
Assertions.assertNotNull(network1.getPortal(PORTAL));
Expand All @@ -108,7 +108,7 @@ void playerConnectOnline() {

bungeeManager.playerConnect(BungeeHelper.generateTeleportJsonMessage(PLAYER, realPortal));
Component componentMessage = player.nextComponentMessage();
Assertions.assertFalse(componentMessage != null && componentMessage.toString().contains("[ERROR]"), "A error message was sent to the player '" + componentMessage + "'");
Assertions.assertFalse(componentMessage != null && componentMessage.toString().contains("[ERROR]"), "An error message was sent to the player '" + componentMessage + "'");
}

@Test
Expand All @@ -125,7 +125,7 @@ void legacyPlayerConnectOnline() {

bungeeManager.legacyPlayerConnect(BungeeHelper.generateLegacyTeleportMessage(PLAYER, bungeePortal));
Component componentMessage = player.nextComponentMessage();
Assertions.assertFalse(componentMessage != null && componentMessage.toString().contains("[ERROR]"), "A error message was sent to the player '" + componentMessage + "'");
Assertions.assertFalse(componentMessage != null && componentMessage.toString().contains("[ERROR]"), "An error message was sent to the player '" + componentMessage + "'");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.sgrewritten.stargate.api.BlockHandlerResolver;
import org.sgrewritten.stargate.api.config.ConfigurationOption;
import org.sgrewritten.stargate.api.gate.GateFormatRegistry;
import org.sgrewritten.stargate.api.network.NetworkManager;
import org.sgrewritten.stargate.config.StargateYamlConfiguration;
import org.sgrewritten.stargate.container.TwoTuple;
import org.sgrewritten.stargate.database.SQLDatabase;
Expand Down Expand Up @@ -68,6 +69,7 @@ public class DataMigratorTest {
static private ServerMock server;
private static StargateRegistry registry;
private static StargateAPIMock stargateAPI;
private static NetworkManager networkManager;

@BeforeAll
public static void setUp() throws IOException, InvalidConfigurationException, SQLException {
Expand All @@ -87,6 +89,7 @@ public static void setUp() throws IOException, InvalidConfigurationException, SQ
StorageAPI storageAPI = new SQLDatabase(sqlDatabase, false, false);
registry = new StargateRegistry(storageAPI,new BlockHandlerResolver(storageAPI));
stargateAPI = new StargateAPIMock(storageAPI,registry);
networkManager = stargateAPI.getNetworkManager();

defaultConfigFile = new File("src/main/resources", "config.yml");
server = MockBukkit.mock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ public static RealPortal generateFakePortal(Location topLeft, Network network, S
throws InvalidStructureException, NameLengthException {
if (createInterServerPortal) {
flags.add(PortalFlag.FANCY_INTER_SERVER);
} else {
flags.remove(PortalFlag.FANCY_INTER_SERVER);
}
BlockFace facing = BlockFace.EAST;
String gateFileName = "nether.gate";
Expand All @@ -165,7 +167,7 @@ public static RealPortal generateFakePortal(Block signBlock, Network network, Se


RealPortal portal = PortalCreationHelper.createPortal(network, name, "destination", "server", flags, new HashSet<>(), gate, UUID.randomUUID(), stargateAPI, null);
network.addPortal(portal);
stargateAPI.getNetworkManager().savePortal(portal,network);
return portal;
}

Expand Down

0 comments on commit fdf393f

Please sign in to comment.