/* * Copyright (c) 2015 Paul Scherrer Institute. All rights reserved. */ import ch.psi.utils.swing.SwingUtils; import java.awt.Desktop; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; 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(); } /** * 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") // //GEN-BEGIN:initComponents private void initComponents() { 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(); cmCancel = new javax.swing.JButton(); cmOk = new javax.swing.JButton(); jLabel8 = new javax.swing.JLabel(); jScrollPane3 = new javax.swing.JScrollPane(); txtTestDescription = new javax.swing.JTextArea(); jScrollPane4 = new javax.swing.JScrollPane(); jEditorPaneHelp = new javax.swing.JEditorPane(); 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" } ) { boolean[] canEdit = new boolean [] { false, true, false }; public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit [columnIndex]; } }); jTableParams.setDragEnabled(true); jTableParams.setRowHeight(20); 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"); cmCancel.setText("Cancel"); this.cmCancel.setVisible(false); this.cmOk.setVisible(false); cmCancel.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmCancelActionPerformed(evt); } }); cmOk.setText("Ok"); this.cmCancel.setVisible(false); this.cmOk.setVisible(false); 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); jEditorPaneHelp.setContentType("text/html"); // NOI18N jEditorPaneHelp.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N jEditorPaneHelp.setDropMode(javax.swing.DropMode.INSERT); jScrollPane4.setViewportView(jEditorPaneHelp); 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, false) .addGroup(layout.createSequentialGroup() .addGap(331, 331, 331) .addComponent(cmOk, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(cmCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .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))))) .addGap(18, 18, 18) .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE) .addContainerGap()) ); layout.setVerticalGroup( 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.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(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(jLabel7) .addGap(0, 0, Short.MAX_VALUE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(cmCancel) .addComponent(cmOk))) .addGroup(layout.createSequentialGroup() .addComponent(jScrollPane4) .addContainerGap()))) ); }// //GEN-END:initComponents 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_TEST_FILEPATH = Paths.get(".", "home", "script", "tests","templates","testTemplate.html"); public static Path TEMPLATES_CONFIG_FILEPATH = Paths.get(".", "home", "script", "tests","templates","configTemplate.html"); private void fillComponents(HashMap hDetails) { } public void generateTestFiles(){ HashMap hmTestParams = new HashMap(); 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, "saveSettings()", "Cannot create directory " + testSuiteDir.toString()); }else{ //new test directory inside tests suite dir Path testDir = Paths.get(testSuiteDir.toString(),sTestName); success = testDir.toFile().mkdirs(); if (!success) { // Directory creation failed SwingUtils.showMessage(this, "saveSettings()", "Cannot create directory " + testDir.toString()); } else{ //generate test files inside test dir generateTestConfig(testDir, sTestName, sTestDescription, sTestSuite, hmTestParams); } } } } } public void generateTestScript(String sTestName, String sTestDescription, String sTestSuite, HashMap hmTestParams){ } public void generateTestConfig(Path testDir, String sTestName, String sTestDescription, String sTestSuite, HashMap hmTestParams){ //generate test config file from template try { Path path = Paths.get(testDir + FilenameUtils.separatorsToSystem("/"+TESTS_CONFIG_FILENAME)); Files.copy(TEMPLATES_CONFIG_FILEPATH, path); File configFile = path.toFile(); Properties properties = new Properties(); properties.setProperty("name", sTestName); properties.setProperty("description", sTestDescription); OutputStream is = new FileOutputStream(configFile); properties.store(is, "Test config"); is.close(); } catch (FileNotFoundException ex) { Logger.getLogger(NewTest.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(NewTest.class.getName()).log(Level.SEVERE, null, ex); } } public void 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)); try { Files.copy(TEMPLATES_HELP_FILEPATH, path); File configFile = path.toFile(); } catch (IOException ex) { Logger.getLogger(NewTest.class.getName()).log(Level.SEVERE, null, ex); } } private void cmCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmCancelActionPerformed }//GEN-LAST:event_cmCancelActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton cmCancel; private javax.swing.JButton cmOk; private javax.swing.JEditorPane jEditorPaneHelp; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JScrollPane jScrollPane4; 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 }