1
+ package com .baeldung .compilerapi ;
2
+
3
+ import org .junit .jupiter .api .*;
4
+ import org .junit .jupiter .api .io .TempDir ;
5
+
6
+ import java .nio .file .Files ;
7
+ import java .nio .file .Path ;
8
+ import java .nio .file .Paths ;
9
+
10
+ import static com .github .stefanbirkner .systemlambda .SystemLambda .tapSystemErr ;
11
+ import static com .github .stefanbirkner .systemlambda .SystemLambda .tapSystemOut ;
12
+ import static org .junit .jupiter .api .Assertions .*;
13
+
14
+ public class JavaCompilerUnitTest {
15
+
16
+ @ TempDir
17
+ static Path tempDir ;
18
+
19
+ private JavaCompilerUtils compilerUtil ;
20
+
21
+ @ BeforeEach
22
+ void setUp () throws Exception {
23
+ Path outputDir = tempDir .resolve ("classes" );
24
+ Files .createDirectories (outputDir );
25
+
26
+ compilerUtil = new JavaCompilerUtils (outputDir );
27
+ }
28
+
29
+ @ Test
30
+ void givenSimpleHelloWorldClass_whenCompiledFromString_thenCompilationSucceeds () {
31
+ String className = "HelloWorld" ;
32
+ String sourceCode = "public class HelloWorld {\n " +
33
+ " public static void main(String[] args) {\n " +
34
+ " System.out.println(\" Hello, World!\" );\n " +
35
+ " }\n " +
36
+ "}" ;
37
+
38
+ boolean result = compilerUtil .compileFromString (className , sourceCode );
39
+
40
+ assertTrue (result , "Compilation should succeed" );
41
+
42
+ Path classFile = compilerUtil .getOutputDirectory ().resolve (className + ".class" );
43
+ assertTrue (Files .exists (classFile ), "Class file should be created" );
44
+ }
45
+
46
+ @ Test
47
+ void givenClassWithPackage_whenCompiledFromString_thenCompilationSucceedsInPackageDirectory () {
48
+ String className = "com.example.PackagedClass" ;
49
+ String sourceCode = "package com.example;\n \n " +
50
+ "public class PackagedClass {\n " +
51
+ " public static void main(String[] args) {\n " +
52
+ " System.out.println(\" Hello from packaged class!\" );\n " +
53
+ " }\n " +
54
+ "}" ;
55
+
56
+ boolean result = compilerUtil .compileFromString (className , sourceCode );
57
+
58
+ assertTrue (result , "Compilation should succeed" );
59
+
60
+ Path classFile = compilerUtil .getOutputDirectory ().resolve (
61
+ Paths .get ("com" , "example" , "PackagedClass.class" ));
62
+ assertTrue (Files .exists (classFile ), "Class file should be created in the package directory" );
63
+ }
64
+
65
+ @ Test
66
+ void givenClassWithSyntaxError_whenCompiledFromString_thenCompilationFails () {
67
+ String className = "ErrorClass" ;
68
+ String sourceCode = "public class ErrorClass {\n " +
69
+ " public static void main(String[] args) {\n " +
70
+ " System.out.println(\" This has an error\" )\n " +
71
+ " }\n " +
72
+ "}" ;
73
+
74
+ boolean result = compilerUtil .compileFromString (className , sourceCode );
75
+ assertFalse (result , "Compilation should fail due to syntax error" );
76
+
77
+ Path classFile = compilerUtil .getOutputDirectory ().resolve (className + ".class" );
78
+ assertFalse (Files .exists (classFile ), "No class file should be created for failed compilation" );
79
+ }
80
+
81
+ @ Test
82
+ void givenJavaSourceFile_whenCompiled_thenCompilationSucceeds () throws Exception {
83
+ String className = "FileTest" ;
84
+ String sourceCode = "public class FileTest {\n " +
85
+ " public static void main(String[] args) {\n " +
86
+ " System.out.println(\" Hello from file!\" );\n " +
87
+ " }\n " +
88
+ "}" ;
89
+
90
+ Path sourceFile = tempDir .resolve (className + ".java" );
91
+ Files .write (sourceFile , sourceCode .getBytes ());
92
+
93
+ boolean result = compilerUtil .compileFile (sourceFile );
94
+
95
+ assertTrue (result , "Compilation should succeed" );
96
+
97
+ Path classFile = compilerUtil .getOutputDirectory ().resolve (className + ".class" );
98
+ assertTrue (Files .exists (classFile ), "Class file should be created" );
99
+ }
100
+
101
+ @ Test
102
+ void givenCompiledClass_whenRunWithArguments_thenOutputsExpectedResult () throws Exception {
103
+ String className = "Runner" ;
104
+ String sourceCode = "public class Runner {\n " +
105
+ " public static void main(String[] args) {\n " +
106
+ " System.out.println(\" Running: \" + String.join(\" , \" , args));\n " +
107
+ " }\n " +
108
+ "}" ;
109
+
110
+ boolean result = compilerUtil .compileFromString (className , sourceCode );
111
+ assertTrue (result , "Compilation should succeed" );
112
+
113
+ String output = tapSystemOut (() -> {
114
+ compilerUtil .runClass (className , "arg1" , "arg2" );
115
+ });
116
+
117
+ assertEquals ("Running: arg1, arg2" , output .trim ());
118
+ }
119
+
120
+ @ Test
121
+ void whenCompilingNonExistentFile_thenThrowsIllegalArgumentException () {
122
+ Path nonExistentFile = tempDir .resolve ("NonExistent.java" );
123
+
124
+ assertThrows (IllegalArgumentException .class , () -> {
125
+ compilerUtil .compileFile (nonExistentFile );
126
+ });
127
+ }
128
+ }
0 commit comments