Program: 12060
Project: 12060
Buggy source:
/** 
 * Run method to invoked.
 * @param action
 */
public void run(IAction action){
  InputStream javaFileIStream=null;
  OutputStreamWriter testFileOSWriter=null;
  FileOutputStream fos=null;
  boolean refreshFlag=true;
  String projectName=null;
  String testCaseDirResource=null;
  String testCaseResource=null;
  try {
    IWorkspaceRoot workspaceRoot=ResourcesPlugin.getWorkspace().getRoot();
    String testTargetClassname=null;
    String testCaseFilename=null;
    String testCaseClassname=null;
    String testCaseCreateDirpath=null;
    StructuredSelection structuredSelection=null;
    if (selection instanceof StructuredSelection)     structuredSelection=(StructuredSelection)selection;
    if (structuredSelection != null && structuredSelection.size() == 0) {
      Shell shell=new Shell();
      MessageDialog.openWarning(shell,Dialog.Common.TITLE,Dialog.Common.REQUIRED);
      refreshFlag=false;
    }
 else     if (structuredSelection != null && structuredSelection.size() > 1) {
      Shell shell=new Shell();
      MessageDialog.openWarning(shell,Dialog.Common.TITLE,Dialog.Common.SELECT_ONLY_ONE);
      refreshFlag=false;
    }
 else {
      String pathFromProjectRoot=ResourcePathUtil.getPathStartsFromProjectRoot(structuredSelection);
      String[] dirArrFromProjectRoot=pathFromProjectRoot.split(STR.DIR_SEP);
      String selected=STR.EMPTY;
      int len=dirArrFromProjectRoot.length;
      for (int i=2; i < len - 1; i++)       selected+=dirArrFromProjectRoot[i] + STR.DIR_SEP;
      selected+=dirArrFromProjectRoot[len - 1];
      projectName=dirArrFromProjectRoot[1];
      String testTargetClassFilename=dirArrFromProjectRoot[dirArrFromProjectRoot.length - 1];
      testTargetClassname=testTargetClassFilename.replace(STR.JAVA_EXP,STR.EMPTY);
      testCaseClassname=testTargetClassname + STR.SUFFIX_OF_TESTCASE;
      testCaseFilename=testCaseClassname + STR.JAVA_EXP;
      String projectRootPath=workspaceRoot.getLocation() + STR.DIR_SEP + projectName+ STR.DIR_SEP;
      testCaseResource=selected.replace(STR.SRC_MAIN_JAVA,STR.SRC_TEST_JAVA).replace(STR.JAVA_EXP,STR.SUFFIX_OF_TESTCASE + STR.JAVA_EXP);
      String[] selectedDirArr=selected.split(STR.DIR_SEP);
      testCaseDirResource="String_Node_Str";
      int selectedDirArrLength=selectedDirArr.length;
      for (int i=0; i < selectedDirArrLength - 1; i++)       testCaseDirResource+=selectedDirArr[i] + STR.DIR_SEP;
      testCaseDirResource=testCaseDirResource.replace(STR.SRC_MAIN_JAVA,STR.SRC_TEST_JAVA);
      testCaseCreateDirpath=projectRootPath + testCaseDirResource;
      File testDir=new File(testCaseCreateDirpath);
      String[] dirArr=testCaseCreateDirpath.split(STR.DIR_SEP);
      String tmpDirPath=STR.EMPTY;
      String tmpResourceDirPath=STR.EMPTY;
      for (      String each : dirArr) {
        tmpDirPath+=STR.DIR_SEP + each;
        File tmpDir=new File(tmpDirPath);
        if (tmpDir.getPath().length() <= projectRootPath.length())         continue;
        tmpResourceDirPath+=STR.DIR_SEP + each;
        if (!tmpDir.exists()) {
          if (!tmpDir.mkdir())           System.err.println("String_Node_Str" + tmpDir.getPath());
          if (!ResourceRefreshUtil.refreshLocal(null,projectName + STR.DIR_SEP + tmpResourceDirPath+ "String_Node_Str")) {
            String msg=Dialog.Common.RESOURCE_REFRESH_ERROR;
            MessageDialog.openWarning(new Shell(),Dialog.Common.TITLE,msg);
            System.err.println("String_Node_Str");
          }
        }
      }
      if (!testDir.mkdirs())       System.err.println("String_Node_Str");
      if (!ResourceRefreshUtil.refreshLocal(null,projectName + STR.DIR_SEP + testCaseDirResource)) {
        MessageDialog.openWarning(new Shell(),Dialog.Common.TITLE,Dialog.Common.RESOURCE_REFRESH_ERROR);
        System.err.println("String_Node_Str");
      }
      try {
        File outputFile=new File(testCaseCreateDirpath + STR.DIR_SEP + testCaseFilename);
        String msg=Dialog.Common.ALREADY_EXIST + "String_Node_Str" + testCaseFilename+ "String_Node_Str"+ STR.LINE_FEED+ Dialog.Common.CONFIRM_PROCEED;
        if (!outputFile.exists() || MessageDialog.openConfirm(new Shell(),Dialog.Common.TITLE,msg)) {
          String targetClass="String_Node_Str" + projectName + "String_Node_Str"+ selected;
          IResource targetClassResource=workspaceRoot.findMember(targetClass);
          IFile file=(IFile)targetClassResource;
          ClassInfo testClassInfo=TestCaseGenerateUtil.getTestClassInfoFromTargetClass(file);
          List<MethodInfo> testMethods=testClassInfo.methods;
          String writeEncoding=FileResourceUtil.detectEncoding(file);
          fos=new FileOutputStream(testCaseCreateDirpath + STR.DIR_SEP + testCaseFilename);
          testFileOSWriter=new OutputStreamWriter(fos,writeEncoding);
          StringBuilder sb=new StringBuilder();
          String CRLF=STR.CARRIAGE_RETURN + STR.LINE_FEED;
          String testPackageString=STR.EMPTY;
          String[] tmpDirArr=selected.split(STR.DIR_SEP);
          StringBuilder dirSb=new StringBuilder();
          int packageArrLen=tmpDirArr.length - 2;
          int mainJavaLen=STR.SRC_MAIN_JAVA.split("String_Node_Str").length;
          for (int i=mainJavaLen; i < packageArrLen; i++) {
            dirSb.append(tmpDirArr[i]);
            dirSb.append("String_Node_Str");
          }
          dirSb.append(tmpDirArr[packageArrLen]);
          testPackageString=dirSb.toString();
          sb.append("String_Node_Str");
          sb.append(testPackageString);
          sb.append("String_Node_Str");
          sb.append(CRLF);
          sb.append(CRLF);
          IPreferenceStore store=Activator.getDefault().getPreferenceStore();
          boolean usingJUnitHelperRuntime=store.getBoolean(Preference.TestClassGen.USING_JUNIT_HELPER_RUNTIME_LIB);
          String testCase=usingJUnitHelperRuntime ? RuntimeLibrary.TEST_CASE : store.getString(Preference.TestClassGen.CLASS_TO_EXTEND);
          String[] tmpTestCaseArr=testCase.split("String_Node_Str");
          String testCaseName=tmpTestCaseArr[tmpTestCaseArr.length - 1];
          sb.append("String_Node_Str");
          sb.append(testCase);
          sb.append("String_Node_Str");
          sb.append(CRLF);
          boolean enabledTestMethodsGen=Activator.getDefault().getPreferenceStore().getBoolean(Preference.TestMethodGen.ENABLE);
          boolean enabledNotBlankMethods=Activator.getDefault().getPreferenceStore().getBoolean(Preference.TestMethodGen.METHOD_SAMPLE_IMPL);
          if (enabledTestMethodsGen && enabledNotBlankMethods) {
            List<String> importedPackageList=testClassInfo.importList;
            for (            String importedPackage : importedPackageList) {
              sb.append("String_Node_Str");
              sb.append(importedPackage);
              sb.append("String_Node_Str");
              sb.append(CRLF);
            }
          }
          sb.append(CRLF);
          sb.append("String_Node_Str");
          sb.append(testCaseClassname);
          sb.append("String_Node_Str");
          sb.append(testCaseName);
          sb.append("String_Node_Str");
          sb.append(CRLF);
          sb.append(CRLF);
          if (enabledTestMethodsGen) {
            for (            MethodInfo testMethod : testMethods) {
              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,testClassInfo,testTargetClassname);
                sb.append(notBlankSourceCode);
              }
              sb.append("String_Node_Str");
              sb.append(CRLF);
              sb.append(CRLF);
            }
          }
          sb.append("String_Node_Str");
          sb.append(CRLF);
          testFileOSWriter.write(sb.toString());
        }
      }
 catch (      FileNotFoundException fnfe) {
        fnfe.printStackTrace();
      }
 finally {
        FileResourceUtil.close(testFileOSWriter);
        FileResourceUtil.close(fos);
      }
    }
  }
 catch (  Exception e) {
    e.printStackTrace();
  }
 finally {
    FileResourceUtil.close(javaFileIStream);
    FileResourceUtil.close(testFileOSWriter);
  }
  if (refreshFlag && !ResourceRefreshUtil.refreshLocal(null,projectName + STR.DIR_SEP + testCaseDirResource+ "String_Node_Str")) {
    MessageDialog.openWarning(new Shell(),Dialog.Common.TITLE,Dialog.Common.RESOURCE_REFRESH_ERROR);
    System.err.println("String_Node_Str");
  }
 else {
    int retryCount=0;
    IEditorPart editorPart=null;
    ThreadUtil.sleep(1500);
    while (true) {
      try {
        IWorkspace workspace=ResourcesPlugin.getWorkspace();
        IWorkspaceRoot root=workspace.getRoot();
        IProject project=root.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);
        if (editorPart == null)         throw new NullPointerException();
        break;
      }
 catch (      Exception e) {
        retryCount++;
        if (retryCount > 3)         break;
        ThreadUtil.sleep(1500);
      }
    }
    editorPart.setFocus();
  }
}

