Skip to content

Commit 9435599

Browse files
committed
refactoring and restructuring
1 parent 4e85cca commit 9435599

File tree

3 files changed

+257
-30
lines changed

3 files changed

+257
-30
lines changed

src/main/java/TestCodePropertyAnalyzer.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ public class TestCodePropertyAnalyzer {
3636
private static ArrayList<String> jsFileNames = new ArrayList<String>();
3737
private static JSAnalyzer codeAnalyzer;
3838
private static TraceAnalyzer traceAnalyzer = new TraceAnalyzer();
39+
private static int NumTests;
40+
private static int NumAsyncTests;
41+
private static int NumAssertions;
42+
private static int MaxFunCall;
43+
private static float AveFunCall;
44+
private static int NumDOMFixture;
45+
private static int NumTriggerTest;
46+
private static int NumObjCreate;
3947

4048
public static void main(String[] args) throws Exception {
4149

@@ -45,12 +53,12 @@ public static void main(String[] args) throws Exception {
4553
System.out.println("Test framework: " + testsFramework);
4654
File[] files = new File(testsFolder).listFiles();
4755
if (files==null){
48-
System.out.println("No test file found in directory: " + testsFolder);
56+
System.out.println("No file found in directory: " + testsFolder);
4957
return;
5058
}
51-
for (File file : files) {
59+
for (File file : files)
5260
processFile(file);
53-
}
61+
5462

5563

5664
/*
@@ -100,27 +108,16 @@ private static void processFile(File file) throws IOException, Exception {
100108
System.out.println("No test file found in directory: " + file.getAbsolutePath().replace(testsFolder, ""));
101109
return;
102110
}
103-
for (File innerFile : files) {
111+
for (File innerFile : files)
104112
processFile(innerFile);
105-
}
106113
}
107114
if (file.isFile()) {
108115
String fileName = file.getName();
109-
110116
//if (fileName.endsWith(".js")){
111117
if (!fileName.contains("qunit") && fileName.endsWith(".js")) {
112118
//&& !fileName.equals("es.js") && !fileName.equals("helpers.js") && !fileName.equals("karma.conf.js") && !fileName.equals("es.js") && !fileName.equals("library.js")
113119
//){
114-
115-
codeAnalyzer.setJSFileName(fileName);
116-
codeAnalyzer.setJSAddress(testsFolder + "/" + fileName);
117-
118-
System.out.println("Analysing the test suite in file " + fileName);
119-
// calc fun calls
120-
121-
//codeAnalyzer.instrumentJavaScript();
122120
analyseJSTestFile(file.getCanonicalPath());
123-
124121
}
125122
}
126123

@@ -145,8 +142,11 @@ private static void analyseJSTestFile(String canonicalPath) throws Exception {
145142
System.out.println(canonicalPath);
146143
codeAnalyzer.setJSFileName(fileName);
147144
codeAnalyzer.setJSAddress(canonicalPath);
145+
//codeAnalyzer.setJSAddress(testsFolder + "/" + fileName);
146+
System.out.println("Analysing the test suite in file " + fileName);
148147
codeAnalyzer.analyzeTestCodeProperties();
149-
148+
149+
150150
NumTests += codeAnalyzer.getNumTests();
151151
NumAsyncTests += codeAnalyzer.getNumAsyncTests();
152152
NumAssertions += codeAnalyzer.getNumAssertions();

src/main/java/core/JSAnalyzer.java

Lines changed: 165 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.io.File;
66
import java.io.FileInputStream;
7+
import java.io.FileNotFoundException;
78
import java.io.FileOutputStream;
89
import java.io.IOException;
910
import java.util.ArrayList;
@@ -21,10 +22,15 @@
2122

2223

2324
public class JSAnalyzer {
24-
25-
26-
2725

26+
private static int NumTests = 0;
27+
private static int NumAsyncTests = 0;
28+
private static int NumAssertions = 0;
29+
private static int MaxFunCall = 0;
30+
private static float AveFunCall = 0;
31+
private static int NumDOMFixture = 0;
32+
private static int NumTriggerTest = 0;
33+
private static int NumObjCreate = 0;
2834

2935
private int coveredEventCallback = 0;
3036
public int getCoveredEventCallback() {
@@ -300,7 +306,7 @@ else if (sub.charAt(endIndex) == '}') {
300306

301307
public void analyzeProductionCodeCoverage(ArrayList<Integer> coveredLines, ArrayList<Integer> missedLines, ArrayList<Integer> coveredFunctionsIndices, ArrayList<Integer> missedFunctionsIndices) throws Exception {
302308

303-
astVisitor.setInstrumentationEnable(false);
309+
astVisitor.setVisitType("AnalyzeProductionCode");
304310
astVisitor.setFunctionCounter(0); // resetting the index of visited Function nodes for annotating covered functions
305311
astVisitor.clearFunctionsList(); // clearing list of covered and missed function from previous visit
306312

@@ -423,13 +429,165 @@ public void analyzeProductionCodeCoverage(ArrayList<Integer> coveredLines, Array
423429
}
424430

425431
//System.out.println("Here is the corresponding buffer: \n" + input + "\n");
432+
433+
astVisitor.setVisitType("");
434+
}
435+
436+
public void analyzeTestCodeProperties() throws Exception {
437+
System.out.println("===== analyzeTestCodeProperties ====");
438+
439+
astVisitor.setVisitType("AnalyzeTestCode");
440+
441+
// reading js form the input file
442+
String input = "";
443+
FileInputStream inputStream = new FileInputStream(jsAddress);
444+
try {
445+
input = IOUtils.toString(inputStream);
446+
} finally {
447+
inputStream.close();
448+
}
449+
450+
try {
451+
AstRoot ast = null;
452+
/* initialize JavaScript context */
453+
Context cx = Context.enter();
454+
/* create a new parser */
455+
Parser rhinoParser = new Parser(new CompilerEnvirons(), cx.getErrorReporter());
456+
/* parse some script and save it in AST */
457+
ast = rhinoParser.parse(new String(input), scopeName, 0);
458+
459+
//System.out.println("************** AST ******************");
460+
//System.out.println(ast.toSource());
461+
//System.out.println(ast.debugPrint());
462+
//writeJSToFile(scopename, input);
463+
//writeFunctionsToFile(input);
464+
//System.out.println("AST BEFORE : ");
465+
//System.out.println(ast.toSource());
466+
467+
astVisitor.setScopeName(scopeName);
468+
/* recurse through AST */
469+
astVisitor.setVisitOnly("FunctionNode");
470+
ast.visit(astVisitor);
471+
472+
System.out.println("CoveredFunctions :" + astVisitor.getCoveredFunctions());
473+
//System.out.println("CoveredFunctions.size() :" + astVisitor.getCoveredFunctions().size());
474+
System.out.println("CoveredFunctionLines :" + astVisitor.getCoveredFunctionLines());
475+
System.out.println("MissedFunctions :" + astVisitor.getMissedFunctions());
476+
//System.out.println("MissedFunctions.size() :" + astVisitor.getMissedFunctions().size());
477+
System.out.println("MissedFunctionLines :" + astVisitor.getMissedFunctionLines());
478+
479+
480+
astVisitor.setVisitOnly("FunctionCall");
481+
ast.visit(astVisitor);
426482

427-
astVisitor.setInstrumentationEnable(true);
483+
System.out.println("FunctionCalls :" + astVisitor.getFunctionCalls());
428484

485+
for (String functionCall : astVisitor.getFunctionCalls()){
486+
if (functionCall.contains(".call") || functionCall.contains(".apply")) // The call() and apply() methods calls a function with a given this value and arguments
487+
functionCall = functionCall.replace(".call", "").replace(".apply", "");
488+
if (astVisitor.getMissedFunctions().contains(functionCall)){
489+
System.out.println("The call to function " + functionCall + " was never executed!");
490+
neverExecFunCallSites++;
491+
}
492+
}
493+
494+
495+
coveredRegularFunc = astVisitor.getCoveredRegularFunc();
496+
missedRegularFunc = astVisitor.getMissedRegularFunc();
497+
coveredCallback = astVisitor.getCoveredCallback();
498+
missedCallback = astVisitor.getMissedCallback();
499+
coveredAsyncCallback = astVisitor.getCoveredAsyncCallback();
500+
missedAsyncCallback = astVisitor.getMissedAsyncCallback();
501+
coveredEventCallback = astVisitor.getCoveredAsyncCallback();
502+
missedEventCallback = astVisitor.getMissedEventCallback();
503+
coveredClosure = astVisitor.getCoveredClosure();
504+
missedClosure = astVisitor.getMissedClosure();
505+
506+
507+
System.out.println("++++ coveredRegularFunc: " + astVisitor.getCoveredRegularFunc());
508+
System.out.println("++++ missedRegularFunc: " + astVisitor.getMissedRegularFunc());
509+
System.out.println("++++ coveredCallback: " + astVisitor.getCoveredCallback());
510+
System.out.println("++++ missedCallback: " + astVisitor.getMissedCallback());
511+
System.out.println("++++ coveredAsyncCallback: " + astVisitor.getCoveredAsyncCallback());
512+
System.out.println("++++ missedAsyncCallback: " + astVisitor.getMissedAsyncCallback());
513+
System.out.println("++++ coveredEventCallback: " + astVisitor.getCoveredAsyncCallback());
514+
System.out.println("++++ missedEventCallback: " + astVisitor.getMissedEventCallback());
515+
System.out.println("++++ coveredClosure: " + astVisitor.getCoveredClosure());
516+
System.out.println("++++ missedClosure: " + astVisitor.getMissedClosure());
517+
518+
System.out.println("++++ neverExecFunCallSites: " + neverExecFunCallSites);
519+
520+
521+
ArrayList<Integer> msimf = astVisitor.getMissedStatementInMissedFunction();
522+
//System.out.println("msimf: " + msimf);
523+
for (int i=0; i<msimf.size(); i++){
524+
if (msimf.get(i) >= 0)
525+
totalMissedStatementLinesInMissedFunctionCounter ++;
526+
}
527+
528+
totalMissedStatementLines = astVisitor.getMissedStatementLines().size();
529+
530+
System.out.println("@ Total missed statement lines in missed functioncounter = " + totalMissedStatementLinesInMissedFunctionCounter);
531+
System.out.println("@ Total number of missed statements = " + totalMissedStatementLines);
532+
if (totalMissedStatementLines!=0){
533+
float ratio = (float)totalMissedStatementLinesInMissedFunctionCounter/(float)totalMissedStatementLines;
534+
System.out.println("@ Percentage of missed statement in missed functions = " + ratio*100 + "%");
535+
}
536+
537+
538+
/*
539+
System.out.println("assertionCounter: " + astVisitor.getAssertionCounter());
540+
System.out.println("newExpressionCounter: " + astVisitor.getNewExpressionCounter());
541+
System.out.println("testCounter: " + astVisitor.getTestCounter());
542+
System.out.println("asynchTestCounter: " + astVisitor.getAsynchTestCounter());
543+
System.out.println("trieggerCounter: " + astVisitor.getTriggerCounetr());
544+
*/
545+
546+
/* clean up */
547+
Context.exit();
548+
} catch (RhinoException re) {
549+
System.err.println(re.getMessage());
550+
System.out.println("Unable to instrument. This might be a JSON response sent"
551+
+ " with the wrong Content-Type or a syntax error.");
552+
} catch (IllegalArgumentException iae) {
553+
554+
System.out.println("Invalid operator exception catched. Not instrumenting code.");
555+
}
556+
557+
//System.out.println("Here is the corresponding buffer: \n" + input + "\n");
558+
astVisitor.setVisitType("");
429559
}
430560

431-
public void analyzeTestCodeProperties() {
432-
System.out.println("===== analyzeTestCodeProperties ====");
561+
public int getNumTests() {
562+
return NumTests;
563+
}
564+
565+
public int getNumAsyncTests() {
566+
return NumAsyncTests;
567+
}
568+
569+
public int getNumAssertions() {
570+
return NumAssertions;
571+
}
572+
573+
public int getMaxFunCall() {
574+
return MaxFunCall;
575+
}
576+
577+
public float getAveFunCall() {
578+
return AveFunCall;
579+
}
580+
581+
public int getNumDOMFixture() {
582+
return NumDOMFixture;
583+
}
584+
585+
public int getNumTriggerTest() {
586+
return NumTriggerTest;
587+
}
588+
589+
public int getNumObjCreate() {
590+
return NumObjCreate;
433591
}
434592

435593
}

0 commit comments

Comments
 (0)