@Before
  public void setUp() throws Exception {
    // 準備測試檔案樣本內容
    javaProjectMaker = new JavaProjectMaker(testProjectName);
    javaProjectMaker.setJREDefaultContainer();

    // 建立新的檔案DummyAndIgnoreExample
    javaFile2String = new JavaFileToString();
    javaFile2String.read(CatchClauseSampleCode.class, JavaProjectMaker.FOLDERNAME_TEST);
    javaProjectMaker.createJavaFile(
        CatchClauseSampleCode.class.getPackage().getName(),
        CatchClauseSampleCode.class.getSimpleName() + JavaProjectMaker.JAVA_FILE_EXTENSION,
        "package "
            + CatchClauseSampleCode.class.getPackage().getName()
            + ";\n"
            + javaFile2String.getFileContent());

    Path path =
        new Path(
            PathUtils.getPathOfClassUnderSrcFolder(CatchClauseSampleCode.class, testProjectName));
    // Create AST to parse
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    // 設定要被建立AST的檔案
    parser.setSource(
        JavaCore.createCompilationUnitFrom(ResourcesPlugin.getWorkspace().getRoot().getFile(path)));
    parser.setResolveBindings(true);
    // 取得AST
    compilationUnit = (CompilationUnit) parser.createAST(null);
    compilationUnit.recordModifications();
  }
예제 #2
0
  /**
   * 另外測試若 try statement 位於非 try statement 之中時,是否會正確偵測
   *
   * @throws Exception
   */
  @Test
  public void testDetectDummyHandlerWithTryStatementInNonTryStatement() throws Exception {
    CompilationUnit compilationUnitWithTSINTS;

    // 新建立測試用的 DummyHandlerExampleWithTryStatementInNonTryStatement
    javaFile2String.clear();
    javaFile2String.read(
        DummyHandlerExampleWithTryStatementInNonTryStatement.class,
        JavaProjectMaker.FOLDERNAME_TEST);
    javaProjectMaker.createJavaFile(
        DummyHandlerExampleWithTryStatementInNonTryStatement.class.getPackage().getName(),
        DummyHandlerExampleWithTryStatementInNonTryStatement.class.getSimpleName()
            + JavaProjectMaker.JAVA_FILE_EXTENSION,
        "package "
            + DummyHandlerExampleWithTryStatementInNonTryStatement.class.getPackage().getName()
            + ";\n"
            + javaFile2String.getFileContent());

    Path path =
        new Path(
            PathUtils.getPathOfClassUnderSrcFolder(
                DummyHandlerExampleWithTryStatementInNonTryStatement.class, testProjectName));
    // Create AST to parse
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    // 設定要被建立AST的檔案
    parser.setSource(
        JavaCore.createCompilationUnitFrom(ResourcesPlugin.getWorkspace().getRoot().getFile(path)));
    parser.setResolveBindings(true);
    // 取得AST
    compilationUnitWithTSINTS = (CompilationUnit) parser.createAST(null);
    compilationUnitWithTSINTS.recordModifications();
    dummyHandlerVisitor = new DummyHandlerVisitor(compilationUnitWithTSINTS);

    // 確認初始值
    assertEquals(0, dummyHandlerVisitor.getDummyList().size());

    // do this test
    compilationUnitWithTSINTS.accept(dummyHandlerVisitor);

    // 驗證總共抓到幾個bad smell
    assertEquals(2, dummyHandlerVisitor.getDummyList().size());
  }
