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

Commit e3c54d7

Browse files
author
Aleksi Salmela
committed
Improve Application unit tests.
1 parent 77643e1 commit e3c54d7

File tree

3 files changed

+71
-11
lines changed

3 files changed

+71
-11
lines changed

src/main/java/fi/helsinki/cs/tmc/cli/Application.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public Application(CliContext context) {
4848

4949
options.addOption("h", "help", false, "Display help information about tmc-cli");
5050
options.addOption("v", "version", false, "Give the version of the tmc-cli");
51-
options.addOption("f", "force-update", false, "Force the auto-update");
51+
options.addOption("u", "force-update", false, "Force the auto-update");
5252

5353
//TODO implement the inTests as context.property
5454
if (!context.inTests()) {
@@ -73,7 +73,6 @@ private String[] parseArgs(String[] args) {
7373
try {
7474
line = this.parser.parse(this.options, args, true);
7575
} catch (ParseException e) {
76-
io.println("Invalid command line arguments.");
7776
io.println(e.getMessage());
7877
return null;
7978
}
@@ -92,7 +91,7 @@ private String[] parseArgs(String[] args) {
9291

9392
boolean showHelp = line.hasOption("h");
9493
boolean showVersion = line.hasOption("v");
95-
boolean forceUpdate = line.hasOption("f");
94+
boolean forceUpdate = line.hasOption("u");
9695

9796
if (showHelp) {
9897
// don't run the help sub-command with -h switch
@@ -170,8 +169,8 @@ private boolean versionCheck() {
170169
public boolean runAutoUpdate() {
171170
Map<String, String> properties = context.getProperties();
172171
Date now = new Date();
173-
TmcCliUpdater update = new TmcCliUpdater(io, EnvironmentUtil.getVersion(),
174-
EnvironmentUtil.isWindows());
172+
TmcCliUpdater update = TmcCliUpdater.createUpdater(io,
173+
EnvironmentUtil.getVersion(), EnvironmentUtil.isWindows());
175174
boolean updated = update.run();
176175

177176
long timestamp = now.getTime();
@@ -181,7 +180,7 @@ public boolean runAutoUpdate() {
181180
return updated;
182181
}
183182

184-
//TODO rename this as getColorProperty
183+
//TODO rename this as getColorProperty and move it somewhere else
185184
public Color.AnsiColor getColor(String propertyName) {
186185
String propertyValue = context.getProperties().get(propertyName);
187186
Color.AnsiColor color = Color.getColor(propertyValue);

src/main/java/fi/helsinki/cs/tmc/cli/updater/TmcCliUpdater.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public TmcCliUpdater(Io io, String currentVersion, boolean isWindows) {
4141
this.isWindows = isWindows;
4242
}
4343

44+
public static TmcCliUpdater createUpdater(Io io, String currentVersion, boolean isWindows) {
45+
return new TmcCliUpdater(io, currentVersion, isWindows);
46+
}
47+
4448
/**
4549
* Checks if there's a newer tmc-cli version released on Github and asks if
4650
* the user wants to download it. TODO: split it up
@@ -61,7 +65,7 @@ public boolean run() {
6165

6266
if (isWindows) { //just show a link for Windows users now, todo...
6367
io.println("Download: https://github.com/tmc-cli/tmc-cli/releases/latest");
64-
return false;
68+
return true;
6569
}
6670

6771
if (! io.readConfirmation("Do you want to download it?", true)) {
Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
11
package fi.helsinki.cs.tmc.cli;
22

33
import static org.junit.Assert.assertTrue;
4+
import static org.mockito.Matchers.any;
5+
import static org.mockito.Matchers.anyString;
6+
import static org.mockito.Mockito.doReturn;
7+
import static org.mockito.Mockito.mock;
8+
import static org.mockito.Mockito.spy;
9+
import static org.mockito.Mockito.times;
10+
import static org.mockito.Mockito.verify;
11+
import static org.mockito.Mockito.when;
12+
import static org.powermock.api.mockito.PowerMockito.mockStatic;
13+
import static org.powermock.api.mockito.PowerMockito.verifyStatic;
414

15+
import fi.helsinki.cs.tmc.cli.io.Io;
516
import fi.helsinki.cs.tmc.cli.io.TestIo;
17+
import fi.helsinki.cs.tmc.cli.updater.TmcCliUpdater;
618

719
import org.junit.Before;
820
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.powermock.core.classloader.annotations.PrepareForTest;
23+
import org.powermock.modules.junit4.PowerMockRunner;
924

25+
import java.util.HashMap;
26+
27+
@RunWith(PowerMockRunner.class)
28+
@PrepareForTest(TmcCliUpdater.class)
1029
public class ApplicationTest {
1130

1231
private Application app;
@@ -16,33 +35,71 @@ public class ApplicationTest {
1635
public void setUp() {
1736
io = new TestIo();
1837
app = new Application(new CliContext(io));
38+
mockStatic(TmcCliUpdater.class);
1939
}
2040

2141
@Test
2242
public void versionWorksWithRightParameter() {
23-
String[] args = {"-v"};
43+
String[] args = {"-v", "foo"};
2444
app.run(args);
2545
assertTrue(io.out().contains("TMC-CLI version"));
46+
io.assertNotContains("Command foo doesn't exist.");
2647
}
2748

2849
@Test
2950
public void failWhenInvalidOption() {
3051
String[] args = {"-a34t3"};
3152
app.run(args);
32-
assertTrue(io.out().contains("Unrecognized option"));
53+
io.assertContains("Unrecognized option");
3354
}
3455

3556
@Test
3657
public void helpWorksWithRightParameter() {
3758
String[] args = {"-h"};
3859
app.run(args);
39-
assertTrue(io.out().contains("Usage: tmc"));
60+
io.assertContains("Usage: tmc");
61+
}
62+
63+
@Test
64+
public void helpOfHelpCommandIsNotGiven() {
65+
String[] args = {"-h", "help"};
66+
app.run(args);
67+
io.assertContains("help");
68+
io.assertNotContains("Usage: tmc help");
4069
}
4170

4271
@Test
4372
public void runCommandWorksWithWrongParameter() {
4473
String[] args = {"foo"};
4574
app.run(args);
46-
assertTrue(io.out().contains("Command foo doesn't exist"));
75+
io.assertContains("Command foo doesn't exist");
76+
}
77+
78+
@Test
79+
public void runAutoUpdate() {
80+
String[] args = {"help"};
81+
TmcCliUpdater mockUpdater = mock(TmcCliUpdater.class);
82+
when(TmcCliUpdater.createUpdater(any(Io.class), anyString(), any(Boolean.class)))
83+
.thenReturn(mockUpdater);
84+
when(mockUpdater.run()).thenReturn(true);
85+
86+
CliContext ctx = spy(new CliContext(null));
87+
when(ctx.getProperties()).thenReturn(new HashMap<String, String>());
88+
89+
app = new Application(ctx);
90+
app.run(args);
91+
92+
verifyStatic(times(1));
93+
TmcCliUpdater.createUpdater(any(Io.class), anyString(), any(Boolean.class));
94+
}
95+
96+
@Test
97+
public void runWithForceUpdate() {
98+
String[] args = {"--force-update", "foo"};
99+
app = spy(app);
100+
doReturn(true).when(app).runAutoUpdate();
101+
app.run(args);
102+
io.assertNotContains("Command foo doesn't exist.");
103+
verify(app, times(1)).runAutoUpdate();
47104
}
48105
}

0 commit comments

Comments
 (0)