#421 Create SpawnLoader

- Non-static service class which handles the spawnpoints used in AuthMe
This commit is contained in:
ljacqu
2016-03-12 10:27:15 +01:00
parent 8b27444a49
commit 8293766f98
18 changed files with 464 additions and 274 deletions
@@ -11,6 +11,7 @@ import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.SpawnLoader;
import fr.xephi.authme.settings.domain.Property;
import fr.xephi.authme.settings.properties.SecuritySettings;
import org.bukkit.command.CommandSender;
@@ -44,6 +45,7 @@ public class CommandServiceTest {
private NewSetting settings;
private IpAddressManager ipAddressManager;
private PluginHooks pluginHooks;
private SpawnLoader spawnLoader;
@Before
public void setUpService() {
@@ -56,8 +58,9 @@ public class CommandServiceTest {
settings = mock(NewSetting.class);
ipAddressManager = mock(IpAddressManager.class);
pluginHooks = mock(PluginHooks.class);
spawnLoader = mock(SpawnLoader.class);
commandService = new CommandService(authMe, commandMapper, helpProvider, messages, passwordSecurity,
permissionsManager, settings, ipAddressManager, pluginHooks);
permissionsManager, settings, ipAddressManager, pluginHooks, spawnLoader);
}
@Test
@@ -0,0 +1,68 @@
package fr.xephi.authme.settings;
import com.google.common.io.Files;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.hooks.PluginHooks;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.IOException;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Test for {@link SpawnLoader}.
*/
public class SpawnLoaderTest {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
private File testFolder;
private NewSetting settings;
@Before
public void setup() throws IOException {
// Copy test config into a new temporary folder
testFolder = temporaryFolder.newFolder();
File source = TestHelper.getJarFile("/spawn/spawn-firstspawn.yml");
File destination = new File(testFolder, "spawn.yml");
Files.copy(source, destination);
// Create a settings mock with default values
settings = mock(NewSetting.class);
given(settings.getProperty(RestrictionSettings.SPAWN_PRIORITY))
.willReturn("authme, essentials, multiverse, default");
}
@Test
public void shouldSetSpawn() {
// given
SpawnLoader spawnLoader = new SpawnLoader(testFolder, settings, mock(PluginHooks.class));
World world = mock(World.class);
given(world.getName()).willReturn("new_world");
Location newSpawn = new Location(world, 123, 45.0, -67.89);
// when
boolean result = spawnLoader.setSpawn(newSpawn);
// then
assertThat(result, equalTo(true));
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(new File(testFolder, "spawn.yml"));
assertThat(configuration.getDouble("spawn.x"), equalTo(123.0));
assertThat(configuration.getDouble("spawn.y"), equalTo(45.0));
assertThat(configuration.getDouble("spawn.z"), equalTo(-67.89));
assertThat(configuration.getString("spawn.world"), equalTo("new_world"));
}
}
@@ -0,0 +1,15 @@
# Sample spawn.yml file with both spawn and firstspawn defined
spawn:
world: 'world'
x: 300.12
y: 120.34
z: -89.12
yaw: 0.23
pitch: 112.25
firstspawn:
world: 'firstspawn'
x: -30.12
y: 204.43
z: 10.32
yaw: 0.00
pitch: 10.23