9
9
import java .nio .file .Path ;
10
10
import java .nio .file .Paths ;
11
11
12
+ import static com .github .stefanbirkner .systemlambda .SystemLambda .tapSystemErr ;
13
+ import static com .github .stefanbirkner .systemlambda .SystemLambda .tapSystemOut ;
12
14
import static org .junit .jupiter .api .Assertions .*;
13
15
14
16
public class JavaCompilerTest {
@@ -17,8 +19,6 @@ public class JavaCompilerTest {
17
19
static Path tempDir ;
18
20
19
21
private JavaCompilerUtils compilerUtil ;
20
- private final ByteArrayOutputStream outputCaptor = new ByteArrayOutputStream ();
21
- private PrintStream standardOut ;
22
22
23
23
@ BeforeEach
24
24
void setUp () throws Exception {
@@ -28,20 +28,10 @@ void setUp() throws Exception {
28
28
29
29
// Initialize the compiler util with the output directory
30
30
compilerUtil = new JavaCompilerUtils (outputDir );
31
-
32
- // Set up System.out capture
33
- standardOut = System .out ;
34
- System .setOut (new PrintStream (outputCaptor ));
35
- }
36
-
37
- @ AfterEach
38
- void tearDown () {
39
- // Restore System.out
40
- System .setOut (standardOut );
41
31
}
42
32
43
33
@ Test
44
- void testCompileFromString_Success () {
34
+ void given_simpleHelloWorldClass_when_compiledFromString_then_compilationSucceeds () {
45
35
// Simple "Hello World" class
46
36
String className = "HelloWorld" ;
47
37
String sourceCode = "public class HelloWorld {\n " +
@@ -60,7 +50,7 @@ void testCompileFromString_Success() {
60
50
}
61
51
62
52
@ Test
63
- void testCompileFromString_WithPackage () {
53
+ void given_classWithPackage_when_compiledFromString_then_compilationSucceedsInPackageDirectory () {
64
54
// Class with a package
65
55
String className = "com.example.PackagedClass" ;
66
56
String sourceCode = "package com.example;\n \n " +
@@ -81,7 +71,7 @@ void testCompileFromString_WithPackage() {
81
71
}
82
72
83
73
@ Test
84
- void testCompileFromString_CompilationError () {
74
+ void given_classWithSyntaxError_when_compiledFromString_then_compilationFails () {
85
75
// Class with syntax error (missing semicolon)
86
76
String className = "ErrorClass" ;
87
77
String sourceCode = "public class ErrorClass {\n " +
@@ -90,15 +80,17 @@ void testCompileFromString_CompilationError() {
90
80
" }\n " +
91
81
"}" ;
92
82
83
+ // Just verify compilation fails and no class file is created
93
84
boolean result = compilerUtil .compileFromString (className , sourceCode );
85
+ assertFalse (result , "Compilation should fail due to syntax error" );
94
86
95
- assertFalse ( result , "Compilation should fail" );
96
- assertTrue ( outputCaptor . toString ().contains ( "';' expected" ),
97
- "Diagnostic should mention missing semicolon " );
87
+ // Check that no class file was created
88
+ Path classFile = compilerUtil . getOutputDirectory ().resolve ( className + ".class" );
89
+ assertFalse ( Files . exists ( classFile ), "No class file should be created for failed compilation " );
98
90
}
99
91
100
92
@ Test
101
- void testCompileFile_Success () throws Exception {
93
+ void given_javaSourceFile_when_compiled_then_compilationSucceeds () throws Exception {
102
94
// Create a temporary Java file
103
95
String className = "FileTest" ;
104
96
String sourceCode = "public class FileTest {\n " +
@@ -120,7 +112,7 @@ void testCompileFile_Success() throws Exception {
120
112
}
121
113
122
114
@ Test
123
- void testRunClass () throws Exception {
115
+ void given_compiledClass_when_runWithArguments_then_outputsExpectedResult () throws Exception {
124
116
// Compile a simple class
125
117
String className = "Runner" ;
126
118
String sourceCode = "public class Runner {\n " +
@@ -132,23 +124,21 @@ void testRunClass() throws Exception {
132
124
boolean result = compilerUtil .compileFromString (className , sourceCode );
133
125
assertTrue (result , "Compilation should succeed" );
134
126
135
- // Clear the output capture
136
- outputCaptor .reset ();
137
-
138
- // Run the compiled class
139
- compilerUtil .runClass (className , "arg1" , "arg2" );
127
+ // Use system-lambda to capture the output
128
+ String output = tapSystemOut (() -> {
129
+ compilerUtil .runClass (className , "arg1" , "arg2" );
130
+ });
140
131
141
132
// Check the output
142
- assertEquals ("Running: arg1, arg2" , outputCaptor . toString () .trim ());
133
+ assertEquals ("Running: arg1, arg2" , output .trim ());
143
134
}
144
135
145
136
@ Test
146
- void testCompileFile_FileNotExists () {
137
+ void when_compilingNonExistentFile_then_throwsIllegalArgumentException () {
147
138
Path nonExistentFile = tempDir .resolve ("NonExistent.java" );
148
139
149
140
assertThrows (IllegalArgumentException .class , () -> {
150
141
compilerUtil .compileFile (nonExistentFile );
151
142
});
152
143
}
153
-
154
- }
144
+ }
0 commit comments