Program: 12063
Project: 12063
Buggy source:
public void run(IAction action){
  try {
    IWorkspaceRoot workspaceRoot=ResourcesPlugin.getWorkspace().getRoot();
    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.SELECT_ONLY_ONE);
      return;
    }
    String pathFromProjectRoot=ResourcePathUtil.getPathStartsFromProjectRoot(structuredSelection);
    String[] dirArrFromProjectRoot=pathFromProjectRoot.split(STR.DIR_SEP);
    String selected=STR.EMPTY;
    int allDirLen=dirArrFromProjectRoot.length - 1;
    for (int i=2; i < allDirLen; i++)     selected+=dirArrFromProjectRoot[i] + STR.DIR_SEP;
    selected+=dirArrFromProjectRoot[allDirLen];
    projectName=dirArrFromProjectRoot[1];
    String testTargetClassFilename=dirArrFromProjectRoot[dirArrFromProjectRoot.length - 1];
    testTargetClassname=testTargetClassFilename.replace(STR.JAVA_EXP,STR.EMPTY);
    testCaseFilename=testTargetClassname + STR.SUFFIX_OF_TESTCASE + STR.JAVA_EXP;
    String projectRootPath=workspaceRoot.getLocation() + STR.DIR_SEP + projectName+ STR.DIR_SEP;
    if (testTargetClassname == null) {
      MessageDialog.openWarning(new Shell(),Dialog.Common.TITLE,Dialog.Common.SELECT_JAVA_FILE);
    }
 else {
      testCaseResource=selected.replace(STR.SRC_MAIN_JAVA,STR.SRC_TEST_JAVA).replace(STR.JAVA_EXP,STR.SUFFIX_OF_TESTCASE + STR.JAVA_EXP);
      testCaseCreateFilepath=projectRootPath + selected.replace(STR.SRC_MAIN_JAVA,STR.SRC_TEST_JAVA).replace(STR.JAVA_EXP,STR.SUFFIX_OF_TESTCASE + STR.JAVA_EXP);
      File outputFile=new File(testCaseCreateFilepath);
      if (!outputFile.exists()) {
        boolean testClassGen=Activator.getDefault().getPreferenceStore().getBoolean(Preference.TestClassGen.ENABLE);
        if (!testClassGen) {
          MessageDialog.openInformation(new Shell(),Dialog.Common.TITLE,Dialog.Common.NOT_CREATE_NEW_FILE);
          return;
        }
        String msg=Dialog.Common.NOT_EXIST + "String_Node_Str" + testCaseFilename+ "String_Node_Str"+ STR.LINE_FEED+ Dialog.Common.COMFIRM_CREATE_NEW_FILE;
        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 (Activator.getDefault().getPreferenceStore().getBoolean(Preference.TestMethodGen.ENABLE)) {
            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=TestCaseGenerateUtil.getAllSourceCodeLineList(testCaseFile);
              OutputStreamWriter testFileOSWriter=null;
              FileOutputStream fos=null;
              try {
                fos=new FileOutputStream(projectRootPath + STR.DIR_SEP + testCaseResource);
                testFileOSWriter=new OutputStreamWriter(fos,writeEncoding);
                boolean enabledTestMethodsGen=Activator.getDefault().getPreferenceStore().getBoolean(Preference.TestMethodGen.ENABLE);
                boolean enabledNotBlankMethods=Activator.getDefault().getPreferenceStore().getBoolean(Preference.TestMethodGen.METHOD_SAMPLE_IMPLEMENTATION);
                StringBuilder sb=new StringBuilder();
                String CRLF=STR.CARRIAGE_RETURN + STR.LINE_FEED;
                boolean isImportedTypesAdded=false;
                for (                String line : lines) {
                  if (line.equals("String_Node_Str") && enabledTestMethodsGen) {
                    for (                    MethodInfo testMethod : unimpledTestMethodNames) {
                      if (testMethod.testMethodName == null || testMethod.testMethodName.equals(STR.EMPTY))                       continue;
                      sb.append("String_Node_Str");
                      sb.append(testMethod.testMethodName);
                      sb.append("String_Node_Str");
                      sb.append(CRLF);
                      sb.append("String_Node_Str");
                      sb.append(STR.AUTO_GEN_MSG_TODO);
                      sb.append(CRLF);
                      if (enabledNotBlankMethods) {
                        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 && enabledNotBlankMethods) {
                      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();
                testFileOSWriter.write(content);
              }
  finally {
                FileResourceUtil.close(testFileOSWriter);
                FileResourceUtil.close(fos);
              }
            }
            if (!ResourceRefreshUtil.refreshLocal(null,projectName + STR.DIR_SEP + testCaseResource+ "String_Node_Str")) {
              MessageDialog.openWarning(new Shell(),Dialog.Common.TITLE,Dialog.Common.RESOURCE_REFRESH_ERROR);
              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){
  try {
    IWorkspaceRoot workspaceRoot=ResourcesPlugin.getWorkspace().getRoot();
    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.SELECT_ONLY_ONE);
      return;
    }
    String pathFromProjectRoot=ResourcePathUtil.getPathStartsFromProjectRoot(structuredSelection);
    String[] dirArrFromProjectRoot=pathFromProjectRoot.split(STR.DIR_SEP);
    String selected=STR.EMPTY;
    int allDirLen=dirArrFromProjectRoot.length - 1;
    for (int i=2; i < allDirLen; i++)     selected+=dirArrFromProjectRoot[i] + STR.DIR_SEP;
    selected+=dirArrFromProjectRoot[allDirLen];
    projectName=dirArrFromProjectRoot[1];
    String testTargetClassFilename=dirArrFromProjectRoot[dirArrFromProjectRoot.length - 1];
    testTargetClassname=testTargetClassFilename.replace(STR.JAVA_EXP,STR.EMPTY);
    testCaseFilename=testTargetClassname + STR.SUFFIX_OF_TESTCASE + STR.JAVA_EXP;
    String projectRootPath=workspaceRoot.getLocation() + STR.DIR_SEP + projectName+ STR.DIR_SEP;
    if (testTargetClassname == null) {
      MessageDialog.openWarning(new Shell(),Dialog.Common.TITLE,Dialog.Common.SELECT_JAVA_FILE);
    }
 else {
      testCaseResource=selected.replace(STR.SRC_MAIN_JAVA,STR.SRC_TEST_JAVA).replace(STR.JAVA_EXP,STR.SUFFIX_OF_TESTCASE + STR.JAVA_EXP);
      testCaseCreateFilepath=projectRootPath + selected.replace(STR.SRC_MAIN_JAVA,STR.SRC_TEST_JAVA).replace(STR.JAVA_EXP,STR.SUFFIX_OF_TESTCASE + STR.JAVA_EXP);
      File outputFile=new File(testCaseCreateFilepath);
      if (!outputFile.exists()) {
        boolean testClassGen=Activator.getDefault().getPreferenceStore().getBoolean(Preference.TestClassGen.ENABLE);
        if (!testClassGen) {
          MessageDialog.openInformation(new Shell(),Dialog.Common.TITLE,Dialog.Common.NOT_CREATE_NEW_FILE);
          return;
        }
        String msg=Dialog.Common.NOT_EXIST + "String_Node_Str" + testCaseFilename+ "String_Node_Str"+ STR.LINE_FEED+ Dialog.Common.COMFIRM_CREATE_NEW_FILE;
        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 (Activator.getDefault().getPreferenceStore().getBoolean(Preference.TestMethodGen.ENABLE)) {
            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=TestCaseGenerateUtil.getAllSourceCodeLineList(testCaseFile);
              OutputStreamWriter testFileOSWriter=null;
              FileOutputStream fos=null;
              try {
                fos=new FileOutputStream(projectRootPath + STR.DIR_SEP + testCaseResource);
                testFileOSWriter=new OutputStreamWriter(fos,writeEncoding);
                boolean enabledTestMethodsGen=Activator.getDefault().getPreferenceStore().getBoolean(Preference.TestMethodGen.ENABLE);
                boolean enabledNotBlankMethods=Activator.getDefault().getPreferenceStore().getBoolean(Preference.TestMethodGen.METHOD_SAMPLE_IMPL);
                StringBuilder sb=new StringBuilder();
                String CRLF=STR.CARRIAGE_RETURN + STR.LINE_FEED;
                boolean isImportedTypesAdded=false;
                for (                String line : lines) {
                  if (line.equals("String_Node_Str") && enabledTestMethodsGen) {
                    for (                    MethodInfo testMethod : unimpledTestMethodNames) {
                      if (testMethod.testMethodName == null || testMethod.testMethodName.equals(STR.EMPTY))                       continue;
                      sb.append("String_Node_Str");
                      sb.append(testMethod.testMethodName);
                      sb.append("String_Node_Str");
                      sb.append(CRLF);
                      sb.append("String_Node_Str");
                      sb.append(STR.AUTO_GEN_MSG_TODO);
                      sb.append(CRLF);
                      if (enabledNotBlankMethods) {
                        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 && enabledNotBlankMethods) {
                      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();
                testFileOSWriter.write(content);
              }
  finally {
                FileResourceUtil.close(testFileOSWriter);
                FileResourceUtil.close(fos);
              }
            }
            if (!ResourceRefreshUtil.refreshLocal(null,projectName + STR.DIR_SEP + testCaseResource+ "String_Node_Str")) {
              MessageDialog.openWarning(new Shell(),Dialog.Common.TITLE,Dialog.Common.RESOURCE_REFRESH_ERROR);
              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();
  }
}