mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-08-21 20:22:45 +02:00
[Test] Fixed SpongeSystemTest. Mocked Sponge
This commit is contained in:
parent
3096e962b5
commit
bd7a7ef9d0
|
@ -24,6 +24,8 @@ import dagger.Module;
|
|||
import dagger.Provides;
|
||||
import org.bstats.sponge.Metrics;
|
||||
import org.slf4j.Logger;
|
||||
import org.spongepowered.api.Game;
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.config.ConfigDir;
|
||||
import org.spongepowered.api.event.Listener;
|
||||
import org.spongepowered.api.event.game.state.GameStartedServerEvent;
|
||||
|
@ -196,4 +198,8 @@ public class PlanSponge extends SpongePlugin implements PlanPlugin {
|
|||
public PlanSystem getSystem() {
|
||||
return system;
|
||||
}
|
||||
|
||||
public Game getGame() {
|
||||
return Sponge.getGame();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package com.djrapitops.plan.modules.server.sponge;
|
||||
|
||||
import com.djrapitops.plan.PlanSponge;
|
||||
import com.djrapitops.plan.system.info.server.properties.ServerProperties;
|
||||
import com.djrapitops.plan.system.info.server.properties.SpongeServerProperties;
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
import org.spongepowered.api.Sponge;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
|
@ -18,7 +18,7 @@ public class SpongeServerPropertiesModule {
|
|||
|
||||
@Provides
|
||||
@Singleton
|
||||
ServerProperties provideServerProperties() {
|
||||
return new SpongeServerProperties(Sponge.getGame());
|
||||
ServerProperties provideServerProperties(PlanSponge plugin) {
|
||||
return new SpongeServerProperties(plugin.getGame());
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package com.djrapitops.plan.system.database;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.EnableException;
|
||||
import com.djrapitops.plan.system.database.databases.sql.SQLiteDB;
|
||||
import com.djrapitops.plan.system.database.databases.sql.SpongeMySQLDB;
|
||||
import com.djrapitops.plan.system.locale.Locale;
|
||||
|
@ -20,6 +21,8 @@ import javax.inject.Singleton;
|
|||
@Singleton
|
||||
public class SpongeDBSystem extends DBSystem {
|
||||
|
||||
private final PlanConfig config;
|
||||
|
||||
@Inject
|
||||
public SpongeDBSystem(
|
||||
Locale locale,
|
||||
|
@ -31,11 +34,16 @@ public class SpongeDBSystem extends DBSystem {
|
|||
ErrorHandler errorHandler
|
||||
) {
|
||||
super(locale, sqLiteDB, logger, timings, errorHandler);
|
||||
this.config = config;
|
||||
|
||||
databases.add(spongeMySQLDB);
|
||||
databases.add(sqLiteDB.usingDefaultFile());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enable() throws EnableException {
|
||||
String dbType = config.getString(Settings.DB_TYPE).toLowerCase().trim();
|
||||
db = getActiveDatabaseByName(dbType);
|
||||
super.enable();
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package com.djrapitops.plan.system.tasks.server;
|
||||
|
||||
import com.djrapitops.plan.PlanSponge;
|
||||
import com.djrapitops.plan.data.container.TPS;
|
||||
import com.djrapitops.plan.data.container.builders.TPSBuilder;
|
||||
import com.djrapitops.plan.system.info.server.properties.ServerProperties;
|
||||
|
@ -8,7 +9,6 @@ import com.djrapitops.plan.system.processing.processors.Processors;
|
|||
import com.djrapitops.plan.system.tasks.TPSCountTimer;
|
||||
import com.djrapitops.plugin.logging.console.PluginLogger;
|
||||
import com.djrapitops.plugin.logging.error.ErrorHandler;
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.world.World;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
@ -18,10 +18,12 @@ import javax.inject.Singleton;
|
|||
public class SpongeTPSCountTimer extends TPSCountTimer {
|
||||
|
||||
private long lastCheckNano;
|
||||
private final PlanSponge plugin;
|
||||
private ServerProperties serverProperties;
|
||||
|
||||
@Inject
|
||||
public SpongeTPSCountTimer(
|
||||
PlanSponge plugin,
|
||||
Processors processors,
|
||||
Processing processing,
|
||||
ServerProperties serverProperties,
|
||||
|
@ -29,6 +31,7 @@ public class SpongeTPSCountTimer extends TPSCountTimer {
|
|||
ErrorHandler errorHandler
|
||||
) {
|
||||
super(processors, processing, logger, errorHandler);
|
||||
this.plugin = plugin;
|
||||
this.serverProperties = serverProperties;
|
||||
lastCheckNano = -1;
|
||||
}
|
||||
|
@ -58,7 +61,7 @@ public class SpongeTPSCountTimer extends TPSCountTimer {
|
|||
|
||||
long usedMemory = getUsedMemory();
|
||||
|
||||
double tps = Sponge.getGame().getServer().getTicksPerSecond();
|
||||
double tps = plugin.getGame().getServer().getTicksPerSecond();
|
||||
int playersOnline = serverProperties.getOnlinePlayers();
|
||||
latestPlayersOnline = playersOnline;
|
||||
int loadedChunks = -1; // getLoadedChunks();
|
||||
|
@ -83,7 +86,7 @@ public class SpongeTPSCountTimer extends TPSCountTimer {
|
|||
private int getLoadedChunks() {
|
||||
// DISABLED
|
||||
int loaded = 0;
|
||||
for (World world : Sponge.getGame().getServer().getWorlds()) {
|
||||
for (World world : plugin.getGame().getServer().getWorlds()) {
|
||||
loaded += world.getLoadedChunks().spliterator().estimateSize();
|
||||
}
|
||||
return loaded;
|
||||
|
@ -95,6 +98,6 @@ public class SpongeTPSCountTimer extends TPSCountTimer {
|
|||
* @return amount of entities
|
||||
*/
|
||||
private int getEntityCount() {
|
||||
return Sponge.getGame().getServer().getWorlds().stream().mapToInt(world -> world.getEntities().size()).sum();
|
||||
return plugin.getGame().getServer().getWorlds().stream().mapToInt(world -> world.getEntities().size()).sum();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ package com.djrapitops.plan;
|
|||
|
||||
import com.djrapitops.plan.api.exceptions.EnableException;
|
||||
import com.djrapitops.plan.system.PlanSystem;
|
||||
import com.djrapitops.plan.system.settings.Settings;
|
||||
import org.junit.*;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.runner.RunWith;
|
||||
|
@ -24,16 +25,22 @@ public class SpongeSystemTest {
|
|||
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
private static PlanSponge planMock;
|
||||
private PlanSystem spongeSystem;
|
||||
private PlanSpongeComponent component;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpClass() throws Exception {
|
||||
PlanSpongeMocker mockUtil = PlanSpongeMocker.setUp()
|
||||
.withDataFolder(temporaryFolder.getRoot())
|
||||
.withLogging()
|
||||
.withResourceFetchingFromJar();
|
||||
.withResourceFetchingFromJar()
|
||||
.withGame();
|
||||
planMock = mockUtil.getPlanMock();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
component = DaggerPlanSpongeComponent.builder().plan(planMock).build();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
if (spongeSystem != null) {
|
||||
|
@ -42,11 +49,10 @@ public class SpongeSystemTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
@Ignore("Sponge mock required")
|
||||
public void testEnable() throws EnableException {
|
||||
// Settings.WEBSERVER_PORT.setTemporaryValue(9005);
|
||||
spongeSystem = component.system();
|
||||
spongeSystem.getConfigSystem().getConfig().set(Settings.WEBSERVER_PORT, 9005);
|
||||
|
||||
spongeSystem = null; //TODO
|
||||
spongeSystem.enable();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,15 +5,33 @@
|
|||
package utilities.mocks;
|
||||
|
||||
import com.djrapitops.plan.PlanSponge;
|
||||
import com.djrapitops.plugin.benchmarking.Timings;
|
||||
import com.djrapitops.plugin.command.ColorScheme;
|
||||
import com.djrapitops.plugin.logging.console.PluginLogger;
|
||||
import com.djrapitops.plugin.logging.console.TestPluginLogger;
|
||||
import com.djrapitops.plugin.logging.debug.CombineDebugLogger;
|
||||
import com.djrapitops.plugin.logging.debug.DebugLogger;
|
||||
import com.djrapitops.plugin.logging.debug.MemoryDebugLogger;
|
||||
import com.djrapitops.plugin.logging.error.ConsoleErrorLogger;
|
||||
import com.djrapitops.plugin.logging.error.ErrorHandler;
|
||||
import com.djrapitops.plugin.task.thread.ThreadRunnableFactory;
|
||||
import org.mockito.Mockito;
|
||||
import utilities.mocks.objects.TestLogger;
|
||||
import org.slf4j.Logger;
|
||||
import org.spongepowered.api.Game;
|
||||
import org.spongepowered.api.MinecraftVersion;
|
||||
import org.spongepowered.api.Platform;
|
||||
import org.spongepowered.api.Server;
|
||||
import org.spongepowered.api.entity.living.player.Player;
|
||||
import org.spongepowered.api.text.Text;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Mocking Utility for Sponge version of Plan.
|
||||
|
@ -35,11 +53,22 @@ public class PlanSpongeMocker extends Mocker {
|
|||
planMock = Mockito.mock(PlanSponge.class);
|
||||
super.planMock = planMock;
|
||||
|
||||
doReturn("4.2.0").when(planMock).getVersion();
|
||||
doCallRealMethod().when(planMock).getColorScheme();
|
||||
doReturn(new ColorScheme("§1", "§2", "§3")).when(planMock).getColorScheme();
|
||||
doReturn("1.0.0").when(planMock).getVersion();
|
||||
|
||||
Logger logger = Mockito.mock(Logger.class);
|
||||
ThreadRunnableFactory runnableFactory = new ThreadRunnableFactory();
|
||||
PluginLogger testPluginLogger = new TestPluginLogger();
|
||||
DebugLogger debugLogger = new CombineDebugLogger(new MemoryDebugLogger());
|
||||
ErrorHandler consoleErrorLogger = new ConsoleErrorLogger(testPluginLogger);
|
||||
Timings timings = new Timings(debugLogger);
|
||||
|
||||
doReturn(logger).when(planMock).getLogger();
|
||||
doReturn(runnableFactory).when(planMock).getRunnableFactory();
|
||||
doReturn(testPluginLogger).when(planMock).getPluginLogger();
|
||||
doReturn(debugLogger).when(planMock).getDebugLogger();
|
||||
doReturn(consoleErrorLogger).when(planMock).getErrorHandler();
|
||||
doReturn(timings).when(planMock).getTimings();
|
||||
|
||||
return this;
|
||||
}
|
||||
|
@ -49,16 +78,45 @@ public class PlanSpongeMocker extends Mocker {
|
|||
return this;
|
||||
}
|
||||
|
||||
public PlanSpongeMocker withLogging() {
|
||||
TestLogger testLogger = new TestLogger();
|
||||
doReturn(testLogger).when(planMock).getLogger();
|
||||
TestPluginLogger testPluginLogger = new TestPluginLogger();
|
||||
doReturn(testPluginLogger).when(planMock).getPluginLogger();
|
||||
ConsoleErrorLogger consoleErrorLogger = new ConsoleErrorLogger(testPluginLogger);
|
||||
doReturn(consoleErrorLogger).when(planMock).getErrorHandler();
|
||||
public PlanSpongeMocker withGame() {
|
||||
Game game = Mockito.mock(Game.class);
|
||||
|
||||
Platform platform = mockPlatform();
|
||||
Server server = mockServer();
|
||||
doReturn(platform).when(game).getPlatform();
|
||||
doReturn(server).when(game).getServer();
|
||||
|
||||
doReturn(game).when(planMock).getGame();
|
||||
return this;
|
||||
}
|
||||
|
||||
private Platform mockPlatform() {
|
||||
Platform platform = Mockito.mock(Platform.class);
|
||||
|
||||
MinecraftVersion version = Mockito.mock(MinecraftVersion.class);
|
||||
doReturn("1.12").when(version).getName();
|
||||
doReturn(version).when(platform).getMinecraftVersion();
|
||||
|
||||
return platform;
|
||||
}
|
||||
|
||||
private Server mockServer() {
|
||||
Server server = Mockito.mock(Server.class);
|
||||
|
||||
Text motd = Mockito.mock(Text.class);
|
||||
doReturn("Motd").when(motd).toPlain();
|
||||
Optional<InetSocketAddress> ip = Optional.of(new InetSocketAddress(25565));
|
||||
int maxPlayers = 20;
|
||||
List<Player> online = new ArrayList<>();
|
||||
|
||||
doReturn(motd).when(server).getMotd();
|
||||
doReturn(ip).when(server).getBoundAddress();
|
||||
doReturn(maxPlayers).when(server).getMaxPlayers();
|
||||
doReturn(online).when(server).getOnlinePlayers();
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
public PlanSpongeMocker withResourceFetchingFromJar() throws Exception {
|
||||
withPluginFiles();
|
||||
return this;
|
||||
|
|
Loading…
Reference in New Issue
Block a user