Skip to content

Commit c7f6cea

Browse files
Fix and enable DevUtilRunTestThreadTest.testRunHotkeyReaderThread test (#492)
* Fix and enable DevUtilRunTestThreadTest.testRunHotkeyReaderThread test * Use Log4j in tests and include slf4j2 provider --------- Co-authored-by: Gilbert Kwan <gkwan@ca.ibm.com>
1 parent e030a26 commit c7f6cea

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@
9898
<artifactId>jackson-core</artifactId>
9999
<version>2.20.0</version>
100100
</dependency>
101+
<dependency>
102+
<groupId>org.apache.logging.log4j</groupId>
103+
<artifactId>log4j-core</artifactId>
104+
<version>2.25.1</version>
105+
<scope>test</scope>
106+
</dependency>
107+
<dependency>
108+
<groupId>org.apache.logging.log4j</groupId>
109+
<artifactId>log4j-slf4j2-impl</artifactId>
110+
<version>2.25.1</version>
111+
<scope>test</scope>
112+
</dependency>
101113
<!-- AI dependencies -->
102114
<dependency>
103115
<groupId>dev.langchain4j</groupId>

src/main/java/io/openliberty/tools/common/ai/util/Utils.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,21 @@ public static String getAbsolutePath(File p) {
5959
}
6060

6161
public static LineReader getReader() {
62+
return getReader(null);
63+
}
64+
65+
public static LineReader getReader(Terminal overrideTerminal) {
6266
if (reader == null) {
6367
try {
64-
terminal = TerminalBuilder.builder().system(true).build();
68+
if (overrideTerminal == null) {
69+
if (terminal == null) {
70+
overrideTerminal = terminal = TerminalBuilder.builder().system(true).build();
71+
} else {
72+
overrideTerminal = terminal;
73+
}
74+
}
6575
reader = LineReaderBuilder.builder()
66-
.terminal(terminal)
76+
.terminal(overrideTerminal)
6777
.parser(new MultiLineParser())
6878
.variable(LineReader.SECONDARY_PROMPT_PATTERN, "")
6979
.build();

src/test/java/io/openliberty/tools/common/plugins/util/DevUtilRunTestThreadTest.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,23 @@
1818
import static org.junit.Assert.assertEquals;
1919
import static org.junit.Assert.fail;
2020

21-
import java.io.ByteArrayInputStream;
2221
import java.io.File;
2322
import java.io.IOException;
24-
import java.io.InputStream;
23+
import java.io.OutputStream;
24+
import java.io.PipedInputStream;
25+
import java.io.PipedOutputStream;
2526
import java.util.concurrent.ArrayBlockingQueue;
2627
import java.util.concurrent.ThreadPoolExecutor;
2728
import java.util.concurrent.TimeUnit;
2829

30+
import org.jline.reader.LineReader;
31+
import org.jline.terminal.TerminalBuilder;
2932
import org.junit.Rule;
3033
import org.junit.Test;
3134
import org.junit.rules.TemporaryFolder;
3235

36+
import io.openliberty.tools.common.ai.util.Utils;
37+
3338
public class DevUtilRunTestThreadTest extends BaseDevUtilTest {
3439

3540
private class RunTestThreadUtil extends DevTestUtil {
@@ -110,20 +115,27 @@ public void testRunHotTestThread() throws Exception {
110115
assertEquals(1, util.counter);
111116
}
112117

113-
//@Test
118+
@Test
114119
public void testRunHotkeyReaderThread() throws Exception {
115120
RunTestThreadUtil util = new RunTestThreadUtil(false);
116121
assertEquals(0, util.counter);
117122

118123
final ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1, true));
119124

120-
InputStream previousSystemIn = System.in;
125+
LineReader previousReader = Utils.reader;
121126
try {
122-
// replace system input with a newline string
123-
String enter = "\n";
124-
System.setIn(new ByteArrayInputStream(enter.getBytes()));
127+
// replace line reader using a terminal on piped input stream
128+
PipedOutputStream out = new PipedOutputStream();
129+
PipedInputStream in = new PipedInputStream(out);
130+
Utils.reader = null;
131+
Utils.getReader(
132+
TerminalBuilder.builder()
133+
.streams(in, OutputStream.nullOutputStream())
134+
.build());
125135

126136
// run test on newline input
137+
String enter = "\n";
138+
out.write(enter.getBytes());
127139
util.runHotkeyReaderThread(executor);
128140

129141
// wait for executor to pickup test job
@@ -146,7 +158,7 @@ public void testRunHotkeyReaderThread() throws Exception {
146158
// verify that runTests() was called once
147159
assertEquals(1, util.counter);
148160
} finally {
149-
System.setIn(previousSystemIn);
161+
Utils.reader = previousReader;
150162
}
151163

152164
}

0 commit comments

Comments
 (0)