Github user FSchumacher commented on a diff in the pull request: https://github.com/apache/jmeter/pull/432#discussion_r236800696 --- Diff: src/core/org/apache/jmeter/gui/action/SelectTemplatesDialog.java --- @@ -137,67 +160,138 @@ private void checkDirtyAndLoad(final ActionEvent actionEvent) if (template == null) { return; } + templateList.setValues(TemplateManager.getInstance().reset().getTemplateNames()); // reload the templates before loading + final boolean isTestPlan = template.isTestPlan(); // Check if the user wants to drop any changes - if (isTestPlan) { - ActionRouter.getInstance().doActionNow(new ActionEvent(actionEvent.getSource(), actionEvent.getID(), ActionNames.CHECK_DIRTY)); - GuiPackage guiPackage = GuiPackage.getInstance(); - if (guiPackage.isDirty()) { - // Check if the user wants to create from template - int response = JOptionPane.showConfirmDialog(GuiPackage.getInstance().getMainFrame(), - JMeterUtils.getResString("cancel_new_from_template"), // $NON-NLS-1$ - JMeterUtils.getResString("template_load?"), // $NON-NLS-1$ - JOptionPane.YES_NO_CANCEL_OPTION, - JOptionPane.QUESTION_MESSAGE); - if(response == JOptionPane.YES_OPTION) { - ActionRouter.getInstance().doActionNow(new ActionEvent(actionEvent.getSource(), actionEvent.getID(), ActionNames.SAVE)); - } - if (response == JOptionPane.CLOSED_OPTION || response == JOptionPane.CANCEL_OPTION) { - return; // Don't clear the plan - } - } + if (isTestPlan && !checkDirty(actionEvent)) { + return; } ActionRouter.getInstance().doActionNow(new ActionEvent(actionEvent.getSource(), actionEvent.getID(), ActionNames.STOP_THREAD)); final File parent = template.getParent(); - final File fileToCopy = parent != null + File fileToCopy = parent != null ? new File(parent, template.getFileName()) - : new File(JMeterUtils.getJMeterHome(), template.getFileName()); - Load.loadProjectFile(actionEvent, fileToCopy, !isTestPlan, false); - this.setVisible(false); + : new File(JMeterUtils.getJMeterHome(), template.getFileName()); + replaceTemplateParametersAndLoad(actionEvent, template, isTestPlan, fileToCopy); + } + + /** + * @param actionEvent {@link ActionEvent} + * @param template {@link Template} definition + * @param isTestPlan If it's a full test plan or a part + * @param templateFile Template file to load + */ + void replaceTemplateParametersAndLoad(final ActionEvent actionEvent, final Template template, + final boolean isTestPlan, File templateFile) { + File temporaryGeneratedFile = null; + try { + // handle customized templates (the .jmx.fmkr files) + if (template.getParameters() != null && !template.getParameters().isEmpty()) { + File jmxFile = new File(templateFile.getAbsolutePath()); + Map<String, String> userParameters = getUserParameters(); + Configuration templateCfg = TemplateUtil.getTemplateConfig(); + try { + temporaryGeneratedFile = File.createTempFile(template.getName(), ".output"); + templateFile = temporaryGeneratedFile; + TemplateUtil.processTemplate(jmxFile, temporaryGeneratedFile, templateCfg, userParameters); + } catch (IOException | TemplateException ex) { + log.error("Error generating output file {} from template {}", temporaryGeneratedFile, jmxFile, ex); + return; + } + } + Load.loadProjectFile(actionEvent, templateFile, !isTestPlan, false); + this.dispose(); + } finally { + if (temporaryGeneratedFile != null && !temporaryGeneratedFile.delete()) { + log.warn("Could not delete generated output file {} from template {}", temporaryGeneratedFile, templateFile); + } + } + } + + /** + * @param actionEvent {@link ActionEvent} + * @return true if plan is not dirty or has been saved + */ + boolean checkDirty(final ActionEvent actionEvent) { --- End diff -- Any reason for not using `private`?
---