Added basic JUnit5 testing framework. (#202)

This commit is contained in:
Jeremy Wood
2019-03-23 12:01:49 -04:00
committed by Daniel Ennis
parent 2577fe1844
commit 1c056cea45
10 changed files with 586 additions and 0 deletions
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2016-2019 Daniel Ennis (Aikar) - MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package co.aikar.commands;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.verify;
@ExtendWith(MockitoExtension.class)
public class CommandManagerTests {
@Spy
private TestCommandSender sender;
private TestCommandManager manager;
@BeforeEach
public void initEach() {
manager = new TestCommandManager();
manager.registerCommand(new EchoCommand());
}
@Test
public void echoCommandSendsMessageArgument() {
manager.dispatchCommand(sender, "echo this is a test");
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
verify(sender).sendMessage(argumentCaptor.capture());
assertEquals("this is a test", argumentCaptor.getValue());
}
}
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2016-2019 Daniel Ennis (Aikar) - MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package co.aikar.commands;
import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.Default;
@CommandAlias("echo")
public class EchoCommand extends BaseCommand {
@Default
public void onCommand(TestCommandSender sender, String message) {
sender.sendMessage(message);
}
}
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2016-2019 Daniel Ennis (Aikar) - MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package co.aikar.commands;
public class TestCommandContexts extends CommandContexts<TestCommandExecutionContext> {
public TestCommandContexts(CommandManager manager) {
super(manager);
registerIssuerOnlyContext(TestCommandSender.class, c -> c.getIssuer().getIssuer());
}
}
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2016-2019 Daniel Ennis (Aikar) - MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package co.aikar.commands;
import java.util.List;
import java.util.Map;
public class TestCommandExecutionContext extends CommandExecutionContext<TestCommandExecutionContext, TestCommandIssuer> {
public TestCommandExecutionContext(RegisteredCommand cmd, CommandParameter param, TestCommandIssuer sender, List<String> args, int index, Map<String, Object> passedArgs) {
super(cmd, param, sender, args, index, passedArgs);
}
}
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2016-2019 Daniel Ennis (Aikar) - MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package co.aikar.commands;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
public class TestCommandIssuer implements CommandIssuer {
private final TestCommandManager manager;
private final TestCommandSender issuer;
public TestCommandIssuer(TestCommandManager manager, TestCommandSender issuer) {
this.manager = manager;
this.issuer = issuer;
}
@Override
public TestCommandSender getIssuer() {
return issuer;
}
@Override
public TestCommandManager getManager() {
return manager;
}
@Override
public boolean isPlayer() {
return false;
}
@Override
public @NotNull UUID getUniqueId() {
return issuer.getUuid();
}
@Override
public boolean hasPermission(String permission) {
return issuer.hasPermission(permission);
}
@Override
public void sendMessageInternal(String message) {
issuer.sendMessage(message);
}
}
@@ -0,0 +1,165 @@
/*
* Copyright (c) 2016-2019 Daniel Ennis (Aikar) - MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package co.aikar.commands;
import co.aikar.commands.apachecommonslang.ApacheCommonsExceptionUtil;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
public class TestCommandManager extends CommandManager<
TestCommandSender,
TestCommandIssuer,
String,
MessageFormatter<String>,
TestCommandExecutionContext,
TestConditionContext> {
private final Logger logger = Logger.getLogger(this.getClass().getSimpleName());
private final Map<String, TestRootCommand> commands = new HashMap<>();
private final CommandCompletions<CommandCompletionContext<TestCommandIssuer>> completions = new CommandCompletions<>(this);
protected TestCommandContexts contexts;
protected Locales locales;
@Override
public CommandContexts<?> getCommandContexts() {
if (contexts == null) {
contexts = new TestCommandContexts(this);
}
return contexts;
}
@Override
public CommandCompletions<?> getCommandCompletions() {
return completions;
}
@Override
public void registerCommand(BaseCommand command) {
command.onRegister(this);
for (Map.Entry<String, RootCommand> entry : command.registeredCommands.entrySet()) {
String commandName = entry.getKey().toLowerCase();
TestRootCommand cmd = (TestRootCommand) entry.getValue();
if (!cmd.isRegistered) {
cmd.isRegistered = true;
commands.put(commandName, cmd);
}
}
}
public void unregisterCommand(BaseCommand command) {
for (Map.Entry<String, RootCommand> entry : command.registeredCommands.entrySet()) {
String jdaCommandName = entry.getKey().toLowerCase();
TestRootCommand jdaCommand = (TestRootCommand) entry.getValue();
jdaCommand.getSubCommands().values().removeAll(command.subCommands.values());
if (jdaCommand.isRegistered && jdaCommand.getSubCommands().isEmpty()) {
jdaCommand.isRegistered = false;
commands.remove(jdaCommandName);
}
}
}
@Override
public boolean hasRegisteredCommands() {
return !commands.isEmpty();
}
@Override
public boolean isCommandIssuer(Class<?> type) {
return TestCommandIssuer.class.isAssignableFrom(type);
}
@Override
public TestCommandIssuer getCommandIssuer(Object issuer) {
if (!(issuer instanceof TestCommandSender)) {
throw new IllegalArgumentException(issuer.getClass().getName() + " is not a TestCommandSender.");
}
return new TestCommandIssuer(this, (TestCommandSender) issuer);
}
@Override
public RootCommand createRootCommand(String cmd) {
return new TestRootCommand(this, cmd);
}
@Override
public Locales getLocales() {
if (locales == null) {
locales = new Locales(this);
locales.loadLanguages();
}
return locales;
}
@Override
public CommandExecutionContext createCommandContext(RegisteredCommand command, CommandParameter parameter, CommandIssuer sender, List<String> args, int i, Map<String, Object> passedArgs) {
return new TestCommandExecutionContext(command, parameter, (TestCommandIssuer) sender, args, i, passedArgs);
}
@Override
public CommandCompletionContext createCompletionContext(RegisteredCommand command, CommandIssuer sender, String input, String config, String[] args) {
return new CommandCompletionContext(command, sender, input, config, args);
}
@Override
public void log(LogLevel level, String message, Throwable throwable) {
Level logLevel = level == LogLevel.INFO ? Level.INFO : Level.SEVERE;
logger.log(logLevel, LogLevel.LOG_PREFIX + message);
if (throwable != null) {
for (String line : ACFPatterns.NEWLINE.split(ApacheCommonsExceptionUtil.getFullStackTrace(throwable))) {
logger.log(logLevel, LogLevel.LOG_PREFIX + line);
}
}
}
@Override
public Collection<RootCommand> getRegisteredRootCommands() {
return Collections.unmodifiableCollection(commands.values());
}
public void dispatchCommand(TestCommandSender sender, String command) {
String[] args = ACFPatterns.SPACE.split(command, -1);
if (args.length == 0) {
return;
}
String cmd = args[0].toLowerCase();
TestRootCommand rootCommand = commands.get(cmd);
if (rootCommand == null) {
return;
}
if (args.length > 1) {
args = Arrays.copyOfRange(args, 1, args.length);
} else {
args = new String[0];
}
rootCommand.execute(this.getCommandIssuer(sender), cmd, args);
}
}
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2016-2019 Daniel Ennis (Aikar) - MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package co.aikar.commands;
import java.util.UUID;
/**
* Simple command sender. Use with {@link org.mockito.Mockito#spy(Object)} or {@link org.mockito.Spy}.
*/
public class TestCommandSender {
private final UUID uuid = UUID.randomUUID();
UUID getUuid() {
return uuid;
}
public boolean hasPermission(String permission) {
return false;
}
public void sendMessage(String message) {
}
}
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2016-2019 Daniel Ennis (Aikar) - MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package co.aikar.commands;
public class TestConditionContext extends ConditionContext<TestCommandIssuer> {
public TestConditionContext(TestCommandIssuer issuer, String config) {
super(issuer, config);
}
}
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2016-2019 Daniel Ennis (Aikar) - MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package co.aikar.commands;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.SetMultimap;
import java.util.ArrayList;
import java.util.List;
public class TestRootCommand implements RootCommand {
private final String name;
boolean isRegistered = false;
private TestCommandManager manager;
private BaseCommand defCommand;
private SetMultimap<String, RegisteredCommand> subCommands = HashMultimap.create();
private List<BaseCommand> children = new ArrayList<>();
TestRootCommand(TestCommandManager manager, String name) {
this.manager = manager;
this.name = name;
}
@Override
public void addChild(BaseCommand command) {
if (this.defCommand == null || !command.subCommands.get(BaseCommand.DEFAULT).isEmpty()) {
this.defCommand = command;
}
addChildShared(this.children, this.subCommands, command);
}
@Override
public CommandManager getManager() {
return this.manager;
}
@Override
public SetMultimap<String, RegisteredCommand> getSubCommands() {
return this.subCommands;
}
@Override
public List<BaseCommand> getChildren() {
return this.children;
}
@Override
public String getCommandName() {
return this.name;
}
@Override
public BaseCommand getDefCommand() {
return defCommand;
}
}
+38
View File
@@ -32,6 +32,9 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>5.4.0</junit.version>
<junit.platform.version>1.4.0</junit.platform.version>
<mockito.version>2.25.1</mockito.version>
</properties>
<distributionManagement>
@@ -71,6 +74,10 @@
</compilerArgs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
@@ -139,6 +146,37 @@
<version>15.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<modules>