-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Jdk19 regexp fix #10972
base: master
Are you sure you want to change the base?
Jdk19 regexp fix #10972
Changes from all commits
d14ba19
de77114
bf95c0b
1fdbfd8
c2e3242
7c78310
be43eed
129852d
bd299bd
47c9805
3d94338
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,15 +23,7 @@ | |
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStreamWriter; | ||
import java.io.PrintStream; | ||
import java.io.PrintWriter; | ||
import java.io.*; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
|
@@ -42,7 +34,7 @@ | |
* | ||
* @author Marcin Miłkowski | ||
*/ | ||
public class MainTest extends AbstractSecurityTestCase { | ||
public class MainTest { | ||
|
||
private final File enTestFile; | ||
private final File xxRuleFile; | ||
|
@@ -113,8 +105,7 @@ public MainTest() throws IOException { | |
} | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
public void setUp() { | ||
this.stdout = System.out; | ||
this.stderr = System.err; | ||
this.out = new ByteArrayOutputStream(); | ||
|
@@ -124,38 +115,33 @@ public void setUp() throws Exception { | |
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
public void tearDown() { | ||
System.setOut(this.stdout); | ||
System.setErr(this.stderr); | ||
super.tearDown(); | ||
} | ||
|
||
@Test | ||
public void testUsageMessage() throws Exception { | ||
try { | ||
String[] args = {"-h"}; | ||
Main.main(args); | ||
fail("LT should have exited with status 0!"); | ||
} catch (ExitException e) { | ||
String output = new String(this.out.toByteArray()); | ||
assertTrue(output.contains("Usage: java -jar languagetool-commandline.jar")); | ||
assertEquals("Exit status", 1, e.status); | ||
} | ||
Process process = new ProcessBuilder( | ||
"java", "-cp", System.getProperty("java.class.path"), "org.languagetool.commandline.Main", "-h" | ||
).start(); | ||
int exitCode = process.waitFor(); | ||
String output = readProcessOutput(process); | ||
assertTrue(output.contains("Usage: java -jar languagetool-commandline.jar")); | ||
assertEquals("Exit status", 1, exitCode); | ||
} | ||
|
||
@Test | ||
public void testPrintLanguages() throws Exception { | ||
try { | ||
String[] args = {"--list"}; | ||
Main.main(args); | ||
fail("LT should have exited with status 0!"); | ||
} catch (ExitException e) { | ||
String output = new String(this.out.toByteArray()); | ||
assertTrue(output.contains("German")); | ||
assertTrue(output.contains("de-DE")); | ||
assertTrue(output.contains("English")); | ||
assertEquals("Exit status", 0, e.status); | ||
} | ||
Process process = new ProcessBuilder( | ||
"java", "-cp", System.getProperty("java.class.path"), "org.languagetool.commandline.Main", "--list" | ||
).start(); | ||
int exitCode = process.waitFor(); | ||
String output = readProcessOutput(process); | ||
assertTrue(output.contains("German")); | ||
assertTrue(output.contains("de-DE")); | ||
assertTrue(output.contains("English")); | ||
assertEquals("Exit status", 0, exitCode); | ||
} | ||
|
||
@Test | ||
|
@@ -670,4 +656,14 @@ private String getExternalFalseFriends() { | |
return xxFalseFriendFile.getAbsolutePath(); | ||
} | ||
|
||
private String readProcessOutput(Process process) throws IOException { | ||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) { | ||
StringBuilder output = new StringBuilder(); | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
output.append(line).append(System.lineSeparator()); | ||
} | ||
return output.toString(); | ||
} | ||
} | ||
Comment on lines
+659
to
+668
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Prevent potential deadlocks by consuming both output streams of the subprocess. The Refactor the method to consume both streams: private String readProcessOutput(Process process) throws IOException {
StringBuilder output = new StringBuilder();
try (
BufferedReader stdOutReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader stdErrReader = new BufferedReader(new InputStreamReader(process.getErrorStream()))
) {
String line;
while ((line = stdOutReader.readLine()) != null) {
output.append(line).append(System.lineSeparator());
}
while ((line = stdErrReader.readLine()) != null) {
// Optionally append to output or handle separately
output.append(line).append(System.lineSeparator());
}
}
return output.toString();
} |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Capture and assert the error stream when running subprocesses.
In the test method, only the standard output from the subprocess is captured. To ensure comprehensive testing, consider capturing the error stream (
process.getErrorStream()
) as well. This allows you to assert that no unexpected errors are occurring during execution and helps in diagnosing issues that may not appear in standard output.Modify the test to read and assert the error output:
And add a method to read the error stream: