You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jmeter-dev@jakarta.apache.org by ms...@apache.org on 2003/04/25 00:06:12 UTC
cvs commit: jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/modifier/gui AnchorModifierGui.java
mstover1 2003/04/24 15:06:12
Modified: src/protocol/http/org/apache/jmeter/protocol/http/modifier
AnchorModifier.java
src/protocol/http/org/apache/jmeter/protocol/http/modifier/gui
AnchorModifierGui.java
Log:
Making HTML Link Parser work with new JMeterProperties
Revision Changes Path
1.5 +212 -176 jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/modifier/AnchorModifier.java
Index: AnchorModifier.java
===================================================================
RCS file: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/modifier/AnchorModifier.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- AnchorModifier.java 7 Apr 2003 14:35:26 -0000 1.4
+++ AnchorModifier.java 24 Apr 2003 22:06:12 -0000 1.5
@@ -54,6 +54,7 @@
*/
package org.apache.jmeter.protocol.http.modifier;
+import java.io.FileInputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
@@ -63,6 +64,8 @@
import java.util.List;
import java.util.Random;
+import junit.framework.TestCase;
+
import org.apache.jmeter.config.Argument;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.ConfigElement;
@@ -71,9 +74,11 @@
import org.apache.jmeter.protocol.http.sampler.HTTPSampler;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
+import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.AbstractTestElement;
import org.apache.jmeter.testelement.property.PropertyIterator;
import org.apache.jmeter.threads.JMeterContextService;
+import org.apache.jorphan.io.TextFile;
import org.apache.log.Hierarchy;
import org.apache.log.Logger;
import org.w3c.dom.Document;
@@ -91,182 +96,213 @@
*@version 1.0
***********************************************************/
-public class AnchorModifier extends AbstractTestElement implements PreProcessor,
- Serializable
+public class AnchorModifier extends AbstractTestElement implements PreProcessor, Serializable
{
- transient private static Logger log = Hierarchy.getDefaultHierarchy().getLoggerFor(
- "jmeter.protocol.http");
- private static Random rand = new Random();
-
- /************************************************************
- * !ToDo (Constructor description)
- ***********************************************************/
- public AnchorModifier()
- {
- }
-
- /************************************************************
- * Modifies an Entry object based on HTML response text.
- *
- *@param entry !ToDo (Parameter description)
- *@param result !ToDo (Parameter description)
- *@return !ToDo (Return description)
- ***********************************************************/
- public void process()
- {
+ transient private static Logger log = Hierarchy.getDefaultHierarchy().getLoggerFor("jmeter.protocol.http");
+ private static Random rand = new Random();
+
+ /************************************************************
+ * !ToDo (Constructor description)
+ ***********************************************************/
+ public AnchorModifier()
+ {}
+
+ /************************************************************
+ * Modifies an Entry object based on HTML response text.
+ *
+ *@param entry !ToDo (Parameter description)
+ *@param result !ToDo (Parameter description)
+ *@return !ToDo (Return description)
+ ***********************************************************/
+ public void process()
+ {
SampleResult result = JMeterContextService.getContext().getPreviousResult();
Sampler sam = JMeterContextService.getContext().getCurrentSampler();
- HTTPSampler sampler = null;
- if(result == null || !(sam instanceof HTTPSampler))
- {
- return;
- }
- else
- {
- sampler = (HTTPSampler)sam;
- }
- List potentialLinks = new ArrayList();
- String responseText = "";
- try {
- responseText = new String(result.getResponseData(),"8859_1");
- } catch(UnsupportedEncodingException e) {
- }
- Document html;
- try
- {
- int index = responseText.indexOf("<");
- if(index == -1)
- {
- index = 0;
- }
- html = (Document)HtmlParser.getDOM(responseText.substring(index));
- }
- catch (SAXException e)
- {
- return;
- }
- addAnchorUrls(html, result, sampler, potentialLinks);
- addFormUrls(html,result,sampler,potentialLinks);
- if (potentialLinks.size() > 0)
- {
- HTTPSampler url = (HTTPSampler)potentialLinks.get(rand.nextInt(potentialLinks.size()));
- sampler.setDomain(url.getDomain());
- sampler.setPath(url.getPath());
- if(url.getMethod().equals(HTTPSampler.POST))
- {
- PropertyIterator iter = sampler.getArguments().iterator();
- while(iter.hasNext())
- {
- Argument arg = (Argument)iter.next().getObjectValue();
- modifyArgument(arg,url.getArguments());
- }
- }
- else
- {
- sampler.setArguments(url.getArguments());
- //config.parseArguments(url.getQueryString());
- }
- sampler.setProtocol(url.getProtocol());
- return;
- }
- return;
- }
-
- private void modifyArgument(Argument arg,Arguments args)
- {
- List possibleReplacements = new ArrayList();
- PropertyIterator iter = args.iterator();
- Argument replacementArg;
- while (iter.hasNext())
- {
- replacementArg = (Argument)iter.next().getObjectValue();
- try
- {
- if(HtmlParser.isArgumentMatched(replacementArg,arg))
- {
- possibleReplacements.add(replacementArg);
- }
- }
- catch (Exception ex) {
- log.error("",ex);
- }
- }
- if(possibleReplacements.size() > 0)
- {
- replacementArg = (Argument)possibleReplacements.get(rand.nextInt(possibleReplacements.size()));
- arg.setName(replacementArg.getName());
- arg.setValue(replacementArg.getValue());
- args.removeArgument(replacementArg);
- }
- }
-
- /************************************************************
- * !ToDo
- *
- *@param config !ToDo
- ***********************************************************/
- public void addConfigElement(ConfigElement config)
- {
- }
-
- private void addFormUrls(Document html,SampleResult result,HTTPSampler config,
- List potentialLinks)
- {
- NodeList rootList = html.getChildNodes();
- List urls = new LinkedList();
- for(int x = 0;x < rootList.getLength();x++)
- {
- urls.addAll(HtmlParser.createURLFromForm(rootList.item(x),
- (HTTPSampler)JMeterContextService.getContext().getPreviousSampler()));
- }
- Iterator iter = urls.iterator();
- while (iter.hasNext())
- {
- HTTPSampler newUrl = (HTTPSampler)iter.next();
- try
- {
- newUrl.setMethod(HTTPSampler.POST);
- if(HtmlParser.isAnchorMatched(newUrl,config))
- {
- potentialLinks.add(newUrl);
- }
- }
- catch (org.apache.oro.text.regex.MalformedPatternException e)
- {
- log.error("Bad pattern",e);
- }
- }
- }
-
- private void addAnchorUrls(Document html, SampleResult result, HTTPSampler config, List potentialLinks)
- {
- NodeList nodeList = html.getElementsByTagName("a");
- for (int i = 0; i < nodeList.getLength(); i++)
- {
- Node tempNode = nodeList.item(i);
- NamedNodeMap nnm = tempNode.getAttributes();
- Node namedItem = nnm.getNamedItem("href");
- if(namedItem == null)
- {
- continue;
- }
- String hrefStr = namedItem.getNodeValue();
- try
- {
- HTTPSampler newUrl = HtmlParser.createUrlFromAnchor(hrefStr, (HTTPSampler)JMeterContextService.getContext().getPreviousSampler());
- newUrl.setMethod(HTTPSampler.GET);
- if (HtmlParser.isAnchorMatched(newUrl, config))
- {
- potentialLinks.add(newUrl);
- }
- }
- catch (MalformedURLException e)
- {
- }
- catch (org.apache.oro.text.regex.MalformedPatternException e)
- {
- log.error("Bad pattern",e);
- }
- }
- }
+ HTTPSampler sampler = null;
+ if (result == null || !(sam instanceof HTTPSampler))
+ {
+ return;
+ }
+ else
+ {
+ sampler = (HTTPSampler) sam;
+ }
+ List potentialLinks = new ArrayList();
+ String responseText = "";
+ try
+ {
+ responseText = new String(result.getResponseData(), "8859_1");
+ }
+ catch (UnsupportedEncodingException e)
+ {}
+ Document html;
+ try
+ {
+ int index = responseText.indexOf("<");
+ if (index == -1)
+ {
+ index = 0;
+ }
+ html = (Document) HtmlParser.getDOM(responseText.substring(index));
+ }
+ catch (SAXException e)
+ {
+ return;
+ }
+ addAnchorUrls(html, result, sampler, potentialLinks);
+ addFormUrls(html, result, sampler, potentialLinks);
+ if (potentialLinks.size() > 0)
+ {
+ HTTPSampler url = (HTTPSampler) potentialLinks.get(rand.nextInt(potentialLinks.size()));
+ sampler.setDomain(url.getDomain());
+ sampler.setPath(url.getPath());
+ if (url.getMethod().equals(HTTPSampler.POST))
+ {
+ PropertyIterator iter = sampler.getArguments().iterator();
+ while (iter.hasNext())
+ {
+ Argument arg = (Argument) iter.next().getObjectValue();
+ modifyArgument(arg, url.getArguments());
+ }
+ }
+ else
+ {
+ sampler.setArguments(url.getArguments());
+ //config.parseArguments(url.getQueryString());
+ }
+ sampler.setProtocol(url.getProtocol());
+ return;
+ }
+ return;
+ }
+
+ private void modifyArgument(Argument arg, Arguments args)
+ {
+ log.debug("Modifying argument: " + arg);
+ List possibleReplacements = new ArrayList();
+ PropertyIterator iter = args.iterator();
+ Argument replacementArg;
+ while (iter.hasNext())
+ {
+ replacementArg = (Argument) iter.next().getObjectValue();
+ try
+ {
+ if (HtmlParser.isArgumentMatched(replacementArg, arg))
+ {
+ possibleReplacements.add(replacementArg);
+ }
+ }
+ catch (Exception ex)
+ {
+ log.error("", ex);
+ }
+ }
+ if (possibleReplacements.size() > 0)
+ {
+ replacementArg = (Argument) possibleReplacements.get(rand.nextInt(possibleReplacements.size()));
+ arg.setName(replacementArg.getName());
+ arg.setValue(replacementArg.getValue());
+ log.debug("Just set argument to values: " + arg.getName() + " = " + arg.getValue());
+ args.removeArgument(replacementArg);
+ }
+ }
+
+ /************************************************************
+ * !ToDo
+ *
+ *@param config !ToDo
+ ***********************************************************/
+ public void addConfigElement(ConfigElement config)
+ {}
+
+ private void addFormUrls(Document html, SampleResult result, HTTPSampler config, List potentialLinks)
+ {
+ NodeList rootList = html.getChildNodes();
+ List urls = new LinkedList();
+ for (int x = 0; x < rootList.getLength(); x++)
+ {
+ urls.addAll(HtmlParser.createURLFromForm(rootList.item(x), (HTTPSampler) JMeterContextService.getContext().getPreviousSampler()));
+ }
+ Iterator iter = urls.iterator();
+ while (iter.hasNext())
+ {
+ HTTPSampler newUrl = (HTTPSampler) iter.next();
+ try
+ {
+ newUrl.setMethod(HTTPSampler.POST);
+ if (HtmlParser.isAnchorMatched(newUrl, config))
+ {
+ potentialLinks.add(newUrl);
+ }
+ }
+ catch (org.apache.oro.text.regex.MalformedPatternException e)
+ {
+ log.error("Bad pattern", e);
+ }
+ }
+ }
+
+ private void addAnchorUrls(Document html, SampleResult result, HTTPSampler config, List potentialLinks)
+ {
+ NodeList nodeList = html.getElementsByTagName("a");
+ for (int i = 0; i < nodeList.getLength(); i++)
+ {
+ Node tempNode = nodeList.item(i);
+ NamedNodeMap nnm = tempNode.getAttributes();
+ Node namedItem = nnm.getNamedItem("href");
+ if (namedItem == null)
+ {
+ continue;
+ }
+ String hrefStr = namedItem.getNodeValue();
+ try
+ {
+ HTTPSampler newUrl = HtmlParser.createUrlFromAnchor(hrefStr, (HTTPSampler) JMeterContextService.getContext().getPreviousSampler());
+ newUrl.setMethod(HTTPSampler.GET);
+ log.debug("possible match: " + newUrl);
+ if (HtmlParser.isAnchorMatched(newUrl, config))
+ {
+ log.debug("Is a match! " + newUrl);
+ potentialLinks.add(newUrl);
+ }
+ }
+ catch (MalformedURLException e)
+ {}
+ catch (org.apache.oro.text.regex.MalformedPatternException e)
+ {
+ log.error("Bad pattern", e);
+ }
+ }
+ }
+
+ public static class Test extends TestCase
+ {
+ public Test(String name)
+ {
+ super(name);
+ }
+
+ public void testModifySampler() throws Exception
+ {
+ HTTPSampler config = (HTTPSampler) SaveService.loadSubTree(new FileInputStream(System.getProperty("user.dir") + "/testfiles/load_bug_list.jmx")).getArray()[0];
+ config.setRunningVersion(true);
+ SampleResult result = new SampleResult();
+ HTTPSampler context = (HTTPSampler) SaveService.loadSubTree(new FileInputStream(System.getProperty("user.dir") + "/testfiles/Load_JMeter_Page.jmx")).getArray()[0];
+ JMeterContextService.getContext().setCurrentSampler(context);
+ JMeterContextService.getContext().setCurrentSampler(config);
+ result.setResponseData(new TextFile(System.getProperty("user.dir") + "/testfiles/jmeter_home_page.html").getText().getBytes());
+ result.setSampleLabel(context.toString());
+ result.setSamplerData(context.toString());
+ JMeterContextService.getContext().setPreviousResult(result);
+ AnchorModifier modifier = new AnchorModifier();
+ modifier.process();
+ assertEquals(
+ "http://nagoya.apache.org:80/bugzilla/buglist.cgi?bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&email1=&emailtype1=substring&emailassigned_to1=1&email2=&emailtype2=substring&emailreporter2=1&bugidtype=include&bug_id=&changedin=&votes=&chfieldfrom=&chfieldto=Now&chfieldvalue=&product=JMeter&short_desc=&short_desc_type=substring&long_desc=&long_desc_type=substring&bug_file_loc=&bug_file_loc_type=substring&keywords=&keywords_type=anywords&field0-0-0=noop&type0-0-0=noop&value0-0-0=&cmdtype=doit&order=Reuse+same+sort+as+last+time",
+ config.toString());
+ config.recoverRunningVersion();
+ assertEquals("http://nagoya.apache.org:80/bugzilla/buglist.cgi?bug_status=.*&bug_status=.*&bug_status=.*&email1=&emailtype1=substring&emailassigned_to1=1&email2=&emailtype2=substring&emailreporter2=1&bugidtype=include&bug_id=&changedin=&votes=&chfieldfrom=&chfieldto=Now&chfieldvalue=&product=JMeter&short_desc=&short_desc_type=substring&long_desc=&long_desc_type=substring&bug_file_loc=&bug_file_loc_type=substring&keywords=&keywords_type=anywords&field0-0-0=noop&type0-0-0=noop&value0-0-0=&cmdtype=doit&order=Reuse+same+sort+as+last+time",
+ config.toString());
+ }
+
+ }
}
1.6 +3 -6 jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/modifier/gui/AnchorModifierGui.java
Index: AnchorModifierGui.java
===================================================================
RCS file: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/modifier/gui/AnchorModifierGui.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- AnchorModifierGui.java 23 Apr 2003 14:23:08 -0000 1.5
+++ AnchorModifierGui.java 24 Apr 2003 22:06:12 -0000 1.6
@@ -53,14 +53,11 @@
* <http://www.apache.org/>.
*/
package org.apache.jmeter.protocol.http.modifier.gui;
-import java.awt.Font;
-
-import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
-import org.apache.jmeter.config.gui.AbstractResponseBasedModifierGui;
+import org.apache.jmeter.processor.gui.AbstractPreProcessorGui;
import org.apache.jmeter.protocol.http.modifier.AnchorModifier;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.util.JMeterUtils;
@@ -74,7 +71,7 @@
*@version 1.0
***************************************/
-public class AnchorModifierGui extends AbstractResponseBasedModifierGui
+public class AnchorModifierGui extends AbstractPreProcessorGui
{
/****************************************
---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org