From 7af31f857b57805d8a7f37a6aef0de25c1213992 Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Sat, 19 Jul 2014 18:37:21 +0800 Subject: [PATCH] refactor refactor --- Bench4Q-Agent/pom.xml | 13 +++- .../agent/plugin/basic/http/HttpPlugin.java | 73 ++++++++++--------- .../agent/plugin/basic/random/Random.java | 16 +++- .../agent/test/TestWithScriptFile.java | 2 +- .../agent/test/plugin/Test_HttpPlugin.java | 26 ++++++- Bench4Q-Master/pom.xml | 4 +- .../java/org/bench4q/master/MasterServer.java | 2 +- .../bench4q/master/domain/entity/Script.java | 3 +- .../domain/factory/PluginEntityFactory.java | 45 +++++++++--- .../domain/factory/TestPlanFactory.java | 6 +- .../integrated/Test_TestResultSave.java | 12 +-- Bench4Q-Web/pom.xml | 4 +- 12 files changed, 135 insertions(+), 71 deletions(-) diff --git a/Bench4Q-Agent/pom.xml b/Bench4Q-Agent/pom.xml index 17daabe7..85c40981 100644 --- a/Bench4Q-Agent/pom.xml +++ b/Bench4Q-Agent/pom.xml @@ -20,12 +20,12 @@ org.eclipse.jetty jetty-server - 9.1.0.M0 + 8.1.14.v20131031 org.eclipse.jetty jetty-servlet - 9.1.0.M0 + 8.1.14.v20131031 @@ -105,6 +105,15 @@ + + org.apache.maven.plugins + maven-compiler-plugin + 2.0.2 + + 1.6 + 1.6 + + maven-jar-plugin diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/http/HttpPlugin.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/http/HttpPlugin.java index ea8f1ed3..6b2ce15c 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/http/HttpPlugin.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/http/HttpPlugin.java @@ -96,8 +96,7 @@ public class HttpPlugin extends ParameterBarn { return nvPairs; } - private static NameValuePair[] setInputParameters( - List inputParameters) { + private NameValuePair[] setInputParameters(List inputParameters) { Set res = new HashSet(); Iterator paramIter = inputParameters.iterator(); @@ -148,38 +147,39 @@ public class HttpPlugin extends ParameterBarn { } private Callable buildTas(final Row row) { - switch (row.getCell(0).toLowerCase()) { - case "get": - return new Callable() { - @Override - public HttpReturn call() throws Exception { - return get(row.getCell(1), null, null, null); - } - }; - case "post": - return new Callable() { - @Override - public HttpReturn call() throws Exception { - return post(row.getCell(1), null, null, null, null, null); - } - }; - case "put": - return new Callable() { - @Override - public HttpReturn call() throws Exception { - return put(row.getCell(1), null, null, null, null, null); - } - }; - case "delete": - return new Callable() { - @Override - public HttpReturn call() throws Exception { - return delete(row.getCell(1), null, null, null); - } - }; - default: - return null; - } + // switch (row.getCell(0).toLowerCase()) { + // case "get": + // return new Callable() { + // @Override + // public HttpReturn call() throws Exception { + // return get(row.getCell(1), null, null, null); + // } + // }; + // case "post": + // return new Callable() { + // @Override + // public HttpReturn call() throws Exception { + // return post(row.getCell(1), null, null, null, null, null); + // } + // }; + // case "put": + // return new Callable() { + // @Override + // public HttpReturn call() throws Exception { + // return put(row.getCell(1), null, null, null, null, null); + // } + // }; + // case "delete": + // return new Callable() { + // @Override + // public HttpReturn call() throws Exception { + // return delete(row.getCell(1), null, null, null); + // } + // }; + // default: + // return null; + // } + return null; } /** @@ -204,7 +204,8 @@ public class HttpPlugin extends ParameterBarn { GetMethod method = new GetMethod(completeUri(url)); if (isValid(queryParams)) { - method.setQueryString(queryParams); + method.setQueryString(setInputParameters(ParameterParser + .buildNField(queryParams))); } setHeaders(method, headers); method.getParams().makeLenient(); @@ -217,8 +218,8 @@ public class HttpPlugin extends ParameterBarn { int responseCode = -1; long contentLength = 0; String contentType = ""; - try { + System.out.println(method.getURI().toString()); responseCode = this.getHttpClient().executeMethod(method); method.getStatusLine().toString(); Header[] responseHeaders = method.getResponseHeaders(); diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/random/Random.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/random/Random.java index 1bc2e613..c7b4856d 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/random/Random.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/random/Random.java @@ -1,18 +1,23 @@ package org.bench4q.agent.plugin.basic.random; +import java.util.ArrayList; +import java.util.List; + import org.bench4q.agent.plugin.ParameterBarn; import org.bench4q.agent.plugin.Constructor; import org.bench4q.agent.plugin.Parameter; import org.bench4q.agent.plugin.Plugin; import org.bench4q.agent.plugin.behavior.Behavior; import org.bench4q.agent.plugin.behavior.BehaviorType; +import org.bench4q.agent.utils.ParameterParser; import org.bench4q.agent.utils.Type.SupportTypes; @Plugin(value = "Random") public class Random extends ParameterBarn { private String format; - private int begin; - private int end; + private int begin = -1; + private int end = -1; + private List valueList = new ArrayList(); private String currentValue; @Constructor @@ -20,11 +25,13 @@ public class Random extends ParameterBarn { @Parameter(type = SupportTypes.Field, value = "testId") String testId, @Parameter(type = SupportTypes.Field, value = "begin") String begin, @Parameter(type = SupportTypes.Field, value = "end") String end, + @Parameter(type = SupportTypes.NField, value = "valueList") String valueList, @Parameter(type = SupportTypes.Field, value = "format") String format) { super(testId); this.format = format; this.begin = Integer.parseInt(begin); this.end = Integer.parseInt(end); + this.valueList = ParameterParser.buildNField(valueList); } @Behavior(value = "next", type = BehaviorType.CONTROL_BEHAVIOR) @@ -35,6 +42,11 @@ public class Random extends ParameterBarn { } public String getValue(String var) { + if (begin == -1 && end == -1) { + java.util.Random r = new java.util.Random(); + int index = r.nextInt(this.valueList.size()); + this.currentValue = this.valueList.get(index); + } return (this.format == null || this.format.isEmpty()) ? this.currentValue : String.format(this.format, this.currentValue); } diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java index 82a41dcc..c8b474e6 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java @@ -62,7 +62,7 @@ public class TestWithScriptFile { public TestWithScriptFile() { this.setFilePath("Scripts" + System.getProperty("file.separator") - + "mongo9To1.xml"); + + "http.xml"); this.setHttpRequester(new HttpRequester()); } diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_HttpPlugin.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_HttpPlugin.java index ec40d037..15c9c8b3 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_HttpPlugin.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_HttpPlugin.java @@ -1,20 +1,33 @@ package org.bench4q.agent.test.plugin; +import java.io.File; +import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.UUID; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.cookie.MalformedCookieException; +import org.apache.commons.io.FileUtils; import org.bench4q.agent.plugin.basic.PluginReturn; import org.bench4q.agent.plugin.basic.http.HttpPlugin; import org.bench4q.agent.plugin.basic.http.HttpReturn; +import org.bench4q.agent.scenario.Scenario; +import org.bench4q.agent.scenario.VUser; +import org.bench4q.agent.test.TestBase; +import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.helper.TestHelper; +import org.bench4q.share.models.agent.RunScenarioModel; import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static org.junit.Assert.*; -public class Test_HttpPlugin { +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = { "classpath:application-context.xml" }) +public class Test_HttpPlugin extends TestBase { private static final int UnAuthorized = 401; private HttpPlugin httpPlugin; @@ -128,4 +141,15 @@ public class Test_HttpPlugin { null, null, null, null); assertEquals(200, result.getStatusCode()); } + + @Test + public void buildAScenario() throws IOException { + RunScenarioModel scenarioMode = (RunScenarioModel) MarshalHelper + .tryUnmarshal(RunScenarioModel.class, FileUtils + .readFileToString(new File("Scripts/http.xml"))); + VUser vUser = createVUser( + Scenario.scenarioBuilderWithCompile(scenarioMode), + UUID.randomUUID()); + vUser.run(); + } } diff --git a/Bench4Q-Master/pom.xml b/Bench4Q-Master/pom.xml index f1579237..7ca44930 100644 --- a/Bench4Q-Master/pom.xml +++ b/Bench4Q-Master/pom.xml @@ -121,12 +121,12 @@ org.eclipse.jetty jetty-servlet - 9.1.0.RC2 + 8.1.14.v20131031 org.eclipse.jetty jetty-server - 9.1.0.RC2 + 8.1.14.v20131031 diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/MasterServer.java b/Bench4Q-Master/src/main/java/org/bench4q/master/MasterServer.java index 7c5c1234..a51df545 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/MasterServer.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/MasterServer.java @@ -59,7 +59,7 @@ public class MasterServer { if (this.getServer() != null) { for (Connector connector : this.getServer().getConnectors()) { if (connector != null) { - connector.shutdown(); + connector.close(); } } this.getServer().stop(); diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Script.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Script.java index 08cf1308..096e0f73 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Script.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Script.java @@ -1,7 +1,6 @@ package org.bench4q.master.domain.entity; import java.io.File; -import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.Date; @@ -115,7 +114,7 @@ public class Script { + Main.FILE_SEPARATOR + multipartFile.getOriginalFilename())); - } catch (IllegalStateException | IOException e) { + } catch (Exception e) { logger.info(ExceptionLog.getStackTrace(e)); return false; } diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/PluginEntityFactory.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/PluginEntityFactory.java index a87da708..233ec15c 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/PluginEntityFactory.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/PluginEntityFactory.java @@ -175,28 +175,49 @@ public class PluginEntityFactory { private static class ParamTypeFactory { public static ParamType createParamTypeInfo(Element element) { String type = element.getName(); - switch (type) { - case "field": + if (type.equals("field")) { return createFieldType(element); - case "nfield": + } else if (type.equals("nfield")) { return createNFieldType(element); - case "table": + } else if (type.equals("table")) { return createTableType(element); - case "radiobutton": + } else if (type.equals("radiobutton")) { return createRadioButtonType(element); - case "checkbox": + } else if (type.equalsIgnoreCase("checkbox")) { return createCheckBoxType(element); - case "combo": + } else if (type.equals("combo")) { return createComboType(element); - case "file": + } else if (type.equals("file")) { return createFileType(element); - case "date": + } else if (type.equals("date")) { return createDateType(element); - case "dropdownbox": + } else if (type.equals("dropdownbox")) { return createSelectType(element); - default: + } else { throw new Bench4QRunTimeException("no such type:" + type); } + // switch (type) { + // case "field": + // return createFieldType(element); + // case "nfield": + // return createNFieldType(element); + // case "table": + // return createTableType(element); + // case "radiobutton": + // return createRadioButtonType(element); + // case "checkbox": + // return createCheckBoxType(element); + // case "combo": + // return createComboType(element); + // case "file": + // return createFileType(element); + // case "date": + // return createDateType(element); + // case "dropdownbox": + // return createSelectType(element); + // default: + // throw new Bench4QRunTimeException("no such type:" + type); + // } } @@ -362,7 +383,7 @@ class XmlParseHelper { public static List getChildElementsByName(Element element, String elementName) { - List elements = new LinkedList<>(); + List elements = new LinkedList(); for (Iterator iterator = element.elementIterator(); iterator.hasNext();) { Element childElement = (Element) iterator.next(); if (childElement.getName().equals(elementName)) diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java index f91e0b10..793559c6 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java @@ -192,8 +192,7 @@ public class TestPlanFactory { String content = MarshalHelper.marshal(fields[i].getType(), fields[i].get(scriptResultModel)); testPlanScriptResult.setResultContent(content); - } catch (IllegalArgumentException | IllegalAccessException - | JAXBException e) { + } catch (Exception e) { Logger.getLogger(this.getClass()).info( ExceptionLog.getStackTrace(e)); return null; @@ -220,8 +219,7 @@ public class TestPlanFactory { try { monitorResult.setContent(MarshalHelper.marshal( fields[i].getType(), fields[i].get(monitorMain))); - } catch (IllegalArgumentException | IllegalAccessException - | JAXBException e) { + } catch (Exception e) { Logger.getLogger(this.getClass()).info( ExceptionLog.getStackTrace(e)); return null; diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/integrated/Test_TestResultSave.java b/Bench4Q-Master/src/test/java/org/bench4q/master/integrated/Test_TestResultSave.java index e5453aa9..9389cf90 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/integrated/Test_TestResultSave.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/integrated/Test_TestResultSave.java @@ -71,14 +71,14 @@ public class Test_TestResultSave extends TestBase_MakeUpTestPlan { @Test public void testDoSaveMonitorResult() { - TestPlan testPlanFromRepo = this.getTestPlanRepository().getTestPlanInDomainBy( - this.getTestPlanRunIdUuid()); + TestPlan testPlanFromRepo = this.getTestPlanRepository() + .getTestPlanInDomainBy(this.getTestPlanRunIdUuid()); int beforeInsertCount = getMonitorResultSize(getOneMonitor( testPlanFromRepo, Monitor_Host_Name)); assertTrue(beforeInsertCount == 0); TestPlan testPlan = this.getTestPlanFactory().convertToDomain( testPlanFromRepo); - List resultList = new ArrayList<>(); + List resultList = new ArrayList(); resultList.addAll(getOneMonitor(testPlan, Monitor_Host_Name) .doAfterRun()); this.getTestResultSave().update(testPlan, resultList); @@ -93,8 +93,8 @@ public class Test_TestResultSave extends TestBase_MakeUpTestPlan { @Test public void testDoSaveTestPlanScriptResult() { - TestPlan testPlanFromRepo = this.getTestPlanRepository().getTestPlanInDomainBy( - this.getTestPlanRunIdUuid()); + TestPlan testPlanFromRepo = this.getTestPlanRepository() + .getTestPlanInDomainBy(this.getTestPlanRunIdUuid()); TestPlanScript testPlanScript = getOneTestPlanScript(testPlanFromRepo); int beforeInsertCount = getTestPlanScriptResultSize(testPlanScript); assertTrue(beforeInsertCount == 0); @@ -104,7 +104,7 @@ public class Test_TestResultSave extends TestBase_MakeUpTestPlan { assertEquals(TestPlanStatus.InRunning.name(), testPlan.getCurrentStatus()); testPlanScript = getOneTestPlanScript(testPlan); - List resultList = new ArrayList<>(); + List resultList = new ArrayList(); resultList.addAll(testPlanScript.doAfterRun()); this.getTestResultSave().update(testPlan, resultList); testPlanFromRepo = this.getTestPlanRepository().getTestPlanInDomainBy( diff --git a/Bench4Q-Web/pom.xml b/Bench4Q-Web/pom.xml index 6988d2f6..b3370896 100644 --- a/Bench4Q-Web/pom.xml +++ b/Bench4Q-Web/pom.xml @@ -178,8 +178,8 @@ maven-compiler-plugin 2.3.2 - 1.7 - 1.7 + 1.6 + 1.6