Program: 12030
Project: 12030
Buggy source:
public void run(IAction action){
  PreferenceLoader pref=new PreferenceLoader();
  IWorkspaceRoot workspaceRoot=ResourcesPlugin.getWorkspace().getRoot();
  try {
    String projectName=null;
    String testTargetClassname=null;
    String testCaseFilename=null;
    String testCaseCreateFilepath=null;
    String testCaseResource=null;
    StructuredSelection structuredSelection=null;
    if (selection instanceof StructuredSelection) {
      structuredSelection=(StructuredSelection)selection;
    }
 else {
    }
    if (structuredSelection != null && structuredSelection.size() == 0) {
      MessageDialog.openWarning(new Shell(),Dialog.Common.title,Dialog.Common.required);
      return;
    }
 else     if (structuredSelection != null && structuredSelection.size() > 1) {
      MessageDialog.openWarning(new Shell(),Dialog.Common.title,Dialog.Common.selectOneOnly);
      return;
    }
    String pathFromProjectRoot=ResourcePathUtil.getPathStartsFromProjectRoot(structuredSelection);
    String[] dirArrFromProjectRoot=pathFromProjectRoot.split(StrConst.dirSep);
    String selected=StrConst.empty;
    int allDirLen=dirArrFromProjectRoot.length - 1;
    for (int i=2; i < allDirLen; i++)     selected+=dirArrFromProjectRoot[i] + StrConst.dirSep;
    selected+=dirArrFromProjectRoot[allDirLen];
    projectName=dirArrFromProjectRoot[1];
    String testTargetClassFilename=dirArrFromProjectRoot[dirArrFromProjectRoot.length - 1];
    testTargetClassname=testTargetClassFilename.replace(StrConst.javaFileExp,StrConst.empty);
    testCaseFilename=testTargetClassname + StrConst.suffixOfTestcase + StrConst.javaFileExp;
    String projectRootPath=workspaceRoot.getLocation() + StrConst.dirSep + projectName+ StrConst.dirSep;
    if (testTargetClassname == null) {
      MessageDialog.openWarning(new Shell(),Dialog.Common.title,Dialog.Common.selectJavaFile);
    }
 else {
      testCaseResource=selected.replace(pref.commonSrcMainJavaDir,pref.commonTestMainJavaDir).replace(StrConst.javaFileExp,StrConst.suffixOfTestcase + StrConst.javaFileExp);
      testCaseCreateFilepath=projectRootPath + selected.replace(pref.commonSrcMainJavaDir,pref.commonTestMainJavaDir).replace(StrConst.javaFileExp,StrConst.suffixOfTestcase + StrConst.javaFileExp);
      File outputFile=new File(testCaseCreateFilepath);
      if (!outputFile.exists()) {
        if (!pref.isTestClassGenEnabled) {
          MessageDialog.openInformation(new Shell(),Dialog.Common.title,Dialog.Common.notToCreateNewFilePreference);
          return;
        }
        String msg=Dialog.Common.notExist + "String_Node_Str" + testCaseFilename+ "String_Node_Str"+ StrConst.lineFeed+ Dialog.Common.confirmToCreateNewFile;
        if (testCaseFilename != null && MessageDialog.openConfirm(new Shell(),Dialog.Common.title,msg)) {
          new CreateNewTestCaseAction().run(action,selection);
        }
        return;
      }
      int retryCount=0;
      IEditorPart editorPart=null;
      while (true) {
        try {
          IProject project=workspaceRoot.getProject(projectName);
          IFile testCaseFile=project.getFile(testCaseResource);
          String editorId=IDE.getEditorDescriptor(testCaseFile.getName()).getId();
          IWorkbenchPage page=PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
          editorPart=IDE.openEditor(page,testCaseFile,editorId);
          editorPart.setFocus();
          if (pref.isTestMethodGenEnabled) {
            IFile testTargetFile=project.getFile(selected);
            ClassInfo testClassWithNotImpledMethods=TestCaseGenerateUtil.getClassInfoWithUnimplementedTestMethods(testTargetFile,testCaseFile);
            List<MethodInfo> unimpledTestMethodNames=testClassWithNotImpledMethods.methods;
            String writeEncoding=FileResourceUtil.detectEncoding(testCaseFile);
            if (unimpledTestMethodNames.size() > 0) {
              List<String> lines=SourceCodeParseUtil.getLineList(testCaseFile);
              StringBuilder sb=new StringBuilder();
              String CRLF=StrConst.carriageReturn + StrConst.lineFeed;
              boolean isImportedTypesAdded=false;
              for (              String line : lines) {
                if (line.equals("String_Node_Str") && pref.isTestMethodGenEnabled) {
                  for (                  MethodInfo testMethod : unimpledTestMethodNames) {
                    if (testMethod.testMethodName == null || testMethod.testMethodName.equals(StrConst.empty)) {
                      continue;
                    }
                    if (pref.isJUnitVersion4) {
                      sb.append("String_Node_Str");
                      sb.append(CRLF);
                    }
                    sb.append("String_Node_Str");
                    sb.append(testMethod.testMethodName);
                    sb.append("String_Node_Str");
                    sb.append(CRLF);
                    sb.append("String_Node_Str");
                    sb.append(StrConst.autoGenTodoMessage);
                    sb.append(CRLF);
                    if (pref.isTestMethodGenNotBlankEnabled) {
                      String notBlankSourceCode=TestCaseGenerateUtil.getNotBlankTestMethodSource(testMethod,testClassWithNotImpledMethods,testTargetClassname);
                      sb.append(notBlankSourceCode);
                    }
                    sb.append("String_Node_Str");
                    sb.append(CRLF);
                    sb.append(CRLF);
                  }
                  sb.append("String_Node_Str");
                  sb.append(CRLF);
                  break;
                }
 else {
                  sb.append(line);
                  sb.append(CRLF);
                  if (line.matches("String_Node_Str") && !isImportedTypesAdded && pref.isTestMethodGenNotBlankEnabled) {
                    List<String> importTypes=testClassWithNotImpledMethods.importList;
                    for (                    String importType : importTypes) {
                      if (importType.matches("String_Node_Str"))                       continue;
                      sb.append("String_Node_Str");
                      sb.append(importType);
                      sb.append("String_Node_Str");
                      sb.append(CRLF);
                    }
                    isImportedTypesAdded=true;
                  }
                }
              }
              String content=sb.toString();
              OutputStreamWriter testFileOSWriter=null;
              FileOutputStream fos=null;
              try {
                fos=new FileOutputStream(projectRootPath + StrConst.dirSep + testCaseResource);
                testFileOSWriter=new OutputStreamWriter(fos,writeEncoding);
                testFileOSWriter.write(content);
              }
  finally {
                FileResourceUtil.close(testFileOSWriter);
                FileResourceUtil.close(fos);
              }
            }
            if (!ResourceRefreshUtil.refreshLocal(null,projectName + StrConst.dirSep + testCaseResource+ "String_Node_Str")) {
              MessageDialog.openWarning(new Shell(),Dialog.Common.title,Dialog.Common.resourceRefreshError);
              System.err.println("String_Node_Str");
            }
 else {
              retryCount=0;
              ThreadUtil.sleep(1500);
              while (true) {
                try {
                  editorPart=IDE.openEditor(page,testCaseFile,editorId);
                  if (editorPart == null)                   throw new NullPointerException();
                  break;
                }
 catch (                Exception e) {
                  retryCount++;
                  if (retryCount > 3)                   break;
                  ThreadUtil.sleep(1500);
                }
              }
              editorPart.setFocus();
            }
          }
        }
 catch (        Exception e) {
          retryCount++;
          if (retryCount > 10)           break;
          e.printStackTrace();
          ThreadUtil.sleep(1500);
        }
        break;
      }
    }
  }
 catch (  Exception e) {
    e.printStackTrace();
  }
}