예제 #3
0
  @Before
  public void setUp() throws Exception {
    // 準備測試檔案樣本內容
    javaProjectMaker = new JavaProjectMaker(testProjectName);
    javaProjectMaker.setJREDefaultContainer();

    // 新增欲載入的library
    javaProjectMaker.addJarFromProjectToBuildPath(
        JavaProjectMaker.FOLDERNAME_LIB_JAR + "/log4j-1.2.15.jar");

    // 若example code中有robustness notation則有此行可以讓編譯通過
    javaProjectMaker.packAgileExceptionClasses2JarIntoLibFolder(
        JavaProjectMaker.FOLDERNAME_LIB_JAR, JavaProjectMaker.FOLDERNAME_BIN_CLASS);
    javaProjectMaker.addJarFromTestProjectToBuildPath("/" + JavaProjectMaker.RL_LIBRARY_PATH);

    // 建立新的檔案DummyAndIgnoreExample
    javaFile2String = new JavaFileToString();
    javaFile2String.read(DummyAndIgnoreExample.class, JavaProjectMaker.FOLDERNAME_TEST);
    javaProjectMaker.createJavaFile(
        DummyAndIgnoreExample.class.getPackage().getName(),
        DummyAndIgnoreExample.class.getSimpleName() + JavaProjectMaker.JAVA_FILE_EXTENSION,
        "package "
            + DummyAndIgnoreExample.class.getPackage().getName()
            + ";\n"
            + javaFile2String.getFileContent());

    // 繼續建立測試用的UserDefineDummyHandlerFish
    javaFile2String.clear();
    javaFile2String.read(UserDefineDummyHandlerFish.class, JavaProjectMaker.FOLDERNAME_TEST);
    javaProjectMaker.createJavaFile(
        UserDefineDummyHandlerFish.class.getPackage().getName(),
        UserDefineDummyHandlerFish.class.getSimpleName() + JavaProjectMaker.JAVA_FILE_EXTENSION,
        "package "
            + UserDefineDummyHandlerFish.class.getPackage().getName()
            + ";\n"
            + javaFile2String.getFileContent());

    // 建立XML
    dummyHandlerPatternsInXML =
        new String[] {
          SmellSettings.EXTRARULE_ePrintStackTrace, SmellSettings.EXTRARULE_SystemErrPrint,
          SmellSettings.EXTRARULE_SystemErrPrintln, SmellSettings.EXTRARULE_SystemOutPrint,
          SmellSettings.EXTRARULE_SystemOutPrintln, SmellSettings.EXTRARULE_JavaUtilLoggingLogger,
          SmellSettings.EXTRARULE_OrgApacheLog4j
        };
    setNewSettingsWithExtraRules(dummyHandlerPatternsInXML);

    Path path =
        new Path(
            PathUtils.getPathOfClassUnderSrcFolder(DummyAndIgnoreExample.class, testProjectName));
    // Create AST to parse
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    // 設定要被建立AST的檔案
    parser.setSource(
        JavaCore.createCompilationUnitFrom(ResourcesPlugin.getWorkspace().getRoot().getFile(path)));
    parser.setResolveBindings(true);
    // 取得AST
    compilationUnit = (CompilationUnit) parser.createAST(null);
    compilationUnit.recordModifications();
    dummyHandlerVisitor = new DummyHandlerVisitor(compilationUnit);
  }
  @Before
  public void setUp() throws Exception {
    String testProjectName = "CarelessCleanupExampleProject";
    javaFile2String = new JavaFileToString();
    javaProjectMaker = new JavaProjectMaker(testProjectName);
    javaProjectMaker.packAgileExceptionClasses2JarIntoLibFolder(
        JavaProjectMaker.FOLDERNAME_LIB_JAR, JavaProjectMaker.FOLDERNAME_BIN_CLASS);
    javaProjectMaker.addJarFromTestProjectToBuildPath("/" + JavaProjectMaker.RL_LIBRARY_PATH);
    javaProjectMaker.setJREDefaultContainer();
    // 根據測試檔案樣本內容建立新的檔案
    javaFile2String.read(CarelessCleanupExample.class, JavaProjectMaker.FOLDERNAME_TEST);
    javaProjectMaker.createJavaFile(
        CarelessCleanupExample.class.getPackage().getName(),
        CarelessCleanupExample.class.getSimpleName() + JavaProjectMaker.JAVA_FILE_EXTENSION,
        "package "
            + CarelessCleanupExample.class.getPackage().getName()
            + ";"
            + String.format("%n")
            + javaFile2String.getFileContent());
    javaFile2String.clear();

    javaFile2String.read(
        ClassWithNotThrowingExceptionCloseable.class, JavaProjectMaker.FOLDERNAME_TEST);
    javaProjectMaker.createJavaFile(
        ClassWithNotThrowingExceptionCloseable.class.getPackage().getName(),
        ClassWithNotThrowingExceptionCloseable.class.getSimpleName()
            + JavaProjectMaker.JAVA_FILE_EXTENSION,
        "package "
            + ClassWithNotThrowingExceptionCloseable.class.getPackage().getName()
            + ";"
            + System.getProperty("line.separator")
            + javaFile2String.getFileContent());
    javaFile2String.clear();

    javaFile2String.read(ClassImplementCloseable.class, JavaProjectMaker.FOLDERNAME_TEST);
    javaProjectMaker.createJavaFile(
        ClassImplementCloseable.class.getPackage().getName(),
        ClassImplementCloseable.class.getSimpleName() + JavaProjectMaker.JAVA_FILE_EXTENSION,
        "package "
            + ClassImplementCloseable.class.getPackage().getName()
            + ";"
            + System.getProperty("line.separator")
            + javaFile2String.getFileContent());
    javaFile2String.clear();

    /* 測試使用者設定Pattern時候使用 */
    javaFile2String.read(UserDefinedCarelessCleanupWeather.class, JavaProjectMaker.FOLDERNAME_TEST);
    javaProjectMaker.createJavaFile(
        UserDefinedCarelessCleanupWeather.class.getPackage().getName(),
        UserDefinedCarelessCleanupWeather.class.getSimpleName()
            + JavaProjectMaker.JAVA_FILE_EXTENSION,
        "package "
            + UserDefinedCarelessCleanupWeather.class.getPackage().getName()
            + ";"
            + System.getProperty("line.separator")
            + javaFile2String.getFileContent());
    javaFile2String.clear();

    javaFile2String.read(UserDefinedCarelessCleanupDog.class, JavaProjectMaker.FOLDERNAME_TEST);
    javaProjectMaker.createJavaFile(
        UserDefinedCarelessCleanupDog.class.getPackage().getName(),
        UserDefinedCarelessCleanupDog.class.getSimpleName() + JavaProjectMaker.JAVA_FILE_EXTENSION,
        "package "
            + UserDefinedCarelessCleanupDog.class.getPackage().getName()
            + ";"
            + System.getProperty("line.separator")
            + javaFile2String.getFileContent());
    javaFile2String.clear();

    javaFile2String.read(
        ClassImplementCloseableWithoutThrowException.class, JavaProjectMaker.FOLDERNAME_TEST);
    javaProjectMaker.createJavaFile(
        ClassImplementCloseableWithoutThrowException.class.getPackage().getName(),
        ClassImplementCloseableWithoutThrowException.class.getSimpleName()
            + JavaProjectMaker.JAVA_FILE_EXTENSION,
        "package "
            + ClassImplementCloseableWithoutThrowException.class.getPackage().getName()
            + ";"
            + System.getProperty("line.separator")
            + javaFile2String.getFileContent());
    javaFile2String.clear();

    Path ccExamplePath =
        new Path(
            PathUtils.getPathOfClassUnderSrcFolder(CarelessCleanupExample.class, testProjectName));
    // Create AST to parse

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    // 設定要被建立AST的檔案
    parser.setSource(
        JavaCore.createCompilationUnitFrom(
            ResourcesPlugin.getWorkspace().getRoot().getFile(ccExamplePath)));
    parser.setResolveBindings(true);
    // 建立XML
    CreateSettings();
    // 取得AST
    compilationUnit = (CompilationUnit) parser.createAST(null);
    compilationUnit.recordModifications();
    carelessCleanupVisitor = new CarelessCleanupVisitor2(compilationUnit);
  }