2727
2828from cldk .analysis import AnalysisLevel
2929from cldk .analysis .java .codeanalyzer import JCodeanalyzer
30- from cldk .models .java .models import JApplication , JType , JCallable , JCompilationUnit
30+ from cldk .models .java .models import JApplication , JType , JCallable , JCompilationUnit , JMethodDetail
3131from cldk .models .java import JGraphEdges
3232
3333
34+ ANALYSIS_JSON = {}
35+
36+
37+ # This might be another global test fixture but I didn't have time to work it out
3438def get_analysis_json (base_path : str ) -> str :
3539 """Opens the analysis.json file and returns the contents as a json string"""
36- # check if the folder exists
37- if not os .path .exists (base_path ):
38- raise ValueError ("Error: Folder '%s' does not exist" , base_path )
40+ global ANALYSIS_JSON
41+
42+ if not ANALYSIS_JSON :
43+ # check if the folder exists
44+ if not os .path .exists (base_path ):
45+ raise ValueError (f"Error: Folder '{ base_path } ' does not exist" )
46+
47+ # Read the json file and return it as a json string
48+ with open (os .path .join (base_path , "analysis.json" ), "r" , encoding = "utf-8" ) as json_data :
49+ ANALYSIS_JSON = json .dumps (json .load (json_data ))
3950
40- # Read the json file and return it a a json string
41- analysis_json = {}
42- with open (os .path .join (base_path , "analysis.json" ), "r" , encoding = "utf-8" ) as json_data :
43- analysis_json = json .dumps (json .load (json_data ))
44- return analysis_json
51+ return ANALYSIS_JSON
4552
4653
4754def test_init_japplication (test_fixture , codeanalyzer_jar_path , analysis_json_fixture ):
@@ -396,18 +403,24 @@ def test_get_all_callers(test_fixture, analysis_json_fixture):
396403 target_files = None ,
397404 )
398405
399- # TODO: This currently doesn't work. Code has bad call
400- # # Call using symbol table
401- # all_callers = code_analyzer.get_all_callers("com.ibm.websphere.samples.daytrader.TradeAction", "getQuote(String)", True)
402- # assert all_callers is not None
403- # assert isinstance(all_callers, Dict)
404- # assert "caller_details" in all_callers
405-
406406 # Call without using symbol table
407- all_callers = code_analyzer .get_all_callers ("com.ibm.websphere.samples.daytrader.TradeAction " , "getQuote (String)" , False )
407+ all_callers = code_analyzer .get_all_callers ("com.ibm.websphere.samples.daytrader.util.Log " , "log (String)" , False )
408408 assert all_callers is not None
409409 assert isinstance (all_callers , Dict )
410410 assert "caller_details" in all_callers
411+ assert len (all_callers ["caller_details" ]) == 18
412+ for method in all_callers ["caller_details" ]:
413+ assert isinstance (method ["caller_method" ], JMethodDetail )
414+
415+ # TODO: This currently doesn't work. Code has bad call as seen in this error message:
416+ # TypeError: JavaSitter.get_calling_lines() missing 1 required positional argument: 'is_target_method_a_constructor'
417+ # Uncomment when code is fixed.
418+
419+ # # Call using symbol table
420+ # all_callers = code_analyzer.get_all_callers("com.ibm.websphere.samples.daytrader.util.Log", "log(String)", True)
421+ # assert all_callers is not None
422+ # assert isinstance(all_callers, Dict)
423+ # assert "caller_details" in all_callers
411424
412425
413426def test_get_all_callees (test_fixture , analysis_json_fixture ):
@@ -429,18 +442,21 @@ def test_get_all_callees(test_fixture, analysis_json_fixture):
429442 target_files = None ,
430443 )
431444
432- # TODO: This currently doesn't work. Code has bad call
433- # # Call using symbol table
434- # all_callers = code_analyzer.get_all_callees("com.ibm.websphere.samples.daytrader.TradeAction", "getQuote(String)", True)
435- # assert all_callers is not None
436- # assert isinstance(all_callers, Dict)
437- # assert "caller_details" in all_callers
438-
439445 # Call without using symbol table
440- all_callers = code_analyzer .get_all_callees ("com.ibm.websphere.samples.daytrader.TradeAction" , "getQuote(String)" , False )
441- assert all_callers is not None
442- assert isinstance (all_callers , Dict )
443- assert "callee_details" in all_callers
446+ all_callees = code_analyzer .get_all_callees ("com.ibm.websphere.samples.daytrader.util.Log" , "log(String)" , False )
447+ assert all_callees is not None
448+ assert isinstance (all_callees , Dict )
449+ assert "callee_details" in all_callees
450+ assert len (all_callees ["callee_details" ]) == 0
451+
452+ # Call using symbol table
453+ all_callees = code_analyzer .get_all_callees ("com.ibm.websphere.samples.daytrader.util.Log" , "log(String)" , True )
454+ assert all_callees is not None
455+ assert isinstance (all_callees , Dict )
456+ # TODO: Bug, the Dict is empty.
457+ # need to uncomment when code is fixed
458+ # assert "callee_details" in all_callees
459+ # assert len(all_callees["callee_details"]) == 0
444460
445461
446462def test_get_all_methods_in_application (test_fixture , analysis_json_fixture ):
@@ -509,13 +525,13 @@ def test_get_class(test_fixture, analysis_json_fixture):
509525 target_files = None ,
510526 )
511527
512- class_info = code_analyzer .get_class ("com.ibm.websphere.samples.daytrader.TradeAction " )
528+ class_info = code_analyzer .get_class ("com.ibm.websphere.samples.daytrader.impl.direct.TradeDirect " )
513529 assert class_info is not None
514530 assert isinstance (class_info , JType )
515531
516532
517533def test_get_method (test_fixture , analysis_json_fixture ):
518- """It should return all of the callees """
534+ """It should return the method """
519535 # Get a known good analysis file
520536 analysis_json = get_analysis_json (analysis_json_fixture )
521537
@@ -527,13 +543,13 @@ def test_get_method(test_fixture, analysis_json_fixture):
527543 source_code = None ,
528544 analysis_backend_path = None ,
529545 analysis_json_path = None ,
530- analysis_level = AnalysisLevel .call_graph ,
546+ analysis_level = AnalysisLevel .symbol_table ,
531547 use_graalvm_binary = False ,
532548 eager_analysis = False ,
533549 target_files = None ,
534550 )
535551
536- method = code_analyzer .get_method ("com.ibm.websphere.samples.daytrader.TradeAction " , "getQuote(String )" )
552+ method = code_analyzer .get_method ("com.ibm.websphere.samples.daytrader.impl.direct.TradeDirect " , "publishQuotePriceChange(QuoteDataBean, BigDecimal, BigDecimal, double )" )
537553 assert method is not None
538554 assert isinstance (method , JCallable )
539555
@@ -551,17 +567,16 @@ def test_get_java_file(test_fixture, analysis_json_fixture):
551567 source_code = None ,
552568 analysis_backend_path = None ,
553569 analysis_json_path = None ,
554- analysis_level = AnalysisLevel .call_graph ,
570+ analysis_level = AnalysisLevel .symbol_table ,
555571 use_graalvm_binary = False ,
556572 eager_analysis = False ,
557573 target_files = None ,
558574 )
559575
560- java_file = code_analyzer .get_java_file ("com.ibm.websphere.samples.daytrader.TradeAction " )
576+ java_file = code_analyzer .get_java_file ("com.ibm.websphere.samples.daytrader.impl.direct.TradeDirect " )
561577 assert java_file is not None
562578 assert isinstance (java_file , str )
563- relative_filename = java_file .split ("/src" )[1 ]
564- assert relative_filename == "/main/java/com/ibm/websphere/samples/daytrader/TradeAction.java"
579+ assert java_file == "/codellm-devkit/tests/resources/java/application/sample.daytrader8-1.2/src/main/java/com/ibm/websphere/samples/daytrader/impl/direct/TradeDirect.java"
565580
566581 # Test compilation unit for this file
567582 comp_unit = code_analyzer .get_java_compilation_unit (java_file )
@@ -582,13 +597,13 @@ def test_get_all_methods_in_class(test_fixture, analysis_json_fixture):
582597 source_code = None ,
583598 analysis_backend_path = None ,
584599 analysis_json_path = None ,
585- analysis_level = AnalysisLevel .call_graph ,
600+ analysis_level = AnalysisLevel .symbol_table ,
586601 use_graalvm_binary = False ,
587602 eager_analysis = False ,
588603 target_files = None ,
589604 )
590605
591- all_methods = code_analyzer .get_all_methods_in_class ("com.ibm.websphere.samples.daytrader.TradeAction " )
606+ all_methods = code_analyzer .get_all_methods_in_class ("com.ibm.websphere.samples.daytrader.impl.direct.TradeDirect " )
592607 assert all_methods is not None
593608 assert isinstance (all_methods , Dict )
594609 assert len (all_methods ) > 0
@@ -613,10 +628,10 @@ def test_get_all_constructors(test_fixture, analysis_json_fixture):
613628 target_files = None ,
614629 )
615630
616- all_constructors = code_analyzer .get_all_constructors ("com.ibm.websphere.samples.daytrader.TradeAction " )
631+ all_constructors = code_analyzer .get_all_constructors ("com.ibm.websphere.samples.daytrader.entities.AccountDataBean " )
617632 assert all_constructors is not None
618633 assert isinstance (all_constructors , Dict )
619- assert len (all_constructors ) > 0
634+ assert len (all_constructors ) == 3
620635
621636
622637def test_get_all_sub_classes (test_fixture , analysis_json_fixture ):
@@ -656,16 +671,16 @@ def test_get_all_fields(test_fixture, analysis_json_fixture):
656671 source_code = None ,
657672 analysis_backend_path = None ,
658673 analysis_json_path = None ,
659- analysis_level = AnalysisLevel .call_graph ,
674+ analysis_level = AnalysisLevel .symbol_table ,
660675 use_graalvm_binary = False ,
661676 eager_analysis = False ,
662677 target_files = None ,
663678 )
664679
665- all_fields = code_analyzer .get_all_fields ("com.ibm.websphere.samples.daytrader.TradeAction " )
680+ all_fields = code_analyzer .get_all_fields ("com.ibm.websphere.samples.daytrader.entities.AccountDataBean " )
666681 assert all_fields is not None
667682 assert isinstance (all_fields , List )
668- assert len (all_fields ) > 0
683+ assert len (all_fields ) == 12
669684
670685 # Handle get fields for class not found
671686 all_fields = code_analyzer .get_all_fields ("com.not.Found" )
0 commit comments