Skip to content

Commit e173bdc

Browse files
[BAEL-8962] resolve review
1 parent b1d5d74 commit e173bdc

File tree

4 files changed

+28
-143
lines changed

4 files changed

+28
-143
lines changed

core-java-modules/core-java-compiler/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
<artifactId>slf4j-api</artifactId>
2626
<version>${org.slf4j.version}</version>
2727
</dependency>
28+
<dependency>
29+
<groupId>com.github.stefanbirkner</groupId>
30+
<artifactId>system-lambda</artifactId>
31+
<version>1.2.1</version>
32+
</dependency>
2833
</dependencies>
2934

3035
<properties>

core-java-modules/core-java-compiler/src/main/java/com/baeldung/compilerApi/JavaCompilerApiDemo.java

-111
This file was deleted.

core-java-modules/core-java-compiler/src/main/java/com/baeldung/compilerApi/JavaCompilerUtils.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ private boolean compile(Iterable<? extends JavaFileObject> compilationUnits) {
9393

9494

9595
JavaCompiler.CompilationTask task = compiler.getTask(
96-
null, // Writer for compiler output
96+
null, // Writer for compiler output
9797
standardFileManager, // File manager
9898
diagnostics, // Diagnostic listener
99-
null, // Compiler options
99+
null, // Compiler options
100100
null, // Classes to be processed by annotation processors
101101
compilationUnits // Compilation units
102102
);
@@ -111,8 +111,9 @@ private boolean compile(Iterable<? extends JavaFileObject> compilationUnits) {
111111
return success;
112112
}
113113

114+
// Add this method to JavaCompilerUtils
114115
/**
115-
* Loads and executes the main method of a compiled class.
116+
* Loads and executes the main method of a compiled class, capturing and returning the output.
116117
*
117118
* @param className The fully qualified name of the class to run
118119
* @param args Arguments to pass to the main method

core-java-modules/core-java-compiler/src/test/java/com/baeldung/compilerApi/JavaCompilerTest.java

+19-29
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.nio.file.Path;
1010
import java.nio.file.Paths;
1111

12+
import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemErr;
13+
import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOut;
1214
import static org.junit.jupiter.api.Assertions.*;
1315

1416
public class JavaCompilerTest {
@@ -17,8 +19,6 @@ public class JavaCompilerTest {
1719
static Path tempDir;
1820

1921
private JavaCompilerUtils compilerUtil;
20-
private final ByteArrayOutputStream outputCaptor = new ByteArrayOutputStream();
21-
private PrintStream standardOut;
2222

2323
@BeforeEach
2424
void setUp() throws Exception {
@@ -28,20 +28,10 @@ void setUp() throws Exception {
2828

2929
// Initialize the compiler util with the output directory
3030
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);
4131
}
4232

4333
@Test
44-
void testCompileFromString_Success() {
34+
void given_simpleHelloWorldClass_when_compiledFromString_then_compilationSucceeds() {
4535
// Simple "Hello World" class
4636
String className = "HelloWorld";
4737
String sourceCode = "public class HelloWorld {\n" +
@@ -60,7 +50,7 @@ void testCompileFromString_Success() {
6050
}
6151

6252
@Test
63-
void testCompileFromString_WithPackage() {
53+
void given_classWithPackage_when_compiledFromString_then_compilationSucceedsInPackageDirectory() {
6454
// Class with a package
6555
String className = "com.example.PackagedClass";
6656
String sourceCode = "package com.example;\n\n" +
@@ -81,7 +71,7 @@ void testCompileFromString_WithPackage() {
8171
}
8272

8373
@Test
84-
void testCompileFromString_CompilationError() {
74+
void given_classWithSyntaxError_when_compiledFromString_then_compilationFails() {
8575
// Class with syntax error (missing semicolon)
8676
String className = "ErrorClass";
8777
String sourceCode = "public class ErrorClass {\n" +
@@ -90,15 +80,17 @@ void testCompileFromString_CompilationError() {
9080
" }\n" +
9181
"}";
9282

83+
// Just verify compilation fails and no class file is created
9384
boolean result = compilerUtil.compileFromString(className, sourceCode);
85+
assertFalse(result, "Compilation should fail due to syntax error");
9486

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");
9890
}
9991

10092
@Test
101-
void testCompileFile_Success() throws Exception {
93+
void given_javaSourceFile_when_compiled_then_compilationSucceeds() throws Exception {
10294
// Create a temporary Java file
10395
String className = "FileTest";
10496
String sourceCode = "public class FileTest {\n" +
@@ -120,7 +112,7 @@ void testCompileFile_Success() throws Exception {
120112
}
121113

122114
@Test
123-
void testRunClass() throws Exception {
115+
void given_compiledClass_when_runWithArguments_then_outputsExpectedResult() throws Exception {
124116
// Compile a simple class
125117
String className = "Runner";
126118
String sourceCode = "public class Runner {\n" +
@@ -132,23 +124,21 @@ void testRunClass() throws Exception {
132124
boolean result = compilerUtil.compileFromString(className, sourceCode);
133125
assertTrue(result, "Compilation should succeed");
134126

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+
});
140131

141132
// Check the output
142-
assertEquals("Running: arg1, arg2", outputCaptor.toString().trim());
133+
assertEquals("Running: arg1, arg2", output.trim());
143134
}
144135

145136
@Test
146-
void testCompileFile_FileNotExists() {
137+
void when_compilingNonExistentFile_then_throwsIllegalArgumentException() {
147138
Path nonExistentFile = tempDir.resolve("NonExistent.java");
148139

149140
assertThrows(IllegalArgumentException.class, () -> {
150141
compilerUtil.compileFile(nonExistentFile);
151142
});
152143
}
153-
154-
}
144+
}

0 commit comments

Comments
 (0)