[TEST][FIX] Написаны тесты для CommandHandler/ProviderMap/Modules system. Исправлегы баги, найденные с помощью тестов

This commit is contained in:
Gravit 2019-08-28 20:30:47 +07:00
parent 31356af213
commit f6e2df3e1c
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
19 changed files with 452 additions and 16 deletions

View file

@ -5,6 +5,14 @@
compile project(':LauncherCore')
compileOnly 'org.apache.httpcomponents:httpclient:4.5.7'
compileOnly 'io.netty:netty-codec-http:4.1.36.Final'
testCompile 'org.junit.jupiter:junit-jupiter:5.4.1'
}
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}
jar {

View file

@ -82,7 +82,7 @@ public void initModules(LauncherInitContext initContext) {
loaded++;
}
}
if(modules_size >= loaded) return;
//if(loaded >= modules_size) return;
}
for(LauncherModule module : modules)
{

View file

@ -0,0 +1,67 @@
package pro.gravit.launcher;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import pro.gravit.launcher.impl.*;
import pro.gravit.launcher.impl.event.CancelEvent;
import pro.gravit.launcher.impl.event.NormalEvent;
import pro.gravit.launcher.modules.LauncherModule;
import pro.gravit.launcher.modules.impl.SimpleModuleManager;
import java.nio.file.Path;
public class ModulesTest {
@TempDir
public static Path configDir;
@TempDir
public static Path modulesDir;
public static SimpleModuleManager moduleManager;
public static int dependInt = 0;
public static void add(int a)
{
if(dependInt != a) throw new IllegalStateException(String.valueOf(a));
dependInt++;
}
@BeforeAll
public static void prepare()
{
moduleManager = new SimpleModuleManager(modulesDir, configDir);
}
@Test
public void baseModule()
{
moduleManager.loadModule(new TestModule());
moduleManager.initModules(null);
NormalEvent e = new NormalEvent();
moduleManager.invokeEvent(e);
Assertions.assertTrue(e.passed);
CancelEvent e1 = new CancelEvent();
moduleManager.invokeEvent(e1);
Assertions.assertTrue(e1.isCancel());
}
@Test
public void dependenciesTest()
{
moduleManager.loadModule(new Depend1Module());
moduleManager.loadModule(new Depend2Module());
moduleManager.loadModule(new Depend3Module());
moduleManager.loadModule(new MainModule());
moduleManager.initModules(null);
Assertions.assertEquals(moduleManager.getModule("depend1").getInitStatus(), LauncherModule.InitStatus.FINISH);
Assertions.assertEquals(moduleManager.getModule("depend2").getInitStatus(), LauncherModule.InitStatus.FINISH);
Assertions.assertEquals(moduleManager.getModule("depend3").getInitStatus(), LauncherModule.InitStatus.FINISH);
Assertions.assertEquals(moduleManager.getModule("internal").getInitStatus(), LauncherModule.InitStatus.FINISH);
Assertions.assertEquals(moduleManager.getModule("main").getInitStatus(), LauncherModule.InitStatus.FINISH);
}
@Test
public void cyclicTest()
{
moduleManager.loadModule(new CyclicDependModule());
moduleManager.loadModule(new Cyclic2DependModule());
moduleManager.initModules(null);
Assertions.assertEquals(moduleManager.getModule("cyclic1").getInitStatus(), LauncherModule.InitStatus.FINISH);
Assertions.assertEquals(moduleManager.getModule("cyclic2").getInitStatus(), LauncherModule.InitStatus.FINISH);
}
}

View file

@ -0,0 +1,19 @@
package pro.gravit.launcher.impl;
import pro.gravit.launcher.modules.LauncherInitContext;
import pro.gravit.launcher.modules.LauncherModule;
import pro.gravit.launcher.modules.LauncherModuleInfo;
import pro.gravit.utils.Version;
public class Cyclic2DependModule extends LauncherModule {
public Cyclic2DependModule() {
super(new LauncherModuleInfo("cyclic2",
new Version(1,0,0),
2, new String[]{"cyclic1"}));
}
@Override
public void init(LauncherInitContext initContext) {
}
}

View file

@ -0,0 +1,19 @@
package pro.gravit.launcher.impl;
import pro.gravit.launcher.modules.LauncherInitContext;
import pro.gravit.launcher.modules.LauncherModule;
import pro.gravit.launcher.modules.LauncherModuleInfo;
import pro.gravit.utils.Version;
public class CyclicDependModule extends LauncherModule {
public CyclicDependModule() {
super(new LauncherModuleInfo("cyclic1",
new Version(1,0,0),
2, new String[]{"cyclic2"}));
}
@Override
public void init(LauncherInitContext initContext) {
}
}

View file

@ -0,0 +1,24 @@
package pro.gravit.launcher.impl;
import org.junit.jupiter.api.Assertions;
import pro.gravit.launcher.ModulesTest;
import pro.gravit.launcher.modules.LauncherInitContext;
import pro.gravit.launcher.modules.LauncherModule;
import pro.gravit.launcher.modules.LauncherModuleInfo;
import pro.gravit.utils.Version;
public class Depend1Module extends LauncherModule {
public Depend1Module() {
super(new LauncherModuleInfo("depend1", new Version(1,0,0),
0,
new String[]{"depend3", "internal"}));
}
@Override
public void init(LauncherInitContext initContext) {
InternalModule module = modulesManager.getModule(InternalModule.class);
Assertions.assertEquals(module.getInitStatus(), InitStatus.FINISH);
Depend3Module module1 = modulesManager.getModule(Depend3Module.class);
Assertions.assertEquals(module1.getInitStatus(), InitStatus.FINISH);
}
}

View file

@ -0,0 +1,22 @@
package pro.gravit.launcher.impl;
import pro.gravit.launcher.ModulesTest;
import pro.gravit.launcher.modules.LauncherInitContext;
import pro.gravit.launcher.modules.LauncherModule;
import pro.gravit.launcher.modules.LauncherModuleInfo;
public class Depend2Module extends LauncherModule {
public Depend2Module() {
super(new LauncherModuleInfo("depend2"));
}
@Override
public void preInit() {
super.preInit();
modulesManager.loadModule(new InternalModule());
}
@Override
public void init(LauncherInitContext initContext) {
}
}

View file

@ -0,0 +1,16 @@
package pro.gravit.launcher.impl;
import pro.gravit.launcher.ModulesTest;
import pro.gravit.launcher.modules.LauncherInitContext;
import pro.gravit.launcher.modules.LauncherModule;
import pro.gravit.launcher.modules.LauncherModuleInfo;
public class Depend3Module extends LauncherModule {
public Depend3Module() {
super(new LauncherModuleInfo("depend3"));
}
@Override
public void init(LauncherInitContext initContext) {
}
}

View file

@ -0,0 +1,16 @@
package pro.gravit.launcher.impl;
import pro.gravit.launcher.ModulesTest;
import pro.gravit.launcher.modules.LauncherInitContext;
import pro.gravit.launcher.modules.LauncherModule;
import pro.gravit.launcher.modules.LauncherModuleInfo;
public class InternalModule extends LauncherModule {
public InternalModule() {
super(new LauncherModuleInfo("internal"));
}
@Override
public void init(LauncherInitContext initContext) {
}
}

View file

@ -0,0 +1,25 @@
package pro.gravit.launcher.impl;
import org.junit.jupiter.api.Assertions;
import pro.gravit.launcher.ModulesTest;
import pro.gravit.launcher.modules.LauncherInitContext;
import pro.gravit.launcher.modules.LauncherModule;
import pro.gravit.launcher.modules.LauncherModuleInfo;
import pro.gravit.utils.Version;
public class MainModule extends LauncherModule {
public MainModule() {
super(new LauncherModuleInfo("main",
new Version(1,0,0),
0, new String[]{"depend1", "depend2"}));
}
@Override
public void init(LauncherInitContext initContext) {
Depend1Module module = modulesManager.getModule(Depend1Module.class);
Assertions.assertEquals(module.getInitStatus(), InitStatus.FINISH);
Depend2Module module2 = modulesManager.getModule(Depend2Module.class);
Assertions.assertEquals(module2.getInitStatus(), InitStatus.FINISH);
}
}

View file

@ -0,0 +1,30 @@
package pro.gravit.launcher.impl;
import pro.gravit.launcher.impl.event.CancelEvent;
import pro.gravit.launcher.impl.event.NormalEvent;
import pro.gravit.launcher.modules.LauncherInitContext;
import pro.gravit.launcher.modules.LauncherModule;
import pro.gravit.launcher.modules.LauncherModuleInfo;
public class TestModule extends LauncherModule {
public TestModule() {
super(new LauncherModuleInfo("testModule"));
}
@Override
public void init(LauncherInitContext initContext) {
registerEvent(this::testevent, NormalEvent.class);
registerEvent(this::testevent2, CancelEvent.class);
}
public void testevent(NormalEvent event)
{
event.passed = true;
}
public void testevent2(CancelEvent cancelEvent)
{
cancelEvent.cancel();
}
}

View file

@ -0,0 +1,6 @@
package pro.gravit.launcher.impl.event;
import pro.gravit.launcher.modules.LauncherModule;
public class CancelEvent extends LauncherModule.Event {
}

View file

@ -0,0 +1,7 @@
package pro.gravit.launcher.impl.event;
import pro.gravit.launcher.modules.LauncherModule;
public class NormalEvent extends LauncherModule.Event {
public boolean passed;
}

View file

@ -7,6 +7,14 @@
compileOnly 'org.jline:jline-reader:3.11.0'
compileOnly 'org.jline:jline-terminal:3.11.0'
compile 'com.google.code.gson:gson:2.8.5'
testCompile 'org.junit.jupiter:junit-jupiter:5.4.1'
}
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}
jar {

View file

@ -36,38 +36,32 @@ public void eval(String line, boolean bell) {
// Parse line to tokens
String[] args;
try {
args = CommonHelper.parseCommand(line);
if (args.length > 0) args[0] = args[0].toLowerCase();
evalNative(line, bell);
} catch (Exception e) {
LogHelper.error(e);
return;
}
}
// Evaluate command
public void evalNative(String line, boolean bell) throws Exception {
String[] args;
args = CommonHelper.parseCommand(line);
if (args.length > 0) args[0] = args[0].toLowerCase();
eval(args, bell);
}
public void eval(String[] args, boolean bell) {
public void eval(String[] args, boolean bell) throws Exception {
if (args.length == 0)
return;
// Measure start time and invoke command
long startTime = System.currentTimeMillis();
try {
lookup(args[0]).invoke(Arrays.copyOfRange(args, 1, args.length));
} catch (Exception e) {
LogHelper.error(e);
}
lookup(args[0]).invoke(Arrays.copyOfRange(args, 1, args.length));
// Bell if invocation took > 1s
long endTime = System.currentTimeMillis();
if (bell && endTime - startTime >= 5000)
try {
bell();
} catch (IOException e) {
LogHelper.error(e);
}
bell();
}

View file

@ -0,0 +1,51 @@
package pro.gravit.launcher;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import pro.gravit.launcher.impl.Test2Command;
import pro.gravit.launcher.impl.TestCommand;
import pro.gravit.utils.command.*;
public class CommandHandlerTest {
public static CommandHandler commandHandler;
@BeforeAll
public static void prepare() {
commandHandler = new StdCommandHandler(false);
commandHandler.registerCommand("test", new TestCommand());
}
@Test
public void baseTest() throws Exception {
commandHandler.evalNative("test test1 \"test 2\" \"test\\\" 3\" \"test\\\\ 4\"", false);
Assertions.assertTrue(((TestCommand) commandHandler.findCommand("test")).ok);
}
@Test
public void failNumberTest() throws Exception {
Command cmd = commandHandler.findCommand("test");
try {
cmd.invoke("test1");
Assertions.fail("CommandException not throw");
} catch (CommandException e)
{
}
}
@Test
public void categoryTest() throws Exception
{
BaseCommandCategory category = new BaseCommandCategory();
category.registerCommand("test2", new Test2Command());
CommandHandler.Category category1 = new CommandHandler.Category(category,"testCat");
commandHandler.registerCategory(category1);
commandHandler.evalNative("test2", false);
commandHandler.unregisterCategory(category1);
try {
commandHandler.evalNative("test2", false);
Assertions.fail("CommandException not throw");
} catch (CommandException e)
{
}
}
}

View file

@ -0,0 +1,66 @@
package pro.gravit.launcher;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import pro.gravit.utils.ProviderMap;
import pro.gravit.utils.UniversalJsonAdapter;
public class SerializeTest {
public static GsonBuilder builder;
public static Gson gson;
public static ProviderMap<TestInterface> map;
public interface TestInterface
{
String get();
}
public static class MyTestClass implements TestInterface
{
public String a;
public MyTestClass(String a) {
this.a = a;
}
@Override
public String get() {
return a;
}
}
public static class MyTestClass2 implements TestInterface
{
public String b;
public MyTestClass2(String a) {
this.b = a;
}
@Override
public String get() {
return b;
}
}
@BeforeAll
public static void prepare()
{
builder = new GsonBuilder();
map = new ProviderMap<>();
map.register("test", MyTestClass.class);
map.register("test2", MyTestClass2.class);
builder.registerTypeAdapter(TestInterface.class, new UniversalJsonAdapter<>(map));
gson = builder.create();
}
@Test
public void main()
{
Assertions.assertNotNull(gson);
String json = gson.toJson(new MyTestClass("AAAA"), TestInterface.class);
String json2 = gson.toJson(new MyTestClass2("BBBB"), TestInterface.class);
TestInterface test1 = gson.fromJson(json, TestInterface.class);
TestInterface test2 = gson.fromJson(json2, TestInterface.class);
Assertions.assertEquals(test1.get(), "AAAA");
Assertions.assertEquals(test2.get(), "BBBB");
}
}

View file

@ -0,0 +1,20 @@
package pro.gravit.launcher.impl;
import pro.gravit.utils.command.Command;
public class Test2Command extends Command {
@Override
public String getArgsDescription() {
return null;
}
@Override
public String getUsageDescription() {
return null;
}
@Override
public void invoke(String... args) throws Exception {
}
}

View file

@ -0,0 +1,38 @@
package pro.gravit.launcher.impl;
import pro.gravit.utils.command.Command;
public class TestCommand extends Command {
public boolean ok = true;
@Override
public String getArgsDescription() {
return "TEST ARGS";
}
@Override
public String getUsageDescription() {
return "TEST USAGE";
}
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args, 4);
if(!args[0].equals("test1"))
{
throw new IllegalArgumentException(args[0]);
}
if(!args[1].equals("test 2"))
{
throw new IllegalArgumentException(args[1]);
}
if(!args[2].equals("test\" 3"))
{
throw new IllegalArgumentException(args[2]);
}
if(!args[3].equals("test\\ 4"))
{
throw new IllegalArgumentException(args[3]);
}
ok = true;
}
}