Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit a26e7dc

Browse files
author
Aleksi Salmela
committed
Reload CommandFactory for every unit test.
1 parent 31e1ae6 commit a26e7dc

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

src/main/java/fi/helsinki/cs/tmc/cli/core/CommandFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ public static void addCommand(String name, String packageName, Class commandClas
4545
/**
4646
* Merge this method implementation with the above version.
4747
*
48+
* @param packageName The package/category of the command
4849
* @param commandClass The class of the command
4950
*/
50-
public static void addCommand(Class commandClass) {
51+
protected static void addCommand(String packageName, Class commandClass) {
5152
Class<Command> klass = castToCommandClass(commandClass);
5253
Annotation annotation = klass.getAnnotation(Command.class);
5354
if (annotation == null) {
@@ -57,7 +58,7 @@ public static void addCommand(Class commandClass) {
5758
if (!AbstractCommand.class.isAssignableFrom(commandClass)) {
5859
throw new RuntimeException("Command must implement CommandInterface");
5960
}
60-
CommandFactory.commands.put(command.name(), klass);
61+
CommandFactory.addCommand(command.name(), packageName, commandClass);
6162
}
6263

6364
/**

src/test/java/fi/helsinki/cs/tmc/cli/core/CommandFactoryTest.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package fi.helsinki.cs.tmc.cli.core;
22

3+
import static org.junit.Assert.assertEquals;
34
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertNotSame;
46
import static org.junit.Assert.assertNull;
57
import static org.junit.Assert.assertTrue;
68

79
import fi.helsinki.cs.tmc.cli.Application;
10+
import fi.helsinki.cs.tmc.cli.command.HelpCommand;
811
import fi.helsinki.cs.tmc.cli.io.TestIo;
912

1013
import org.apache.commons.cli.CommandLine;
@@ -26,6 +29,7 @@ public class CommandFactoryTest {
2629
@Before
2730
public void setUp() {
2831
ctx = new CliContext(new TestIo());
32+
CommandFactory.reload();
2933
}
3034

3135
@Test
@@ -34,12 +38,12 @@ public void constructorAddsCommands() {
3438
}
3539

3640
@Test
37-
public void createCommandWorksWithRealCommand() {
41+
public void createHelpCommand() {
3842
assertNotNull(CommandFactory.createCommand("help"));
3943
}
4044

4145
@Test
42-
public void createCommandWorksWithBadCommand() {
46+
public void createNonexistingCommand() {
4347
assertNull(CommandFactory.createCommand("foobar"));
4448
}
4549

@@ -59,7 +63,7 @@ public void run(CliContext context, CommandLine args) {
5963

6064
@Test
6165
public void addGoodCommand() {
62-
CommandFactory.addCommand(GoodCommand.class);
66+
CommandFactory.addCommand("category", GoodCommand.class);
6367
//TODO check the all the stuff in the command
6468
assertNotNull(CommandFactory.createCommand("good"));
6569
}
@@ -82,7 +86,7 @@ public void addCommandWithoutProperAnnotation() {
8286
//WARNING The exception code leads to unpredicatble test results
8387
//exception.expect(RuntimeException.class);
8488
//exception.expectMessage(contains("annotation"));
85-
CommandFactory.addCommand(BadCommand.class);
89+
CommandFactory.addCommand("category", BadCommand.class);
8690
}
8791

8892
private static class ReallyBadCommand {}
@@ -92,6 +96,6 @@ public void addCommandThatDoesntExtendInterface() {
9296
//WARNING The exception code leads to unpredicatble test results
9397
//exception.expect(RuntimeException.class);
9498
//exception.expectMessage(contains("Interface"));
95-
CommandFactory.addCommand(ReallyBadCommand.class);
99+
CommandFactory.addCommand("category", ReallyBadCommand.class);
96100
}
97101
}

0 commit comments

Comments
 (0)