From b6626afe2a2d81611448c9bb8922c4ab1e745f5b Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Tue, 15 Apr 2014 15:19:29 +0800 Subject: [PATCH] now we can use the csvProvider as the now we can use the csvProvider as the --- .../org/bench4q/agent/scenario/Scenario.java | 2 +- .../agent/scenario/ScenarioEngine.java | 4 +- .../org/bench4q/agent/scenario/VUser.java | 86 +++++++++++++++---- .../agent/test/TestBase_Parameterization.java | 63 ++++++++++++++ .../Test_ParameterManager.java | 10 +-- .../agent/test/plugin/Test_ClassHelper.java | 2 +- .../bench4q/agent/test/scenario/Test_DFA.java | 4 +- .../agent/test/scenario/Test_Scenario.java | 44 ++++------ .../test/scenario/Test_ScenarioContext.java | 73 +++++++--------- .../agent/test/scenario/Test_VUser.java | 63 ++++++++++++-- 10 files changed, 249 insertions(+), 102 deletions(-) diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Scenario.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Scenario.java index b1c7a03b..46dc93e8 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Scenario.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Scenario.java @@ -165,7 +165,7 @@ public class Scenario { } } - private static Behavior extractBehavior(BehaviorModel behaviorModel) { + public static Behavior extractBehavior(BehaviorModel behaviorModel) { Behavior behavior = BehaviorFactory.getBuisinessObject(behaviorModel); behavior.setName(behaviorModel.getName()); behavior.setUse(behaviorModel.getUse()); diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java index 9f80f95b..2a24037c 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java @@ -78,9 +78,11 @@ public class ScenarioEngine { taskMaker.execute(new Runnable() { public void run() { + int currentIterationId = 0; while (!scenarioContext.isFinished()) { scenarioContext.getExecutor().execute( - new VUser(scenarioContext)); + new VUser(scenarioContext, currentIterationId)); + currentIterationId++; } } }); diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/VUser.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/VUser.java index d085bf06..2ec2c7a1 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/VUser.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/VUser.java @@ -7,21 +7,33 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import org.apache.log4j.Logger; import org.bench4q.agent.datacollector.DataCollector; import org.bench4q.agent.helper.ApplicationContextHelper; import org.bench4q.agent.parameterization.SessionObject; +import org.bench4q.agent.plugin.BaseParameterization; import org.bench4q.agent.plugin.Plugin; import org.bench4q.agent.plugin.PluginManager; import org.bench4q.agent.plugin.basic.PluginReturn; import org.bench4q.agent.plugin.basic.http.HttpReturn; import org.bench4q.agent.scenario.behavior.Behavior; +import org.bench4q.agent.scenario.dfa.ParamPart; public class VUser implements Runnable { private ScenarioContext scenarioContext; private SessionObject sessionObject; + private int currentIterationId; private PluginManager pluginManager; + private int getCurrentIterationId() { + return currentIterationId; + } + + private void setCurrentIterationId(int currentIterationId) { + this.currentIterationId = currentIterationId; + } + private ScenarioContext getScenarioContext() { return scenarioContext; } @@ -46,11 +58,12 @@ public class VUser implements Runnable { this.pluginManager = pluginManager; } - public VUser(ScenarioContext scenarioContext) { + public VUser(ScenarioContext scenarioContext, int currentIterationId) { this.setScenarioContext(scenarioContext); this.setPluginManager(ApplicationContextHelper.getContext().getBean( PluginManager.class)); this.setSessionObject(scenarioContext.buildVUserContext()); + this.setCurrentIterationId(currentIterationId); } public void run() { @@ -93,10 +106,10 @@ public class VUser implements Runnable { for (Behavior behavior : batch.getBehaviors()) { behavior.distillParams(this.getSessionObject()); Object plugin = plugins.get(behavior.getUse()); - Map behaviorParameters = prepareBehaviorParameters(behavior); Date startDate = new Date(System.currentTimeMillis()); PluginReturn pluginReturn = (PluginReturn) this.getPluginManager() - .doBehavior(plugin, behavior.getName(), behaviorParameters); + .doBehavior(plugin, behavior.getName(), + reassamblyParameters(behavior, plugins)); extractRunTimeParams(pluginReturn); @@ -113,6 +126,60 @@ public class VUser implements Runnable { return results; } + private Map reassamblyParameters(Behavior behavior, + Map plugins) { + Map behaviorParameters = new HashMap(); + for (Parameter parameter : behavior.getParameters()) { + behaviorParameters.put(parameter.getKey(), + reassamblyParameter(parameter.getParamParts(), plugins)); + } + return behaviorParameters; + } + + private String reassamblyParameter(ParamPart[] parts, + Map plugins) { + StringBuilder buf = new StringBuilder(); + for (int i = 0, len = parts.length; i < len; i++) { + switch (parts[i].getType()) { + case STRING: + buf.append(parts[i].getContentForStringType()); + break; + case SESSION_ID: + buf.append(this.getCurrentIterationId()); + break; + case CONTEXT_CALL: + try { + Object dpObj = plugins.get(parts[i] + .getPluginIdForContextCallType()); + if (dpObj == null) { + throw new RuntimeException("No such plug-in id: " + + parts[i].getPluginIdForContextCallType()); + } else if (dpObj instanceof BaseParameterization) { + buf.append(((BaseParameterization) dpObj).getValue(parts[i] + .getVariableForContextCallAndProperty())); + } else { + throw new RuntimeException("Plug-in " + + parts[i].getPluginIdForContextCallType() + + " is not a data provider."); + } + } catch (Exception e) { + Logger.getLogger(VUser.class) + .error(parts[i].getPluginIdForContextCallType() + + "variable " + + parts[i] + .getVariableForContextCallAndProperty(), + e); + } + break; + case PROPERTY: + buf.append(System.getProperty( + parts[i].getVariableForContextCallAndProperty(), "")); + break; + } + } + return buf.toString(); + } + private void extractRunTimeParams(PluginReturn pluginReturn) { if (pluginReturn == null || !pluginReturn.hasRunTimeParams()) return; @@ -150,19 +217,6 @@ public class VUser implements Runnable { return result; } - private Map prepareBehaviorParameters(Behavior behavior) { - Map behaviorParameters = new HashMap(); - for (Parameter parameter : behavior.getParameters()) { - compile(parameter.getValue()); - behaviorParameters.put(parameter.getKey(), parameter.getValue()); - } - return behaviorParameters; - } - - private void compile(String value) { - // TODO: Add the state pattern to this and parse the value - } - private void preparePlugins(Scenario scenario, Map plugins) { for (UsePlugin usePlugin : scenario.getUsePlugins()) { String pluginId = usePlugin.getId(); diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestBase_Parameterization.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestBase_Parameterization.java index 3cfff3f1..d50f90a2 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestBase_Parameterization.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestBase_Parameterization.java @@ -2,11 +2,19 @@ package org.bench4q.agent.test; import java.io.File; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; import org.apache.commons.io.FileUtils; import org.bench4q.agent.plugin.BaseParameterization; import org.bench4q.share.helper.TestHelper; +import org.bench4q.share.models.agent.ParameterModel; +import org.bench4q.share.models.agent.RunScenarioModel; +import org.bench4q.share.models.agent.scriptrecord.BatchModel; +import org.bench4q.share.models.agent.scriptrecord.BehaviorModel; +import org.bench4q.share.models.agent.scriptrecord.PageModel; +import org.bench4q.share.models.agent.scriptrecord.UsePluginModel; public abstract class TestBase_Parameterization { private BaseParameterization basePara; @@ -31,4 +39,59 @@ public abstract class TestBase_Parameterization { File dirFile = new File(dirPath); FileUtils.deleteDirectory(dirFile); } + + protected UsePluginModel buildCsvProviderWith(final String id, + final String fileName, final String fieldNames) { + UsePluginModel usePluginModel = new UsePluginModel() { + { + setId(id); + setName("CsvProvider"); + setParameters(new ArrayList() { + private static final long serialVersionUID = 1L; + { + add(ParameterModel + .createParameter("fileName", fileName)); + add(ParameterModel.createParameter("separator", ",")); + add(ParameterModel.createParameter("fieldNames", + fieldNames)); + add(ParameterModel.createParameter("shared", + "Notenable")); + add(ParameterModel.createParameter("loop", "enable")); + add(ParameterModel.createParameter("loadAtRunTime", + "Notenable")); + add(ParameterModel.createParameter("commentPrefix", + null)); + } + }); + } + }; + + return usePluginModel; + } + + public RunScenarioModel buildRunScenarioModelWith( + List plugins, BehaviorModel... behaviors) { + RunScenarioModel runScenarioModel = new RunScenarioModel(); + UsePluginModel httpPlugin = new UsePluginModel() { + { + setId("http"); + setName("Http"); + setParameters(new ArrayList()); + } + }; + for (UsePluginModel pluginModel : plugins) { + runScenarioModel.getUsePlugins().add(pluginModel); + } + runScenarioModel.getUsePlugins().add(httpPlugin); + + PageModel page = new PageModel(); + BatchModel batch = new BatchModel(); + batch.setBehaviors(new ArrayList()); + for (BehaviorModel behavior : behaviors) { + batch.getBehaviors().add(behavior); + } + page.getBatches().add(batch); + runScenarioModel.getPages().add(page); + return runScenarioModel; + } } diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/parameterization/Test_ParameterManager.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/parameterization/Test_ParameterManager.java index 6e3023de..08fd1916 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/parameterization/Test_ParameterManager.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/parameterization/Test_ParameterManager.java @@ -56,12 +56,12 @@ public class Test_ParameterManager extends TestBase_Parameterization { UpdateStrategy.ONCE.name()))); ScenarioContext scenarioContext = ScenarioContext.buildScenarioContext( testId, scenario, 1); - VUser vUser = new VUser(scenarioContext); + VUser vUser = new VUser(scenarioContext, 1); String v1_firstValue = vUser.getParam(paramName); String v1_secondValue = vUser.getParam(paramName); assertEquals("12", v1_firstValue); assertEquals(v1_firstValue, v1_secondValue); - VUser vUser2 = new VUser(scenarioContext); + VUser vUser2 = new VUser(scenarioContext, 1); String v2_firstValue = vUser2.getParam(paramName); String v2_secondValue = vUser2.getParam(paramName); assertEquals(v1_firstValue, v2_firstValue); @@ -80,12 +80,12 @@ public class Test_ParameterManager extends TestBase_Parameterization { UpdateStrategy.EACH_ITERATION.name()))); ScenarioContext scenarioContext = ScenarioContext.buildScenarioContext( testId, scenario, 1); - VUser vUser = new VUser(scenarioContext); + VUser vUser = new VUser(scenarioContext, 1); String v1_firstValue = vUser.getParam(paramName); String v1_secondeValue = vUser.getParam(paramName); assertNotEquals(paramName, v1_firstValue); assertEquals(v1_firstValue, v1_secondeValue); - VUser vUser2 = new VUser(scenarioContext); + VUser vUser2 = new VUser(scenarioContext, 1); String v2_firstValue = vUser2.getParam(paramName); String v2_secondValue = vUser2.getParam(paramName); assertNotEquals(paramName, v2_firstValue); @@ -105,7 +105,7 @@ public class Test_ParameterManager extends TestBase_Parameterization { UpdateStrategy.EACH_OCCURRENCE.name()))); ScenarioContext scenarioContext = ScenarioContext.buildScenarioContext( testId, scenario, 1); - VUser vUser = new VUser(scenarioContext); + VUser vUser = new VUser(scenarioContext, 1); String vuserFirstValue = vUser.getParam(paramName); String vuserSecondeValue = vUser.getParam(paramName); assertNotEquals(vuserFirstValue, vuserSecondeValue); diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_ClassHelper.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_ClassHelper.java index 290f8eb7..7af2fdfa 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_ClassHelper.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_ClassHelper.java @@ -24,7 +24,7 @@ public class Test_ClassHelper { .value(); } }); - assertEquals(5, plugins.size()); + assertEquals(6, plugins.size()); assertEquals(true, plugins.containsKey("Http")); } } diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_DFA.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_DFA.java index 34ad1a80..df91ce2f 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_DFA.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_DFA.java @@ -10,10 +10,10 @@ import org.bench4q.agent.scenario.dfa.ParamPart.ParamPartType; import org.junit.Test; public class Test_DFA { - public static final String TEST_CASE = "${csvProvider0:userName}"; + private static final String TEST_CASE = "${csvProvider0:userName}"; private static final String TEST_CASE2 = "${csvProvider0:userName}$#"; private static final String TEST_CASE3 = "${csvProvider0:userName}$#okOrNot"; - static final String TEST_CASE4 = "${csvProvider0:userName}$#okOrNot${file.separator}"; + private static final String TEST_CASE4 = "${csvProvider0:userName}$#okOrNot${file.separator}"; private static final String TEST_CASE_NOTSUPPORT1 = "${{abc"; private static final String TEST_CASE_NOTSUPPORT2 = "${csvProvider0:adc{"; diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_Scenario.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_Scenario.java index b1c53f3a..b6dbd5c7 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_Scenario.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_Scenario.java @@ -11,23 +11,24 @@ import org.bench4q.agent.scenario.Scenario; import org.bench4q.agent.scenario.behavior.Behavior; import org.bench4q.agent.scenario.dfa.ParamPart; import org.bench4q.agent.scenario.dfa.ParamPart.ParamPartType; +import org.bench4q.agent.test.TestBase_Parameterization; import org.bench4q.agent.test.plugin.Test_HBasePlugin; import org.bench4q.share.models.agent.DefinedParameterModel; import org.bench4q.share.models.agent.ParameterModel; import org.bench4q.share.models.agent.RunScenarioModel; -import org.bench4q.share.models.agent.scriptrecord.BatchModel; import org.bench4q.share.models.agent.scriptrecord.BehaviorModel; -import org.bench4q.share.models.agent.scriptrecord.PageModel; +import org.bench4q.share.models.agent.scriptrecord.UsePluginModel; import org.junit.Test; -public class Test_Scenario { +public class Test_Scenario extends TestBase_Parameterization { + private static final String TEST_CASE = "${csvProvider0:userName}$#okOrNot${file.separator}"; @Test public void testScenarioBuilderWithZeroDefinedParameter() { RunScenarioModel inputModel = Test_HBasePlugin.buildScenario(10); Scenario scenario = Scenario.scenarioBuilderWithCompile(inputModel); assertNotNull(scenario.getDefinedParameters()); - assertEquals(0, scenario.getDefinedParameters().length); + assertEquals(1, scenario.getDefinedParameters().length); } @Test @@ -38,7 +39,7 @@ public class Test_Scenario { "each_iteration")); Scenario scenario = Scenario.scenarioBuilderWithCompile(inputModel); assertNotNull(scenario.getDefinedParameters()); - assertEquals(1, scenario.getDefinedParameters().length); + assertEquals(2, scenario.getDefinedParameters().length); } private DefinedParameterModel buildDefinedParameterModel(String name, @@ -53,18 +54,16 @@ public class Test_Scenario { @Test public void test_ScenarioCompile() { - - List behaviors = new ArrayList(); - behaviors.add(BehaviorModel.UserBehaviorBuilder(0, "first", "http", - new ArrayList() { - private static final long serialVersionUID = 1L; - { - add(ParameterModel.createParameter("withParamPart", - Test_DFA.TEST_CASE4)); - } - })); - - RunScenarioModel inputModel = buildRunScenarioModelWith(behaviors); + RunScenarioModel inputModel = buildRunScenarioModelWith( + new ArrayList(), + BehaviorModel.UserBehaviorBuilder(0, "first", "http", + new ArrayList() { + private static final long serialVersionUID = 1L; + { + add(ParameterModel.createParameter( + "withParamPart", TEST_CASE)); + } + })); Scenario scenario = Scenario.scenarioBuilderWithCompile(inputModel); assertNotNull(scenario.getDefinedParameters()); for (Behavior behavior : scenario.getAllBehaviors()) { @@ -100,15 +99,4 @@ public class Test_Scenario { } } - private RunScenarioModel buildRunScenarioModelWith( - List behaviors) { - RunScenarioModel runScenarioModel = new RunScenarioModel(); - PageModel page = new PageModel(); - BatchModel batch = new BatchModel(); - batch.setBehaviors(new ArrayList()); - batch.getBehaviors().addAll(behaviors); - page.getBatches().add(batch); - runScenarioModel.getPages().add(page); - return runScenarioModel; - } } diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java index f9c2e4e9..8cf48bc2 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java @@ -2,22 +2,11 @@ package org.bench4q.agent.test.scenario; import static org.junit.Assert.*; -import java.io.File; -import java.io.IOException; import java.util.UUID; import java.util.concurrent.TimeUnit; -import org.apache.commons.io.FileUtils; -import org.bench4q.agent.parameterization.PickOrder; -import org.bench4q.agent.parameterization.UpdateStrategy; -import org.bench4q.agent.parameterization.impl.Para_Table; import org.bench4q.agent.scenario.Scenario; import org.bench4q.agent.scenario.ScenarioContext; -import org.bench4q.agent.scenario.VUser; -import org.bench4q.agent.test.plugin.Test_HBasePlugin; -import org.bench4q.share.helper.TestHelper; -import org.bench4q.share.models.agent.DefinedParameterModel; -import org.bench4q.share.models.agent.RunScenarioModel; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; @@ -36,36 +25,36 @@ public class Test_ScenarioContext { assertEquals(20, context.getExecutor().getMaximumPoolSize()); } - @Test - public void testForParameterization() throws IOException { - RunScenarioModel model = Test_HBasePlugin.buildScenario(10); - String param_name = "param1"; - UUID testId = UUID.randomUUID(); + // @Test + // public void testForParameterization() throws IOException { + // RunScenarioModel model = Test_HBasePlugin.buildScenario(10); + // String param_name = "param1"; + // UUID testId = UUID.randomUUID(); + // + // createParamFileAndFillParamContent(param_name, testId); + // + // model.getDefinedParameters().add( + // new DefinedParameterModel(param_name, "Para_Table", + // PickOrder.SEQUENTIAL.name(), + // UpdateStrategy.EACH_OCCURRENCE.name())); + // + // ScenarioContext scenarioContext = ScenarioContext.buildScenarioContext( + // testId, Scenario.scenarioBuilderWithCompile(model), 10); + // + // VUser vUser = new VUser(scenarioContext, 1); + // String firstValue = vUser.getParam(param_name); + // String secondValue = vUser.getParam(param_name); + // assertNotEquals(firstValue, secondValue); + // } - createParamFileAndFillParamContent(param_name, testId); - - model.getDefinedParameters().add( - new DefinedParameterModel(param_name, "Para_Table", - PickOrder.SEQUENTIAL.name(), - UpdateStrategy.EACH_OCCURRENCE.name())); - - ScenarioContext scenarioContext = ScenarioContext.buildScenarioContext( - testId, Scenario.scenarioBuilderWithCompile(model), 10); - - VUser vUser = new VUser(scenarioContext); - String firstValue = vUser.getParam(param_name); - String secondValue = vUser.getParam(param_name); - assertNotEquals(firstValue, secondValue); - } - - private void createParamFileAndFillParamContent(String param_name, - UUID testId) throws IOException { - File parmFile = new File( - new Para_Table(testId, PickOrder.SEQUENTIAL, - UpdateStrategy.EACH_ITERATION, param_name, -1) - .getParamFileFullPath(param_name)); - TestHelper.createFileIfNotExist(parmFile); - FileUtils.writeStringToFile(parmFile, - "row1;10;11~row2;20;21~row3,30,31~"); - } + // private void createParamFileAndFillParamContent(String param_name, + // UUID testId) throws IOException { + // File parmFile = new File( + // new Para_Table(testId, PickOrder.SEQUENTIAL, + // UpdateStrategy.EACH_ITERATION, param_name, -1) + // .getParamFileFullPath(param_name)); + // TestHelper.createFileIfNotExist(parmFile); + // FileUtils.writeStringToFile(parmFile, + // "row1;10;11~row2;20;21~row3,30,31~"); + // } } diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_VUser.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_VUser.java index befa5a16..3395e439 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_VUser.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_VUser.java @@ -1,17 +1,26 @@ package org.bench4q.agent.test.scenario; +import java.util.ArrayList; import java.util.HashMap; +import java.util.Map; import java.util.UUID; import static org.junit.Assert.*; import org.bench4q.agent.plugin.basic.PluginReturn; +import org.bench4q.agent.plugin.basic.csvprovider.CsvProvider; import org.bench4q.agent.plugin.basic.http.HttpReturn; import org.bench4q.agent.scenario.Scenario; import org.bench4q.agent.scenario.ScenarioContext; import org.bench4q.agent.scenario.VUser; +import org.bench4q.agent.scenario.behavior.Behavior; +import org.bench4q.agent.test.TestBase_Parameterization; import org.bench4q.agent.test.plugin.Test_HBasePlugin; import org.bench4q.share.helper.TestHelper; +import org.bench4q.share.models.agent.ParameterModel; +import org.bench4q.share.models.agent.RunScenarioModel; +import org.bench4q.share.models.agent.scriptrecord.BehaviorModel; +import org.bench4q.share.models.agent.scriptrecord.UsePluginModel; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; @@ -19,7 +28,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:application-context.xml" }) -public class Test_VUser { +public class Test_VUser extends TestBase_Parameterization { + static final String TEST_CASE = "${csvProvider0:userName}$#okOrNot${file.separator}"; @Test public void testExtractRunTimeParamsWithiNullPluginReturn() { @@ -94,17 +104,58 @@ public class Test_VUser { } private static Scenario getTestScenario() { - return Scenario.scenarioBuilderWithCompile(Test_HBasePlugin.buildScenario(10)); + return Scenario.scenarioBuilderWithCompile(Test_HBasePlugin + .buildScenario(10)); } public static VUser createVUser(Scenario scenario, UUID testId) { return new VUser(ScenarioContext.buildScenarioContext(testId, scenario, - 10)); + 10), 1); } @Test - public void test_run() { - VUser vUser = createAWorker(); - vUser.run(); + public void test_reassamblyParameters() throws Exception { + UUID testId = UUID.randomUUID(); + final String paramKey = "userName"; + final String fileName = "param1"; + final String fieldNames = paramKey + ", password"; + createFileAndWriteContent(testId, fileName, + "chen, 123" + System.getProperty("line.separator") + "kkk, 321"); + final BehaviorModel behaviorModel = BehaviorModel.UserBehaviorBuilder( + 1, "beh1", "http", new ArrayList() { + private static final long serialVersionUID = 1L; + { + add(ParameterModel.createParameter(paramKey, TEST_CASE)); + } + }); + RunScenarioModel runScenarioModel = buildRunScenarioModelWith( + new ArrayList() { + private static final long serialVersionUID = 1L; + { + add(buildCsvProviderWith("csvProvider0", fileName, + fieldNames)); + } + }, behaviorModel); + Scenario scenario = Scenario + .scenarioBuilderWithCompile(runScenarioModel); + VUser vUser = createVUser(scenario, testId); + Map plugins = new HashMap(); + TestHelper + .invokePrivate(vUser, "preparePlugins", new Class[] { + Scenario.class, Map.class }, new Object[] { scenario, + plugins }); + assertEquals(2, plugins.size()); + assertTrue(plugins.containsKey("csvProvider0")); + CsvProvider csvProvider = (CsvProvider) plugins.get("csvProvider0"); + csvProvider.doNext(); + @SuppressWarnings("unchecked") + Map params = (Map) TestHelper + .invokePrivate(vUser, "reassamblyParameters", new Class[] { + Behavior.class, Map.class }, new Object[] { + scenario.getAllBehaviors().get(0), plugins }); + assertEquals(1, params.size()); + assertTrue(params.containsKey(paramKey)); + assertEquals("chen1okOrNot\\", params.get(paramKey)); + dropFiles(testId); } }