Skip to content

Commit b348487

Browse files
committed
Java: Added basic test cases for java/jvm-exit
1 parent dfe4401 commit b348487

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
| ExampleRuntimeExit.java:22:17:22:44 | exit(...) | Avoid calls to Runtime.exit() as this makes code harder to reuse. |
2+
| ExampleRuntimeExit.java:25:17:25:44 | exit(...) | Avoid calls to Runtime.exit() as this makes code harder to reuse. |
3+
| ExampleRuntimeExit.java:35:9:35:43 | exit(...) | Avoid calls to Runtime.exit() as this makes code harder to reuse. |
4+
| ExampleRuntimeHalt.java:18:17:18:44 | halt(...) | Avoid calls to Runtime.halt() as this makes code harder to reuse. |
5+
| ExampleRuntimeHalt.java:21:17:21:44 | halt(...) | Avoid calls to Runtime.halt() as this makes code harder to reuse. |
6+
| ExampleSystemExit.java:22:17:22:30 | exit(...) | Avoid calls to System.exit() as this makes code harder to reuse. |
7+
| ExampleSystemExit.java:25:17:25:30 | exit(...) | Avoid calls to System.exit() as this makes code harder to reuse. |
8+
| ExampleSystemExit.java:35:9:35:29 | exit(...) | Avoid calls to System.exit() as this makes code harder to reuse. |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Violations of Best Practice/Undesirable Calls/CallsToSystemExit.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.io.FileOutputStream;
2+
import java.io.IOException;
3+
4+
public class ExampleRuntimeExit {
5+
6+
public static void main(String[] args) {
7+
Action action = new Action();
8+
try {
9+
action.run();
10+
} catch (Exception e) {
11+
printUsageAndExit(e.getMessage(), 1);
12+
}
13+
Runtime.getRuntime().exit(0); // COMPLIANT
14+
}
15+
16+
static class Action {
17+
public void run() {
18+
try {
19+
FileOutputStream fos = new FileOutputStream("output.txt");
20+
fos.write("Hello, World!".getBytes());
21+
fos.close();
22+
Runtime.getRuntime().exit(0); // $ Alert
23+
} catch (IOException e) {
24+
e.printStackTrace();
25+
Runtime.getRuntime().exit(1); // $ Alert
26+
} catch (Exception e) {
27+
// re-throw the exception
28+
throw e;
29+
}
30+
}
31+
}
32+
33+
protected static void printUsageAndExit(final String message, final int exitCode) {
34+
System.err.println("Usage: <example_cmd> <example_args> : " + message);
35+
Runtime.getRuntime().exit(exitCode); // $ SPURIOUS: Alert
36+
}
37+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.io.FileOutputStream;
2+
import java.io.IOException;
3+
4+
public class ExampleRuntimeHalt {
5+
6+
public static void main(String[] args) {
7+
Action action = new Action();
8+
action.run();
9+
Runtime.getRuntime().halt(0); // COMPLIANT
10+
}
11+
12+
static class Action {
13+
public void run() {
14+
try {
15+
FileOutputStream fos = new FileOutputStream("output.txt");
16+
fos.write("Hello, World!".getBytes());
17+
fos.close();
18+
Runtime.getRuntime().halt(0); // $ Alert
19+
} catch (IOException e) {
20+
e.printStackTrace();
21+
Runtime.getRuntime().halt(1); // $ Alert
22+
}
23+
}
24+
}
25+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.io.FileOutputStream;
2+
import java.io.IOException;
3+
4+
public class ExampleSystemExit {
5+
6+
public static void main(String[] args) {
7+
Action action = new Action();
8+
try {
9+
action.run();
10+
} catch (Exception e) {
11+
printUsageAndExit(e.getMessage(), 1);
12+
}
13+
System.exit(0); // COMPLIANT
14+
}
15+
16+
static class Action {
17+
public void run() {
18+
try {
19+
FileOutputStream fos = new FileOutputStream("output.txt");
20+
fos.write("Hello, World!".getBytes());
21+
fos.close();
22+
System.exit(0); // $ Alert
23+
} catch (IOException e) {
24+
e.printStackTrace();
25+
System.exit(1); // $ Alert
26+
} catch (Exception e) {
27+
// re-throw the exception
28+
throw e;
29+
}
30+
}
31+
}
32+
33+
protected static void printUsageAndExit(final String message, final int exitCode) {
34+
System.err.println("Usage: <example_cmd> <example_args> : " + message);
35+
System.exit(exitCode); // $ SPURIOUS: Alert
36+
}
37+
}

0 commit comments

Comments
 (0)