@Test public void testAddDummyHandlerSmellInfoWithUserDefinedPatternAndDetecting() { // 使用者自定義 *.toString 的Pattern,並且要求要偵測 setEmptySetting(); smellSettings.addDummyHandlerPattern("*.toString", true); smellSettings.writeXMLFile(UserDefinedMethodAnalyzer.SETTINGFILEPATH); dummyHandlerVisitor = new DummyHandlerVisitor(compilationUnit); compilationUnit.accept(dummyHandlerVisitor); assertEquals(2, dummyHandlerVisitor.getDummyList().size()); MethodInvocation mi = null; // 符合 *.toString 的程式碼 -> e.toString() mi = ASTNodeFinder.getMethodInvocationByMethodNameAndCode( compilationUnit, "true_systemOutPrintlnWithoutE", "e.toString()") .get(0); dummyHandlerVisitor = new DummyHandlerVisitor(compilationUnit); dummyHandlerVisitor.detectDummyHandler(mi); assertEquals(1, dummyHandlerVisitor.getDummyList().size()); // 不符合 *.toString 的程式碼 -> e.toString.toCharArray() mi = ASTNodeFinder.getMethodInvocationByMethodNameAndCode( compilationUnit, "true_systemOutPrintlnWithoutE", "e.toString().toCharArray()") .get(0); dummyHandlerVisitor = new DummyHandlerVisitor(compilationUnit); dummyHandlerVisitor.detectDummyHandler(mi); assertEquals(0, dummyHandlerVisitor.getDummyList().size()); }
@Test public void testAddDummyHandlerSmellInfoForExtraRule_OrgApacheLog4j() throws Exception { Method method = DummyHandlerVisitor.class.getDeclaredMethod( "addDummyHandlerSmellInfo", MethodInvocation.class); method.setAccessible(true); // 確認初始值 setEmptySetting(); dummyHandlerVisitor = new DummyHandlerVisitor(compilationUnit); compilationUnit.accept(dummyHandlerVisitor); assertEquals(0, dummyHandlerVisitor.getDummyList().size()); // OrgApacheLog4j case dummyHandlerPatternsInXML = new String[] {SmellSettings.EXTRARULE_OrgApacheLog4j}; setNewSettingsWithExtraRules(dummyHandlerPatternsInXML); // 分別測試:全部例子、符合例子、不符合例子 是否有抓出 dummyHandlerVisitor = new DummyHandlerVisitor(compilationUnit); compilationUnit.accept(dummyHandlerVisitor); assertEquals(1, dummyHandlerVisitor.getDummyList().size()); MethodInvocation mi = null; mi = ASTNodeFinder.getMethodInvocationByMethodNameAndCode( compilationUnit, "true_Log4J", "log4j.info(\"message\")") .get(0); method.invoke(dummyHandlerVisitor, mi); assertEquals(2, dummyHandlerVisitor.getDummyList().size()); mi = ASTNodeFinder.getMethodInvocationByMethodNameAndCode( compilationUnit, "true_javaLogInfo", "javaLog.info(\"\")") .get(0); method.invoke(dummyHandlerVisitor, mi); assertEquals(2, dummyHandlerVisitor.getDummyList().size()); }
@Test public void testDetectDummyHandler() { // 確認初始值 assertEquals(0, dummyHandlerVisitor.getDummyList().size()); // #1 正常的DummyHandler MethodInvocation methodInvocation = ASTNodeFinder.getMethodInvocationByMethodNameAndCode( compilationUnit, "true_printStackTrace_public", "e.printStackTrace()") .get(0); dummyHandlerVisitor.detectDummyHandler(methodInvocation); assertEquals(1, dummyHandlerVisitor.getDummyList().size()); // #2 有throw methodInvocation = ASTNodeFinder.getMethodInvocationByMethodNameAndCode( compilationUnit, "false_throwAndPrint", "e.printStackTrace()") .get(0); dummyHandlerVisitor.detectDummyHandler(methodInvocation); assertEquals(1, dummyHandlerVisitor.getDummyList().size()); // #3 測 Catch 外面 methodInvocation = ASTNodeFinder.getMethodInvocationByMethodNameAndCode( compilationUnit, "true_printStackTrace_protected", "fis.read()") .get(0); dummyHandlerVisitor.detectDummyHandler(methodInvocation); assertEquals(1, dummyHandlerVisitor.getDummyList().size()); }
@Test public void testAddDummyHandlerSmellInfoForExtraRule_PrintStackTrace() throws Exception { Method method = DummyHandlerVisitor.class.getDeclaredMethod( "addDummyHandlerSmellInfo", MethodInvocation.class); method.setAccessible(true); // 確認初始值 setEmptySetting(); dummyHandlerVisitor = new DummyHandlerVisitor(compilationUnit); compilationUnit.accept(dummyHandlerVisitor); assertEquals(0, dummyHandlerVisitor.getDummyList().size()); // ePrintStackTrace case dummyHandlerPatternsInXML = new String[] {SmellSettings.EXTRARULE_ePrintStackTrace}; setNewSettingsWithExtraRules(dummyHandlerPatternsInXML); // 分別測試:全部例子、符合例子、不符合例子 是否有抓出 dummyHandlerVisitor = new DummyHandlerVisitor(compilationUnit); compilationUnit.accept(dummyHandlerVisitor); assertEquals(10, dummyHandlerVisitor.getDummyList().size()); MethodInvocation mi = null; mi = ASTNodeFinder.getMethodInvocationByMethodNameAndCode( compilationUnit, "true_printStackTrace_protected", "e.printStackTrace()") .get(0); method.invoke(dummyHandlerVisitor, mi); assertEquals(11, dummyHandlerVisitor.getDummyList().size()); }
@Test public void testVisitMethodInvocation() { MethodInvocation methodInvocation = ASTNodeFinder.getMethodInvocationByMethodNameAndCode( compilationUnit, "true_printStackTrace_public", "e.printStackTrace()") .get(0); assertFalse(dummyHandlerVisitor.visit(methodInvocation)); assertEquals(1, dummyHandlerVisitor.getDummyList().size()); }
@Test public void testIsThrowStatementInCatchClause() { TryStatement tryStatement; CatchClause catchStatement; // 測試 符合的例子 是否會抓出 MethodDeclaration md = null; md = ASTNodeFinder.getMethodDeclarationNodeByName(compilationUnit, "false_throwAndPrint"); tryStatement = (TryStatement) md.getBody().statements().get(0); catchStatement = (CatchClause) tryStatement.catchClauses().get(0); assertTrue(dummyHandlerVisitor.isThrowStatementInCatchClause(catchStatement)); // 測試 不符合例子 是否會抓出 md = ASTNodeFinder.getMethodDeclarationNodeByName( compilationUnit, "true_DummyHandlerTryNestedTry"); tryStatement = (TryStatement) md.getBody().statements().get(1); catchStatement = (CatchClause) tryStatement.catchClauses().get(0); assertFalse(dummyHandlerVisitor.isThrowStatementInCatchClause(catchStatement)); }