1 18 19 package org.apache.jmeter.gui.action; 20 21 import java.awt.event.ActionEvent ; 22 import java.io.FileOutputStream ; 23 import java.io.OutputStream ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.LinkedList ; 27 import java.util.Set ; 28 29 import javax.swing.JFileChooser ; 30 31 import org.apache.jmeter.config.Arguments; 32 import org.apache.jmeter.exceptions.IllegalUserActionException; 33 import org.apache.jmeter.gui.GuiPackage; 34 import org.apache.jmeter.gui.tree.JMeterTreeNode; 35 import org.apache.jmeter.gui.util.FileDialoger; 36 import org.apache.jmeter.save.SaveService; 37 import org.apache.jmeter.testelement.TestElement; 38 import org.apache.jmeter.util.JMeterUtils; 39 import org.apache.jorphan.collections.HashTree; 40 import org.apache.jorphan.collections.ListedHashTree; 41 import org.apache.jorphan.logging.LoggingManager; 42 import org.apache.log.Logger; 43 44 49 public class Save implements Command 50 { 51 transient private static Logger log = LoggingManager.getLoggerForClass(); 52 public final static String SAVE_ALL_AS = "save_all_as"; 53 public final static String SAVE_AS = "save_as"; 54 public final static String SAVE = "save"; 55 private String testPlanFile; 57 58 private static Set commands = new HashSet (); 59 static { 60 commands.add(SAVE_AS); 61 commands.add(SAVE_ALL_AS); 62 commands.add(SAVE); 63 } 64 65 68 public Save() 69 { 70 } 71 72 77 public Set getActionNames() 78 { 79 return commands; 80 } 81 82 public void setTestPlanFile(String f) 83 { 84 testPlanFile = f; 85 GuiPackage.getInstance().getMainFrame().setTitle(JMeterUtils.getExtendedFrameTitle(testPlanFile)); 86 } 87 88 public void doAction(ActionEvent e) throws IllegalUserActionException 89 { 90 HashTree subTree = null; 91 if (!commands.contains(e.getActionCommand())) 92 { 93 throw new IllegalUserActionException("Invalid user command:" + e.getActionCommand()); 94 } 95 if (e.getActionCommand().equals(SAVE_AS)) 96 { 97 subTree = GuiPackage.getInstance().getCurrentSubTree(); 98 } 99 else 100 { 101 subTree = GuiPackage.getInstance().getTreeModel().getTestPlan(); 102 } 103 104 String updateFile = testPlanFile; 105 if (!SAVE.equals(e.getActionCommand()) 106 || testPlanFile == null) 107 { 108 JFileChooser chooser = 109 FileDialoger.promptToSaveFile( 110 GuiPackage 111 .getInstance() 112 .getTreeListener() 113 .getCurrentNode() 114 .getName() 115 + ".jmx"); 116 if (chooser == null) 117 { 118 return; 119 } 120 updateFile = chooser.getSelectedFile().getAbsolutePath(); 121 if (!e.getActionCommand().equals(SAVE_AS)) 122 { 123 testPlanFile = updateFile; 124 } 125 } 126 129 ActionRouter.getInstance().doActionNow( 130 new ActionEvent (subTree, e.getID(), CheckDirty.SUB_TREE_SAVED)); 131 try 132 { 133 convertSubTree(subTree); 134 } 135 catch (Exception err) 136 { 137 } 138 OutputStream writer = null; 139 try 140 { 141 writer = new FileOutputStream (updateFile); 142 SaveService.saveSubTree(subTree, writer); 143 } 144 catch (Throwable ex) 145 { 146 testPlanFile = null; 147 log.error("", ex); 148 throw new IllegalUserActionException( 149 "Couldn't save test plan to file: " + updateFile); 150 } 151 finally 152 { 153 closeWriter(writer); 154 if(testPlanFile != null) 155 { 156 GuiPackage.getInstance().getMainFrame().setTitle(JMeterUtils.getExtendedFrameTitle(testPlanFile)); 157 } 158 GuiPackage.getInstance().getMainFrame().repaint(); 159 } 160 } 161 162 private void convertSubTree(HashTree tree) 163 { 164 Iterator iter = new LinkedList (tree.list()).iterator(); 165 while (iter.hasNext()) 166 { 167 JMeterTreeNode item = (JMeterTreeNode) iter.next(); 168 convertSubTree(tree.getTree(item)); 169 TestElement testElement = item.getTestElement(); 170 tree.replace(item, testElement); 171 } 172 } 173 174 public static class Test extends junit.framework.TestCase 175 { 176 Save save; 177 public Test(String name) 178 { 179 super(name); 180 } 181 182 public void setUp() 183 { 184 save = new Save(); 185 } 186 187 public void testTreeConversion() throws Exception 188 { 189 HashTree tree = new ListedHashTree(); 190 JMeterTreeNode root = new JMeterTreeNode(new Arguments(), null); 191 tree.add(root, root); 192 tree.getTree(root).add(root, root); 193 save.convertSubTree(tree); 194 assertEquals( 195 tree.getArray()[0].getClass().getName(), 196 root.getTestElement().getClass().getName()); 197 tree = tree.getTree(tree.getArray()[0]); 198 assertEquals( 199 tree.getArray()[0].getClass().getName(), 200 root.getTestElement().getClass().getName()); 201 assertEquals( 202 tree 203 .getTree(tree.getArray()[0]) 204 .getArray()[0] 205 .getClass() 206 .getName(), 207 root.getTestElement().getClass().getName()); 208 } 209 } 210 211 private void closeWriter(OutputStream writer) 212 { 213 if (writer != null) 214 { 215 try 216 { 217 writer.close(); 218 } 219 catch (Exception ex) 220 { 221 log.error("", ex); 222 } 223 } 224 } 225 } 226 | Popular Tags |