11import com .github .javaparser .JavaParser ;
22import com .github .javaparser .ast .CompilationUnit ;
3+ import com .github .javaparser .ast .ImportDeclaration ;
34import com .github .javaparser .ast .body .MethodDeclaration ;
45import com .github .javaparser .ast .visitor .VoidVisitorAdapter ;
56import entity .MethodEntity ;
67
78import java .io .FileInputStream ;
89import java .io .FileNotFoundException ;
910import java .util .ArrayList ;
11+ import java .util .HashMap ;
1012import java .util .List ;
13+ import java .util .Map ;
1114
1215public class TestFileDetector {
1316 private String absoluteFilePath ;
1417 private List <MethodEntity > methods ;
18+ private Map <String , Boolean > imports ;
1519
16- public TestFileDetector () {
20+ public TestFileDetector (String absoluteFilePath ) throws FileNotFoundException {
1721 methods = new ArrayList <>();
22+ imports = new HashMap <>();
23+ imports .put ("junit.framework.Test" ,false );
24+ imports .put ("junit.framework.TestCase" ,false );
25+ imports .put ("org.junit.Test" ,false );
26+ imports .put ("android.test.AndroidTestCase" ,false );
27+ imports .put ("android.test.InstrumentationTestCase" ,false );
28+ imports .put ("org.junit.Assert" ,false );
29+
30+ parseFile (absoluteFilePath );
1831 }
1932
20- public List < MethodEntity > parseFile (String absoluteFilePath ) throws FileNotFoundException {
33+ private void parseFile (String absoluteFilePath ) throws FileNotFoundException {
2134 this .absoluteFilePath = absoluteFilePath ;
2235
2336 if (absoluteFilePath .length ()!=0 ){
@@ -26,9 +39,17 @@ public List<MethodEntity> parseFile(String absoluteFilePath) throws FileNotFound
2639 ClassVisitor cv = new ClassVisitor ();
2740 cv .visit (compilationUnit ,null );
2841 }
42+ }
2943
44+ public List <MethodEntity > getMethodDetails (){
3045 return methods ;
3146 }
47+
48+ public Map <String , Boolean > getImportDetails (){
49+ return imports ;
50+ }
51+
52+
3253 private class ClassVisitor extends VoidVisitorAdapter <Void > {
3354 @ Override
3455 public void visit (MethodDeclaration n , Void arg ) {
@@ -47,6 +68,34 @@ public void visit(MethodDeclaration n, Void arg) {
4768 super .visit (n , arg );
4869 }
4970
71+ @ Override
72+ public void visit (ImportDeclaration n , Void arg ) {
73+ if (n .getNameAsString ().contains ("junit.framework.Test" )){
74+ imports .replace ("junit.framework.Test" ,false ,true );
75+ }
5076
77+ if (n .getNameAsString ().contains ("junit.framework.TestCase" )){
78+ imports .replace ("junit.framework.TestCase" ,false ,true );
79+ }
80+
81+ if (n .getNameAsString ().contains ("org.junit.Test" )){
82+ imports .replace ("org.junit.Test" ,false ,true );
83+ }
84+
85+ if (n .getNameAsString ().contains ("android.test.AndroidTestCase" )){
86+ imports .replace ("android.test.AndroidTestCase" ,false ,true );
87+ }
88+
89+ if (n .getNameAsString ().contains ("android.test.InstrumentationTestCase" )){
90+ imports .replace ("android.test.InstrumentationTestCase" ,false ,true );
91+ }
92+
93+ if (n .getNameAsString ().contains ("org.junit.Assert" )){
94+ imports .replace ("org.junit.Assert" ,false ,true );
95+ }
96+
97+
98+ super .visit (n , arg );
99+ }
51100 }
52101}
0 commit comments