1+ /*
2+ * Copyright (c) 2025, Oracle and/or its affiliates.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * https://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+ package org .netbeans .modules .nbcode .java .notebook ;
17+
18+ import org .junit .Test ;
19+ import org .junit .Before ;
20+ import org .junit .After ;
21+ import static org .junit .Assert .*;
22+
23+ import jdk .jshell .JShell ;
24+ import jdk .jshell .Snippet ;
25+ import jdk .jshell .SnippetEvent ;
26+
27+ import java .util .List ;
28+
29+ public class CodeEvalTest {
30+
31+ private JShell jshell ;
32+ private CodeEval instance ;
33+
34+ @ Before
35+ public void setUp () {
36+ jshell = JShell .create ();
37+ instance = CodeEval .getInstance ();
38+ }
39+
40+ @ After
41+ public void tearDown () {
42+ if (jshell != null ) {
43+ jshell .close ();
44+ }
45+ }
46+
47+ @ Test
48+ public void testGetCompilationErrors_WithSyntaxError () {
49+ List <SnippetEvent > events = jshell .eval ("int x = ;" );
50+ SnippetEvent event = events .get (0 );
51+
52+ List <String > errors = instance .getCompilationErrors (jshell , event );
53+
54+ assertFalse ("Should have compilation errors" , errors .isEmpty ());
55+ }
56+
57+ @ Test
58+ public void testGetCompilationErrors_WithValidCode () {
59+ List <SnippetEvent > events = jshell .eval ("int x = 5;" );
60+ SnippetEvent event = events .get (0 );
61+
62+ List <String > errors = instance .getCompilationErrors (jshell , event );
63+
64+ assertTrue ("Should not have compilation errors for valid code" , errors .isEmpty ());
65+ }
66+
67+ @ Test
68+ public void testGetCompilationErrors_WithUnresolvedDependency () {
69+ List <SnippetEvent > events = jshell .eval ("void testMethod() { System.out.println(undefinedVar); }" );
70+ SnippetEvent event = events .get (0 );
71+
72+ List <String > errors = instance .getCompilationErrors (jshell , event );
73+
74+ assertFalse ("Should have unresolved dependency error" , errors .isEmpty ());
75+ boolean hasCannotBeMessage = false ;
76+ for (String error : errors ) {
77+ if (error .contains ("cannot be" )) {
78+ hasCannotBeMessage = true ;
79+ break ;
80+ }
81+ }
82+ assertTrue ("Error message should mention that it cannot be invoked/used" , hasCannotBeMessage );
83+ }
84+
85+ @ Test
86+ public void testGetCompilationErrors_MethodWithUnresolvedDependency () {
87+ List <SnippetEvent > events = jshell .eval ("int calculate() { return undefinedMethod(); }" );
88+ SnippetEvent event = events .get (0 );
89+
90+ List <String > errors = instance .getCompilationErrors (jshell , event );
91+
92+ assertFalse ("Should have errors for undefined method" , errors .isEmpty ());
93+ boolean hasMethodReference = false ;
94+ for (String error : errors ) {
95+ if (error .contains ("method" ) || error .contains ("calculate" )) {
96+ hasMethodReference = true ;
97+ break ;
98+ }
99+ }
100+ assertTrue ("Error should reference the method" , hasMethodReference );
101+ }
102+
103+ @ Test
104+ public void testGetCompilationErrors_ClassWithUnresolvedDependency () {
105+ List <SnippetEvent > events = jshell .eval ("class MyClass { UndefinedType field; }" );
106+ SnippetEvent event = events .get (0 );
107+
108+ List <String > errors = instance .getCompilationErrors (jshell , event );
109+
110+ assertFalse ("Should have errors for undefined type" , errors .isEmpty ());
111+ }
112+
113+ @ Test
114+ public void testGetCompilationErrors_VariableDeclaration () {
115+ List <SnippetEvent > events = jshell .eval ("String name = \" test\" ;" );
116+ SnippetEvent event = events .get (0 );
117+
118+ List <String > errors = instance .getCompilationErrors (jshell , event );
119+
120+ assertTrue ("Valid variable declaration should have no errors" , errors .isEmpty ());
121+ }
122+
123+ @ Test
124+ public void testGetCompilationErrors_MultipleErrors () {
125+ List <SnippetEvent > events = jshell .eval ("int x = ; int y = ;" );
126+
127+ for (SnippetEvent event : events ) {
128+ List <String > errors = instance .getCompilationErrors (jshell , event );
129+ if (event .snippet ().kind () == Snippet .Kind .VAR ) {
130+ assertFalse ("Should have compilation errors" , errors .isEmpty ());
131+ }
132+ }
133+ }
134+
135+ @ Test
136+ public void testGetCompilationErrors_InterfaceDeclaration () {
137+ List <SnippetEvent > events = jshell .eval ("interface MyInterface { void method(); }" );
138+ SnippetEvent event = events .get (0 );
139+
140+ List <String > errors = instance .getCompilationErrors (jshell , event );
141+
142+ assertTrue ("Valid interface should have no errors" , errors .isEmpty ());
143+ }
144+
145+ @ Test
146+ public void testGetCompilationErrors_EnumDeclaration () {
147+ List <SnippetEvent > events = jshell .eval ("enum Color { RED, GREEN, BLUE }" );
148+ SnippetEvent event = events .get (0 );
149+
150+ List <String > errors = instance .getCompilationErrors (jshell , event );
151+
152+ assertTrue ("Valid enum should have no errors" , errors .isEmpty ());
153+ }
154+
155+ @ Test
156+ public void testGetCompilationErrors_MultipleUnresolvedDependencies () {
157+ List <SnippetEvent > events = jshell .eval (
158+ "void process() { undefinedVar1.toString(); undefinedVar2.toString(); }" );
159+ SnippetEvent event = events .get (0 );
160+
161+ List <String > errors = instance .getCompilationErrors (jshell , event );
162+
163+ assertFalse ("Should have errors for multiple dependencies" , errors .isEmpty ());
164+ }
165+
166+ @ Test
167+ public void testGetCompilationErrors_MethodDeclaration () {
168+ List <SnippetEvent > events = jshell .eval ("int add(int a, int b) { return a + b; }" );
169+ SnippetEvent event = events .get (0 );
170+
171+ List <String > errors = instance .getCompilationErrors (jshell , event );
172+
173+ assertTrue ("Valid method declaration should have no errors" , errors .isEmpty ());
174+ }
175+
176+ @ Test
177+ public void testGetCompilationErrors_ClassDeclaration () {
178+ List <SnippetEvent > events = jshell .eval ("class Person { String name; int age; }" );
179+ SnippetEvent event = events .get (0 );
180+
181+ List <String > errors = instance .getCompilationErrors (jshell , event );
182+
183+ assertTrue ("Valid class declaration should have no errors" , errors .isEmpty ());
184+ }
185+ }
0 commit comments