Skip to content

Commit

Permalink
Обновление перехвата System.exit под jdk17+
Browse files Browse the repository at this point in the history
  • Loading branch information
nixel2007 committed Dec 15, 2022
1 parent 99c37d9 commit 12a96eb
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 97 deletions.
5 changes: 4 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ dependencies {
}

// test utils
testImplementation("com.ginsberg", "junit5-system-exit", "1.1.2")
testImplementation("org.jmockit", "jmockit", "1.49")
testImplementation("org.awaitility", "awaitility", "4.2.0")
}

Expand Down Expand Up @@ -177,6 +177,9 @@ tasks.test {
reports {
html.required.set(true)
}

val jmockitPath = classpath.find { it.name.contains("jmockit") }!!.absolutePath
jvmArgs("-javaagent:${jmockitPath}")
}

tasks.check {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
*/
package com.github._1c_syntax.bsl.languageserver;

import com.ginsberg.junit.exit.ExpectSystemExitWithStatus;
import com.github._1c_syntax.bsl.languageserver.util.CleanupContextBeforeClassAndAfterEachTestMethod;
import mockit.Mock;
import mockit.MockUp;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -34,6 +35,7 @@
import java.io.PrintStream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

@SpringBootTest
@CleanupContextBeforeClassAndAfterEachTestMethod
Expand All @@ -44,6 +46,13 @@ class BSLLSPLauncherTest {

@BeforeEach
void setUpStreams() {
new MockUp<System>() {
@Mock
public void exit(int value) {
throw new RuntimeException(String.valueOf(value));
}
};

outContent = new ByteArrayOutputStream();
errContent = new ByteArrayOutputStream();
System.setOut(new PrintStream(outContent));
Expand All @@ -57,34 +66,28 @@ void restoreStreams() {
}

@Test
@ExpectSystemExitWithStatus(2)
void testParseError() {
void testParseError() throws Exception {
// given
String[] args = new String[]{"--error"};

// when
try {
BSLLSPLauncher.main(args);
} catch (RuntimeException ignored) {
// catch prevented system.exit call
}
// when-then
assertThatThrownBy(() -> BSLLSPLauncher.main(args))
.isInstanceOf(RuntimeException.class)
.hasMessage("2");

// then
assertThat(errContent.toString()).containsIgnoringCase("Unknown option: '--error'");
}

@Test
@ExpectSystemExitWithStatus(0)
void testAnalyze() {
// given
String[] args = "--analyze --srcDir ./src/test/resources/cli".split(" ");

// when
try {
BSLLSPLauncher.main(args);
} catch (RuntimeException ignored) {
// catch prevented system.exit call
}
// when-then
assertThatThrownBy(() -> BSLLSPLauncher.main(args))
.isInstanceOf(RuntimeException.class)
.hasMessage("0");

// then
// main-method should run without exceptions
Expand All @@ -94,17 +97,14 @@ void testAnalyze() {
}

@Test
@ExpectSystemExitWithStatus(0)
void testAnalyzeSilent() {
// given
String[] args = "--analyze --srcDir ./src/test/resources/cli --silent".split(" ");

// when
try {
BSLLSPLauncher.main(args);
} catch (RuntimeException ignored) {
// catch prevented system.exit call
}
// when-then
assertThatThrownBy(() -> BSLLSPLauncher.main(args))
.isInstanceOf(RuntimeException.class)
.hasMessage("0");

// then
// main-method should runs without exceptions
Expand All @@ -113,17 +113,14 @@ void testAnalyzeSilent() {
}

@Test
@ExpectSystemExitWithStatus(1)
void testAnalyzeError() {
// given
String[] args = "--analyze --srcDir fake-dir".split(" ");

// when
try {
BSLLSPLauncher.main(args);
} catch (RuntimeException ignored) {
// catch prevented system.exit call
}
// when-then
assertThatThrownBy(() -> BSLLSPLauncher.main(args))
.isInstanceOf(RuntimeException.class)
.hasMessage("1");

// then
// main-method should runs without exceptions
Expand All @@ -132,17 +129,14 @@ void testAnalyzeError() {
}

@Test
@ExpectSystemExitWithStatus(0)
void testFormat() {
// given
String[] args = "--format --src ./src/test/resources/cli".split(" ");

// when
try {
BSLLSPLauncher.main(args);
} catch (RuntimeException ignored) {
// catch prevented system.exit call
}
// when-then
assertThatThrownBy(() -> BSLLSPLauncher.main(args))
.isInstanceOf(RuntimeException.class)
.hasMessage("0");

// then
// main-method should runs without exceptions
Expand All @@ -152,17 +146,14 @@ void testFormat() {
}

@Test
@ExpectSystemExitWithStatus(0)
void testFormatOneFile() {
// given
String[] args = "--format --src ./src/test/resources/cli/test.bsl.txt".split(" ");

// when
try {
BSLLSPLauncher.main(args);
} catch (RuntimeException ignored) {
// catch prevented system.exit call
}
// when-then
assertThatThrownBy(() -> BSLLSPLauncher.main(args))
.isInstanceOf(RuntimeException.class)
.hasMessage("0");

// then
// main-method should runs without exceptions
Expand All @@ -172,17 +163,14 @@ void testFormatOneFile() {
}

@Test
@ExpectSystemExitWithStatus(0)
void testFormatTwoFiles() {
// given
String[] args = "--format --src ./src/test/resources/cli/test.bsl.txt,./src/test/resources/cli/test.bsl".split(" ");

// when
try {
BSLLSPLauncher.main(args);
} catch (RuntimeException ignored) {
// catch prevented system.exit call
}
// when-then
assertThatThrownBy(() -> BSLLSPLauncher.main(args))
.isInstanceOf(RuntimeException.class)
.hasMessage("0");

// then
// main-method should runs without exceptions
Expand All @@ -192,17 +180,14 @@ void testFormatTwoFiles() {
}

@Test
@ExpectSystemExitWithStatus(0)
void testFormatSilent() {
// given
String[] args = "--format --src ./src/test/resources/cli --silent".split(" ");

// when
try {
BSLLSPLauncher.main(args);
} catch (RuntimeException ignored) {
// catch prevented system.exit call
}
// when-then
assertThatThrownBy(() -> BSLLSPLauncher.main(args))
.isInstanceOf(RuntimeException.class)
.hasMessage("0");

// then
// main-method should runs without exceptions
Expand All @@ -211,17 +196,14 @@ void testFormatSilent() {
}

@Test
@ExpectSystemExitWithStatus(1)
void testFormatError() {
// given
String[] args = "--format --src fake-dir".split(" ");

// when
try {
BSLLSPLauncher.main(args);
} catch (RuntimeException ignored) {
// catch prevented system.exit call
}
// when-then
assertThatThrownBy(() -> BSLLSPLauncher.main(args))
.isInstanceOf(RuntimeException.class)
.hasMessage("1");

// then
// main-method should runs without exceptions
Expand All @@ -230,17 +212,14 @@ void testFormatError() {
}

@Test
@ExpectSystemExitWithStatus(0)
void testVersion() {
// given
String[] args = {"-v"};

// when
try {
BSLLSPLauncher.main(args);
} catch (RuntimeException ignored) {
// catch prevented system.exit call
}
// when-then
assertThatThrownBy(() -> BSLLSPLauncher.main(args))
.isInstanceOf(RuntimeException.class)
.hasMessage("0");

// then
// main-method should runs without exceptions
Expand Down Expand Up @@ -269,12 +248,8 @@ void testWithoutCommandWithConfig() {
// given
String[] args = "-c .".split(" ");

// when
try {
BSLLSPLauncher.main(args);
} catch (RuntimeException ignored) {
// catch prevented system.exit call
}
// when-then
BSLLSPLauncher.main(args);

// then
assertThat(outContent.toString()).contains("LanguageServerStartCommand");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@
*/
package com.github._1c_syntax.bsl.languageserver;

import com.ginsberg.junit.exit.ExpectSystemExitWithStatus;
import com.github._1c_syntax.bsl.languageserver.util.CleanupContextBeforeClassAndAfterEachTestMethod;
import com.github._1c_syntax.utils.Absolute;
import mockit.Mock;
import mockit.MockUp;
import org.eclipse.lsp4j.ClientCapabilities;
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4j.InitializeResult;
import org.eclipse.lsp4j.RenameCapabilities;
import org.eclipse.lsp4j.TextDocumentClientCapabilities;
import org.eclipse.lsp4j.WorkspaceFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -40,6 +42,7 @@

import static com.github._1c_syntax.bsl.languageserver.util.TestUtils.PATH_TO_METADATA;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

@SpringBootTest
@CleanupContextBeforeClassAndAfterEachTestMethod
Expand All @@ -48,6 +51,16 @@ class BSLLanguageServerTest {
@Autowired
private BSLLanguageServer server;

@BeforeEach
void setUp() {
new MockUp<System>() {
@Mock
public void exit(int value) {
throw new RuntimeException(String.valueOf(value));
}
};
}

@Test
void initialize() throws ExecutionException, InterruptedException {
// given
Expand Down Expand Up @@ -94,24 +107,22 @@ void shutdown() throws ExecutionException, InterruptedException {
}

@Test
@ExpectSystemExitWithStatus(1)
void exitWithoutShutdown() {
// when
server.exit();

// then ExpectSystemExitWithStatus should not throw exception
// when-then
assertThatThrownBy(() -> server.exit())
.isInstanceOf(RuntimeException.class)
.hasMessage("1");
}

@Test
@ExpectSystemExitWithStatus(0)
void exitWithShutdown() {
// given
server.shutdown();

// when
server.exit();

// then ExpectSystemExitWithStatus should not throw exception
// when-then
assertThatThrownBy(() -> server.exit())
.isInstanceOf(RuntimeException.class)
.hasMessage("0");
}

}
Loading

0 comments on commit 12a96eb

Please sign in to comment.