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

Commit d5e394e

Browse files
author
Irene
committed
Fix and add tests
1 parent 3a0a141 commit d5e394e

File tree

12 files changed

+62
-38
lines changed

12 files changed

+62
-38
lines changed

src/main/java/fi/helsinki/cs/tmc/cli/command/SubmitCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import fi.helsinki.cs.tmc.core.domain.Course;
1818
import fi.helsinki.cs.tmc.core.domain.Exercise;
19+
import fi.helsinki.cs.tmc.core.domain.Organization;
1920
import fi.helsinki.cs.tmc.core.domain.submission.FeedbackQuestion;
2021
import fi.helsinki.cs.tmc.core.domain.submission.SubmissionResult;
2122

src/test/java/fi/helsinki/cs/tmc/cli/command/ConfigCommandTest.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,12 @@ public void cleanUp() {
8888

8989
@Test
9090
public void doNotRunIfNotLoggedIn() {
91-
ctx = spy(new CliContext(io, core, new WorkDir(), new Settings(), analyticsFacade));
91+
when(SettingsIo.loadAccountList()).thenReturn(new AccountList());
9292
app = new Application(ctx);
93-
doReturn(false).when(ctx).checkIsLoggedIn(false, true);
9493

9594
String[] args = {"config -l"};
9695
app.run(args);
97-
io.assertNotContains("=");
96+
io.assertContains("You are not logged in");
9897
}
9998
@Test
10099
public void printsErrorIfNoArgumentsGiven() {
@@ -133,9 +132,9 @@ public void listsAllPropertiesWithExtraArgument() {
133132

134133
@Test
135134
public void getProperty() {
136-
props.put("thing", "value");
137-
app.run(new String[] {"config", "--get", "thing"});
138-
io.assertContains("value");
135+
props.put("testresults-right", "red");
136+
app.run(new String[] {"config", "--get", "testresults-right"});
137+
io.assertContains("red");
139138
io.assertAllPromptsUsed();
140139
}
141140

@@ -194,8 +193,8 @@ public void setsOneExistingProperty() {
194193
public void setsMultiplePropertiesCorrectly() {
195194
io.addConfirmationPrompt(true);
196195
io.addConfirmationPrompt(true);
197-
app.run(new String[] {"config", "update-date=12345", "testresults-right=cyan"});
198-
assertEquals("12345", props.get("update-date"));
196+
app.run(new String[] {"config", "testresults-left=blue", "testresults-right=cyan"});
197+
assertEquals("blue", props.get("testresults-left"));
199198
assertEquals("cyan", props.get("testresults-right"));
200199
io.assertAllPromptsUsed();
201200
}

src/test/java/fi/helsinki/cs/tmc/cli/command/DownloadExercisesCommandTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,12 @@ public void tearDown() throws IOException {
9494

9595
@Test
9696
public void doNotRunIfNotLoggedIn() {
97-
ctx = spy(new CliContext(io, mockCore, workDir, new Settings(), analyticsFacade));
97+
when(SettingsIo.loadAccountList()).thenReturn(new AccountList());
9898
app = new Application(ctx);
99-
doReturn(false).when(ctx).checkIsLoggedIn(false, true);
10099

101100
String[] args = {"download", "foo"};
102101
app.run(args);
103-
io.assertNotContains("Course doesn't exist");
102+
io.assertContains("You are not logged in");
104103
}
105104

106105
@Test

src/test/java/fi/helsinki/cs/tmc/cli/command/InfoCommandTest.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fi.helsinki.cs.tmc.cli.command;
22

33
import static org.junit.Assert.assertNotNull;
4+
import static org.mockito.Matchers.any;
45
import static org.mockito.Matchers.eq;
56
import static org.mockito.Mockito.doReturn;
67
import static org.mockito.Mockito.mock;
@@ -10,9 +11,7 @@
1011

1112
import fi.helsinki.cs.tmc.cli.Application;
1213
import fi.helsinki.cs.tmc.cli.analytics.AnalyticsFacade;
13-
import fi.helsinki.cs.tmc.cli.backend.Account;
14-
import fi.helsinki.cs.tmc.cli.backend.Settings;
15-
import fi.helsinki.cs.tmc.cli.backend.TmcUtil;
14+
import fi.helsinki.cs.tmc.cli.backend.*;
1615
import fi.helsinki.cs.tmc.cli.core.CliContext;
1716
import fi.helsinki.cs.tmc.cli.io.TestIo;
1817
import fi.helsinki.cs.tmc.cli.io.WorkDir;
@@ -38,7 +37,7 @@
3837
import java.util.List;
3938

4039
@RunWith(PowerMockRunner.class)
41-
@PrepareForTest(TmcUtil.class)
40+
@PrepareForTest({ TmcUtil.class, SettingsIo.class })
4241
public class InfoCommandTest {
4342

4443
private static final String COURSE_NAME = "2016-aalto-c";
@@ -53,6 +52,7 @@ public class InfoCommandTest {
5352
private TmcCore mockCore;
5453
private WorkDir workDir;
5554
private Course course;
55+
private AnalyticsFacade analyticsFacade;
5656

5757
@BeforeClass
5858
public static void setUpClass() throws Exception {
@@ -74,7 +74,7 @@ public void setUp() {
7474

7575
mockCore = new TmcCore(new Settings(), new TaskExecutorImpl());
7676
EventSendBuffer eventSendBuffer = new EventSendBuffer(new Settings(), new EventStore());
77-
AnalyticsFacade analyticsFacade = new AnalyticsFacade(new Settings(), eventSendBuffer);
77+
analyticsFacade = new AnalyticsFacade(new Settings(), eventSendBuffer);
7878
ctx = new CliContext(io, mockCore, new WorkDir(), new Settings(), analyticsFacade);
7979
app = new Application(ctx);
8080
workDir = ctx.getWorkDir();
@@ -86,15 +86,22 @@ public void setUp() {
8686
course.setExercises(exercises);
8787

8888
mockStatic(TmcUtil.class);
89+
mockStatic(SettingsIo.class);
90+
AccountList t = new AccountList();
91+
t.addAccount(new Account("username"));
92+
when(SettingsIo.loadAccountList()).thenReturn(t);
93+
when(SettingsIo.saveAccountList(any(AccountList.class))).thenReturn(true);
94+
8995
}
9096

9197
@Test
92-
public void failIfBackendFails() {
98+
public void doNotRunIfNotLoggedIn() {
99+
when(SettingsIo.loadAccountList()).thenReturn(new AccountList());
93100
app = new Application(ctx);
94101

95102
String[] args = {"info", "course", "-i"};
96103
app.run(args);
97-
io.assertNotContains("doesn't exist on this server.");
104+
io.assertContains("You haven't logged in");
98105
}
99106

100107
@Test

src/test/java/fi/helsinki/cs/tmc/cli/command/ListCoursesCommandTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ public void setUp() {
6262
when(SettingsIo.loadAccountList()).thenReturn(accountList);
6363
}
6464

65+
@Test
66+
public void failIfNotLoggedIn() {
67+
when(SettingsIo.loadAccountList()).thenReturn(new AccountList());
68+
String[] args = {"courses"};
69+
app.run(args);
70+
io.assertContains("You are not logged in");
71+
}
72+
6573
@Test
6674
public void failIfThereIsNoConnection() {
6775
when(TmcUtil.hasConnection(eq(ctx))).thenReturn(false);

src/test/java/fi/helsinki/cs/tmc/cli/command/ListExercisesCommandTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,12 @@ public void setUp() {
8686

8787
@Test
8888
public void doNotRunIfNotLoggedIn() {
89-
ctx = spy(new CliContext(io, mockCore, new WorkDir(), new Settings(), null));
89+
when(SettingsIo.loadAccountList()).thenReturn(new AccountList());
9090
app = new Application(ctx);
91-
doReturn(false).when(ctx).checkIsLoggedIn(false, true);
9291

9392
String[] args = {"exercises", "-n", "foo", "-i"};
9493
app.run(args);
95-
io.assertNotContains("Course 'foo' doesn't exist");
94+
io.assertContains("You are not logged in");
9695
}
9796

9897
@Test

src/test/java/fi/helsinki/cs/tmc/cli/command/LoginCommandTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,14 @@ public void analyticsAskedOnFirstLogin() {
243243
app.run(args);
244244
io.assertContains("analytics");
245245
}
246+
247+
@Test
248+
public void logInNotAllowedIfAlreadyLoggedIn() {
249+
AccountList list = new AccountList();
250+
list.addAccount(new Account("loggedin"));
251+
when(SettingsIo.loadAccountList()).thenReturn(list);
252+
String[] args = {"login"};
253+
app.run(args);
254+
io.assertContains("You are already logged in");
255+
}
246256
}

src/test/java/fi/helsinki/cs/tmc/cli/command/OrganizationCommandTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,12 @@ public void setUp() {
7777

7878
@Test
7979
public void doNotRunIfNotLoggedIn() {
80-
ctx = spy(new CliContext(io, mockCore, new WorkDir(), new Settings(), analyticsFacade));
80+
when(SettingsIo.loadAccountList()).thenReturn(new AccountList());
8181
app = new Application(ctx);
82-
doReturn(false).when(ctx).checkIsLoggedIn(false, true);
8382

8483
String[] args = {"organization"};
8584
app.run(args);
86-
io.assertNotContains("slug");
85+
io.assertContains("You are not logged in");
8786
}
8887

8988
@Test

src/test/java/fi/helsinki/cs/tmc/cli/command/PasteCommandTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ public void setup() throws URISyntaxException {
107107
@Test
108108
public void doNotRunIfNotLoggedIn() {
109109
when(CourseInfoIo.load(any(Path.class))).thenReturn(null);
110-
list = new AccountList();
110+
when(SettingsIo.loadAccountList()).thenReturn(new AccountList());
111111
app = new Application(ctx);
112112

113113
String[] args = {"paste"};
114114
app.run(args);
115-
io.assertNotContains("No exercise specified");
115+
io.assertContains("You are not logged in");
116116
}
117117

118118
@Test

src/test/java/fi/helsinki/cs/tmc/cli/command/RunTestsCommandTest.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
import fi.helsinki.cs.tmc.cli.Application;
1313
import fi.helsinki.cs.tmc.cli.analytics.AnalyticsFacade;
14-
import fi.helsinki.cs.tmc.cli.backend.Settings;
15-
import fi.helsinki.cs.tmc.cli.backend.TmcUtil;
14+
import fi.helsinki.cs.tmc.cli.backend.*;
1615
import fi.helsinki.cs.tmc.cli.core.CliContext;
1716
import fi.helsinki.cs.tmc.cli.io.TestIo;
1817
import fi.helsinki.cs.tmc.cli.io.WorkDir;
@@ -44,7 +43,7 @@
4443

4544
/*TODO test the command line options */
4645
@RunWith(PowerMockRunner.class)
47-
@PrepareForTest(TmcUtil.class)
46+
@PrepareForTest({ TmcUtil.class, SettingsIo.class })
4847
public class RunTestsCommandTest {
4948

5049
private static final String COURSE_NAME = "2016-aalto-c";
@@ -98,17 +97,22 @@ public void setUp() {
9897
runResult = new RunResult(status, testResults, logs);
9998

10099
mockStatic(TmcUtil.class);
100+
mockStatic(SettingsIo.class);
101+
when(TmcUtil.hasConnection(eq(ctx))).thenReturn(true);
102+
AccountList t = new AccountList();
103+
t.addAccount(new Account("testuser"));
104+
when(SettingsIo.loadAccountList()).thenReturn(t);
105+
when(SettingsIo.saveAccountList(any(AccountList.class))).thenReturn(true);
101106
}
102107

103108
@Test
104109
public void doNotRunIfNotLoggedIn() {
105-
ctx = spy(new CliContext(io, mockCore, new WorkDir(), new Settings(), null));
110+
when(SettingsIo.loadAccountList()).thenReturn(new AccountList());
106111
app = new Application(ctx);
107-
doReturn(false).when(ctx).checkIsLoggedIn(false, true);
108112

109113
String[] args = {"test"};
110114
app.run(args);
111-
io.assertNotContains("Testing:");
115+
io.assertContains("You are not logged in");
112116
}
113117

114118
@Test

0 commit comments

Comments
 (0)