Skip to content

Commit 943c42a

Browse files
Citymonstretjpenilla
authored andcommitted
Chore: Improve the test setup for cloud-core and clean up some test cases.
1 parent af58148 commit 943c42a

19 files changed

+356
-193
lines changed

build-logic/src/main/kotlin/Versions.kt

+3
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,7 @@ object Versions {
3333
// TEST DEPENDENCIES
3434
const val jupiterEngine = "5.8.1"
3535
const val jmh = "1.27"
36+
const val mockitoCore = "4.1.0"
37+
const val mockitoKotlin = "4.0.0"
38+
const val truth = "1.1.3"
3639
}

build-logic/src/main/kotlin/cloud.base-conventions.gradle.kts

+4
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ repositories {
8989
dependencies {
9090
compileOnlyApi("org.checkerframework", "checker-qual", Versions.checkerQual)
9191
testImplementation("org.junit.jupiter", "junit-jupiter-engine", Versions.jupiterEngine)
92+
testImplementation("org.mockito", "mockito-core", Versions.mockitoCore)
93+
testImplementation("org.mockito.kotlin", "mockito-kotlin", Versions.mockitoKotlin)
94+
testImplementation("com.google.truth", "truth", Versions.truth)
95+
testImplementation("com.google.truth.extensions", "truth-java8-extension", Versions.truth)
9296
errorprone("com.google.errorprone", "error_prone_core", Versions.errorprone)
9397
// Silences compiler warnings from guava using errorprone
9498
compileOnly("com.google.errorprone", "error_prone_annotations", Versions.errorprone)

cloud-core/src/test/java/cloud/commandframework/AnnotationAccessorTest.java

+14-5
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
//
2424
package cloud.commandframework;
2525

26+
import static com.google.common.truth.Truth.assertThat;
27+
2628
import cloud.commandframework.annotations.AnnotationAccessor;
27-
import org.junit.jupiter.api.Assertions;
2829
import org.junit.jupiter.api.Test;
2930

3031
import java.lang.annotation.ElementType;
@@ -36,21 +37,29 @@
3637

3738
public class AnnotationAccessorTest {
3839

39-
@Qualifier("method")
40-
public static void annotatedMethod(@Qualifier("parameter") final String parameter) {
40+
private static final String QUALIFIER_TEST_STRING_PARAMETER = "parameter";
41+
private static final String QUALIFIER_TEST_STRING_METHOD = "method";
42+
43+
@Qualifier(QUALIFIER_TEST_STRING_METHOD)
44+
public static void annotatedMethod(@Qualifier(QUALIFIER_TEST_STRING_PARAMETER) final String parameter) {
4145
}
4246

4347
@Test
4448
void testQualifierResolutionOrder() throws Exception {
49+
// Given
4550
final Method method = AnnotationAccessorTest.class.getMethod("annotatedMethod", String.class);
4651
final Parameter parameter = method.getParameters()[0];
4752
final AnnotationAccessor accessor = AnnotationAccessor.of(
4853
AnnotationAccessor.of(parameter),
4954
AnnotationAccessor.of(method)
5055
);
56+
57+
// When
5158
final Qualifier qualifier = accessor.annotation(Qualifier.class);
52-
Assertions.assertNotNull(qualifier);
53-
Assertions.assertEquals("parameter", qualifier.value());
59+
60+
// Then
61+
assertThat(qualifier).isNotNull();
62+
assertThat(qualifier.value()).isEqualTo(QUALIFIER_TEST_STRING_PARAMETER);
5463
}
5564

5665
@Target({ElementType.PARAMETER, ElementType.METHOD})

cloud-core/src/test/java/cloud/commandframework/CommandHelpHandlerTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
//
2424
package cloud.commandframework;
2525

26+
import static cloud.commandframework.util.TestUtils.createManager;
27+
2628
import cloud.commandframework.arguments.CommandArgument;
2729
import cloud.commandframework.arguments.StaticArgument;
2830
import cloud.commandframework.arguments.standard.IntegerArgument;
@@ -47,7 +49,7 @@ class CommandHelpHandlerTest {
4749

4850
@BeforeAll
4951
static void setup() {
50-
manager = new TestCommandManager();
52+
manager = createManager();
5153
final SimpleCommandMeta meta1 = SimpleCommandMeta.builder().with(CommandMeta.DESCRIPTION, "Command with only literals").build();
5254
manager.command(manager.commandBuilder("test", meta1).literal("this").literal("thing").build());
5355
final SimpleCommandMeta meta2 = SimpleCommandMeta.builder().with(CommandMeta.DESCRIPTION, "Command with variables").build();
@@ -103,10 +105,8 @@ void testPredicateFilter() {
103105
* This predicate only displays the commands starting with /test
104106
* The one command ending in 'thing' is excluded as well, for complexity
105107
*/
106-
final Predicate<Command<TestCommandSender>> predicate = (command) -> {
107-
return command.toString().startsWith("test ")
108-
&& !command.toString().endsWith(" thing");
109-
};
108+
final Predicate<Command<TestCommandSender>> predicate = (command) -> command.toString().startsWith("test ")
109+
&& !command.toString().endsWith(" thing");
110110

111111
/*
112112
* List all commands from root, which should show only:
@@ -132,7 +132,7 @@ void testPredicateFilter() {
132132
*/
133133
final CommandHelpHandler.HelpTopic<TestCommandSender> query3 = manager.getCommandHelpHandler(predicate).queryHelp("test int");
134134
Assertions.assertTrue(query3 instanceof CommandHelpHandler.VerboseHelpTopic);
135-
Assertions.assertEquals(Arrays.asList("test int <int>"), getSortedSyntaxStrings(query3));
135+
Assertions.assertEquals(Collections.singletonList("test int <int>"), getSortedSyntaxStrings(query3));
136136

137137
/*
138138
* List all commands from /vec, which should show none
@@ -228,7 +228,7 @@ private void printMultiHelpTopic(final CommandHelpHandler.MultiHelpTopic<TestCom
228228
printBuilder.append("└── ");
229229
}
230230
printBuilder.append(suggestion);
231-
System.out.println(printBuilder.toString());
231+
System.out.println(printBuilder);
232232
}
233233
}
234234

cloud-core/src/test/java/cloud/commandframework/CommandPerformanceTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
//
2424
package cloud.commandframework;
2525

26+
import static cloud.commandframework.util.TestUtils.createManager;
27+
2628
import cloud.commandframework.context.CommandContext;
2729
import cloud.commandframework.execution.CommandResult;
2830
import org.junit.jupiter.api.Assertions;
@@ -42,7 +44,7 @@ final class CommandPerformanceTest {
4244

4345
@BeforeAll
4446
static void setup() {
45-
manager = new TestCommandManager();
47+
manager = createManager();
4648

4749
final StringBuilder literalBuilder = new StringBuilder("literals");
4850

cloud-core/src/test/java/cloud/commandframework/CommandPermissionTest.java

+17-10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
//
2424
package cloud.commandframework;
2525

26+
import static com.google.common.truth.Truth.assertThat;
27+
2628
import cloud.commandframework.arguments.standard.IntegerArgument;
2729
import cloud.commandframework.execution.CommandExecutionCoordinator;
2830
import cloud.commandframework.keys.SimpleCloudKey;
@@ -33,7 +35,7 @@
3335
import cloud.commandframework.permission.OrPermission;
3436
import cloud.commandframework.permission.Permission;
3537
import cloud.commandframework.permission.PredicatePermission;
36-
import org.junit.jupiter.api.Assertions;
38+
import org.checkerframework.checker.nullness.qual.NonNull;
3739
import org.junit.jupiter.api.BeforeAll;
3840
import org.junit.jupiter.api.Test;
3941

@@ -42,6 +44,7 @@
4244
import java.util.concurrent.atomic.AtomicBoolean;
4345

4446
import static org.junit.jupiter.api.Assertions.assertFalse;
47+
import static org.junit.jupiter.api.Assertions.assertThrows;
4548
import static org.junit.jupiter.api.Assertions.assertTrue;
4649

4750
class CommandPermissionTest {
@@ -58,16 +61,18 @@ static void setup() {
5861

5962
@Test
6063
void testCompoundPermission() {
61-
Assertions.assertTrue(manager.suggest(new TestCommandSender(), "t").isEmpty());
62-
assertFalse(manager.suggest(new TestCommandSender("test.permission.four"), "t").isEmpty());
64+
assertThat(manager.suggest(new TestCommandSender(), "t")).isEmpty();
65+
assertThat(manager.suggest(new TestCommandSender("test.permission.four"), "t")).isNotEmpty();
6366
}
6467

6568
@Test
6669
void testComplexPermissions() {
6770
manager.command(manager.commandBuilder("first").permission("first"));
6871
manager.command(manager.commandBuilder("first").argument(IntegerArgument.of("second")).permission("second"));
72+
6973
manager.executeCommand(new TestCommandSender(), "first").join();
70-
Assertions.assertThrows(
74+
75+
assertThrows(
7176
CompletionException.class,
7277
() -> manager.executeCommand(new TestCommandSender(), "first 10").join()
7378
);
@@ -77,10 +82,12 @@ void testComplexPermissions() {
7782
void testAndPermissions() {
7883
final CommandPermission test = Permission.of("one").and(Permission.of("two"));
7984
final TestCommandSender sender = new TestCommandSender("one");
80-
assertFalse(manager.hasPermission(sender, test));
81-
assertFalse(manager.hasPermission(new TestCommandSender("two"), test));
85+
86+
assertThat(manager.hasPermission(sender, test)).isFalse();
87+
assertThat(manager.hasPermission(new TestCommandSender("two"), test)).isFalse();
88+
8289
sender.addPermission("two");
83-
assertTrue(manager.hasPermission(sender, test));
90+
assertThat(manager.hasPermission(sender, test)).isTrue();
8491
}
8592

8693
@Test
@@ -126,7 +133,7 @@ void testPredicatePermissions() {
126133
manager.executeCommand(new TestCommandSender(), "predicate").join();
127134
// Now we force it to fail
128135
condition.set(false);
129-
Assertions.assertThrows(
136+
assertThrows(
130137
CompletionException.class,
131138
() -> manager.executeCommand(new TestCommandSender(), "predicate").join()
132139
);
@@ -141,7 +148,7 @@ private PermissionOutputtingCommandManager() {
141148

142149
@Override
143150
public boolean hasPermission(
144-
final TestCommandSender sender,
151+
final @NonNull TestCommandSender sender,
145152
final String permission
146153
) {
147154
if (permission.equalsIgnoreCase("first")) {
@@ -154,7 +161,7 @@ public boolean hasPermission(
154161
}
155162

156163
@Override
157-
public CommandMeta createDefaultCommandMeta() {
164+
public @NonNull CommandMeta createDefaultCommandMeta() {
158165
return SimpleCommandMeta.empty();
159166
}
160167

cloud-core/src/test/java/cloud/commandframework/CommandPostProcessorTest.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323
//
2424
package cloud.commandframework;
2525

26+
import static cloud.commandframework.util.TestUtils.createManager;
27+
2628
import cloud.commandframework.execution.postprocessor.CommandPostprocessingContext;
2729
import cloud.commandframework.execution.postprocessor.CommandPostprocessor;
2830
import cloud.commandframework.meta.SimpleCommandMeta;
2931
import cloud.commandframework.services.types.ConsumerService;
32+
import org.checkerframework.checker.nullness.qual.NonNull;
3033
import org.junit.jupiter.api.Assertions;
3134
import org.junit.jupiter.api.BeforeAll;
3235
import org.junit.jupiter.api.Test;
@@ -38,7 +41,7 @@ public class CommandPostProcessorTest {
3841

3942
@BeforeAll
4043
static void newTree() {
41-
manager = new TestCommandManager();
44+
manager = createManager();
4245
manager.command(manager.commandBuilder("test", SimpleCommandMeta.empty())
4346
.handler(c -> state[0] = true)
4447
.build());
@@ -48,13 +51,13 @@ static void newTree() {
4851
@Test
4952
void testPreprocessing() {
5053
manager.executeCommand(new TestCommandSender(), "test").join();
51-
Assertions.assertEquals(false, state[0]);
54+
Assertions.assertFalse(state[0]);
5255
}
5356

5457
static final class SamplePostprocessor implements CommandPostprocessor<TestCommandSender> {
5558

5659
@Override
57-
public void accept(final CommandPostprocessingContext<TestCommandSender> context) {
60+
public void accept(final @NonNull CommandPostprocessingContext<TestCommandSender> context) {
5861
ConsumerService.interrupt();
5962
}
6063

cloud-core/src/test/java/cloud/commandframework/CommandPreProcessorTest.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323
//
2424
package cloud.commandframework;
2525

26+
import static cloud.commandframework.util.TestUtils.createManager;
27+
2628
import cloud.commandframework.arguments.standard.EnumArgument;
2729
import cloud.commandframework.execution.preprocessor.CommandPreprocessingContext;
2830
import cloud.commandframework.execution.preprocessor.CommandPreprocessor;
2931
import cloud.commandframework.meta.SimpleCommandMeta;
3032
import cloud.commandframework.services.types.ConsumerService;
33+
import org.checkerframework.checker.nullness.qual.NonNull;
3134
import org.junit.jupiter.api.Assertions;
3235
import org.junit.jupiter.api.BeforeAll;
3336
import org.junit.jupiter.api.Test;
@@ -38,7 +41,7 @@ public class CommandPreProcessorTest {
3841

3942
@BeforeAll
4043
static void newTree() {
41-
manager = new TestCommandManager();
44+
manager = createManager();
4245
manager.command(manager.commandBuilder("test", SimpleCommandMeta.empty())
4346
.argument(EnumArgument.of(SampleEnum.class, "enum"))
4447
.handler(
@@ -72,7 +75,7 @@ enum SampleEnum {
7275
static final class SamplePreprocessor implements CommandPreprocessor<TestCommandSender> {
7376

7477
@Override
75-
public void accept(final CommandPreprocessingContext<TestCommandSender> context) {
78+
public void accept(final @NonNull CommandPreprocessingContext<TestCommandSender> context) {
7679
try {
7780
final int num = Integer.parseInt(context.getInputQueue().removeFirst());
7881
context.getCommandContext().store("int", num);

cloud-core/src/test/java/cloud/commandframework/CommandRegistrationStateTest.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,43 @@
2626
import cloud.commandframework.internal.CommandRegistrationHandler;
2727
import org.junit.jupiter.api.Test;
2828

29+
import static cloud.commandframework.util.TestUtils.createManager;
2930
import static org.junit.jupiter.api.Assertions.assertEquals;
3031
import static org.junit.jupiter.api.Assertions.assertThrows;
3132

3233
public class CommandRegistrationStateTest {
3334

3435
@Test
3536
void testInitialState() {
36-
final TestCommandManager manager = new TestCommandManager();
37+
final CommandManager<TestCommandSender> manager = createManager();
3738
assertEquals(CommandManager.RegistrationState.BEFORE_REGISTRATION, manager.getRegistrationState());
3839
}
3940

4041
@Test
4142
void testRegistrationChangesState() {
42-
final TestCommandManager manager = new TestCommandManager();
43+
final CommandManager<TestCommandSender> manager = createManager();
44+
4345
manager.command(manager.commandBuilder("test").handler(ctx -> {
4446
}));
47+
4548
assertEquals(CommandManager.RegistrationState.REGISTERING, manager.getRegistrationState());
46-
// And a second registration maintains it
49+
}
50+
51+
@Test
52+
void testDoubleRegistrationPersistsState() {
53+
final CommandManager<TestCommandSender> manager = createManager();
54+
55+
manager.command(manager.commandBuilder("test").handler(ctx -> {
56+
}));
4757
manager.command(manager.commandBuilder("test2").handler(ctx -> {
4858
}));
59+
4960
assertEquals(CommandManager.RegistrationState.REGISTERING, manager.getRegistrationState());
5061
}
5162

5263
@Test
5364
void testChangingRegistrationHandlerFails() {
54-
final TestCommandManager manager = new TestCommandManager();
65+
final CommandManager<TestCommandSender> manager = createManager();
5566
manager.command(manager.commandBuilder("test").handler(ctx -> {
5667
}));
5768
assertThrows(
@@ -62,7 +73,7 @@ void testChangingRegistrationHandlerFails() {
6273

6374
@Test
6475
void testRegistrationFailsInAfterRegistrationState() {
65-
final TestCommandManager manager = new TestCommandManager();
76+
final CommandManager<TestCommandSender> manager = createManager();
6677
manager.command(manager.commandBuilder("test").handler(ctx -> {
6778
}));
6879

@@ -76,7 +87,7 @@ void testRegistrationFailsInAfterRegistrationState() {
7687

7788
@Test
7889
void testAllowUnsafeRegistration() {
79-
final TestCommandManager manager = new TestCommandManager();
90+
final CommandManager<TestCommandSender> manager = createManager();
8091
manager.setSetting(CommandManager.ManagerSettings.ALLOW_UNSAFE_REGISTRATION, true);
8192
manager.command(manager.commandBuilder("test").handler(ctx -> {
8293
}));

0 commit comments

Comments
 (0)