Program: 12046
Project: 12046
Buggy source:
/** 
 * 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.selectOneOnly);
      refreshFlag=false;
    }
 else {
      String pathFromProjectRoot=ResourcePathUtil.getPathStartsFromProjectRoot(structuredSelection);
      String[] dirArrFromProjectRoot=pathFromProjectRoot.split(StrConst.dirSep);
      String selected=StrConst.empty;
      int len=dirArrFromProjectRoot.length;
      for (int i=2; i < len - 1; i++)       selected+=dirArrFromProjectRoot[i] + StrConst.dirSep;
      selected+=dirArrFromProjectRoot[len - 1];
      projectName=dirArrFromProjectRoot[1];
      String testTargetClassFilename=dirArrFromProjectRoot[dirArrFromProjectRoot.length - 1];
      testTargetClassname=testTargetClassFilename.replace(StrConst.javaFileExp,StrConst.empty);
      testCaseClassname=testTargetClassname + StrConst.suffixOfTestcase;
      testCaseFilename=testCaseClassname + StrConst.javaFileExp;
      String projectRootPath=workspaceRoot.getLocation() + StrConst.dirSep + projectName+ StrConst.dirSep;
      testCaseResource=selected.replace(pref.commonSrcMainJavaDir,pref.commonTestMainJavaDir).replace(StrConst.javaFileExp,StrConst.suffixOfTestcase + StrConst.javaFileExp);
      String[] selectedDirArr=selected.split(StrConst.dirSep);
      testCaseDirResource="String_Node_Str";
      int selectedDirArrLength=selectedDirArr.length;
      for (int i=0; i < selectedDirArrLength - 1; i++)       testCaseDirResource+=selectedDirArr[i] + StrConst.dirSep;
      testCaseDirResource=testCaseDirResource.replace(pref.commonSrcMainJavaDir,pref.commonTestMainJavaDir);
      testCaseCreateDirpath=projectRootPath + testCaseDirResource;
      File testDir=new File(testCaseCreateDirpath);
      String[] dirArr=testCaseCreateDirpath.split(StrConst.dirSep);
      String tmpDirPath=StrConst.empty;
      String tmpResourceDirPath=StrConst.empty;
      for (      String each : dirArr) {
        tmpDirPath+=StrConst.dirSep + each;
        File tmpDir=new File(tmpDirPath);
        if (tmpDir.getPath().length() <= projectRootPath.length()) {
          continue;
        }
        tmpResourceDirPath+=StrConst.dirSep + each;
        if (!tmpDir.exists()) {
          if (!tmpDir.mkdir()) {
            System.err.println("String_Node_Str" + tmpDir.getPath());
          }
          if (!ResourceRefreshUtil.refreshLocal(null,projectName + StrConst.dirSep + tmpResourceDirPath+ "String_Node_Str")) {
            String msg=Dialog.Common.resourceRefreshError;
            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 + StrConst.dirSep + testCaseDirResource)) {
        MessageDialog.openWarning(new Shell(),Dialog.Common.title,Dialog.Common.resourceRefreshError);
        System.err.println("String_Node_Str");
      }
      try {
        File outputFile=new File(testCaseCreateDirpath + StrConst.dirSep + testCaseFilename);
        String msg=Dialog.Common.alreadyExist + "String_Node_Str" + testCaseFilename+ "String_Node_Str"+ StrConst.lineFeed+ Dialog.Common.confirmToProceed;
        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 + StrConst.dirSep + testCaseFilename);
          testFileOSWriter=new OutputStreamWriter(fos,writeEncoding);
          StringBuilder sb=new StringBuilder();
          String CRLF=StrConst.carriageReturn + StrConst.lineFeed;
          String testPackageString=StrConst.empty;
          String[] tmpDirArr=selected.split(StrConst.dirSep);
          StringBuilder dirSb=new StringBuilder();
          int packageArrLen=tmpDirArr.length - 2;
          int mainJavaLen=pref.commonSrcMainJavaDir.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.testcase : 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(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,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 (      InvalidPreferenceException ipe) {
        MessageDialog.openWarning(new Shell(),Dialog.Common.title,Dialog.Common.invalidPreference);
        return;
      }
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 + StrConst.dirSep + testCaseDirResource+ "String_Node_Str")) {
    MessageDialog.openWarning(new Shell(),Dialog.Common.title,Dialog.Common.resourceRefreshError);
    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.selectOneOnly);
      refreshFlag=false;
    }
 else {
      String pathFromProjectRoot=ResourcePathUtil.getPathStartsFromProjectRoot(structuredSelection);
      String[] dirArrFromProjectRoot=pathFromProjectRoot.split(StrConst.dirSep);
      String selected=StrConst.empty;
      int len=dirArrFromProjectRoot.length;
      for (int i=2; i < len - 1; i++)       selected+=dirArrFromProjectRoot[i] + StrConst.dirSep;
      selected+=dirArrFromProjectRoot[len - 1];
      projectName=dirArrFromProjectRoot[1];
      String testTargetClassFilename=dirArrFromProjectRoot[dirArrFromProjectRoot.length - 1];
      testTargetClassname=testTargetClassFilename.replace(StrConst.javaFileExp,StrConst.empty);
      testCaseClassname=testTargetClassname + StrConst.suffixOfTestcase;
      testCaseFilename=testCaseClassname + StrConst.javaFileExp;
      String projectRootPath=workspaceRoot.getLocation() + StrConst.dirSep + projectName+ StrConst.dirSep;
      testCaseResource=selected.replace(pref.commonSrcMainJavaDir,pref.commonTestMainJavaDir).replace(StrConst.javaFileExp,StrConst.suffixOfTestcase + StrConst.javaFileExp);
      String[] selectedDirArr=selected.split(StrConst.dirSep);
      testCaseDirResource="String_Node_Str";
      int selectedDirArrLength=selectedDirArr.length;
      for (int i=0; i < selectedDirArrLength - 1; i++)       testCaseDirResource+=selectedDirArr[i] + StrConst.dirSep;
      testCaseDirResource=testCaseDirResource.replace(pref.commonSrcMainJavaDir,pref.commonTestMainJavaDir);
      testCaseCreateDirpath=projectRootPath + testCaseDirResource;
      File testDir=new File(testCaseCreateDirpath);
      String[] dirArr=testCaseCreateDirpath.split(StrConst.dirSep);
      String tmpDirPath=StrConst.empty;
      String tmpResourceDirPath=StrConst.empty;
      for (      String each : dirArr) {
        tmpDirPath+=StrConst.dirSep + each;
        File tmpDir=new File(tmpDirPath);
        if (tmpDir.getPath().length() <= projectRootPath.length()) {
          continue;
        }
        tmpResourceDirPath+=StrConst.dirSep + each;
        if (!tmpDir.exists()) {
          if (!tmpDir.mkdir()) {
            System.err.println("String_Node_Str" + tmpDir.getPath());
          }
          if (!ResourceRefreshUtil.refreshLocal(null,projectName + StrConst.dirSep + tmpResourceDirPath+ "String_Node_Str")) {
            String msg=Dialog.Common.resourceRefreshError;
            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 + StrConst.dirSep + testCaseDirResource)) {
        MessageDialog.openWarning(new Shell(),Dialog.Common.title,Dialog.Common.resourceRefreshError);
        System.err.println("String_Node_Str");
      }
      try {
        File outputFile=new File(testCaseCreateDirpath + StrConst.dirSep + testCaseFilename);
        String msg=Dialog.Common.alreadyExist + "String_Node_Str" + testCaseFilename+ "String_Node_Str"+ StrConst.lineFeed+ Dialog.Common.confirmToProceed;
        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 + StrConst.dirSep + testCaseFilename);
          testFileOSWriter=new OutputStreamWriter(fos,writeEncoding);
          StringBuilder sb=new StringBuilder();
          String CRLF=StrConst.carriageReturn + StrConst.lineFeed;
          String testPackageString=StrConst.empty;
          String[] tmpDirArr=selected.split(StrConst.dirSep);
          StringBuilder dirSb=new StringBuilder();
          int packageArrLen=tmpDirArr.length - 2;
          int mainJavaLen=pref.commonSrcMainJavaDir.split("String_Node_Str").length;
          if (mainJavaLen != tmpDirArr.length - 1) {
            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.testcase : 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) {
              importedPackage=importedPackage.trim();
              if (importedPackage != null && importedPackage.length() != 0) {
                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(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,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 (      InvalidPreferenceException ipe) {
        MessageDialog.openWarning(new Shell(),Dialog.Common.title,Dialog.Common.invalidPreference);
        return;
      }
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 + StrConst.dirSep + testCaseDirResource+ "String_Node_Str")) {
    MessageDialog.openWarning(new Shell(),Dialog.Common.title,Dialog.Common.resourceRefreshError);
    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();
  }
}