Fixed version (ground truth):
/** 
 * Run method to invoked.
 * @param action
 */
public void run(IAction action){
  PreferenceLoader pref=new PreferenceLoader();
  InputStream javaFileIStream=null;
  OutputStreamWriter testFileOSWriter=null;
  FileOutputStream fos=null;
  boolean refreshFlag=true;
  String projectName=null;
  String testCaseDirResource=null;
  String testCaseResource=null;
  try {
    IWorkspaceRoot workspaceRoot=ResourcesPlugin.getWorkspace().getRoot();
    String testTargetClassname=null;
    String testCaseFilename=null;
    String testCaseClassname=null;
    String testCaseCreateDirpath=null;
    StructuredSelection structuredSelection=null;
    if (selection instanceof StructuredSelection)     structuredSelection=(StructuredSelection)selection;
    if (structuredSelection != null && structuredSelection.size() == 0) {
      Shell shell=new Shell();
      MessageDialog.openWarning(shell,Dialog.Common.TITLE,Dialog.Common.REQUIRED);
      refreshFlag=false;
    }
 else     if (structuredSelection != null && structuredSelection.size() > 1) {
      Shell shell=new Shell();
      MessageDialog.openWarning(shell,Dialog.Common.TITLE,Dialog.Common.SELECT_ONLY_ONE);
      refreshFlag=false;
    }
 else {
      String pathFromProjectRoot=ResourcePathUtil.getPathStartsFromProjectRoot(structuredSelection);
      String[] dirArrFromProjectRoot=pathFromProjectRoot.split(STR.DIR_SEP);
      String selected=STR.EMPTY;
      int len=dirArrFromProjectRoot.length;
      for (int i=2; i < len - 1; i++)       selected+=dirArrFromProjectRoot[i] + STR.DIR_SEP;
      selected+=dirArrFromProjectRoot[len - 1];
      projectName=dirArrFromProjectRoot[1];
      String testTargetClassFilename=dirArrFromProjectRoot[dirArrFromProjectRoot.length - 1];
      testTargetClassname=testTargetClassFilename.replace(STR.JAVA_EXP,STR.EMPTY);
      testCaseClassname=testTargetClassname + STR.SUFFIX_OF_TESTCASE;
      testCaseFilename=testCaseClassname + STR.JAVA_EXP;
      String projectRootPath=workspaceRoot.getLocation() + STR.DIR_SEP + projectName+ STR.DIR_SEP;
      testCaseResource=selected.replace(STR.SRC_MAIN_JAVA,STR.SRC_TEST_JAVA).replace(STR.JAVA_EXP,STR.SUFFIX_OF_TESTCASE + STR.JAVA_EXP);
      String[] selectedDirArr=selected.split(STR.DIR_SEP);
      testCaseDirResource="String_Node_Str";
      int selectedDirArrLength=selectedDirArr.length;
      for (int i=0; i < selectedDirArrLength - 1; i++)       testCaseDirResource+=selectedDirArr[i] + STR.DIR_SEP;
      testCaseDirResource=testCaseDirResource.replace(STR.SRC_MAIN_JAVA,STR.SRC_TEST_JAVA);
      testCaseCreateDirpath=projectRootPath + testCaseDirResource;
      File testDir=new File(testCaseCreateDirpath);
      String[] dirArr=testCaseCreateDirpath.split(STR.DIR_SEP);
      String tmpDirPath=STR.EMPTY;
      String tmpResourceDirPath=STR.EMPTY;
      for (      String each : dirArr) {
        tmpDirPath+=STR.DIR_SEP + each;
        File tmpDir=new File(tmpDirPath);
        if (tmpDir.getPath().length() <= projectRootPath.length())         continue;
        tmpResourceDirPath+=STR.DIR_SEP + each;
        if (!tmpDir.exists()) {
          if (!tmpDir.mkdir())           System.err.println("String_Node_Str" + tmpDir.getPath());
          if (!ResourceRefreshUtil.refreshLocal(null,projectName + STR.DIR_SEP + tmpResourceDirPath+ "String_Node_Str")) {
            String msg=Dialog.Common.RESOURCE_REFRESH_ERROR;
            MessageDialog.openWarning(new Shell(),Dialog.Common.TITLE,msg);
            System.err.println("String_Node_Str");
          }
        }
      }
      if (!testDir.mkdirs())       System.err.println("String_Node_Str");
      if (!ResourceRefreshUtil.refreshLocal(null,projectName + STR.DIR_SEP + testCaseDirResource)) {
        MessageDialog.openWarning(new Shell(),Dialog.Common.TITLE,Dialog.Common.RESOURCE_REFRESH_ERROR);
        System.err.println("String_Node_Str");
      }
      try {
        File outputFile=new File(testCaseCreateDirpath + STR.DIR_SEP + testCaseFilename);
        String msg=Dialog.Common.ALREADY_EXIST + "String_Node_Str" + testCaseFilename+ "String_Node_Str"+ STR.LINE_FEED+ Dialog.Common.CONFIRM_PROCEED;
        if (!outputFile.exists() || MessageDialog.openConfirm(new Shell(),Dialog.Common.TITLE,msg)) {
          String targetClass="String_Node_Str" + projectName + "String_Node_Str"+ selected;
          IResource targetClassResource=workspaceRoot.findMember(targetClass);
          IFile file=(IFile)targetClassResource;
          ClassInfo testClassInfo=TestCaseGenerateUtil.getTestClassInfoFromTargetClass(file);
          List<MethodInfo> testMethods=testClassInfo.methods;
          String writeEncoding=FileResourceUtil.detectEncoding(file);
          fos=new FileOutputStream(testCaseCreateDirpath + STR.DIR_SEP + testCaseFilename);
          testFileOSWriter=new OutputStreamWriter(fos,writeEncoding);
          StringBuilder sb=new StringBuilder();
          String CRLF=STR.CARRIAGE_RETURN + STR.LINE_FEED;
          String testPackageString=STR.EMPTY;
          String[] tmpDirArr=selected.split(STR.DIR_SEP);
          StringBuilder dirSb=new StringBuilder();
          int packageArrLen=tmpDirArr.length - 2;
          int mainJavaLen=STR.SRC_MAIN_JAVA.split("String_Node_Str").length;
          for (int i=mainJavaLen; i < packageArrLen; i++) {
            dirSb.append(tmpDirArr[i]);
            dirSb.append("String_Node_Str");
          }
          dirSb.append(tmpDirArr[packageArrLen]);
          testPackageString=dirSb.toString();
          sb.append("String_Node_Str");
          sb.append(testPackageString);
          sb.append("String_Node_Str");
          sb.append(CRLF);
          sb.append(CRLF);
          String testCase=pref.isUsingJUnitHelperRuntime ? RuntimeLibrary.TEST_CASE : pref.classToExtend;
          String[] tmpTestCaseArr=testCase.split("String_Node_Str");
          String testCaseName=tmpTestCaseArr[tmpTestCaseArr.length - 1];
          sb.append("String_Node_Str");
          sb.append(testCase);
          sb.append("String_Node_Str");
          sb.append(CRLF);
          if (pref.isTestMethodGenEnabled && pref.isTestMethodGenNotBlankEnabled) {
            List<String> importedPackageList=testClassInfo.importList;
            for (            String importedPackage : importedPackageList) {
              sb.append("String_Node_Str");
              sb.append(importedPackage);
              sb.append("String_Node_Str");
              sb.append(CRLF);
            }
          }
          sb.append(CRLF);
          sb.append("String_Node_Str");
          sb.append(testCaseClassname);
          if (pref.isJUnitVersion3) {
            sb.append("String_Node_Str");
            sb.append(testCaseName);
          }
          sb.append("String_Node_Str");
          sb.append(CRLF);
          sb.append(CRLF);
          if (pref.isTestMethodGenEnabled) {
            for (            MethodInfo testMethod : testMethods) {
              if (testMethod.testMethodName == null || testMethod.testMethodName.equals(STR.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(STR.AUTO_GEN_MSG_TODO);
              sb.append(CRLF);
              if (pref.isTestMethodGenNotBlankEnabled) {
                String notBlankSourceCode=TestCaseGenerateUtil.getNotBlankTestMethodSource(testMethod,testClassInfo,testTargetClassname);
                sb.append(notBlankSourceCode);
              }
              sb.append("String_Node_Str");
              sb.append(CRLF);
              sb.append(CRLF);
            }
          }
          sb.append("String_Node_Str");
          sb.append(CRLF);
          testFileOSWriter.write(sb.toString());
        }
      }
 catch (      FileNotFoundException fnfe) {
        fnfe.printStackTrace();
      }
 finally {
        FileResourceUtil.close(testFileOSWriter);
        FileResourceUtil.close(fos);
      }
    }
  }
 catch (  Exception e) {
    e.printStackTrace();
  }
 finally {
    FileResourceUtil.close(javaFileIStream);
    FileResourceUtil.close(testFileOSWriter);
  }
  if (refreshFlag && !ResourceRefreshUtil.refreshLocal(null,projectName + STR.DIR_SEP + testCaseDirResource+ "String_Node_Str")) {
    MessageDialog.openWarning(new Shell(),Dialog.Common.TITLE,Dialog.Common.RESOURCE_REFRESH_ERROR);
    System.err.println("String_Node_Str");
  }
 else {
    int retryCount=0;
    IEditorPart editorPart=null;
    ThreadUtil.sleep(1500);
    while (true) {
      try {
        IWorkspace workspace=ResourcesPlugin.getWorkspace();
        IWorkspaceRoot root=workspace.getRoot();
        IProject project=root.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);
        if (editorPart == null)         throw new NullPointerException();
        break;
      }
 catch (      Exception e) {
        retryCount++;
        if (retryCount > 3)         break;
        ThreadUtil.sleep(1500);
      }
    }
    editorPart.setFocus();
  }
}