add a test named testDomParserWithinAdapter, add pass it.

Now i can add the childrenUrl with right parentBatchId.
This commit is contained in:
Tienan Chen 2013-12-06 15:20:23 +08:00
parent d2784c7f7d
commit c43a9494fd
6 changed files with 145 additions and 50 deletions

View File

@ -66,6 +66,18 @@ public class Bench4qTestScriptAdapter implements IScriptAdapter {
.add(model);
}
private BatchBehavior createBatchBehavior(BehaviorBaseModel model,
int batchId) {
BatchBehavior batchBehavior = new BatchBehavior();
batchBehavior.setId(batchId);
batchBehavior.setParentId(-1);
batchBehavior.setChildId(-1);
List<BehaviorBaseModel> behaviors = new ArrayList<BehaviorBaseModel>();
behaviors.add(model);
batchBehavior.setBehaviors(behaviors);
return batchBehavior;
}
private boolean isChildBatchAbsent(List<BatchBehavior> batches,
int parentBatchId) {
return batches.get(parentBatchId).getChildId() < 0;
@ -81,24 +93,12 @@ public class Bench4qTestScriptAdapter implements IScriptAdapter {
if (!parameter.getKey().equals("url")) {
continue;
}
parentBatchId = getParentBatchId(parameter.getValue());
parentBatchId = getParentBatchIdWithChildUrl(parameter.getValue());
}
return parentBatchId;
}
private BatchBehavior createBatchBehavior(BehaviorBaseModel model,
int batchId) {
BatchBehavior batchBehavior = new BatchBehavior();
batchBehavior.setId(batchId);
batchBehavior.setParentId(-1);
batchBehavior.setChildId(-1);
List<BehaviorBaseModel> behaviors = new ArrayList<BehaviorBaseModel>();
behaviors.add(model);
batchBehavior.setBehaviors(behaviors);
return batchBehavior;
}
private int getParentBatchId(String url) {
private int getParentBatchIdWithChildUrl(String url) {
for (ChildrenUrl childrenUrl : this.getChildrenUrls()) {
if (childrenUrl.getUrl().equals(url)) {
return childrenUrl.getParentBatchId();
@ -107,6 +107,23 @@ public class Bench4qTestScriptAdapter implements IScriptAdapter {
return -1;
}
public int getParentBatchIdWithParentUrl(String url) {
for (int batchId = 0; batchId < this.getRunScenarioModel().getBatches()
.size(); batchId++) {
BatchBehavior batch = this.getRunScenarioModel().getBatches()
.get(batchId);
for (BehaviorBaseModel behavior : batch.getBehaviors()) {
for (ParameterModel parameter : behavior.getParameters()) {
if (parameter.getKey().equals("url")
&& parameter.getValue().equals(url)) {
return batchId;
}
}
}
}
return -1;
}
public String getText() {
StringWriter stringWriter = new StringWriter();
try {

View File

@ -23,4 +23,5 @@ public interface IScriptAdapter {
// TODO:move this function to more fit place
public abstract List<ChildrenUrl> getChildrenUrls();
public abstract int getParentBatchIdWithParentUrl(String url);
}

View File

@ -46,10 +46,6 @@ public class Bench4qCodeGenerator extends AbstractCodeGenerator {
private int deviation = 0;
private int unit = 1;
private static Logger logger = Logger.getLogger(Bench4qCodeGenerator.class);
// FOR TEST
public Elements links;
public Elements medias;
public Elements imports;
private static String escapeXmlString(String str) {
for (int i = 0; i < escapes.length; ++i)
@ -203,8 +199,7 @@ public class Bench4qCodeGenerator extends AbstractCodeGenerator {
}
@Override
public void doParseHtmlContent(String responseContent, String url) {
// Fulfill it here, extract the dom and static resources
public void doParseHtmlContent(String responseContent, String rootUrl) {
int htmlStart = responseContent.indexOf("<html>");
int htmlEnd = responseContent.indexOf("</html>");
if (htmlStart == -1 || htmlEnd == -1) {
@ -212,21 +207,24 @@ public class Bench4qCodeGenerator extends AbstractCodeGenerator {
}
String htmlContent = responseContent.substring(htmlStart, htmlEnd + 8);
Document document = Jsoup.parse(htmlContent);
document.setBaseUri(url);
Elements links = document.select("a[href]");
Elements medias = document.select("[src]");
Elements imports = document.select("link[href]");
this.links = links;
this.medias = medias;
this.imports = imports;
for (Element element : medias) {
document.setBaseUri(rootUrl);
Elements allChildrenLinks = new Elements();
allChildrenLinks.addAll(document.select("[src]"));
allChildrenLinks.addAll(document.select("[href]"));
for (Element element : allChildrenLinks) {
String childAbsoluteUrl = element.absUrl("src");
this.getScriptAdapter().getChildrenUrls()
.add(ChildrenUrl.createChilldUrl(childAbsoluteUrl, 0));
if (childAbsoluteUrl.isEmpty()) {
childAbsoluteUrl = element.absUrl("href");
}
System.out.println(childAbsoluteUrl);
this.getScriptAdapter()
.getChildrenUrls()
.add(ChildrenUrl.createChilldUrl(childAbsoluteUrl,
this.getScriptAdapter()
.getParentBatchIdWithParentUrl(rootUrl)));
}
// this.getScriptAdapter().getChildrenUrls().add()
}
public void doResponseForStdOut(String url) {

View File

@ -7,34 +7,44 @@ import java.io.IOException;
import java.net.URL;
import org.apache.commons.io.FileUtils;
import org.bench4q.master.scriptrecord.httpcapture.Bench4qTestScriptAdapter;
import org.bench4q.master.scriptrecord.httpcapture.generator.Bench4qCodeGenerator;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.master.scriptrecord.httpcapture.generator.ChildrenUrl;
import org.junit.Test;
public class TestDomGenerator {
private Bench4qCodeGenerator codeGenerator = new Bench4qCodeGenerator(
new Bench4qTestScriptAdapter(new RunScenarioModel()));
public class TestDomGenerator extends TestRecordBase {
private URL url = this.getClass().getResource("testcase.html");
private Bench4qCodeGenerator getCodeGenerator() {
return codeGenerator;
public TestDomGenerator() {
init();
}
@Test
public void testDomParser() throws IOException {
URL url = this.getClass().getResource("testcase.html");
this.getCodeGenerator().doParseHtmlContent(
FileUtils.readFileToString(new File(url.getPath())),
"http://localhost:8080/Bench4QTestCase/testcase.html");
assertNotNull(this.getCodeGenerator().medias);
assertNotNull(this.getCodeGenerator().links);
assertNotNull(this.getCodeGenerator().imports);
assertTrue(this.getCodeGenerator().medias.size() == 5);
assertTrue(this.getCodeGenerator().links.size() == 0);
assertTrue(this.getCodeGenerator().imports.size() == 3);
FileUtils.readFileToString(new File(url.getPath())), parentUrl);
assertTrue(this.getCodeGenerator().getScriptAdapter().getChildrenUrls()
.size() == 5);
.size() == 8);
for (ChildrenUrl childrenUrl : this.getCodeGenerator()
.getScriptAdapter().getChildrenUrls()) {
assertTrue(childrenUrl.getParentBatchId() == -1);
}
}
@Test
public void testDomParserWithinAdapter() throws IOException {
this.getAdpater().insertUserBehaviorsToScenario(
createUserBehaviorModel(independentUrl));
this.getAdpater().insertUserBehaviorsToScenario(
createUserBehaviorModel(parentUrl));
this.getCodeGenerator().doParseHtmlContent(
FileUtils.readFileToString(new File(this.url.getPath())),
parentUrl);
System.out.println(this.getCodeGenerator().getScriptAdapter()
.getChildrenUrls().size());
assertTrue(this.getCodeGenerator().getScriptAdapter().getChildrenUrls()
.size() == 8);
for (ChildrenUrl childrenUrl : this.getAdpater().getChildrenUrls()) {
assertTrue(childrenUrl.getParentBatchId() == 1);
}
}
}

View File

@ -174,4 +174,20 @@ public class TestParentRequest {
createUserBehavior(independentUrl2));
assertTrue(this.getAdapater().getRunScenarioModel().getBatches().size() == 4);
}
@Test
public void testGetParentUrl() {
this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(parentUrl));
this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(independentUrl));
this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(independentUrl2));
assertTrue(this.getAdapater().getParentBatchIdWithParentUrl(parentUrl) == 0);
assertTrue(this.getAdapater().getParentBatchIdWithParentUrl(
independentUrl) == 1);
assertTrue(this.getAdapater().getParentBatchIdWithParentUrl(
independentUrl2) == 2);
}
}

View File

@ -0,0 +1,53 @@
package org.bench4q.master.test.recordscript;
import java.util.ArrayList;
import java.util.List;
import org.bench4q.master.scriptrecord.httpcapture.Bench4qTestScriptAdapter;
import org.bench4q.master.scriptrecord.httpcapture.IScriptAdapter;
import org.bench4q.master.scriptrecord.httpcapture.generator.Bench4qCodeGenerator;
import org.bench4q.share.models.agent.ParameterModel;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.scriptrecord.UserBehaviorModel;
public abstract class TestRecordBase {
private IScriptAdapter adapter;
private Bench4qCodeGenerator codeGenerator;
protected static String parentUrl = "http://localhost:8080/Bench4QTestCase/testcase.html";
protected static String independentUrl = "www.baidu.com";
protected IScriptAdapter getAdpater() {
return adapter;
}
private void setAdpater(IScriptAdapter adapter) {
this.adapter = adapter;
}
protected Bench4qCodeGenerator getCodeGenerator() {
return codeGenerator;
}
private void setCodeGenerator(Bench4qCodeGenerator codeGenerator) {
this.codeGenerator = codeGenerator;
}
protected void init() {
this.setAdpater(new Bench4qTestScriptAdapter(new RunScenarioModel()));
this.setCodeGenerator(new Bench4qCodeGenerator(this.getAdpater()));
}
protected UserBehaviorModel createUserBehaviorModel(String url) {
UserBehaviorModel model = new UserBehaviorModel();
model.setId(1);
model.setName("Get");
model.setUse("http");
List<ParameterModel> params = new ArrayList<ParameterModel>();
ParameterModel param = new ParameterModel();
param.setKey("url");
param.setValue(url);
params.add(param);
model.setParameters(params);
return model;
}
}