/*
* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache JMeter" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache JMeter", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.jmeter.gui.action;
import java.awt.event.ActionEvent;
import java.util.*;
import javax.swing.JFileChooser;
import java.io.*;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.apache.xerces.parsers.SAXParser;
import org.apache.jmeter.gui.*;
import org.apache.jmeter.save.*;
import org.apache.jmeter.save.xml.*;
import org.apache.jmeter.util.*;
import org.apache.jmeter.protocol.http.config.UrlConfig;
import org.apache.jmeter.protocol.http.control.HttpTestSample;
import org.apache.jmeter.protocol.http.config.UrlConfigFull;
import org.apache.jmeter.control.LogicController;
import org.apache.jmeter.exceptions.IllegalUserActionException;
import junit.framework.*;
/************************************************************
* Title: JMeter Description: Copyright: Copyright (c) 2000 Company: Apache
*
*@author Michael Stover
*@created $Date: 2002/03/06 03:51:35 $
*@version 1.0
***********************************************************/
public class Load implements Command
{
private static Set commands = new HashSet();
/************************************************************
* !ToDo (Constructor description)
***********************************************************/
public Load()
{
}
/************************************************************
* !ToDoo (Method description)
*
*@return !ToDo (Return description)
***********************************************************/
public Set getActionNames()
{
return commands;
}
/************************************************************
* !ToDo (Method description)
*
*@param e !ToDo (Parameter description)
***********************************************************/
public void doAction(ActionEvent e)
{
JFileChooser chooser = FileDialoger.promptToOpenFile();
FileInputStream reader = null;
try
{
File f = chooser.getSelectedFile();
if (f != null)
{
reader = new FileInputStream(f);
XmlHandler handler =
new XmlHandler(new JMeterNameSpaceHandler());
XMLReader parser = JMeterUtils.getXMLParser();
parser.setFeature("http://xml.org/sax/features/validation", false);
parser.setContentHandler(handler);
parser.setErrorHandler(handler);
parser.parse(new InputSource(reader));
ListedHashTree tree = handler.getDataTree();
updateTree(tree);
GuiPackage.getInstance().addSubTree(tree);
}
}
catch(IllegalUserActionException ex)
{
JMeterUtils.reportErrorToUser(ex.getMessage());
}
catch (Throwable ex)
{
ex.printStackTrace();
}
finally
{
GuiPackage.getInstance().getMainFrame().repaint();
}
}
private void updateTree(ListedHashTree tree)
{
List items = new LinkedList(tree.list());
Iterator iter = items.iterator();
while (iter.hasNext())
{
Object item = iter.next();
if (item instanceof HttpTestSample)
{
List subItems = new LinkedList(tree.list(item));
boolean replaced = false;
Iterator iter2 = subItems.iterator();
while (iter2.hasNext())
{
Object config = iter2.next();
if (config instanceof UrlConfig)
{
replaced = true;
HttpTestSample newControl = new HttpTestSample();
newControl.setDefaultUrl((UrlConfig)config);
if (config instanceof UrlConfigFull)
{
newControl.setGetImages(true);
}
tree.get(item).replace(config, newControl);
}
}
if (replaced)
{
UrlConfig newConfig = ((HttpTestSample)item).getDefaultUrl();
LogicController newControl = new LogicController();
tree.replace(item, newControl);
tree.add(newControl, newConfig);
}
}
else
{
updateTree(tree.get(item));
}
}
}
/************************************************************
* !ToDo (Class description)
*
*@author $Author: mstover1 $
*@created $Date: 2002/03/06 03:51:35 $
*@version $Revision: 1.14 $
***********************************************************/
public static class Test extends TestCase
{
File testFile1, testFile2,testFile3;
static Load loader = new Load();
public Test(String name)
{
super(name);
}
/************************************************************
* !ToDo
***********************************************************/
public void setUp()
{
testFile1 = new File(System.getProperty("user.dir") + "/bin/testfiles", "Test Plan.jmx");
testFile2 = new File(System.getProperty("user.dir") + "/bin/testfiles", "Modification Manager.jmx");
testFile3 = new File(System.getProperty("user.dir") + "/bin/testfiles", "proxy.jmx");
}
/************************************************************
* !ToDo
*
*@exception Exception !ToDo (Exception description)
***********************************************************/
public void testUpdateTree() throws Exception
{
ListedHashTree tree = getTree(testFile2);
loader.updateTree(tree);
assertTrue(tree.list(tree.list().get(0)).get(0) instanceof LogicController);
}
public void testFile3() throws Exception
{
ListedHashTree tree = getTree(testFile3);
loader.updateTree(tree);
assertTrue(tree.list().get(0) instanceof org.apache.jmeter.threads.ThreadGroup);
}
private ListedHashTree getTree(File f)
{
try
{
FileInputStream reader = new FileInputStream(f);
XmlHandler handler =
new XmlHandler(new JMeterNameSpaceHandler());
XMLReader parser = JMeterUtils.getXMLParser();
parser.setContentHandler(handler);
parser.setErrorHandler(handler);
parser.parse(new InputSource(reader));
ListedHashTree tree = handler.getDataTree();
return tree;
}
catch(Exception e)
{
return null;
}
}
}
static
{
commands.add(JMeterUtils.getResString("open"));
}
}