Skip to content

Commit 55e8710

Browse files
authored
Hotfix/internal converters (#54)
* feat(core): add wrapper and primitive type in internal converters * feat(core): version * feat(core&bom): add bom and fix converter * feat(core): better management of arguments * feat(core): better encapsulation of primitve type
1 parent db7b68b commit 55e8710

5 files changed

Lines changed: 394 additions & 5 deletions

File tree

annotations-addon/src/test/java/fr/traqueur/commands/annotations/AnnotationCommandProcessorTest.java

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,15 +387,97 @@ void commandsShouldBeSortedByDepth() {
387387
processor.register(commands);
388388

389389
List<String> labels = platform.getRegisteredLabels();
390-
390+
391391
int adminIndex = labels.indexOf("admin");
392392
int adminReloadIndex = labels.indexOf("admin.reload");
393393
int adminReloadConfigIndex = labels.indexOf("admin.reload.config");
394-
395-
assertTrue(adminIndex < adminReloadIndex,
394+
395+
assertTrue(adminIndex < adminReloadIndex,
396396
"admin should be registered before admin.reload");
397397
assertTrue(adminReloadIndex < adminReloadConfigIndex,
398398
"admin.reload should be registered before admin.reload.config");
399399
}
400400
}
401+
402+
@Nested
403+
@DisplayName("Primitive Types Support")
404+
class PrimitiveTypesSupport {
405+
406+
@Test
407+
@DisplayName("should register command with primitive int argument")
408+
void shouldRegisterCommandWithPrimitiveInt() {
409+
PrimitiveTypesTestCommands commands = new PrimitiveTypesTestCommands();
410+
processor.register(commands);
411+
412+
assertTrue(platform.hasCommand("primitiveint"));
413+
Command<Object, MockSender> cmd = platform.getCommand("primitiveint");
414+
assertNotNull(cmd);
415+
416+
List<Argument<MockSender>> args = cmd.getArgs();
417+
assertEquals(1, args.size());
418+
assertEquals("value", args.get(0).name());
419+
}
420+
421+
@Test
422+
@DisplayName("should register command with primitive long argument")
423+
void shouldRegisterCommandWithPrimitiveLong() {
424+
PrimitiveTypesTestCommands commands = new PrimitiveTypesTestCommands();
425+
processor.register(commands);
426+
427+
assertTrue(platform.hasCommand("primitivelong"));
428+
Command<Object, MockSender> cmd = platform.getCommand("primitivelong");
429+
assertNotNull(cmd);
430+
431+
List<Argument<MockSender>> args = cmd.getArgs();
432+
assertEquals(1, args.size());
433+
assertEquals("value", args.get(0).name());
434+
}
435+
436+
@Test
437+
@DisplayName("should register command with primitive double argument")
438+
void shouldRegisterCommandWithPrimitiveDouble() {
439+
PrimitiveTypesTestCommands commands = new PrimitiveTypesTestCommands();
440+
processor.register(commands);
441+
442+
assertTrue(platform.hasCommand("primitivedouble"));
443+
Command<Object, MockSender> cmd = platform.getCommand("primitivedouble");
444+
assertNotNull(cmd);
445+
446+
List<Argument<MockSender>> args = cmd.getArgs();
447+
assertEquals(1, args.size());
448+
assertEquals("value", args.get(0).name());
449+
}
450+
451+
@Test
452+
@DisplayName("should register command with primitive boolean argument")
453+
void shouldRegisterCommandWithPrimitiveBoolean() {
454+
PrimitiveTypesTestCommands commands = new PrimitiveTypesTestCommands();
455+
processor.register(commands);
456+
457+
assertTrue(platform.hasCommand("primitivebool"));
458+
Command<Object, MockSender> cmd = platform.getCommand("primitivebool");
459+
assertNotNull(cmd);
460+
461+
List<Argument<MockSender>> args = cmd.getArgs();
462+
assertEquals(1, args.size());
463+
assertEquals("enabled", args.get(0).name());
464+
}
465+
466+
@Test
467+
@DisplayName("should register command with mixed primitive types")
468+
void shouldRegisterCommandWithMixedPrimitives() {
469+
PrimitiveTypesTestCommands commands = new PrimitiveTypesTestCommands();
470+
processor.register(commands);
471+
472+
assertTrue(platform.hasCommand("mixedprimitives"));
473+
Command<Object, MockSender> cmd = platform.getCommand("mixedprimitives");
474+
assertNotNull(cmd);
475+
476+
List<Argument<MockSender>> args = cmd.getArgs();
477+
assertEquals(3, args.size());
478+
assertEquals("count", args.get(0).name());
479+
assertEquals("enabled", args.get(1).name());
480+
assertEquals("ratio", args.get(2).name());
481+
}
482+
}
401483
}
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
package fr.traqueur.commands.annotations;
2+
3+
import fr.traqueur.commands.annotations.commands.PrimitiveTypesTestCommands;
4+
import fr.traqueur.commands.api.arguments.Arguments;
5+
import fr.traqueur.commands.api.models.Command;
6+
import fr.traqueur.commands.test.mocks.MockCommandManager;
7+
import fr.traqueur.commands.test.mocks.MockPlatform;
8+
import fr.traqueur.commands.test.mocks.MockSender;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.DisplayName;
11+
import org.junit.jupiter.api.Test;
12+
13+
import static org.junit.jupiter.api.Assertions.*;
14+
15+
/**
16+
* Integration tests for primitive types support.
17+
* These tests verify that commands with primitive type arguments can be:
18+
* 1. Parsed correctly
19+
* 2. Stored in Arguments
20+
* 3. Retrieved from Arguments
21+
* 4. Passed to method invocation via reflection
22+
*/
23+
@DisplayName("Primitive Types Integration")
24+
class PrimitiveTypesIntegrationTest {
25+
26+
private MockPlatform platform;
27+
private MockCommandManager manager;
28+
private PrimitiveTypesTestCommands commands;
29+
30+
// Mock sender implementation for testing
31+
private static class TestSender implements MockSender {
32+
@Override
33+
public void sendMessage(String message) {
34+
// Do nothing
35+
}
36+
37+
@Override
38+
public boolean hasPermission(String permission) {
39+
return true;
40+
}
41+
}
42+
43+
@BeforeEach
44+
void setUp() {
45+
platform = new MockPlatform();
46+
manager = new MockCommandManager(platform);
47+
AnnotationCommandProcessor<Object, MockSender> processor = new AnnotationCommandProcessor<>(manager);
48+
commands = new PrimitiveTypesTestCommands();
49+
processor.register(commands);
50+
}
51+
52+
@Test
53+
@DisplayName("should execute command with primitive int")
54+
void shouldExecuteCommandWithPrimitiveInt() throws Exception {
55+
Command<Object, MockSender> cmd = platform.getCommand("primitiveint");
56+
assertNotNull(cmd);
57+
58+
// Parse arguments
59+
Arguments args = manager.parse(cmd, new String[]{"42"});
60+
assertNotNull(args);
61+
assertEquals(1, args.size());
62+
63+
// Verify the argument was parsed and stored correctly
64+
Integer value = args.get("value");
65+
assertEquals(42, value);
66+
67+
// Execute the command
68+
TestSender sender = new TestSender();
69+
cmd.execute(sender, args);
70+
71+
// Verify the method was called
72+
assertEquals(1, commands.executedCommands.size());
73+
assertEquals("primitiveint", commands.executedCommands.getFirst());
74+
75+
// Verify arguments were passed correctly
76+
Object[] invokeArgs = commands.executedArgs.getFirst();
77+
assertEquals(2, invokeArgs.length);
78+
assertEquals(sender, invokeArgs[0]);
79+
assertEquals(42, invokeArgs[1]);
80+
assertInstanceOf(Integer.class, invokeArgs[1]);
81+
}
82+
83+
@Test
84+
@DisplayName("should execute command with primitive boolean")
85+
void shouldExecuteCommandWithPrimitiveBoolean() throws Exception {
86+
Command<Object, MockSender> cmd = platform.getCommand("primitivebool");
87+
assertNotNull(cmd);
88+
89+
// Parse arguments
90+
Arguments args = manager.parse(cmd, new String[]{"true"});
91+
assertNotNull(args);
92+
93+
// Verify the argument was parsed and stored correctly
94+
Boolean enabled = args.get("enabled");
95+
assertTrue(enabled);
96+
97+
// Execute the command
98+
TestSender sender = new TestSender();
99+
cmd.execute(sender, args);
100+
101+
// Verify the method was called correctly
102+
assertEquals(1, commands.executedCommands.size());
103+
Object[] invokeArgs = commands.executedArgs.getFirst();
104+
assertEquals(true, invokeArgs[1]);
105+
assertInstanceOf(Boolean.class, invokeArgs[1]);
106+
}
107+
108+
@Test
109+
@DisplayName("should execute command with primitive double")
110+
void shouldExecuteCommandWithPrimitiveDouble() throws Exception {
111+
Command<Object, MockSender> cmd = platform.getCommand("primitivedouble");
112+
assertNotNull(cmd);
113+
114+
// Parse arguments
115+
Arguments args = manager.parse(cmd, new String[]{"3.14"});
116+
assertNotNull(args);
117+
118+
// Verify the argument was parsed and stored correctly
119+
Double value = args.get("value");
120+
assertEquals(3.14, value, 0.001);
121+
122+
// Execute the command
123+
TestSender sender = new TestSender();
124+
cmd.execute(sender, args);
125+
126+
// Verify the method was called correctly
127+
assertEquals(1, commands.executedCommands.size());
128+
Object[] invokeArgs = commands.executedArgs.getFirst();
129+
assertInstanceOf(Double.class, invokeArgs[1]);
130+
assertEquals(3.14, (Double) invokeArgs[1], 0.001);
131+
}
132+
133+
@Test
134+
@DisplayName("should execute command with primitive long")
135+
void shouldExecuteCommandWithPrimitiveLong() throws Exception {
136+
Command<Object, MockSender> cmd = platform.getCommand("primitivelong");
137+
assertNotNull(cmd);
138+
139+
// Parse arguments
140+
Arguments args = manager.parse(cmd, new String[]{"9223372036854775807"});
141+
assertNotNull(args);
142+
143+
// Verify the argument was parsed and stored correctly
144+
Long value = args.get("value");
145+
assertEquals(9223372036854775807L, value);
146+
147+
// Execute the command
148+
TestSender sender = new TestSender();
149+
cmd.execute(sender, args);
150+
151+
// Verify the method was called correctly
152+
assertEquals(1, commands.executedCommands.size());
153+
Object[] invokeArgs = commands.executedArgs.getFirst();
154+
assertEquals(9223372036854775807L, invokeArgs[1]);
155+
assertInstanceOf(Long.class, invokeArgs[1]);
156+
}
157+
158+
@Test
159+
@DisplayName("should execute command with mixed primitive types")
160+
void shouldExecuteCommandWithMixedPrimitives() throws Exception {
161+
Command<Object, MockSender> cmd = platform.getCommand("mixedprimitives");
162+
assertNotNull(cmd);
163+
164+
// Parse arguments
165+
Arguments args = manager.parse(cmd, new String[]{"10", "true", "2.5"});
166+
assertNotNull(args);
167+
assertEquals(3, args.size());
168+
169+
// Verify all arguments were parsed and stored correctly
170+
Integer count = args.get("count");
171+
Boolean enabled = args.get("enabled");
172+
Double ratio = args.get("ratio");
173+
174+
assertEquals(10, count);
175+
assertTrue(enabled);
176+
assertEquals(2.5, ratio, 0.001);
177+
178+
// Execute the command
179+
TestSender sender = new TestSender();
180+
cmd.execute(sender, args);
181+
182+
// Verify the method was called correctly with all arguments
183+
assertEquals(1, commands.executedCommands.size());
184+
assertEquals("mixedprimitives", commands.executedCommands.getFirst());
185+
186+
Object[] invokeArgs = commands.executedArgs.getFirst();
187+
assertEquals(4, invokeArgs.length);
188+
assertEquals(sender, invokeArgs[0]);
189+
assertEquals(10, invokeArgs[1]);
190+
assertEquals(true, invokeArgs[2]);
191+
assertEquals(2.5, (Double) invokeArgs[3], 0.001);
192+
}
193+
194+
@Test
195+
@DisplayName("should handle negative primitive int")
196+
void shouldHandleNegativePrimitiveInt() throws Exception {
197+
Command<Object, MockSender> cmd = platform.getCommand("primitiveint");
198+
Arguments args = manager.parse(cmd, new String[]{"-42"});
199+
200+
Integer value = args.get("value");
201+
assertEquals(-42, value);
202+
203+
TestSender sender = new TestSender();
204+
cmd.execute(sender, args);
205+
206+
Object[] invokeArgs = commands.executedArgs.getFirst();
207+
assertEquals(-42, invokeArgs[1]);
208+
}
209+
210+
@Test
211+
@DisplayName("should handle false boolean primitive")
212+
void shouldHandleFalseBooleanPrimitive() throws Exception {
213+
Command<Object, MockSender> cmd = platform.getCommand("primitivebool");
214+
Arguments args = manager.parse(cmd, new String[]{"false"});
215+
216+
Boolean enabled = args.get("enabled");
217+
assertFalse(enabled);
218+
219+
TestSender sender = new TestSender();
220+
cmd.execute(sender, args);
221+
222+
Object[] invokeArgs = commands.executedArgs.getFirst();
223+
assertEquals(false, invokeArgs[1]);
224+
}
225+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package fr.traqueur.commands.annotations.commands;
2+
3+
import fr.traqueur.commands.annotations.Arg;
4+
import fr.traqueur.commands.annotations.Command;
5+
import fr.traqueur.commands.annotations.CommandContainer;
6+
import fr.traqueur.commands.test.mocks.MockSender;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
/**
12+
* Test commands using primitive types (int, boolean, double, long) instead of wrappers.
13+
* This tests that the ArgumentConverter.Wrapper properly normalizes primitive types to wrappers.
14+
*/
15+
@CommandContainer
16+
public class PrimitiveTypesTestCommands {
17+
18+
public final List<String> executedCommands = new ArrayList<>();
19+
public final List<Object[]> executedArgs = new ArrayList<>();
20+
21+
@Command(name = "primitiveint", description = "Test command with primitive int")
22+
public void primitiveIntCommand(MockSender sender, @Arg("value") int value) {
23+
executedCommands.add("primitiveint");
24+
executedArgs.add(new Object[]{sender, value});
25+
}
26+
27+
@Command(name = "primitivelong", description = "Test command with primitive long")
28+
public void primitiveLongCommand(MockSender sender, @Arg("value") long value) {
29+
executedCommands.add("primitivelong");
30+
executedArgs.add(new Object[]{sender, value});
31+
}
32+
33+
@Command(name = "primitivedouble", description = "Test command with primitive double")
34+
public void primitiveDoubleCommand(MockSender sender, @Arg("value") double value) {
35+
executedCommands.add("primitivedouble");
36+
executedArgs.add(new Object[]{sender, value});
37+
}
38+
39+
@Command(name = "primitivebool", description = "Test command with primitive boolean")
40+
public void primitiveBooleanCommand(MockSender sender, @Arg("enabled") boolean enabled) {
41+
executedCommands.add("primitivebool");
42+
executedArgs.add(new Object[]{sender, enabled});
43+
}
44+
45+
@Command(name = "mixedprimitives", description = "Test command with multiple primitive types")
46+
public void mixedPrimitivesCommand(MockSender sender,
47+
@Arg("count") int count,
48+
@Arg("enabled") boolean enabled,
49+
@Arg("ratio") double ratio) {
50+
executedCommands.add("mixedprimitives");
51+
executedArgs.add(new Object[]{sender, count, enabled, ratio});
52+
}
53+
}

0 commit comments

Comments
 (0)