Repository navigation
Expand file tree
/
Copy pathtest_java_comprehensive.py
More file actions
938 lines (757 loc) · 25.8 KB
/
Copy pathtest_java_comprehensive.py
File metadata and controls
938 lines (757 loc) · 25.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
"""
Comprehensive Java language parsing and relationship testing.
Tests all Java constructs including classes, interfaces, enums, annotations,
methods, constructors, fields, imports, packages, generics, inheritance, and modern features.
"""
from pathlib import Path
from unittest.mock import MagicMock
import pytest
from codebase_rag.graph_updater import GraphUpdater
from codebase_rag.parser_loader import load_parsers
@pytest.fixture
def java_project(temp_repo: Path) -> Path:
"""Create a comprehensive Java project structure."""
project_path = temp_repo / "java_test"
project_path.mkdir()
# Create standard Java project structure
(project_path / "src").mkdir()
(project_path / "src" / "main").mkdir()
(project_path / "src" / "main" / "java").mkdir()
(project_path / "src" / "main" / "java" / "com").mkdir()
(project_path / "src" / "main" / "java" / "com" / "example").mkdir()
(project_path / "src" / "test").mkdir()
(project_path / "src" / "test" / "java").mkdir()
return project_path
def test_basic_java_classes(
java_project: Path,
mock_ingestor: MagicMock,
) -> None:
"""Test basic Java class parsing including inheritance and interfaces."""
test_file = (
java_project / "src" / "main" / "java" / "com" / "example" / "BasicClasses.java"
)
test_file.write_text(
"""
package com.example;
import java.util.List;
import java.util.ArrayList;
// Basic class declaration
public class BasicClass {
private String name;
protected int value;
public BasicClass(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
// Class with inheritance
public class ExtendedClass extends BasicClass {
private boolean flag;
public ExtendedClass(String name, boolean flag) {
super(name);
this.flag = flag;
}
@Override
public String getName() {
return super.getName() + (flag ? " (enabled)" : " (disabled)");
}
}
// Interface declaration
public interface Drawable {
void draw();
default void clear() {
System.out.println("Clearing...");
}
}
// Class implementing interface
public class Circle implements Drawable {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public void draw() {
System.out.println("Drawing circle with radius: " + radius);
}
}
// Abstract class
public abstract class Shape {
protected String color;
public Shape(String color) {
this.color = color;
}
public abstract double area();
public String getColor() {
return color;
}
}
"""
)
parsers, queries = load_parsers()
if "java" not in parsers:
pytest.skip("Java parser not available")
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=java_project,
parsers=parsers,
queries=queries,
)
updater.run()
# Verify classes were detected
project_name = java_project.name
# Get all Class node creation calls
class_calls = [
call
for call in mock_ingestor.ensure_node_batch.call_args_list
if call[0][0] == "Class"
]
# Get all Interface node creation calls (Java interfaces are processed separately)
interface_calls = [
call
for call in mock_ingestor.ensure_node_batch.call_args_list
if call[0][0] == "Interface"
]
created_classes = {call[0][1]["qualified_name"] for call in class_calls}
created_interfaces = {call[0][1]["qualified_name"] for call in interface_calls}
# Expected class qualified names (including interfaces)
expected_classes = {
f"{project_name}.src.main.java.com.example.BasicClasses.BasicClass",
f"{project_name}.src.main.java.com.example.BasicClasses.ExtendedClass",
f"{project_name}.src.main.java.com.example.BasicClasses.Circle",
f"{project_name}.src.main.java.com.example.BasicClasses.Shape",
}
expected_interfaces = {
f"{project_name}.src.main.java.com.example.BasicClasses.Drawable",
}
# Verify all expected classes and interfaces were created
missing_classes = expected_classes - created_classes
missing_interfaces = expected_interfaces - created_interfaces
assert not missing_classes, (
f"Missing expected classes: {sorted(list(missing_classes))}"
)
assert not missing_interfaces, (
f"Missing expected interfaces: {sorted(list(missing_interfaces))}"
)
# Get all Method node creation calls
method_calls = [
call
for call in mock_ingestor.ensure_node_batch.call_args_list
if call[0][0] == "Method"
]
created_methods = {call[0][1]["qualified_name"] for call in method_calls}
# Expected method qualified names (constructors and methods with parameter signatures)
expected_methods = {
f"{project_name}.src.main.java.com.example.BasicClasses.BasicClass.BasicClass(String)",
f"{project_name}.src.main.java.com.example.BasicClasses.BasicClass.getName()",
f"{project_name}.src.main.java.com.example.BasicClasses.ExtendedClass.ExtendedClass(String,boolean)",
f"{project_name}.src.main.java.com.example.BasicClasses.ExtendedClass.getName()",
f"{project_name}.src.main.java.com.example.BasicClasses.Drawable.draw()",
f"{project_name}.src.main.java.com.example.BasicClasses.Drawable.clear()",
f"{project_name}.src.main.java.com.example.BasicClasses.Circle.Circle(double)",
f"{project_name}.src.main.java.com.example.BasicClasses.Circle.draw()",
f"{project_name}.src.main.java.com.example.BasicClasses.Shape.Shape(String)",
f"{project_name}.src.main.java.com.example.BasicClasses.Shape.area()",
f"{project_name}.src.main.java.com.example.BasicClasses.Shape.getColor()",
}
# Verify all expected methods were created
missing_methods = expected_methods - created_methods
assert not missing_methods, (
f"Missing expected methods: {sorted(list(missing_methods))}"
)
def test_java_enums_and_annotations(
java_project: Path,
mock_ingestor: MagicMock,
) -> None:
"""Test Java enum and annotation parsing."""
test_file = (
java_project
/ "src"
/ "main"
/ "java"
/ "com"
/ "example"
/ "EnumsAndAnnotations.java"
)
test_file.write_text(
"""
package com.example;
// Simple enum
public enum Color {
RED, GREEN, BLUE
}
// Enum with methods and fields
public enum Planet {
MERCURY(3.303e+23, 2.4397e6),
VENUS(4.869e+24, 6.0518e6),
EARTH(5.976e+24, 6.37814e6);
private final double mass;
private final double radius;
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
public double getMass() {
return mass;
}
public double getRadius() {
return radius;
}
}
// Custom annotation
@interface MyAnnotation {
String value() default "";
int priority() default 0;
}
// Class using annotations
@MyAnnotation(value = "test", priority = 1)
public class AnnotatedClass {
@Deprecated
public void oldMethod() {
System.out.println("This method is deprecated");
}
@Override
public String toString() {
return "AnnotatedClass";
}
}
"""
)
parsers, queries = load_parsers()
if "java" not in parsers:
pytest.skip("Java parser not available")
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=java_project,
parsers=parsers,
queries=queries,
)
updater.run()
# Get all node creation calls by type (Java has distinct node types)
all_calls = mock_ingestor.ensure_node_batch.call_args_list
class_calls = [call for call in all_calls if call[0][0] == "Class"]
enum_calls = [call for call in all_calls if call[0][0] == "Enum"]
interface_calls = [call for call in all_calls if call[0][0] == "Interface"]
created_classes = {call[0][1]["qualified_name"] for call in class_calls}
created_enums = {call[0][1]["qualified_name"] for call in enum_calls}
created_interfaces = {call[0][1]["qualified_name"] for call in interface_calls}
# Expected type qualified names
project_name = java_project.name
expected_classes = {
f"{project_name}.src.main.java.com.example.EnumsAndAnnotations.AnnotatedClass",
f"{project_name}.src.main.java.com.example.EnumsAndAnnotations.MyAnnotation", # Annotations are processed as classes
}
expected_interfaces: set[str] = set() # No interfaces in this test
expected_enums = {
f"{project_name}.src.main.java.com.example.EnumsAndAnnotations.Color",
f"{project_name}.src.main.java.com.example.EnumsAndAnnotations.Planet",
}
# Verify all expected types were created in their respective categories
missing_classes = expected_classes - created_classes
missing_enums = expected_enums - created_enums
missing_interfaces = expected_interfaces - created_interfaces
assert not missing_classes, f"Missing classes: {sorted(list(missing_classes))}"
assert not missing_enums, f"Missing enums: {sorted(list(missing_enums))}"
assert not missing_interfaces, (
f"Missing interfaces: {sorted(list(missing_interfaces))}"
)
def test_java_generics_and_collections(
java_project: Path,
mock_ingestor: MagicMock,
) -> None:
"""Test Java generics and collection handling."""
test_file = (
java_project / "src" / "main" / "java" / "com" / "example" / "Generics.java"
)
test_file.write_text(
"""
package com.example;
import java.util.*;
// Generic class
public class Container<T> {
private T value;
public Container(T value) {
this.value = value;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}
// Generic interface
public interface Comparable<T> {
int compareTo(T other);
}
// Generic method
public class Utilities {
public static <T> List<T> createList(T... items) {
List<T> list = new ArrayList<>();
Collections.addAll(list, items);
return list;
}
public static <K, V> Map<K, V> createMap() {
return new HashMap<K, V>();
}
}
// Bounded generics
public class NumberContainer<T extends Number> {
private T number;
public NumberContainer(T number) {
this.number = number;
}
public double getDoubleValue() {
return number.doubleValue();
}
}
// Wildcard generics
public class WildcardExample {
public void processNumbers(List<? extends Number> numbers) {
for (Number num : numbers) {
System.out.println(num.doubleValue());
}
}
public void addToList(List<? super Integer> list) {
list.add(42);
}
}
"""
)
parsers, queries = load_parsers()
if "java" not in parsers:
pytest.skip("Java parser not available")
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=java_project,
parsers=parsers,
queries=queries,
)
updater.run()
# Get all node creation calls by type
all_calls = mock_ingestor.ensure_node_batch.call_args_list
class_calls = [call for call in all_calls if call[0][0] == "Class"]
interface_calls = [call for call in all_calls if call[0][0] == "Interface"]
created_classes = {call[0][1]["qualified_name"] for call in class_calls}
created_interfaces = {call[0][1]["qualified_name"] for call in interface_calls}
# Expected type qualified names
project_name = java_project.name
expected_classes = {
f"{project_name}.src.main.java.com.example.Generics.Container",
f"{project_name}.src.main.java.com.example.Generics.Utilities",
f"{project_name}.src.main.java.com.example.Generics.NumberContainer",
f"{project_name}.src.main.java.com.example.Generics.WildcardExample",
}
expected_interfaces = {
f"{project_name}.src.main.java.com.example.Generics.Comparable",
}
# Verify all expected types were created
missing_classes = expected_classes - created_classes
missing_interfaces = expected_interfaces - created_interfaces
assert not missing_classes, (
f"Missing expected generic classes: {sorted(list(missing_classes))}"
)
assert not missing_interfaces, (
f"Missing expected generic interfaces: {sorted(list(missing_interfaces))}"
)
def test_java_static_and_final(
java_project: Path,
mock_ingestor: MagicMock,
) -> None:
"""Test Java static and final modifier parsing."""
test_file = (
java_project / "src" / "main" / "java" / "com" / "example" / "Modifiers.java"
)
test_file.write_text(
"""
package com.example;
public final class Constants {
public static final String APP_NAME = "MyApp";
public static final int VERSION = 1;
private static final double PI = 3.14159;
private Constants() {
// Prevent instantiation
}
public static String getAppInfo() {
return APP_NAME + " v" + VERSION;
}
}
public class Singleton {
private static volatile Singleton instance;
private final String id;
private Singleton() {
this.id = "singleton-" + System.currentTimeMillis();
}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
public String getId() {
return id;
}
}
public abstract class AbstractService {
protected static final String SERVICE_NAME = "Service";
public abstract void start();
public abstract void stop();
public final void restart() {
stop();
start();
}
protected static void log(String message) {
System.out.println("[" + SERVICE_NAME + "] " + message);
}
}
"""
)
parsers, queries = load_parsers()
if "java" not in parsers:
pytest.skip("Java parser not available")
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=java_project,
parsers=parsers,
queries=queries,
)
updater.run()
# Get all Class node creation calls
class_calls = [
call
for call in mock_ingestor.ensure_node_batch.call_args_list
if call[0][0] == "Class"
]
created_classes = {call[0][1]["qualified_name"] for call in class_calls}
# Expected class qualified names
project_name = java_project.name
expected_classes = {
f"{project_name}.src.main.java.com.example.Modifiers.Constants",
f"{project_name}.src.main.java.com.example.Modifiers.Singleton",
f"{project_name}.src.main.java.com.example.Modifiers.AbstractService",
}
# Verify all expected classes were created
missing_classes = expected_classes - created_classes
assert not missing_classes, (
f"Missing expected classes: {sorted(list(missing_classes))}"
)
def test_java_inner_classes(
java_project: Path,
mock_ingestor: MagicMock,
) -> None:
"""Test Java inner and nested class parsing."""
test_file = (
java_project / "src" / "main" / "java" / "com" / "example" / "InnerClasses.java"
)
test_file.write_text(
"""
package com.example;
public class OuterClass {
private String outerField = "Outer";
private static String staticField = "Static";
// Non-static inner class
public class InnerClass {
private String innerField = "Inner";
public void accessOuter() {
System.out.println(outerField); // Can access outer instance field
System.out.println(staticField); // Can access static field
}
public String getInnerField() {
return innerField;
}
}
// Static nested class
public static class StaticNestedClass {
private String nestedField = "Nested";
public void accessOuter() {
System.out.println(staticField); // Can only access static fields
}
public String getNestedField() {
return nestedField;
}
}
// Method with local class
public void methodWithLocalClass() {
final String localVar = "Local";
// Local class
class LocalClass {
public void useLocalVar() {
System.out.println(localVar); // Can access final local variables
System.out.println(outerField); // Can access outer instance field
}
}
LocalClass local = new LocalClass();
local.useLocalVar();
}
// Method with anonymous class
public Runnable createRunnable() {
return new Runnable() {
@Override
public void run() {
System.out.println("Anonymous class: " + outerField);
}
};
}
public String getOuterField() {
return outerField;
}
}
"""
)
parsers, queries = load_parsers()
if "java" not in parsers:
pytest.skip("Java parser not available")
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=java_project,
parsers=parsers,
queries=queries,
)
updater.run()
# Get all Class node creation calls
class_calls = [
call
for call in mock_ingestor.ensure_node_batch.call_args_list
if call[0][0] == "Class"
]
created_classes = {call[0][1]["qualified_name"] for call in class_calls}
# Expected class qualified names
project_name = java_project.name
# Verify all expected classes were created (some inner classes may not be detected)
outer_class_found = any(
f"{project_name}.src.main.java.com.example.InnerClasses.OuterClass" in qn
for qn in created_classes
)
assert outer_class_found, "OuterClass not found"
def test_java_lambda_expressions(
java_project: Path,
mock_ingestor: MagicMock,
) -> None:
"""Test Java lambda expression and functional interface parsing."""
test_file = (
java_project / "src" / "main" / "java" / "com" / "example" / "Lambdas.java"
)
test_file.write_text(
"""
package com.example;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
@FunctionalInterface
interface Calculator {
int calculate(int a, int b);
}
@FunctionalInterface
interface StringProcessor {
String process(String input);
}
public class LambdaExamples {
public void basicLambdas() {
// Simple lambda expressions
Calculator add = (a, b) -> a + b;
Calculator multiply = (x, y) -> x * y;
// Lambda with body
Calculator complexOp = (a, b) -> {
int result = a * 2 + b * 3;
return result;
};
System.out.println(add.calculate(5, 3));
System.out.println(multiply.calculate(4, 7));
System.out.println(complexOp.calculate(2, 4));
}
public void streamOperations() {
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
// Using lambdas with streams
List<String> uppercaseNames = names.stream()
.map(name -> name.toUpperCase())
.filter(name -> name.length() > 3)
.collect(Collectors.toList());
// Method reference
List<String> sortedNames = names.stream()
.sorted(String::compareTo)
.collect(Collectors.toList());
// Complex stream operations
Map<Integer, List<String>> namesByLength = names.stream()
.collect(Collectors.groupingBy(String::length));
}
public void functionalInterfaces() {
// Predicate
Predicate<String> isEmpty = String::isEmpty;
Predicate<String> isLong = s -> s.length() > 10;
// Function
Function<String, Integer> stringLength = String::length;
Function<Integer, String> intToString = Object::toString;
// Consumer
Consumer<String> printer = System.out::println;
// Supplier
Supplier<String> messageSupplier = () -> "Hello World";
// BiFunction
BiFunction<String, String, String> concat = (a, b) -> a + " " + b;
}
public void customFunctionalInterface() {
StringProcessor upperCase = String::toUpperCase;
StringProcessor addPrefix = str -> "PREFIX: " + str;
StringProcessor reverse = str -> new StringBuilder(str).reverse().toString();
String input = "hello world";
System.out.println(upperCase.process(input));
System.out.println(addPrefix.process(input));
System.out.println(reverse.process(input));
}
}
"""
)
parsers, queries = load_parsers()
if "java" not in parsers:
pytest.skip("Java parser not available")
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=java_project,
parsers=parsers,
queries=queries,
)
updater.run()
# Get all node creation calls by type
all_calls = mock_ingestor.ensure_node_batch.call_args_list
class_calls = [call for call in all_calls if call[0][0] == "Class"]
interface_calls = [call for call in all_calls if call[0][0] == "Interface"]
created_classes = {call[0][1]["qualified_name"] for call in class_calls}
created_interfaces = {call[0][1]["qualified_name"] for call in interface_calls}
# Expected type qualified names
project_name = java_project.name
expected_classes = {
f"{project_name}.src.main.java.com.example.Lambdas.LambdaExamples",
}
expected_interfaces = {
f"{project_name}.src.main.java.com.example.Lambdas.Calculator",
f"{project_name}.src.main.java.com.example.Lambdas.StringProcessor",
}
# Verify all expected types were created
missing_classes = expected_classes - created_classes
missing_interfaces = expected_interfaces - created_interfaces
assert not missing_classes, (
f"Missing expected classes: {sorted(list(missing_classes))}"
)
assert not missing_interfaces, (
f"Missing expected interfaces: {sorted(list(missing_interfaces))}"
)
def test_java_exception_handling(
java_project: Path,
mock_ingestor: MagicMock,
) -> None:
"""Test Java exception handling and custom exception parsing."""
test_file = (
java_project / "src" / "main" / "java" / "com" / "example" / "Exceptions.java"
)
test_file.write_text(
"""
package com.example;
import java.io.*;
// Custom checked exception
public class CustomException extends Exception {
private final int errorCode;
public CustomException(String message, int errorCode) {
super(message);
this.errorCode = errorCode;
}
public int getErrorCode() {
return errorCode;
}
}
// Custom unchecked exception
public class CustomRuntimeException extends RuntimeException {
public CustomRuntimeException(String message) {
super(message);
}
public CustomRuntimeException(String message, Throwable cause) {
super(message, cause);
}
}
public class ExceptionHandler {
public void basicTryCatch() {
try {
int result = 10 / 0;
System.out.println(result);
} catch (ArithmeticException e) {
System.err.println("Division by zero: " + e.getMessage());
} finally {
System.out.println("Cleanup");
}
}
public void multipleCatchBlocks() {
try {
String str = null;
int length = str.length();
} catch (NullPointerException e) {
System.err.println("Null pointer: " + e.getMessage());
} catch (Exception e) {
System.err.println("Other exception: " + e.getMessage());
}
}
public void tryWithResources() {
try (BufferedReader reader = new BufferedReader(new FileReader("test.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
writer.write(line.toUpperCase());
writer.newLine();
}
} catch (IOException e) {
System.err.println("IO error: " + e.getMessage());
}
}
public void throwCustomException() throws CustomException {
if (Math.random() < 0.5) {
throw new CustomException("Random error occurred", 500);
}
}
public void handleCustomException() {
try {
throwCustomException();
} catch (CustomException e) {
System.err.println("Custom error: " + e.getMessage() + " (code: " + e.getErrorCode() + ")");
}
}
public void rethrowException() throws IOException {
try {
new FileInputStream("nonexistent.txt");
} catch (FileNotFoundException e) {
throw new IOException("File processing failed", e);
}
}
}
"""
)
parsers, queries = load_parsers()
if "java" not in parsers:
pytest.skip("Java parser not available")
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=java_project,
parsers=parsers,
queries=queries,
)
updater.run()
# Get all Class node creation calls
class_calls = [
call
for call in mock_ingestor.ensure_node_batch.call_args_list
if call[0][0] == "Class"
]
created_classes = {call[0][1]["qualified_name"] for call in class_calls}
# Expected class qualified names
project_name = java_project.name
expected_classes = {
f"{project_name}.src.main.java.com.example.Exceptions.CustomException",
f"{project_name}.src.main.java.com.example.Exceptions.CustomRuntimeException",
f"{project_name}.src.main.java.com.example.Exceptions.ExceptionHandler",
}
# Verify all expected classes were created
missing_classes = expected_classes - created_classes
assert not missing_classes, (
f"Missing expected exception classes: {sorted(list(missing_classes))}"
)