|
4 | 4 |
|
5 | 5 | import java.io.File; |
6 | 6 | import java.io.FileInputStream; |
| 7 | +import java.io.FileNotFoundException; |
7 | 8 | import java.io.FileOutputStream; |
8 | 9 | import java.io.IOException; |
9 | 10 | import java.util.ArrayList; |
|
21 | 22 |
|
22 | 23 |
|
23 | 24 | public class JSAnalyzer { |
24 | | - |
25 | | - |
26 | | - |
27 | 25 |
|
| 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; |
28 | 34 |
|
29 | 35 | private int coveredEventCallback = 0; |
30 | 36 | public int getCoveredEventCallback() { |
@@ -300,7 +306,7 @@ else if (sub.charAt(endIndex) == '}') { |
300 | 306 |
|
301 | 307 | public void analyzeProductionCodeCoverage(ArrayList<Integer> coveredLines, ArrayList<Integer> missedLines, ArrayList<Integer> coveredFunctionsIndices, ArrayList<Integer> missedFunctionsIndices) throws Exception { |
302 | 308 |
|
303 | | - astVisitor.setInstrumentationEnable(false); |
| 309 | + astVisitor.setVisitType("AnalyzeProductionCode"); |
304 | 310 | astVisitor.setFunctionCounter(0); // resetting the index of visited Function nodes for annotating covered functions |
305 | 311 | astVisitor.clearFunctionsList(); // clearing list of covered and missed function from previous visit |
306 | 312 |
|
@@ -423,13 +429,165 @@ public void analyzeProductionCodeCoverage(ArrayList<Integer> coveredLines, Array |
423 | 429 | } |
424 | 430 |
|
425 | 431 | //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); |
426 | 482 |
|
427 | | - astVisitor.setInstrumentationEnable(true); |
| 483 | + System.out.println("FunctionCalls :" + astVisitor.getFunctionCalls()); |
428 | 484 |
|
| 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(""); |
429 | 559 | } |
430 | 560 |
|
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; |
433 | 591 | } |
434 | 592 |
|
435 | 593 | } |
0 commit comments