[Test] Fixed SpongeSystemTest. Mocked Sponge

This commit is contained in:
Rsl1122 2018-10-13 13:06:10 +03:00
parent 3096e962b5
commit bd7a7ef9d0
6 changed files with 104 additions and 23 deletions

View File

@ -24,6 +24,8 @@ import dagger.Module;
import dagger.Provides; import dagger.Provides;
import org.bstats.sponge.Metrics; import org.bstats.sponge.Metrics;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.spongepowered.api.Game;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.config.ConfigDir; import org.spongepowered.api.config.ConfigDir;
import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GameStartedServerEvent; import org.spongepowered.api.event.game.state.GameStartedServerEvent;
@ -196,4 +198,8 @@ public class PlanSponge extends SpongePlugin implements PlanPlugin {
public PlanSystem getSystem() { public PlanSystem getSystem() {
return system; return system;
} }
public Game getGame() {
return Sponge.getGame();
}
} }

View File

@ -1,10 +1,10 @@
package com.djrapitops.plan.modules.server.sponge; 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.ServerProperties;
import com.djrapitops.plan.system.info.server.properties.SpongeServerProperties; import com.djrapitops.plan.system.info.server.properties.SpongeServerProperties;
import dagger.Module; import dagger.Module;
import dagger.Provides; import dagger.Provides;
import org.spongepowered.api.Sponge;
import javax.inject.Singleton; import javax.inject.Singleton;
@ -18,7 +18,7 @@ public class SpongeServerPropertiesModule {
@Provides @Provides
@Singleton @Singleton
ServerProperties provideServerProperties() { ServerProperties provideServerProperties(PlanSponge plugin) {
return new SpongeServerProperties(Sponge.getGame()); return new SpongeServerProperties(plugin.getGame());
} }
} }

View File