Fixed version (ground truth):
public void run(IAction action){
  PreferenceLoader pref=new PreferenceLoader();
  IWorkspaceRoot workspaceRoot=ResourcesPlugin.getWorkspace().getRoot();
  try {
    String projectName=null;
    String testTargetClassname=null;
    String testCaseFilename=null;
    String testCaseCreateFilepath=null;
    String testCaseResource=null;
    StructuredSelection structuredSelection=null;
    if (selection instanceof StructuredSelection) {
      structuredSelection=(StructuredSelection)selection;
    }
 else {
    }
    if (structuredSelection != null && structuredSelection.size() == 0) {
      MessageDialog.openWarning(new Shell(),Dialog.Common.title,Dialog.Common.required);
      return;
    }
 else     if (structuredSelection != null && structuredSelection.size() > 1) {
      MessageDialog.openWarning(new Shell(),Dialog.Common.title,Dialog.Common.selectOneOnly);
      return;
    }
    String pathFromProjectRoot=ResourcePathUtil.getPathStartsFromProjectRoot(structuredSelection);
    String[] dirArrFromProjectRoot=pathFromProjectRoot.split(StrConst.dirSep);
    String selected=StrConst.empty;
    int allDirLen=dirArrFromProjectRoot.length - 1;
    for (int i=2; i < allDirLen; i++)     selected+=dirArrFromProjectRoot[i] + StrConst.dirSep;
    selected+=dirArrFromProjectRoot[allDirLen];
    projectName=dirArrFromProjectRoot[1];
    String testTargetClassFilename=dirArrFromProjectRoot[dirArrFromProjectRoot.length - 1];
    testTargetClassname=testTargetClassFilename.replace(StrConst.javaFileExp,StrConst.empty);
    testCaseFilename=testTargetClassname + StrConst.suffixOfTestcase + StrConst.javaFileExp;
    String projectRootPath=workspaceRoot.getLocation() + StrConst.dirSep + projectName+ StrConst.dirSep;
    if (testTargetClassname == null) {
      MessageDialog.openWarning(new Shell(),Dialog.Common.title,Dialog.Common.selectJavaFile);
    }
 else {
      testCaseResource=selected.replace(pref.commonSrcMainJavaDir,pref.commonTestMainJavaDir).replace(StrConst.javaFileExp,StrConst.suffixOfTestcase + StrConst.javaFileExp);
      testCaseCreateFilepath=projectRootPath + selected.replace(pref.commonSrcMainJavaDir,pref.commonTestMainJavaDir).replace(StrConst.javaFileExp,StrConst.suffixOfTestcase + StrConst.javaFileExp);
      File outputFile=new File(testCaseCreateFilepath);
      if (!outputFile.exists()) {
        if (!pref.isTestClassGenEnabled) {
          MessageDialog.openInformation(new Shell(),Dialog.Common.title,Dialog.Common.notToCreateNewFilePreference);
          return;
        }
        String msg=Dialog.Common.notExist + "String_Node_Str" + testCaseFilename+ "String_Node_Str"+ StrConst.lineFeed+ Dialog.Common.confirmToCreateNewFile;
        if (testCaseFilename != null && MessageDialog.openConfirm(new Shell(),Dialog.Common.title,msg)) {
          new CreateNewTestCaseAction().run(action,selection);
        }
        return;
      }
      int retryCount=0;
      IEditorPart editorPart=null;
      while (true) {
        try {
          IProject project=workspaceRoot.getProject(projectName);
          IFile testCaseFile=project.getFile(testCaseResource);
          String editorId=IDE.getEditorDescriptor(testCaseFile.getName()).getId();
          IWorkbenchPage page=PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
          editorPart=IDE.openEditor(page,testCaseFile,editorId);
          editorPart.setFocus();
          if (pref.isTestMethodGenEnabled) {
            IFile testTargetFile=project.getFile(selected);
            ClassInfo testClassWithNotImpledMethods=TestCaseGenerateUtil.getClassInfoWithUnimplementedTestMethods(testTargetClassname,testTargetFile,testCaseFile);
            List<MethodInfo> unimpledTestMethodNames=testClassWithNotImpledMethods.methods;
            String writeEncoding=FileResourceUtil.detectEncoding(testCaseFile);
            if (unimpledTestMethodNames.size() > 0) {
              List<String> lines=SourceCodeParseUtil.getLineList(testCaseFile);
              StringBuilder sb=new StringBuilder();
              String CRLF=StrConst.carriageReturn + StrConst.lineFeed;
              boolean isImportedTypesAdded=false;
              for (              String line : lines) {
                if (line.equals("String_Node_Str") && pref.isTestMethodGenEnabled) {
                  for (                  MethodInfo testMethod : unimpledTestMethodNames) {
                    if (testMethod.testMethodName == null || testMethod.testMethodName.equals(StrConst.empty)) {
                      continue;
                    }
                    if (pref.isJUnitVersion4) {
                      sb.append("String_Node_Str");
                      sb.append(CRLF);
                    }
                    sb.append("String_Node_Str");
                    sb.append(testMethod.testMethodName);
                    sb.append("String_Node_Str");
                    sb.append(CRLF);
                    sb.append("String_Node_Str");
                    sb.append(StrConst.autoGenTodoMessage);
                    sb.append(CRLF);
                    if (pref.isTestMethodGenNotBlankEnabled) {
                      String notBlankSourceCode=TestCaseGenerateUtil.getNotBlankTestMethodSource(testMethod,testClassWithNotImpledMethods,testTargetClassname);
                      sb.append(notBlankSourceCode);
                    }
                    sb.append("String_Node_Str");
                    sb.append(CRLF);
                    sb.append(CRLF);
                  }
                  sb.append("String_Node_Str");
                  sb.append(CRLF);
                  break;
                }
 else {
                  sb.append(line);
                  sb.append(CRLF);
                  if (line.matches("String_Node_Str") && !isImportedTypesAdded && pref.isTestMethodGenNotBlankEnabled) {
                    List<String> importTypes=testClassWithNotImpledMethods.importList;
                    for (                    String importType : importTypes) {
                      if (importType.matches("String_Node_Str"))                       continue;
                      sb.append("String_Node_Str");
                      sb.append(importType);
                      sb.append("String_Node_Str");
                      sb.append(CRLF);
                    }
                    isImportedTypesAdded=true;
                  }
                }
              }
              String content=sb.toString();
              OutputStreamWriter testFileOSWriter=null;
              FileOutputStream fos=null;
              try {
                fos=new FileOutputStream(projectRootPath + StrConst.dirSep + testCaseResource);
                testFileOSWriter=new OutputStreamWriter(fos,writeEncoding);
                testFileOSWriter.write(content);
              }
  finally {
                FileResourceUtil.close(testFileOSWriter);
                FileResourceUtil.close(fos);
              }
            }
            if (!ResourceRefreshUtil.refreshLocal(null,projectName + StrConst.dirSep + testCaseResource+ "String_Node_Str")) {
              MessageDialog.openWarning(new Shell(),Dialog.Common.title,Dialog.Common.resourceRefreshError);
              System.err.println("String_Node_Str");
            }
 else {
              retryCount=0;
              ThreadUtil.sleep(1500);
              while (true) {
                try {
                  editorPart=IDE.openEditor(page,testCaseFile,editorId);
                  if (editorPart == null)                   throw new NullPointerException();
                  break;
                }
 catch (                Exception e) {
                  retryCount++;
                  if (retryCount > 3)                   break;
                  ThreadUtil.sleep(1500);
                }
              }
              editorPart.setFocus();
            }
          }
        }
 catch (        Exception e) {
          retryCount++;
          if (retryCount > 10)           break;
          e.printStackTrace();
          ThreadUtil.sleep(1500);
        }
        break;
      }
    }
  }
 catch (  Exception e) {
    e.printStackTrace();
  }
}