@@ -9,8 +9,8 @@ class PackageDependencyExtractor {
9
9
dependencyMap : Map < string , Set < string > > = new Map ( ) ;
10
10
artifactMap : Map < string , Set < string > > = new Map ( ) ;
11
11
basePackageDependencyMap : Map < string , Set < string > > = new Map ( ) ;
12
- // Add property to track library counts
13
- libraryCounts : { [ key : string ] : number } = { } ;
12
+ // Update library counts property to use Set for uniqueness
13
+ libraryCounts : { [ key : string ] : Set < string > } = { } ; // Changed from number to Set<string>
14
14
librariesToCount : string [ ] = [ 'struts' , 'commons' , 'log4j' , 'cryptix' ] ; // Default libraries
15
15
16
16
constructor ( librariesToCount ?: string ) {
@@ -20,7 +20,7 @@ class PackageDependencyExtractor {
20
20
21
21
// Initialize counter for each library
22
22
this . librariesToCount . forEach ( lib => {
23
- this . libraryCounts [ lib ] = 0 ;
23
+ this . libraryCounts [ lib ] = new Set < string > ( ) ; // Initialize with an empty Set
24
24
} ) ;
25
25
}
26
26
@@ -199,7 +199,7 @@ class PackageDependencyExtractor {
199
199
200
200
// Display counts for each library dynamically
201
201
Object . keys ( this . libraryCounts ) . forEach ( library => {
202
- markdownContent += `| ${ library . charAt ( 0 ) . toUpperCase ( ) + library . slice ( 1 ) } | ${ this . libraryCounts [ library ] } |\n` ;
202
+ markdownContent += `| ${ library . charAt ( 0 ) . toUpperCase ( ) + library . slice ( 1 ) } | ${ this . libraryCounts [ library ] . size } |\n` ; // Use .size in tests too
203
203
} ) ;
204
204
markdownContent += '\n' ;
205
205
@@ -297,21 +297,25 @@ class PackageDependencyExtractor {
297
297
fs . writeFileSync ( outputFile , markdownContent ) ;
298
298
}
299
299
300
- // Method to count instances of specific libraries
300
+ // Method to count unique classes for specific libraries (Updated for tests)
301
301
countSpecificLibraries ( targetClass : string ) : void {
302
302
const lcTargetClass = targetClass . toLowerCase ( ) ;
303
303
304
- // Check for each library in the target class
305
304
this . librariesToCount . forEach ( library => {
306
305
if ( lcTargetClass . includes ( library ) ) {
307
- this . libraryCounts [ library ] ++ ;
306
+ // Add the original targetClass to the Set for uniqueness
307
+ this . libraryCounts [ library ] . add ( targetClass ) ;
308
308
}
309
309
} ) ;
310
310
}
311
311
312
- // Getter for library counts (useful for testing)
313
- getLibraryCounts ( ) {
314
- return this . libraryCounts ;
312
+ // Method to get the library counts (Set sizes) for testing
313
+ getLibraryCounts ( ) : { [ key : string ] : number } { // Return type changed to number for the size
314
+ const counts : { [ key : string ] : number } = { } ;
315
+ Object . keys ( this . libraryCounts ) . forEach ( lib => {
316
+ counts [ lib ] = this . libraryCounts [ lib ] . size ;
317
+ } ) ;
318
+ return counts ;
315
319
}
316
320
317
321
// Getter for libraries being counted
@@ -670,105 +674,169 @@ describe('PackageDependencyExtractor', () => {
670
674
} ) ;
671
675
672
676
// Add tests for library counts
673
- describe ( 'countSpecificLibraries' , ( ) => {
677
+ describe ( 'Specific Library Counting' , ( ) => {
678
+ let extractor : PackageDependencyExtractor ;
679
+
680
+ beforeEach ( ( ) => {
681
+ extractor = new PackageDependencyExtractor ( ) ;
682
+ } ) ;
683
+
674
684
test ( 'should count struts in targetClass' , ( ) => {
675
- const extractor = new PackageDependencyExtractor ( ) ;
676
685
extractor . countSpecificLibraries ( 'org.apache.struts.actions.Action' ) ;
677
- expect ( extractor . libraryCounts [ 'struts' ] ) . toBe ( 1 ) ;
686
+ expect ( extractor . libraryCounts [ 'struts' ] . size ) . toBe ( 1 ) ; // Check size
678
687
} ) ;
679
688
680
689
test ( 'should count commons in targetClass' , ( ) => {
681
- const extractor = new PackageDependencyExtractor ( ) ;
682
690
extractor . countSpecificLibraries ( 'org.apache.commons.lang.StringUtils' ) ;
683
- expect ( extractor . libraryCounts [ 'commons' ] ) . toBe ( 1 ) ;
691
+ expect ( extractor . libraryCounts [ 'commons' ] . size ) . toBe ( 1 ) ; // Check size
684
692
} ) ;
685
693
686
694
test ( 'should count log4j in targetClass' , ( ) => {
687
- const extractor = new PackageDependencyExtractor ( ) ;
688
695
extractor . countSpecificLibraries ( 'org.apache.log4j.Logger' ) ;
689
- expect ( extractor . libraryCounts [ 'log4j' ] ) . toBe ( 1 ) ;
696
+ expect ( extractor . libraryCounts [ 'log4j' ] . size ) . toBe ( 1 ) ; // Check size
690
697
} ) ;
691
698
692
699
test ( 'should count cryptix in targetClass' , ( ) => {
693
- const extractor = new PackageDependencyExtractor ( ) ;
694
700
extractor . countSpecificLibraries ( 'cryptix.provider.Cipher' ) ;
695
- expect ( extractor . libraryCounts [ 'cryptix' ] ) . toBe ( 1 ) ;
701
+ expect ( extractor . libraryCounts [ 'cryptix' ] . size ) . toBe ( 1 ) ; // Check size
696
702
} ) ;
697
703
698
- test ( 'should handle case insensitivity' , ( ) => {
699
- const extractor = new PackageDependencyExtractor ( ) ;
704
+ test ( 'should handle case-insensitivity' , ( ) => {
700
705
extractor . countSpecificLibraries ( 'org.apache.STRUTS.Action' ) ;
701
706
extractor . countSpecificLibraries ( 'org.apache.COMMONS.FileUtils' ) ;
702
707
extractor . countSpecificLibraries ( 'org.apache.LOG4J.Logger' ) ;
703
708
extractor . countSpecificLibraries ( 'CRYPTIX.provider.Cipher' ) ;
704
709
705
- expect ( extractor . libraryCounts [ 'struts' ] ) . toBe ( 1 ) ;
706
- expect ( extractor . libraryCounts [ 'commons' ] ) . toBe ( 1 ) ;
707
- expect ( extractor . libraryCounts [ 'log4j' ] ) . toBe ( 1 ) ;
708
- expect ( extractor . libraryCounts [ 'cryptix' ] ) . toBe ( 1 ) ;
710
+ expect ( extractor . libraryCounts [ 'struts' ] . size ) . toBe ( 1 ) ;
711
+ expect ( extractor . libraryCounts [ 'commons' ] . size ) . toBe ( 1 ) ;
712
+ expect ( extractor . libraryCounts [ 'log4j' ] . size ) . toBe ( 1 ) ;
713
+ expect ( extractor . libraryCounts [ 'cryptix' ] . size ) . toBe ( 1 ) ;
709
714
} ) ;
710
-
711
- test ( 'should handle multiple libraries in one targetClass ' , ( ) => {
712
- const extractor = new PackageDependencyExtractor ( ) ;
715
+
716
+ test ( 'should handle multiple libraries in one class name ' , ( ) => {
717
+ // Example: a class name containing both 'struts' and 'commons'
713
718
extractor . countSpecificLibraries ( 'org.apache.struts.commons.util' ) ;
714
719
715
- expect ( extractor . libraryCounts [ 'struts' ] ) . toBe ( 1 ) ;
716
- expect ( extractor . libraryCounts [ 'commons' ] ) . toBe ( 1 ) ;
717
- expect ( extractor . libraryCounts [ 'log4j' ] ) . toBe ( 0 ) ;
718
- expect ( extractor . libraryCounts [ 'cryptix' ] ) . toBe ( 0 ) ;
720
+ expect ( extractor . libraryCounts [ 'struts' ] . size ) . toBe ( 1 ) ;
721
+ expect ( extractor . libraryCounts [ 'commons' ] . size ) . toBe ( 1 ) ;
722
+ expect ( extractor . libraryCounts [ 'log4j' ] . size ) . toBe ( 0 ) ;
723
+ expect ( extractor . libraryCounts [ 'cryptix' ] . size ) . toBe ( 0 ) ;
719
724
} ) ;
720
725
721
- test ( 'should increment counts correctly for multiple calls' , ( ) => {
722
- const extractor = new PackageDependencyExtractor ( ) ;
726
+ test ( 'should count unique classes correctly' , ( ) => {
723
727
extractor . countSpecificLibraries ( 'org.apache.struts.Action' ) ;
724
728
extractor . countSpecificLibraries ( 'org.apache.struts.actions.DispatchAction' ) ;
725
729
extractor . countSpecificLibraries ( 'org.apache.commons.lang.StringUtils' ) ;
726
730
extractor . countSpecificLibraries ( 'org.apache.commons.io.FileUtils' ) ;
731
+ // Add the same classes again
732
+ extractor . countSpecificLibraries ( 'org.apache.struts.Action' ) ; // Duplicate
733
+ extractor . countSpecificLibraries ( 'org.apache.commons.lang.StringUtils' ) ; // Duplicate
727
734
728
- expect ( extractor . libraryCounts [ 'struts' ] ) . toBe ( 2 ) ;
729
- expect ( extractor . libraryCounts [ 'commons' ] ) . toBe ( 2 ) ;
735
+ expect ( extractor . libraryCounts [ 'struts' ] . size ) . toBe ( 2 ) ; // Should be 2 unique classes
736
+ expect ( extractor . libraryCounts [ 'commons' ] . size ) . toBe ( 2 ) ; // Should be 2 unique classes
730
737
} ) ;
731
738
732
- test ( 'should support custom libraries list' , ( ) => {
739
+ test ( 'should not count libraries not in the list' , ( ) => {
733
740
const extractor = new PackageDependencyExtractor ( 'spring,hibernate,tomcat' ) ;
734
741
extractor . countSpecificLibraries ( 'org.springframework.context.ApplicationContext' ) ;
735
742
extractor . countSpecificLibraries ( 'org.hibernate.Session' ) ;
736
743
737
- expect ( extractor . libraryCounts [ 'spring' ] ) . toBe ( 1 ) ;
738
- expect ( extractor . libraryCounts [ 'hibernate' ] ) . toBe ( 1 ) ;
739
- expect ( extractor . libraryCounts [ 'tomcat' ] ) . toBe ( 0 ) ;
740
- // The default libraries shouldn't be counted
744
+ expect ( extractor . libraryCounts [ 'spring' ] . size ) . toBe ( 1 ) ;
745
+ expect ( extractor . libraryCounts [ 'hibernate' ] . size ) . toBe ( 1 ) ;
746
+ expect ( extractor . libraryCounts [ 'tomcat' ] . size ) . toBe ( 0 ) ;
747
+ expect ( extractor . libraryCounts [ 'struts' ] ) . toBeUndefined ( ) ;
748
+ } ) ;
749
+
750
+ test ( 'should use custom libraries when provided' , ( ) => {
751
+ const extractor = new PackageDependencyExtractor ( 'spring,hibernate,tomcat' ) ;
752
+ extractor . countSpecificLibraries ( 'org.springframework.context.ApplicationContext' ) ;
753
+ extractor . countSpecificLibraries ( 'org.hibernate.Session' ) ;
754
+
755
+ expect ( extractor . libraryCounts [ 'spring' ] . size ) . toBe ( 1 ) ;
756
+ expect ( extractor . libraryCounts [ 'hibernate' ] . size ) . toBe ( 1 ) ;
757
+ expect ( extractor . libraryCounts [ 'tomcat' ] . size ) . toBe ( 0 ) ;
741
758
expect ( extractor . libraryCounts [ 'struts' ] ) . toBeUndefined ( ) ;
742
759
} ) ;
743
760
} ) ;
744
761
745
- // Add test for processRecord with library counting
746
- test ( 'should count libraries when processing a record' , ( ) => {
747
- const extractor = new PackageDependencyExtractor ( ) ;
748
- const record = {
749
- appSetName : 'TestApp' ,
750
- applicationName : 'TestApp' ,
751
- artifactFileName : 'test.jar' ,
752
- artifactId : 'test' ,
753
- artifactGroup : 'com.test' ,
754
- artifactVersion : '1.0.0' ,
755
- sourceClass : 'com.test.TestClass' ,
756
- sourceMethod : 'testMethod' ,
757
- targetClass : 'org.apache.struts.Action' ,
758
- targetMethod : 'execute'
759
- } ;
760
-
761
- extractor . processRecord ( record ) ;
762
- expect ( extractor . libraryCounts [ 'struts' ] ) . toBe ( 1 ) ;
763
-
764
- // Process another record with a different library
765
- const record2 = {
766
- ...record ,
767
- targetClass : 'org.apache.commons.lang.StringUtils'
768
- } ;
762
+ describe ( 'Integration with processRecord' , ( ) => {
763
+ let extractor : PackageDependencyExtractor ;
769
764
770
- extractor . processRecord ( record2 ) ;
771
- expect ( extractor . libraryCounts [ 'commons' ] ) . toBe ( 1 ) ;
765
+ beforeEach ( ( ) => {
766
+ extractor = new PackageDependencyExtractor ( ) ;
767
+ } ) ;
768
+
769
+ test ( 'should count struts library via processRecord' , ( ) => {
770
+ const record = {
771
+ appSetName : 'TestApp' ,
772
+ applicationName : 'TestApp' ,
773
+ artifactFileName : 'test.jar' ,
774
+ artifactId : 'test' ,
775
+ artifactGroup : 'com.test' ,
776
+ artifactVersion : '1.0.0' ,
777
+ sourceClass : 'com.test.TestClass' ,
778
+ sourceMethod : 'testMethod' ,
779
+ targetClass : 'org.apache.struts.Action' ,
780
+ targetMethod : 'execute'
781
+ } ;
782
+
783
+ extractor . processRecord ( record ) ;
784
+ expect ( extractor . libraryCounts [ 'struts' ] . size ) . toBe ( 1 ) ;
785
+ } ) ;
786
+
787
+ test ( 'should count commons library via processRecord' , ( ) => {
788
+ const record = {
789
+ appSetName : 'TestApp' ,
790
+ applicationName : 'TestApp' ,
791
+ artifactFileName : 'test.jar' ,
792
+ artifactId : 'test' ,
793
+ artifactGroup : 'com.test' ,
794
+ artifactVersion : '1.0.0' ,
795
+ sourceClass : 'com.test.TestClass' ,
796
+ sourceMethod : 'testMethod' ,
797
+ targetClass : 'org.apache.commons.lang.StringUtils' ,
798
+ targetMethod : 'execute'
799
+ } ;
800
+
801
+ extractor . processRecord ( record ) ;
802
+ expect ( extractor . libraryCounts [ 'commons' ] . size ) . toBe ( 1 ) ;
803
+ } ) ;
804
+
805
+ test ( 'should correctly count unique classes with multiple identical records' , ( ) => {
806
+ const recordStruts = {
807
+ appSetName : 'TestApp' ,
808
+ applicationName : 'TestApp' ,
809
+ artifactFileName : 'test.jar' ,
810
+ artifactId : 'test' ,
811
+ artifactGroup : 'com.test' ,
812
+ artifactVersion : '1.0.0' ,
813
+ sourceClass : 'com.test.TestClass' ,
814
+ sourceMethod : 'testMethod' ,
815
+ targetClass : 'org.apache.struts.Action' ,
816
+ targetMethod : 'execute'
817
+ } ;
818
+
819
+ const recordCommons = {
820
+ appSetName : 'TestApp' ,
821
+ applicationName : 'TestApp' ,
822
+ artifactFileName : 'test.jar' ,
823
+ artifactId : 'test' ,
824
+ artifactGroup : 'com.test' ,
825
+ artifactVersion : '1.0.0' ,
826
+ sourceClass : 'com.test.TestClass' ,
827
+ sourceMethod : 'testMethod' ,
828
+ targetClass : 'org.apache.commons.lang.StringUtils' ,
829
+ targetMethod : 'execute'
830
+ } ;
831
+
832
+ extractor . processRecord ( recordStruts ) ;
833
+ extractor . processRecord ( recordCommons ) ;
834
+ extractor . processRecord ( recordStruts ) ;
835
+ extractor . processRecord ( recordCommons ) ;
836
+
837
+ expect ( extractor . libraryCounts [ 'struts' ] . size ) . toBe ( 1 ) ;
838
+ expect ( extractor . libraryCounts [ 'commons' ] . size ) . toBe ( 1 ) ;
839
+ } ) ;
772
840
} ) ;
773
841
774
842
// Add tests for getLibrariesToCount
0 commit comments