refactor
This commit is contained in:
coderfengyun 2014-07-19 18:37:21 +08:00
parent b8c2367d81
commit 7af31f857b
12 changed files with 135 additions and 71 deletions

View File

@ -20,12 +20,12 @@
<dependency> <dependency>
<groupId>org.eclipse.jetty</groupId> <groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId> <artifactId>jetty-server</artifactId>
<version>9.1.0.M0</version> <version>8.1.14.v20131031</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.eclipse.jetty</groupId> <groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId> <artifactId>jetty-servlet</artifactId>
<version>9.1.0.M0</version> <version>8.1.14.v20131031</version>
</dependency> </dependency>
<!-- For spring begin --> <!-- For spring begin -->
<dependency> <dependency>
@ -105,6 +105,15 @@
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin> <plugin>
<artifactId>maven-jar-plugin</artifactId> <artifactId>maven-jar-plugin</artifactId>
<configuration> <configuration>

View File

@ -96,8 +96,7 @@ public class HttpPlugin extends ParameterBarn {
return nvPairs; return nvPairs;
} }
private static NameValuePair[] setInputParameters( private NameValuePair[] setInputParameters(List<String> inputParameters) {
List<String> inputParameters) {
Set<NameValuePair> res = new HashSet<NameValuePair>(); Set<NameValuePair> res = new HashSet<NameValuePair>();
Iterator<String> paramIter = inputParameters.iterator(); Iterator<String> paramIter = inputParameters.iterator();
@ -148,38 +147,39 @@ public class HttpPlugin extends ParameterBarn {
} }
private Callable<HttpReturn> buildTas(final Row row) { private Callable<HttpReturn> buildTas(final Row row) {
switch (row.getCell(0).toLowerCase()) { // switch (row.getCell(0).toLowerCase()) {
case "get": // case "get":
return new Callable<HttpReturn>() { // return new Callable<HttpReturn>() {
@Override // @Override
public HttpReturn call() throws Exception { // public HttpReturn call() throws Exception {
return get(row.getCell(1), null, null, null); // return get(row.getCell(1), null, null, null);
} // }
}; // };
case "post": // case "post":
return new Callable<HttpReturn>() { // return new Callable<HttpReturn>() {
@Override // @Override
public HttpReturn call() throws Exception { // public HttpReturn call() throws Exception {
return post(row.getCell(1), null, null, null, null, null); // return post(row.getCell(1), null, null, null, null, null);
} // }
}; // };
case "put": // case "put":
return new Callable<HttpReturn>() { // return new Callable<HttpReturn>() {
@Override // @Override
public HttpReturn call() throws Exception { // public HttpReturn call() throws Exception {
return put(row.getCell(1), null, null, null, null, null); // return put(row.getCell(1), null, null, null, null, null);
} // }
}; // };
case "delete": // case "delete":
return new Callable<HttpReturn>() { // return new Callable<HttpReturn>() {
@Override // @Override
public HttpReturn call() throws Exception { // public HttpReturn call() throws Exception {
return delete(row.getCell(1), null, null, null); // return delete(row.getCell(1), null, null, null);
} // }
}; // };
default: // default:
return null; // return null;
} // }
return null;
} }
/** /**
@ -204,7 +204,8 @@ public class HttpPlugin extends ParameterBarn {
GetMethod method = new GetMethod(completeUri(url)); GetMethod method = new GetMethod(completeUri(url));
if (isValid(queryParams)) { if (isValid(queryParams)) {
method.setQueryString(queryParams); method.setQueryString(setInputParameters(ParameterParser
.buildNField(queryParams)));
} }
setHeaders(method, headers); setHeaders(method, headers);
method.getParams().makeLenient(); method.getParams().makeLenient();
@ -217,8 +218,8 @@ public class HttpPlugin extends ParameterBarn {
int responseCode = -1; int responseCode = -1;
long contentLength = 0; long contentLength = 0;
String contentType = ""; String contentType = "";
try { try {
System.out.println(method.getURI().toString());
responseCode = this.getHttpClient().executeMethod(method); responseCode = this.getHttpClient().executeMethod(method);
method.getStatusLine().toString(); method.getStatusLine().toString();
Header[] responseHeaders = method.getResponseHeaders(); Header[] responseHeaders = method.getResponseHeaders();

View File

@ -1,18 +1,23 @@
package org.bench4q.agent.plugin.basic.random; 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.ParameterBarn;
import org.bench4q.agent.plugin.Constructor; import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter; import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin; import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.behavior.Behavior; import org.bench4q.agent.plugin.behavior.Behavior;
import org.bench4q.agent.plugin.behavior.BehaviorType; import org.bench4q.agent.plugin.behavior.BehaviorType;
import org.bench4q.agent.utils.ParameterParser;
import org.bench4q.agent.utils.Type.SupportTypes; import org.bench4q.agent.utils.Type.SupportTypes;
@Plugin(value = "Random") @Plugin(value = "Random")
public class Random extends ParameterBarn { public class Random extends ParameterBarn {
private String format; private String format;
private int begin; private int begin = -1;
private int end; private int end = -1;
private List<String> valueList = new ArrayList<String>();
private String currentValue; private String currentValue;
@Constructor @Constructor
@ -20,11 +25,13 @@ public class Random extends ParameterBarn {
@Parameter(type = SupportTypes.Field, value = "testId") String testId, @Parameter(type = SupportTypes.Field, value = "testId") String testId,
@Parameter(type = SupportTypes.Field, value = "begin") String begin, @Parameter(type = SupportTypes.Field, value = "begin") String begin,
@Parameter(type = SupportTypes.Field, value = "end") String end, @Parameter(type = SupportTypes.Field, value = "end") String end,
@Parameter(type = SupportTypes.NField, value = "valueList") String valueList,
@Parameter(type = SupportTypes.Field, value = "format") String format) { @Parameter(type = SupportTypes.Field, value = "format") String format) {
super(testId); super(testId);
this.format = format; this.format = format;
this.begin = Integer.parseInt(begin); this.begin = Integer.parseInt(begin);
this.end = Integer.parseInt(end); this.end = Integer.parseInt(end);
this.valueList = ParameterParser.buildNField(valueList);
} }
@Behavior(value = "next", type = BehaviorType.CONTROL_BEHAVIOR) @Behavior(value = "next", type = BehaviorType.CONTROL_BEHAVIOR)
@ -35,6 +42,11 @@ public class Random extends ParameterBarn {
} }
public String getValue(String var) { 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 return (this.format == null || this.format.isEmpty()) ? this.currentValue
: String.format(this.format, this.currentValue); : String.format(this.format, this.currentValue);
} }

View File

@ -62,7 +62,7 @@ public class TestWithScriptFile {
public TestWithScriptFile() { public TestWithScriptFile() {
this.setFilePath("Scripts" + System.getProperty("file.separator") this.setFilePath("Scripts" + System.getProperty("file.separator")
+ "mongo9To1.xml"); + "http.xml");
this.setHttpRequester(new HttpRequester()); this.setHttpRequester(new HttpRequester());
} }

View File

@ -1,20 +1,33 @@
package org.bench4q.agent.test.plugin; package org.bench4q.agent.test.plugin;
import java.io.File;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.cookie.MalformedCookieException; 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.PluginReturn;
import org.bench4q.agent.plugin.basic.http.HttpPlugin; import org.bench4q.agent.plugin.basic.http.HttpPlugin;
import org.bench4q.agent.plugin.basic.http.HttpReturn; 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.helper.TestHelper;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.junit.Test; 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.*; 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 static final int UnAuthorized = 401;
private HttpPlugin httpPlugin; private HttpPlugin httpPlugin;
@ -128,4 +141,15 @@ public class Test_HttpPlugin {
null, null, null, null); null, null, null, null);
assertEquals(200, result.getStatusCode()); 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();
}
} }

View File

@ -121,12 +121,12 @@
<dependency> <dependency>
<groupId>org.eclipse.jetty</groupId> <groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId> <artifactId>jetty-servlet</artifactId>
<version>9.1.0.RC2</version> <version>8.1.14.v20131031</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.eclipse.jetty</groupId> <groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId> <artifactId>jetty-server</artifactId>
<version>9.1.0.RC2</version> <version>8.1.14.v20131031</version>
</dependency> </dependency>
<!-- this is used to parse html dom tree --> <!-- this is used to parse html dom tree -->

View File

@ -59,7 +59,7 @@ public class MasterServer {
if (this.getServer() != null) { if (this.getServer() != null) {
for (Connector connector : this.getServer().getConnectors()) { for (Connector connector : this.getServer().getConnectors()) {
if (connector != null) { if (connector != null) {
connector.shutdown(); connector.close();
} }
} }
this.getServer().stop(); this.getServer().stop();

View File

@ -1,7 +1,6 @@
package org.bench4q.master.domain.entity; package org.bench4q.master.domain.entity;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
@ -115,7 +114,7 @@ public class Script {
+ Main.FILE_SEPARATOR + Main.FILE_SEPARATOR
+ multipartFile.getOriginalFilename())); + multipartFile.getOriginalFilename()));
} catch (IllegalStateException | IOException e) { } catch (Exception e) {
logger.info(ExceptionLog.getStackTrace(e)); logger.info(ExceptionLog.getStackTrace(e));
return false; return false;
} }

View File

@ -175,28 +175,49 @@ public class PluginEntityFactory {
private static class ParamTypeFactory { private static class ParamTypeFactory {
public static ParamType createParamTypeInfo(Element element) { public static ParamType createParamTypeInfo(Element element) {
String type = element.getName(); String type = element.getName();
switch (type) { if (type.equals("field")) {
case "field":
return createFieldType(element); return createFieldType(element);
case "nfield": } else if (type.equals("nfield")) {
return createNFieldType(element); return createNFieldType(element);
case "table": } else if (type.equals("table")) {
return createTableType(element); return createTableType(element);
case "radiobutton": } else if (type.equals("radiobutton")) {
return createRadioButtonType(element); return createRadioButtonType(element);
case "checkbox": } else if (type.equalsIgnoreCase("checkbox")) {
return createCheckBoxType(element); return createCheckBoxType(element);
case "combo": } else if (type.equals("combo")) {
return createComboType(element); return createComboType(element);
case "file": } else if (type.equals("file")) {
return createFileType(element); return createFileType(element);
case "date": } else if (type.equals("date")) {
return createDateType(element); return createDateType(element);
case "dropdownbox": } else if (type.equals("dropdownbox")) {
return createSelectType(element); return createSelectType(element);
default: } else {
throw new Bench4QRunTimeException("no such type:" + type); 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<Element> getChildElementsByName(Element element, public static List<Element> getChildElementsByName(Element element,
String elementName) { String elementName) {
List<Element> elements = new LinkedList<>(); List<Element> elements = new LinkedList<Element>();
for (Iterator iterator = element.elementIterator(); iterator.hasNext();) { for (Iterator iterator = element.elementIterator(); iterator.hasNext();) {
Element childElement = (Element) iterator.next(); Element childElement = (Element) iterator.next();
if (childElement.getName().equals(elementName)) if (childElement.getName().equals(elementName))

View File

@ -192,8 +192,7 @@ public class TestPlanFactory {
String content = MarshalHelper.marshal(fields[i].getType(), String content = MarshalHelper.marshal(fields[i].getType(),
fields[i].get(scriptResultModel)); fields[i].get(scriptResultModel));
testPlanScriptResult.setResultContent(content); testPlanScriptResult.setResultContent(content);
} catch (IllegalArgumentException | IllegalAccessException } catch (Exception e) {
| JAXBException e) {
Logger.getLogger(this.getClass()).info( Logger.getLogger(this.getClass()).info(
ExceptionLog.getStackTrace(e)); ExceptionLog.getStackTrace(e));
return null; return null;
@ -220,8 +219,7 @@ public class TestPlanFactory {
try { try {
monitorResult.setContent(MarshalHelper.marshal( monitorResult.setContent(MarshalHelper.marshal(
fields[i].getType(), fields[i].get(monitorMain))); fields[i].getType(), fields[i].get(monitorMain)));
} catch (IllegalArgumentException | IllegalAccessException } catch (Exception e) {
| JAXBException e) {
Logger.getLogger(this.getClass()).info( Logger.getLogger(this.getClass()).info(
ExceptionLog.getStackTrace(e)); ExceptionLog.getStackTrace(e));
return null; return null;

View File

@ -71,14 +71,14 @@ public class Test_TestResultSave extends TestBase_MakeUpTestPlan {
@Test @Test
public void testDoSaveMonitorResult() { public void testDoSaveMonitorResult() {
TestPlan testPlanFromRepo = this.getTestPlanRepository().getTestPlanInDomainBy( TestPlan testPlanFromRepo = this.getTestPlanRepository()
this.getTestPlanRunIdUuid()); .getTestPlanInDomainBy(this.getTestPlanRunIdUuid());
int beforeInsertCount = getMonitorResultSize(getOneMonitor( int beforeInsertCount = getMonitorResultSize(getOneMonitor(
testPlanFromRepo, Monitor_Host_Name)); testPlanFromRepo, Monitor_Host_Name));
assertTrue(beforeInsertCount == 0); assertTrue(beforeInsertCount == 0);
TestPlan testPlan = this.getTestPlanFactory().convertToDomain( TestPlan testPlan = this.getTestPlanFactory().convertToDomain(
testPlanFromRepo); testPlanFromRepo);
List<Object> resultList = new ArrayList<>(); List<Object> resultList = new ArrayList<Object>();
resultList.addAll(getOneMonitor(testPlan, Monitor_Host_Name) resultList.addAll(getOneMonitor(testPlan, Monitor_Host_Name)
.doAfterRun()); .doAfterRun());
this.getTestResultSave().update(testPlan, resultList); this.getTestResultSave().update(testPlan, resultList);
@ -93,8 +93,8 @@ public class Test_TestResultSave extends TestBase_MakeUpTestPlan {
@Test @Test
public void testDoSaveTestPlanScriptResult() { public void testDoSaveTestPlanScriptResult() {
TestPlan testPlanFromRepo = this.getTestPlanRepository().getTestPlanInDomainBy( TestPlan testPlanFromRepo = this.getTestPlanRepository()
this.getTestPlanRunIdUuid()); .getTestPlanInDomainBy(this.getTestPlanRunIdUuid());
TestPlanScript testPlanScript = getOneTestPlanScript(testPlanFromRepo); TestPlanScript testPlanScript = getOneTestPlanScript(testPlanFromRepo);
int beforeInsertCount = getTestPlanScriptResultSize(testPlanScript); int beforeInsertCount = getTestPlanScriptResultSize(testPlanScript);
assertTrue(beforeInsertCount == 0); assertTrue(beforeInsertCount == 0);
@ -104,7 +104,7 @@ public class Test_TestResultSave extends TestBase_MakeUpTestPlan {
assertEquals(TestPlanStatus.InRunning.name(), assertEquals(TestPlanStatus.InRunning.name(),
testPlan.getCurrentStatus()); testPlan.getCurrentStatus());
testPlanScript = getOneTestPlanScript(testPlan); testPlanScript = getOneTestPlanScript(testPlan);
List<Object> resultList = new ArrayList<>(); List<Object> resultList = new ArrayList<Object>();
resultList.addAll(testPlanScript.doAfterRun()); resultList.addAll(testPlanScript.doAfterRun());
this.getTestResultSave().update(testPlan, resultList); this.getTestResultSave().update(testPlan, resultList);
testPlanFromRepo = this.getTestPlanRepository().getTestPlanInDomainBy( testPlanFromRepo = this.getTestPlanRepository().getTestPlanInDomainBy(

View File

@ -178,8 +178,8 @@
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version> <version>2.3.2</version>
<configuration> <configuration>
<source>1.7</source> <source>1.6</source>
<target>1.7</target> <target>1.6</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>