Files
ncs/plugins/NewTest.java
boccioli_m fc64356e5e Startup
2015-08-28 07:58:35 +02:00

680 lines
33 KiB
Java

/*
* Copyright (c) 2015 Paul Scherrer Institute. All rights reserved.
*/
import ch.psi.utils.swing.SwingUtils;
import ch.psi.utils.swing.SwingUtils.OptionResult;
import ch.psi.utils.swing.SwingUtils.OptionType;
import java.awt.Desktop;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.table.DefaultTableModel;
import org.apache.commons.io.FilenameUtils;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author boccioli_m
*/
public class NewTest extends javax.swing.JPanel {
/**
* Creates new form TestingListDetails
*/
private String sType;
public NewTest() {
initComponents();
}
public NewTest(String hDetails) {
initComponents();
sType = hDetails;
fillComponents(sType);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPopupMenuTable = new javax.swing.JPopupMenu();
jMenuItemAddRow = new javax.swing.JMenuItem();
jMenuItemReleteRow = new javax.swing.JMenuItem();
lblName = new javax.swing.JLabel();
lblSuite = new javax.swing.JLabel();
lblParams = new javax.swing.JLabel();
lblDescr = new javax.swing.JLabel();
txtTestName = new javax.swing.JTextField();
jComboBoxTestSuites = new javax.swing.JComboBox();
jScrollPane3 = new javax.swing.JScrollPane();
txtTestDescription = new javax.swing.JTextArea();
jScrollPane1 = new javax.swing.JScrollPane();
jTableParams = new javax.swing.JTable();
jButtonGenerate = new javax.swing.JButton();
jMenuItemAddRow.setText("Add Row");
jMenuItemAddRow.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItemAddRowActionPerformed(evt);
}
});
jPopupMenuTable.add(jMenuItemAddRow);
jMenuItemReleteRow.setText("Delete Row");
jMenuItemReleteRow.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItemReleteRowActionPerformed(evt);
}
});
jPopupMenuTable.add(jMenuItemReleteRow);
lblName.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
lblName.setText("Name *");
lblSuite.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
lblSuite.setText("Test Case/Suite *");
lblParams.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
lblParams.setText("Test Parameters");
lblDescr.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
lblDescr.setText("Description *");
txtTestName.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jScrollPane3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
txtTestDescription.setColumns(20);
txtTestDescription.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
txtTestDescription.setLineWrap(true);
txtTestDescription.setRows(5);
jScrollPane3.setViewportView(txtTestDescription);
jTableParams.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jTableParams.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Parameter", "Value", "Description"
}
));
jTableParams.setDragEnabled(true);
jTableParams.setRowHeight(20);
jTableParams.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jTableParamsMouseClicked(evt);
}
});
jScrollPane1.setViewportView(jTableParams);
if (jTableParams.getColumnModel().getColumnCount() > 0) {
jTableParams.getColumnModel().getColumn(0).setMinWidth(150);
jTableParams.getColumnModel().getColumn(0).setPreferredWidth(150);
jTableParams.getColumnModel().getColumn(0).setMaxWidth(150);
jTableParams.getColumnModel().getColumn(1).setPreferredWidth(80);
jTableParams.getColumnModel().getColumn(1).setMaxWidth(100);
}
jButtonGenerate.setText("Generate");
jButtonGenerate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonGenerateActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblParams)
.addComponent(lblDescr))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 513, Short.MAX_VALUE)
.addComponent(txtTestName, javax.swing.GroupLayout.DEFAULT_SIZE, 513, Short.MAX_VALUE)
.addComponent(jComboBoxTestSuites, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButtonGenerate, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 513, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addComponent(lblName)
.addComponent(lblSuite))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblName)
.addComponent(txtTestName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblSuite)
.addComponent(jComboBoxTestSuites, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblDescr))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblParams)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonGenerate, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
}// </editor-fold>//GEN-END:initComponents
private void jButtonGenerateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGenerateActionPerformed
// TODO add your handling code here:
if(checkDataFields()){
generateTestFiles();
}
}//GEN-LAST:event_jButtonGenerateActionPerformed
private void jTableParamsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableParamsMouseClicked
// TODO add your handling code here:
if(evt.getButton() == java.awt.event.MouseEvent.BUTTON3){
jPopupMenuTable.show(jTableParams, evt.getX(), evt.getY());
}
}//GEN-LAST:event_jTableParamsMouseClicked
private void jMenuItemAddRowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemAddRowActionPerformed
// TODO add your handling code here:
DefaultTableModel model = (DefaultTableModel) jTableParams.getModel();
model.addRow(new Object[]{"", "", ""});
jTableParams.setModel(model);
}//GEN-LAST:event_jMenuItemAddRowActionPerformed
private void jMenuItemReleteRowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemReleteRowActionPerformed
// TODO add your handling code here:
int row = jTableParams.getSelectedRow();
if(row>=0 && row<jTableParams.getRowCount()){
DefaultTableModel model = (DefaultTableModel) jTableParams.getModel();
model.removeRow(row);
}
}//GEN-LAST:event_jMenuItemReleteRowActionPerformed
public static Path TESTS_DEVICES_DEFAULT_DIR = Paths.get(".", "home", "script", "tests", "devices");
public static Path TESTS_TESTS_DEFAULT_DIR = Paths.get(".", "home", "script", "tests", "tests");
public static Path TESTS_CONFIG_DEFAULT_DIR = Paths.get(".", "home", "script", "tests", "config");
public static String TESTS_CONFIG_FILENAME = ".config";
public static String TESTS_HELP_FILENAME = "help.html";
public static Path TEMPLATES_HELP_FILEPATH = Paths.get(".", "home", "script", "tests","templates","helpTemplate.html");
public static Path TEMPLATES_TESTSCRIPT_FILEPATH = Paths.get(".", "home", "script", "tests","templates","testTemplate.py");
public static Path TEMPLATES_TESTCONFIG_FILEPATH = Paths.get(".", "home", "script", "tests","templates","testTemplate.config");
public static Path TEMPLATES_DEVICECONFIG_FILEPATH = Paths.get(".", "home", "script", "tests","templates","deviceTemplate.config");
public static String ALLOWED_CHARS = "[a-zA-Z0-9._\\-\\]\\[(){} ]*";
/**
* fill table with example parameters
* @param newType the new can be Device or Test.
*/
private void fillComponents(String newType) {
if(newType == "Device"){
this.lblDescr.setText(newType + " "+ this.lblDescr.getText());
this.lblName.setText(newType + " "+ this.lblName.getText());
lblParams.setVisible(false);
jTableParams.setVisible(false);
jScrollPane1.setVisible(false);
}
//fill test suites combo box
File testsFolder = Paths.get(TESTS_TESTS_DEFAULT_DIR.toString()).toFile();
if (testsFolder.exists() && testsFolder.isDirectory()) {
File[] listOfTests = testsFolder.listFiles();
for (File listOfTest : listOfTests) {
if (listOfTest.isDirectory()) {
this.jComboBoxTestSuites.addItem(listOfTest.getName().toString());
}
}
}
this.jComboBoxTestSuites.setEditable(true);
this.jComboBoxTestSuites.setSelectedItem(null);
//fill parameters table
InputStream ist = null;
try {
Properties propTest = new Properties();
File configFile = TEMPLATES_TESTCONFIG_FILEPATH.toFile();
ist = new FileInputStream(configFile);
propTest.load(ist);
String testParams = propTest.getProperty("parameters");
if (testParams == null) {
testParams = "";
}
ist.close();
HashMap hParams = (HashMap) buildParametersMap(testParams);
String name="", value="", description="";
DefaultTableModel model = (DefaultTableModel) jTableParams.getModel();
for(Object entry : hParams.keySet()){
Object param = entry;
if(param instanceof String){
name = (String) param;
HashMap attributes = (HashMap) hParams.get(param);
value = (String) attributes.get("value");
description = (String) attributes.get("description");
model.addRow(new Object[]{name, value, description});
}
jTableParams.setModel(model);
}
} catch (FileNotFoundException ex) {
SwingUtils.showMessage(this, "fillComponents()", "Cannot find file " + TEMPLATES_TESTCONFIG_FILEPATH.toString());
Logger.getLogger(NewTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
SwingUtils.showMessage(this, "fillComponents()", "Cannot read file " + TEMPLATES_TESTCONFIG_FILEPATH.toString());
Logger.getLogger(NewTest.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
ist.close();
} catch (IOException ex) {
SwingUtils.showMessage(this, "fillComponents()", "Cannot close file " + TEMPLATES_TESTCONFIG_FILEPATH.toString());
Logger.getLogger(NewTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/**
* Build a map with optional parameters to be passed to the testing script.
* The map is like this: parameters \_ name | \_ value | \_ description \_
* name | \_ value | \_ description ... the name 'name' is the mapping key.
* 'value' and 'description' are constant mapping keys of a nested map.
*
* @param parametersString string containing the parameters. Syntax:
* name:value:description[;name:value:description;...]
*/
private HashMap buildParametersMap(String parametersString) {
HashMap mParameters = new HashMap(); // contains name and attributes
HashMap mParameterAttributes = new HashMap(); //contians value and description
String[] dsParameterAttributes = null;
String[] dsParameters = parametersString.split(";");
for (String sParameter : dsParameters) {
dsParameterAttributes = sParameter.split(":");
if (dsParameterAttributes.length > 2) {
mParameterAttributes = new HashMap();
mParameterAttributes.put("value", (Object) dsParameterAttributes[1]);
mParameterAttributes.put("description", dsParameterAttributes[2]);
//add parameter name and attributes (value + description)
mParameters.put(dsParameterAttributes[0], mParameterAttributes);
}
}
return mParameters;
}
private HashMap getParametersFromTable(){
HashMap mParameters = new HashMap(); // contains name and attributes
HashMap mParameterAttributes = new HashMap(); //contians value and description
for(int row=0 ; row<jTableParams.getRowCount() ; row++){
String sParam = jTableParams.getValueAt(row, 0).toString();
String sVal = jTableParams.getValueAt(row, 1).toString();
String sDescr = jTableParams.getValueAt(row, 2).toString();
mParameterAttributes = new HashMap();
mParameterAttributes.put("value", (Object) sVal);
mParameterAttributes.put("description", sDescr);
//add parameter name and attributes (value + description)
mParameters.put(sParam, mParameterAttributes);
}
return mParameters;
}
public boolean checkDataFields(){
//check compulsory data fields
if(this.txtTestName.getText().isEmpty() ||
this.txtTestDescription.getText().isEmpty() ||
this.jComboBoxTestSuites.getSelectedItem().toString().isEmpty()){
SwingUtils.showMessage(this, "checkDataFields()",
"Please fill-in all compulsory fields (marked with *)");
return false;
}
if(!(txtTestName.getText().matches(ALLOWED_CHARS) &&
txtTestDescription.getText().matches(ALLOWED_CHARS) &&
jComboBoxTestSuites.getSelectedItem().toString().matches(ALLOWED_CHARS)
)){
SwingUtils.showMessage(this, "checkDataFields()",
"Please remove forbidden chars: only letters, numbers, parenthesis are allowed");
return false;
}
//check parameters table
for(int row=0 ; row<jTableParams.getRowCount() ; row++){
String sParam = jTableParams.getValueAt(row, 0).toString();
String sVal = jTableParams.getValueAt(row, 1).toString();
String sDescr = jTableParams.getValueAt(row, 2).toString();
if(sParam.isEmpty() || sVal.isEmpty() || sDescr.isEmpty()){
SwingUtils.showMessage(this, "checkDataFields()", "You must fill-in all the values in the parameters table");
return false;
}
if(!(sParam.matches(ALLOWED_CHARS) &&
sVal.matches(ALLOWED_CHARS) &&
sDescr.matches(ALLOWED_CHARS))
){
SwingUtils.showMessage(this, "checkDataFields()",
"Please remove forbidden chars from parameters table: only letters, numbers, parenthesis are allowed");
return false;
}
}
return true;
}
public void generateTestFiles(){
HashMap hmTestParams = (HashMap) getParametersFromTable();
if(sType.equals("Device")){
generateDeviceFiles(this.txtTestName.getText(),
this.txtTestDescription.getText(),
this.jComboBoxTestSuites.getSelectedItem().toString(),
hmTestParams);
}else{
generateTestFiles(this.txtTestName.getText(),
this.txtTestDescription.getText(),
this.jComboBoxTestSuites.getSelectedItem().toString(),
hmTestParams);
}
}
public void generateTestFiles(String sTestName,
String sTestDescription,
String sTestSuite,
HashMap hmTestParams){
if (TESTS_TESTS_DEFAULT_DIR.toFile().isDirectory()) {
//check that the tests suite dir exists. If not, create it
Path testSuiteDir = Paths.get(TESTS_TESTS_DEFAULT_DIR.toString(),sTestSuite);
if( !testSuiteDir.toFile().isDirectory()){
boolean success = testSuiteDir.toFile().mkdirs();
if (!success) {
// Directory creation failed
SwingUtils.showMessage(this, "generateTestFiles()", "Cannot create directory " + testSuiteDir.toString());
return;
}
}
//new test directory inside tests suite dir
Path testDir = Paths.get(testSuiteDir.toString(),sTestName);
if( testDir.toFile().isDirectory()){
//test already exists: ask to overwrite
OptionResult res = SwingUtils.showOption(this, "Overwrite Test?", "Test already exists. Overwrite with this new one?", OptionType.YesNo);
if (res == OptionResult.No){
return;
}
}else{
boolean success = testDir.toFile().mkdirs();
if (!success) {
// Directory creation failed
SwingUtils.showMessage(this, "generateTestFiles()", "Cannot create directory " + testDir.toString());
return;
}
}
//generate test files inside test dir
if (!generateTestConfig(TEMPLATES_TESTCONFIG_FILEPATH,
testDir,
sTestName,
sTestDescription,
sTestSuite,
hmTestParams)) return;
if (!generateTestHelp(testDir,
sTestName,
sTestDescription,
sTestSuite,
hmTestParams)) return;
if (!generateTestScript(testDir,
sTestName,
sTestDescription,
sTestSuite,
hmTestParams)) return;
SwingUtils.showMessage(this, "Test generated", "Test "+sTestName+" successfully generated in \n"+testDir.toString());
}else{
SwingUtils.showMessage(this, "generateTestFiles()", "Cannot find tests default directory " + TESTS_TESTS_DEFAULT_DIR.toString());
}
}
public void generateDeviceFiles(String sTestName,
String sTestDescription,
String sTestSuite,
HashMap hmTestParams){
if (TESTS_DEVICES_DEFAULT_DIR.toFile().isDirectory()) {
//new device directory inside tests suite dir
Path testDir = Paths.get(TESTS_DEVICES_DEFAULT_DIR.toString(),sTestName);
if( testDir.toFile().isDirectory()){
//device already exists: ask to overwrite
OptionResult res = SwingUtils.showOption(this, "Overwrite Device?", "Device already exists. Overwrite with this new one?", OptionType.YesNo);
if (res == OptionResult.No){
return;
}
}else{
boolean success = testDir.toFile().mkdirs();
if (!success) {
// Directory creation failed
SwingUtils.showMessage(this, "generateTestFiles()", "Cannot create directory " + testDir.toString());
return;
}
}
//generate device files inside device dir
if (!generateTestConfig(TEMPLATES_DEVICECONFIG_FILEPATH,
testDir,
sTestName,
sTestDescription,
sTestSuite,
hmTestParams)) return;
SwingUtils.showMessage(this, "Test generated", "Test "+sTestName+" successfully generated in \n"+testDir.toString());
}else{
SwingUtils.showMessage(this, "generateTestFiles()", "Cannot find tests default directory " + TESTS_TESTS_DEFAULT_DIR.toString());
}
}
public boolean generateTestScript(Path testDir,
String sTestName,
String sTestDescription,
String sTestSuite,
HashMap hmTestParams){
boolean success = false;
Path path = Paths.get(testDir + FilenameUtils.separatorsToSystem("/"+sTestName+".py"));
try {
if(Files.exists(path)){
Files.delete(path);
}
Files.copy(TEMPLATES_TESTSCRIPT_FILEPATH, path);
//add test parameters
String name="", value="", description="";
DefaultTableModel model = (DefaultTableModel) jTableParams.getModel();
String sSyntaxGetTestParameters = "";
for(Object entry : hmTestParams.keySet()){
Object param = entry;
if(param instanceof String){
name = (String) param;
HashMap attributes = (HashMap) hmTestParams.get(param);
value = (String) attributes.get("value");
description = (String) attributes.get("description");
//build the python code for getting the test parameter
sSyntaxGetTestParameters = sSyntaxGetTestParameters + name + " = float(params[\""+name+"\"][\"value\"]) ; ";
}
}
replaceParameters(path,
new String[]{"$testName", "$testDescription","$testSuite","#$testParameters"},
new String[]{sTestName, sTestDescription,sTestSuite,sSyntaxGetTestParameters});
success = true;
} catch (IOException ex) {
SwingUtils.showMessage(this, "generateTestScript()", "Cannot copy file " + ex.toString());
Logger.getLogger(NewTest.class.getName()).log(Level.SEVERE, null, ex);
}
return success;
}
public boolean generateTestConfig(Path templateDir,
Path testDir,
String sTestName,
String sTestDescription,
String sTestSuite,
HashMap hmTestParams){
//generate test config file from template
boolean success = false;
try {
Path path = Paths.get(testDir + FilenameUtils.separatorsToSystem("/"+TESTS_CONFIG_FILENAME));
if(Files.exists(path)){
Files.delete(path);
}
Files.copy(templateDir, path);
File configFile = path.toFile();
//Loop among the test parameters and generate variable assignement syntax for python
String sTestParameters = "";
String name="", value="", description="";
for(Object entry : hmTestParams.keySet()){
Object param = entry;
if(param instanceof String){
name = (String) param;
HashMap attributes = (HashMap) hmTestParams.get(param);
value = (String) attributes.get("value");
description = (String) attributes.get("description");
//build the python code for getting the test parameter
sTestParameters = sTestParameters + name + ":" + value + ":" + description + ";" ;
}
}
FileInputStream in = new FileInputStream(configFile);
Properties props = new Properties();
props.load(in);
in.close();
FileOutputStream out = new FileOutputStream(configFile);
if(sType.equals("Devices")){
props.setProperty("name", sTestName);
props.setProperty("description", sTestDescription);
props.setProperty("parameters", sTestParameters);
}else{
props.setProperty("name", sTestName);
props.setProperty("description", sTestDescription);
props.setProperty("tests", sTestSuite);
}
props.store(out, null);
out.close();
success = true;
} catch (FileNotFoundException ex) {
SwingUtils.showMessage(this, "generateTestConfig()", "Cannot find file: " + ex.toString());
Logger.getLogger(NewTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
SwingUtils.showMessage(this, "generateTestConfig()", "Problem with file aaccess: " + ex.toString());
Logger.getLogger(NewTest.class.getName()).log(Level.SEVERE, null, ex);
}
return success;
}
public boolean generateTestHelp(Path testDir,
String sTestName,
String sTestDescription,
String sTestSuite,
HashMap hmTestParams){
//generate test config file from template
Path path = Paths.get(testDir + FilenameUtils.separatorsToSystem("/"+TESTS_HELP_FILENAME));
boolean success = false;
try {
if(Files.exists(path)){
Files.delete(path);
}
Files.copy(TEMPLATES_HELP_FILEPATH, path);
//Loop among the test parameters and generate parameter help
String sTestParameters = "";
String name="", value="", description="";
for(Object entry : hmTestParams.keySet()){
Object param = entry;
if(param instanceof String){
name = (String) param;
HashMap attributes = (HashMap) hmTestParams.get(param);
value = (String) attributes.get("value");
description = (String) attributes.get("description");
//build the python code for getting the test parameter
sTestParameters = sTestParameters + "<code>" + name + " </code>" + description + "<br/>\n" ;
}
}
success = replaceParameters(path,
new String[]{"$testName","$testDescription","$testParameters"},
new String[]{sTestName,sTestDescription,sTestParameters});
} catch (IOException ex) {
SwingUtils.showMessage(this, "generateTestHelp()", "Cannot create directory " + ex.toString());
Logger.getLogger(NewTest.class.getName()).log(Level.SEVERE, null, ex);
}
return success;
}
public boolean replaceParameters(Path path,
String parameterNames[],
String parameterValues[]){
boolean success = false;
try {
//replace parameters in template
String content = new String(Files.readAllBytes(path));
for(int i=0 ; i<parameterNames.length ; i++ ){
content = content.replace(parameterNames[i], parameterValues[i]);
}
//System.out.print(content);
Charset charset = StandardCharsets.UTF_8;
Files.write(path, content.getBytes(charset));
OutputStream is = new FileOutputStream(path.toFile());
is.write(content.getBytes());
is.close();
success = true;
} catch (IOException ex) {
SwingUtils.showMessage(this, "replaceParameters()", "Cannot access file " + path + ". "+ ex.toString());
Logger.getLogger(NewTest.class.getName()).log(Level.SEVERE, null, ex);
}
return success;
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButtonGenerate;
private javax.swing.JComboBox jComboBoxTestSuites;
private javax.swing.JMenuItem jMenuItemAddRow;
private javax.swing.JMenuItem jMenuItemReleteRow;
private javax.swing.JPopupMenu jPopupMenuTable;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTable jTableParams;
private javax.swing.JLabel lblDescr;
private javax.swing.JLabel lblName;
private javax.swing.JLabel lblParams;
private javax.swing.JLabel lblSuite;
private javax.swing.JTextArea txtTestDescription;
private javax.swing.JTextField txtTestName;
// End of variables declaration//GEN-END:variables
}