Startup
This commit is contained in:
607
plugins/NewTest.java
Normal file
607
plugins/NewTest.java
Normal file
@@ -0,0 +1,607 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
public NewTest() {
|
||||
initComponents();
|
||||
}
|
||||
|
||||
public NewTest(String hDetails) {
|
||||
initComponents();
|
||||
fillComponents();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
jLabel4 = new javax.swing.JLabel();
|
||||
txtTestSuite = new javax.swing.JTextField();
|
||||
jLabel5 = new javax.swing.JLabel();
|
||||
txtTestName = new javax.swing.JTextField();
|
||||
jScrollPane1 = new javax.swing.JScrollPane();
|
||||
jTableParams = new javax.swing.JTable();
|
||||
jLabel7 = new javax.swing.JLabel();
|
||||
jLabel8 = new javax.swing.JLabel();
|
||||
jScrollPane3 = new javax.swing.JScrollPane();
|
||||
txtTestDescription = new javax.swing.JTextArea();
|
||||
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);
|
||||
|
||||
jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
|
||||
jLabel4.setText("Test Case/Suite *");
|
||||
|
||||
txtTestSuite.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
|
||||
|
||||
jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
|
||||
jLabel5.setText("Test Name *");
|
||||
|
||||
txtTestName.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
|
||||
jLabel7.setText("Test Parameters");
|
||||
|
||||
jLabel8.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
|
||||
jLabel8.setText("Test Description *");
|
||||
|
||||
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);
|
||||
|
||||
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, false)
|
||||
.addGroup(layout.createSequentialGroup()
|
||||
.addComponent(jLabel4)
|
||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
|
||||
.addComponent(txtTestSuite, javax.swing.GroupLayout.PREFERRED_SIZE, 513, javax.swing.GroupLayout.PREFERRED_SIZE))
|
||||
.addGroup(layout.createSequentialGroup()
|
||||
.addComponent(jLabel5)
|
||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
|
||||
.addComponent(txtTestName, javax.swing.GroupLayout.PREFERRED_SIZE, 513, javax.swing.GroupLayout.PREFERRED_SIZE))
|
||||
.addGroup(layout.createSequentialGroup()
|
||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
||||
.addComponent(jLabel7)
|
||||
.addComponent(jLabel8))
|
||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
||||
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
|
||||
.addGap(19, 19, 19)
|
||||
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 513, javax.swing.GroupLayout.PREFERRED_SIZE))
|
||||
.addGroup(layout.createSequentialGroup()
|
||||
.addGap(18, 18, 18)
|
||||
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 513, javax.swing.GroupLayout.PREFERRED_SIZE))))
|
||||
.addComponent(jButtonGenerate, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE))
|
||||
.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(jLabel5)
|
||||
.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(jLabel4)
|
||||
.addComponent(txtTestSuite, 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(jLabel8))
|
||||
.addGap(18, 18, 18)
|
||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
||||
.addComponent(jLabel7)
|
||||
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))
|
||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
|
||||
.addComponent(jButtonGenerate, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
|
||||
.addContainerGap())
|
||||
);
|
||||
}// </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_CONFIG_FILEPATH = Paths.get(".", "home", "script", "tests","templates","template.config");
|
||||
public static String ALLOWED_CHARS = "[a-zA-Z0-9._\\-\\]\\[(){} ]*";
|
||||
|
||||
/**
|
||||
* fill table with example parameters
|
||||
*/
|
||||
private void fillComponents() {
|
||||
InputStream ist = null;
|
||||
try {
|
||||
Properties propTest = new Properties();
|
||||
File configFile = TEMPLATES_CONFIG_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_CONFIG_FILEPATH.toString());
|
||||
Logger.getLogger(NewTest.class.getName()).log(Level.SEVERE, null, ex);
|
||||
} catch (IOException ex) {
|
||||
SwingUtils.showMessage(this, "fillComponents()", "Cannot read file " + TEMPLATES_CONFIG_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_CONFIG_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.txtTestSuite.getText().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) &&
|
||||
txtTestSuite.getText().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();
|
||||
generateTestFiles(this.txtTestName.getText(),
|
||||
this.txtTestDescription.getText(),
|
||||
this.txtTestSuite.getText(),
|
||||
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(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 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","#$testParameters"},
|
||||
new String[]{sTestName,sTestDescription,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 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(TEMPLATES_CONFIG_FILEPATH, 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 + ";" ;
|
||||
}
|
||||
}
|
||||
String content = "#Test configuration\n";
|
||||
content +=("\nname=" + sTestName);
|
||||
content +=("\ndescription=" + sTestDescription);
|
||||
content +=("\nparameters=" + sTestParameters);
|
||||
OutputStream is = new FileOutputStream(configFile);
|
||||
Charset charset = StandardCharsets.UTF_8;
|
||||
Files.write(path, content.getBytes(charset));
|
||||
is.write(content.getBytes());
|
||||
is.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.JLabel jLabel4;
|
||||
private javax.swing.JLabel jLabel5;
|
||||
private javax.swing.JLabel jLabel7;
|
||||
private javax.swing.JLabel jLabel8;
|
||||
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.JTextArea txtTestDescription;
|
||||
private javax.swing.JTextField txtTestName;
|
||||
private javax.swing.JTextField txtTestSuite;
|
||||
// End of variables declaration//GEN-END:variables
|
||||
}
|
||||
Reference in New Issue
Block a user