@ -1,5 +1,6 @@
package com.djrapitops.plan.system.database; 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.SQLiteDB;
import com.djrapitops.plan.system.database.databases.sql.SpongeMySQLDB; import com.djrapitops.plan.system.database.databases.sql.SpongeMySQLDB;
import com.djrapitops.plan.system.locale.Locale; import com.djrapitops.plan.system.locale.Locale;
@ -20,6 +21,8 @@ import javax.inject.Singleton;
@Singleton @Singleton
public class SpongeDBSystem extends DBSystem { public class SpongeDBSystem extends DBSystem {
private final PlanConfig config;
@Inject @Inject
public SpongeDBSystem( public SpongeDBSystem(
Locale locale, Locale locale,
@ -31,11 +34,16 @@ public class SpongeDBSystem extends DBSystem {
ErrorHandler errorHandler ErrorHandler errorHandler
) { ) {
super(locale, sqLiteDB, logger, timings, errorHandler); super(locale, sqLiteDB, logger, timings, errorHandler);
this.config = config;
databases.add(spongeMySQLDB); databases.add(spongeMySQLDB);
databases.add(sqLiteDB.usingDefaultFile()); databases.add(sqLiteDB.usingDefaultFile());
}
@Override
public void enable() throws EnableException {
String dbType = config.getString(Settings.DB_TYPE).toLowerCase().trim(); String dbType = config.getString(Settings.DB_TYPE).toLowerCase().trim();
db = getActiveDatabaseByName(dbType); db = getActiveDatabaseByName(dbType);
super.enable();
} }
} }

View File

@ -1,5 +1,6 @@
package com.djrapitops.plan.system.tasks.server; 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.TPS;
import com.djrapitops.plan.data.container.builders.TPSBuilder; import com.djrapitops.plan.data.container.builders.TPSBuilder;
import com.djrapitops.plan.system.info.server.properties.ServerProperties; 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.plan.system.tasks.TPSCountTimer;
import com.djrapitops.plugin.logging.console.PluginLogger; import com.djrapitops.plugin.logging.console.PluginLogger;
import com.djrapitops.plugin.logging.error.ErrorHandler; import com.djrapitops.plugin.logging.error.ErrorHandler;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.world.World; import org.spongepowered.api.world.World;
import javax.inject.Inject; import javax.inject.Inject;
@ -18,10 +18,12 @@ import javax.inject.Singleton;
public class SpongeTPSCountTimer extends TPSCountTimer { public class SpongeTPSCountTimer extends TPSCountTimer {
private long lastCheckNano; private long lastCheckNano;
private final PlanSponge plugin;
private ServerProperties serverProperties; private ServerProperties serverProperties;
@Inject @Inject
public SpongeTPSCountTimer( public SpongeTPSCountTimer(
PlanSponge plugin,
Processors processors, Processors processors,
Processing processing, Processing processing,
ServerProperties serverProperties, ServerProperties serverProperties,
@ -29,6 +31,7 @@ public class SpongeTPSCountTimer extends TPSCountTimer {
ErrorHandler errorHandler ErrorHandler errorHandler
) { ) {
super(processors, processing, logger, errorHandler); super(processors, processing, logger, errorHandler);
this.plugin = plugin;
this.serverProperties = serverProperties; this.serverProperties = serverProperties;
lastCheckNano = -1; lastCheckNano = -1;
} }
@ -58,7 +61,7 @@ public class SpongeTPSCountTimer extends TPSCountTimer {
long usedMemory = getUsedMemory(); long usedMemory = getUsedMemory();
double tps = Sponge.getGame().getServer().getTicksPerSecond(); double tps = plugin.getGame().getServer().getTicksPerSecond();
int playersOnline = serverProperties.getOnlinePlayers(); int playersOnline = serverProperties.getOnlinePlayers();
latestPlayersOnline = playersOnline; latestPlayersOnline = playersOnline;
int loadedChunks = -1; // getLoadedChunks(); int loadedChunks = -1; // getLoadedChunks();
@ -83,7 +86,7 @@ public class SpongeTPSCountTimer extends TPSCountTimer {
private int getLoadedChunks() { private int getLoadedChunks() {
// DISABLED // DISABLED
int loaded = 0; int loaded = 0;
for (World world : Sponge.getGame().getServer().getWorlds()) { for (World world : plugin.getGame().getServer().getWorlds()) {
loaded += world.getLoadedChunks().spliterator().estimateSize(); loaded += world.getLoadedChunks().spliterator().estimateSize();
} }
return loaded; return loaded;
@ -95,6 +98,6 @@ public class SpongeTPSCountTimer extends TPSCountTimer {
* @return amount of entities * @return amount of entities
*/ */
private int getEntityCount() { 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();
} }
} }

View File

@ -6,6 +6,7 @@ package com.djrapitops.plan;
import com.djrapitops.plan.api.exceptions.EnableException; import com.djrapitops.plan.api.exceptions.EnableException;
import com.djrapitops.plan.system.PlanSystem; import com.djrapitops.plan.system.PlanSystem;
import com.djrapitops.plan.system.settings.Settings;
import org.junit.*; import org.junit.*;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -24,16 +25,22 @@ public class SpongeSystemTest {
public static TemporaryFolder temporaryFolder = new TemporaryFolder(); public static TemporaryFolder temporaryFolder = new TemporaryFolder();
private static PlanSponge planMock; private static PlanSponge planMock;
private PlanSystem spongeSystem; private PlanSystem spongeSystem;
private PlanSpongeComponent component;
@BeforeClass @BeforeClass
public static void setUpClass() throws Exception { public static void setUpClass() throws Exception {
PlanSpongeMocker mockUtil = PlanSpongeMocker.setUp() PlanSpongeMocker mockUtil = PlanSpongeMocker.setUp()
.withDataFolder(temporaryFolder.getRoot()) .withDataFolder(temporaryFolder.getRoot())
.withLogging() .withResourceFetchingFromJar()
.withResourceFetchingFromJar(); .withGame();
planMock = mockUtil.getPlanMock(); planMock = mockUtil.getPlanMock();
} }
@Before
public void setUp() {
component = DaggerPlanSpongeComponent.builder().plan(planMock).build();
}
@After @After
public void tearDown() { public void tearDown() {
if (spongeSystem != null) { if (spongeSystem != null) {
@ -42,11 +49,10 @@ public class SpongeSystemTest {
} }
@Test @Test
@Ignore("Sponge mock required")
public void testEnable() throws EnableException { 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(); spongeSystem.enable();
} }
} }

View File

@ -5,15 +5,33 @@
package utilities.mocks; package utilities.mocks;
import com.djrapitops.plan.PlanSponge; 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.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.ConsoleErrorLogger;
import com.djrapitops.plugin.logging.error.ErrorHandler;
import com.djrapitops.plugin.task.thread.ThreadRunnableFactory; import com.djrapitops.plugin.task.thread.ThreadRunnableFactory;
import org.mockito.Mockito; 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.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. * Mocking Utility for Sponge version of Plan.
@ -35,11 +53,22 @@ public class PlanSpongeMocker extends Mocker {
planMock = Mockito.mock(PlanSponge.class); planMock = Mockito.mock(PlanSponge.class);
super.planMock = planMock; super.planMock = planMock;
doReturn("4.2.0").when(planMock).getVersion(); doReturn(new ColorScheme("§1", "§2", "§3")).when(planMock).getColorScheme();
doCallRealMethod().when(planMock).getColorScheme(); doReturn("1.0.0").when(planMock).getVersion();
Logger logger = Mockito.mock(Logger.class);
ThreadRunnableFactory runnableFactory = new ThreadRunnableFactory(); 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(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; return this;
} }
@ -49,16 +78,45 @@ public class PlanSpongeMocker extends Mocker {
return this; return this;
} }
public PlanSpongeMocker withLogging() { public PlanSpongeMocker withGame() {
TestLogger testLogger = new TestLogger(); Game game = Mockito.mock(Game.class);
doReturn(testLogger).when(planMock).getLogger();
TestPluginLogger testPluginLogger = new TestPluginLogger(); Platform platform = mockPlatform();
doReturn(testPluginLogger).when(planMock).getPluginLogger(); Server server = mockServer();
ConsoleErrorLogger consoleErrorLogger = new ConsoleErrorLogger(testPluginLogger); doReturn(platform).when(game).getPlatform();
doReturn(consoleErrorLogger).when(planMock).getErrorHandler(); doReturn(server).when(game).getServer();
doReturn(game).when(planMock).getGame();
return this; 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 { public PlanSpongeMocker withResourceFetchingFromJar() throws Exception {
withPluginFiles(); withPluginFiles();
return this; return this;