add three tests and pass them, now i can add the children behaviors to

right batches now. But can only deal with one parent behavior per batch
This commit is contained in:
Tienan Chen 2013-12-05 14:51:37 +08:00
parent cb40ffe4d6
commit 959ecb2109
2 changed files with 131 additions and 32 deletions

View File

@ -47,7 +47,7 @@ public class Bench4qTestScriptAdapter implements IScriptAdapter {
public void insertUserBehaviorsToScenario(BehaviorBaseModel model) { public void insertUserBehaviorsToScenario(BehaviorBaseModel model) {
List<BatchBehavior> batches = this.getRunScenarioModel().getBatches(); List<BatchBehavior> batches = this.getRunScenarioModel().getBatches();
int batchId = -1; int parentBatchId = -1;
if (batches == null) { if (batches == null) {
this.getRunScenarioModel().setBatches( this.getRunScenarioModel().setBatches(
new ArrayList<BatchBehavior>()); new ArrayList<BatchBehavior>());
@ -57,37 +57,40 @@ public class Bench4qTestScriptAdapter implements IScriptAdapter {
if (!parameter.getKey().equals("url")) { if (!parameter.getKey().equals("url")) {
continue; continue;
} }
batchId = contains(parameter.getValue()); parentBatchId = getParentBatchId(parameter.getValue());
} }
if (parentBatchId < 0) {
if (batchId < 0) { batches.add(createBatchBehavior(model, batches.size()));
batches.add(createBatchBehavior(model, batches));
return; return;
} }
while (batches.size() <= batchId) { if (batches.get(parentBatchId).getChildId() < 0) {
batches.add(new BatchBehavior()); BatchBehavior batchBehavior = createBatchBehavior(model,
batches.size());
batchBehavior.setParentId(parentBatchId);
batches.add(batchBehavior);
batches.get(parentBatchId).setChildId(batches.size() - 1);
return;
} }
BatchBehavior batchBehavior = batches.get(batchId); batches.get(batches.get(parentBatchId).getChildId()).getBehaviors()
batchBehavior.setId(batchId); .add(model);
batchBehavior.getBehaviors().add(model);
} }
private BatchBehavior createBatchBehavior(BehaviorBaseModel model, private BatchBehavior createBatchBehavior(BehaviorBaseModel model,
List<BatchBehavior> batches) { int batchId) {
BatchBehavior batchBehavior = new BatchBehavior(); BatchBehavior batchBehavior = new BatchBehavior();
batchBehavior.setId(batches.size()); batchBehavior.setId(batchId);
batchBehavior.setParent_id(-1); batchBehavior.setParentId(-1);
batchBehavior.setChild_id(-1); batchBehavior.setChildId(-1);
List<BehaviorBaseModel> behaviors = new ArrayList<BehaviorBaseModel>(); List<BehaviorBaseModel> behaviors = new ArrayList<BehaviorBaseModel>();
behaviors.add(model); behaviors.add(model);
batchBehavior.setBehaviors(behaviors); batchBehavior.setBehaviors(behaviors);
return batchBehavior; return batchBehavior;
} }
private int contains(String url) { private int getParentBatchId(String url) {
for (ChildrenUrl childrenUrl : this.getChildrenUrls()) { for (ChildrenUrl childrenUrl : this.getChildrenUrls()) {
if (childrenUrl.getUrl().equals(url)) { if (childrenUrl.getUrl().equals(url)) {
return childrenUrl.getStoreBatchId(); return childrenUrl.getParentBatchId();
} }
} }
return -1; return -1;

View File

@ -16,6 +16,11 @@ import org.junit.Test;
public class TestParentRequest { public class TestParentRequest {
private IScriptAdapter adapater; private IScriptAdapter adapater;
private static String parentUrl = "www.apache.com";
private static String childUrl = "www.yahoo.com";
private static String independentUrl = "www.baidu.com";
private static String independentUrl2 = "www.eclipse.org";
private static String childUrl2 = "www.tomcat.com";
private IScriptAdapter getAdapater() { private IScriptAdapter getAdapater() {
return adapater; return adapater;
@ -30,16 +35,40 @@ public class TestParentRequest {
} }
@Test @Test
public void testAddOneBehaviorToBatchWihoutChildrenUrl() { public void testAddOneBehaviorWihoutChildrenUrl() {
this.getAdapater() this.getAdapater().insertUserBehaviorsToScenario(
.insertUserBehaviorsToScenario( createUserBehavior(parentUrl));
createUserBehavior("http://wiki.apache.org/tomcat/TomcatVersions"));
assertNotNull(this.getAdapater().getRunScenarioModel()); assertNotNull(this.getAdapater().getRunScenarioModel());
assertNotNull(this.getAdapater().getRunScenarioModel().getBatches()); assertNotNull(this.getAdapater().getRunScenarioModel().getBatches());
assertNotNull(this.getAdapater().getRunScenarioModel().getBatches() assertNotNull(this.getAdapater().getRunScenarioModel().getBatches()
.get(0)); .get(0));
assertTrue(this.getAdapater().getRunScenarioModel().getBatches().get(0) assertTrue(this.getAdapater().getRunScenarioModel().getBatches().get(0)
.getId() == 0); .getId() == 0);
}
@Test
public void testAddTwoBehaviorWithOutChildrenUrl() {
this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(parentUrl));
this.getAdapater().getChildrenUrls()
.add(createChildrenUrl(childUrl, 0));
this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(independentUrl));
this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(independentUrl2));
RunScenarioModel runScenarioModel = this.getAdapater()
.getRunScenarioModel();
assertNotNull(runScenarioModel);
assertNotNull(runScenarioModel.getBatches());
assertTrue(runScenarioModel.getBatches().size() == 3);
assertTrue(runScenarioModel.getBatches().get(1).getBehaviors().size() == 1);
assertTrue(runScenarioModel.getBatches().get(2).getBehaviors().size() == 1);
assertTrue(runScenarioModel.getBatches().get(1).getBehaviors().get(0)
.getParameters().get(0).getValue().equals(independentUrl));
assertTrue(runScenarioModel.getBatches().get(2).getBehaviors().get(0)
.getParameters().get(0).getValue().equals(independentUrl2));
} }
private UserBehaviorModel createUserBehavior(String url) { private UserBehaviorModel createUserBehavior(String url) {
@ -58,25 +87,92 @@ public class TestParentRequest {
@Test @Test
public void testAddBehaviorWithInChildrenUrl() { public void testAddBehaviorWithInChildrenUrl() {
String url = "http://wiki.apache.org/wiki/modernized/css/common.css";
this.getAdapater().getChildrenUrls().add(createChildrenUrl(url));
this.getAdapater().insertUserBehaviorsToScenario( this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(url)); createUserBehavior(parentUrl));
assertNotNull(this.getAdapater().getRunScenarioModel()); this.getAdapater().getChildrenUrls()
assertNotNull(this.getAdapater().getRunScenarioModel().getBatches()); .add(createChildrenUrl(childUrl, 0));
assertNotNull(this.getAdapater().getRunScenarioModel().getBatches() this.getAdapater().insertUserBehaviorsToScenario(
.get(0)); createUserBehavior(childUrl));
List<BatchBehavior> batches = this.getAdapater().getRunScenarioModel() RunScenarioModel runScenarioModel = this.getAdapater()
.getBatches(); .getRunScenarioModel();
System.out.println(batches.get(1).getId()); assertNotNull(runScenarioModel);
assertNotNull(runScenarioModel.getBatches());
assertNotNull(runScenarioModel.getBatches().get(0));
assertNotNull(runScenarioModel.getBatches().get(1));
List<BatchBehavior> batches = runScenarioModel.getBatches();
assertTrue(batches.get(1).getId() == 1); assertTrue(batches.get(1).getId() == 1);
} }
private ChildrenUrl createChildrenUrl(String url) { private ChildrenUrl createChildrenUrl(String url, int parentBatchId) {
ChildrenUrl childrenUrl = new ChildrenUrl(); ChildrenUrl childrenUrl = new ChildrenUrl();
childrenUrl.setParentBatchId(-1); childrenUrl.setParentBatchId(parentBatchId);
childrenUrl.setStoreBatchId(1); childrenUrl.setStoreBatchId(1);
childrenUrl.setUrl(url); childrenUrl.setUrl(url);
return childrenUrl; return childrenUrl;
} }
@Test
public void testAddBehaviorsIncludeInAndOut() {
this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(parentUrl));
this.getAdapater().getChildrenUrls()
.add(createChildrenUrl(childUrl, 0));
this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(childUrl));
this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(independentUrl));
List<BatchBehavior> batches = this.getAdapater().getRunScenarioModel()
.getBatches();
assertTrue(batches.get(1).getBehaviors().size() == 1);
assertTrue(batches.get(2).getBehaviors().size() == 1);
assertTrue(batches.get(1).getBehaviors().get(0).getParameters().get(0)
.getValue().equals(childUrl));
assertTrue(batches.get(2).getBehaviors().get(0).getParameters().get(0)
.getValue().equals(independentUrl));
}
@Test
public void addTwoBehaviorsInChildrenUrl() {
this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(parentUrl));
this.getAdapater().getChildrenUrls()
.add(createChildrenUrl(childUrl, 0));
this.getAdapater().getChildrenUrls()
.add(createChildrenUrl(childUrl2, 0));
this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(childUrl));
this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(childUrl2));
List<BatchBehavior> batches = this.getAdapater().getRunScenarioModel()
.getBatches();
System.out.println(batches.size());
assertTrue(batches.size() == 2);
assertTrue(batches.get(0).getBehaviors().get(0).getParameters().get(0)
.getValue().equals(parentUrl));
assertTrue(batches.get(1).getBehaviors().get(0).getParameters().get(0)
.getValue().equals(childUrl));
assertTrue(batches.get(1).getBehaviors().get(1).getParameters().get(0)
.getValue().equals(childUrl2));
}
@Test
public void addTwoInTwoOut() {
this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(parentUrl));
this.getAdapater().getChildrenUrls()
.add(createChildrenUrl(childUrl, 0));
this.getAdapater().getChildrenUrls()
.add(createChildrenUrl(childUrl2, 0));
this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(childUrl));
this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(childUrl2));
this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(independentUrl));
this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(independentUrl2));
assertTrue(this.getAdapater().getRunScenarioModel().getBatches().size() == 4);
}
} }