Program: 12094
Project: 12094
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(),STR.Dialog.Common.TITLE,STR.Dialog.Common.REQUIRED);
      return;
    }
 else     if (structuredSelection != null && structuredSelection.size() > 1) {
      MessageDialog.openWarning(new Shell(),STR.Dialog.Common.TITLE,STR.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(),STR.Dialog.Common.TITLE,STR.Dialog.TestCase.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()) {
        String msg=STR.Dialog.Common.NOT_EXIST + "String_Node_Str" + testCaseFilename+ "String_Node_Str"+ STR.LINE_FEED+ STR.Dialog.Common.COMFIRM_CREATE_NEW_FILE;
        if (testCaseFilename != null && MessageDialog.openConfirm(new Shell(),STR.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(STR.Preference.TestMethodGen.ENABLE)) {
            IFile testTargetFile=project.getFile(selected);
            List<GeneratingMethodInfo> unimpledTestMethodNames=TestCaseGenerateUtil.getUnimplementedTestMethodNames(testTargetFile,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);
                boolean enabledNotBlankMethods=Activator.getDefault().getPreferenceStore().getBoolean(STR.Preference.TestMethodGen.METHOD_SAMPLE_IMPLEMENTATION);
                StringBuffer sb=new StringBuffer();
                String CRLF=STR.CARRIAGE_RETURN + STR.LINE_FEED;
                for (                String line : lines) {
                  if (line.equals("String_Node_Str")) {
                    for (                    GeneratingMethodInfo testMethod : unimpledTestMethodNames) {
                      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,unimpledTestMethodNames,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);
                  }
                }
                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(),STR.Dialog.Common.TITLE,STR.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(),STR.Dialog.Common.TITLE,STR.Dialog.Common.REQUIRED);
      return;
    }
 else     if (structuredSelection != null && structuredSelection.size() > 1) {
      MessageDialog.openWarning(new Shell(),STR.Dialog.Common.TITLE,STR.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(),STR.Dialog.Common.TITLE,STR.Dialog.TestCase.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()) {
        String msg=STR.Dialog.Common.NOT_EXIST + "String_Node_Str" + testCaseFilename+ "String_Node_Str"+ STR.LINE_FEED+ STR.Dialog.Common.COMFIRM_CREATE_NEW_FILE;
        if (testCaseFilename != null && MessageDialog.openConfirm(new Shell(),STR.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(STR.Preference.TestMethodGen.ENABLE)) {
            IFile testTargetFile=project.getFile(selected);
            List<GeneratingMethodInfo> unimpledTestMethodNames=TestCaseGenerateUtil.getUnimplementedTestMethodNames(testTargetFile,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);
                boolean enabledTestMethodsGen=Activator.getDefault().getPreferenceStore().getBoolean(STR.Preference.TestMethodGen.ENABLE);
                boolean enabledNotBlankMethods=Activator.getDefault().getPreferenceStore().getBoolean(STR.Preference.TestMethodGen.METHOD_SAMPLE_IMPLEMENTATION);
                StringBuffer sb=new StringBuffer();
                String CRLF=STR.CARRIAGE_RETURN + STR.LINE_FEED;
                for (                String line : lines) {
                  if (line.equals("String_Node_Str") && enabledTestMethodsGen) {
                    for (                    GeneratingMethodInfo testMethod : unimpledTestMethodNames) {
                      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,unimpledTestMethodNames,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);
                  }
                }
                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(),STR.Dialog.Common.TITLE,STR.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();
  }
}