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

Commit 9f3e1db

Browse files
author
Aleksi Salmela
committed
Fix terminal io bug.
1 parent fbad270 commit 9f3e1db

4 files changed

Lines changed: 27 additions & 15 deletions

File tree

src/main/java/fi/helsinki/cs/tmc/cli/io/TerminalIo.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@
44
import org.slf4j.LoggerFactory;
55

66
import java.io.Console;
7+
import java.io.InputStream;
78
import java.util.Scanner;
89

910
public class TerminalIo extends Io {
1011

1112
private static final Logger logger = LoggerFactory.getLogger(TerminalIo.class);
13+
private final Scanner scanner;
14+
15+
public TerminalIo(InputStream stream) {
16+
Scanner newScanner = null;
17+
if (stream != null) {
18+
newScanner = new Scanner(stream);
19+
}
20+
scanner = newScanner;
21+
}
1222

1323
@Override
1424
public void print(String str) {
@@ -19,8 +29,11 @@ public void print(String str) {
1929
public String readLine(String prompt) {
2030
print(prompt);
2131

22-
try (Scanner scanner = new Scanner(System.in)) {
32+
try {
2333
return scanner.nextLine();
34+
} catch (Exception e) {
35+
logger.warn("Line could not be read.", e);
36+
return null;
2437
}
2538
}
2639

src/main/java/fi/helsinki/cs/tmc/cli/io/TmcCliProgressObserver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class TmcCliProgressObserver extends ProgressObserver {
1717
protected Boolean hasProgressBar;
1818

1919
public TmcCliProgressObserver() {
20-
this(new TerminalIo());
20+
this(new TerminalIo(System.in));
2121
}
2222

2323
public TmcCliProgressObserver(Io io) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import fi.helsinki.cs.tmc.cli.Application;
88
import fi.helsinki.cs.tmc.cli.CliContext;
99
import fi.helsinki.cs.tmc.cli.io.Io;
10-
import fi.helsinki.cs.tmc.cli.io.TerminalIo;
10+
import fi.helsinki.cs.tmc.cli.io.TestIo;
1111

1212
import org.apache.commons.cli.CommandLine;
1313
import org.apache.commons.cli.Options;
@@ -28,7 +28,7 @@ public class CommandFactoryTest {
2828

2929
@Before
3030
public void setUp() {
31-
ctx = new CliContext(new TerminalIo());
31+
ctx = new CliContext(new TestIo());
3232
}
3333

3434
@Test

src/test/java/fi/helsinki/cs/tmc/cli/io/TerminalIoTest.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import static org.powermock.api.mockito.PowerMockito.spy;
1010
import static org.powermock.api.mockito.PowerMockito.when;
1111

12-
import org.junit.After;
1312
import org.junit.Before;
1413
import org.junit.Test;
1514
import org.junit.runner.RunWith;
@@ -19,7 +18,6 @@
1918
import java.io.ByteArrayInputStream;
2019
import java.io.ByteArrayOutputStream;
2120
import java.io.Console;
22-
import java.io.InputStream;
2321
import java.io.OutputStream;
2422
import java.io.PrintStream;
2523
import java.nio.charset.StandardCharsets;
@@ -30,27 +28,20 @@ public class TerminalIoTest {
3028

3129
private Io io;
3230
private OutputStream os;
33-
private InputStream oldInputStream;
3431

3532
@Before
3633
public void setUp() throws Exception {
37-
oldInputStream = System.in;
38-
io = new TerminalIo();
34+
io = new TerminalIo(null);
3935
os = new ByteArrayOutputStream();
4036
PrintStream ps = new PrintStream(os);
4137
System.setOut(ps);
4238

4339
spy(System.class);
4440
}
4541

46-
@After
47-
public void cleanUp() {
48-
System.setIn(oldInputStream);
49-
}
50-
5142
public void writeString(String string) {
5243
byte[] bytes = string.getBytes(StandardCharsets.UTF_8);
53-
System.setIn(new ByteArrayInputStream(bytes));
44+
io = new TerminalIo(new ByteArrayInputStream(bytes));
5445
}
5546

5647
@Test
@@ -91,6 +82,14 @@ public void readLineIgnoresRestOfTheLines() {
9182
assertEquals("abc ", os.toString());
9283
}
9384

85+
@Test
86+
public void readTwoLines() {
87+
writeString("test\ncool\n");
88+
assertEquals("test", io.readLine("abc "));
89+
assertEquals("cool", io.readLine("abc "));
90+
assertEquals("abc abc ", os.toString());
91+
}
92+
9493
@Test
9594
public void readPassword() {
9695
Console mockConsole = mock(Console.class);

0 commit comments

Comments
 (0)