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>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.1.0.M0</version>
<version>8.1.14.v20131031</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.1.0.M0</version>
<version>8.1.14.v20131031</version>
</dependency>
<!-- For spring begin -->
<dependency>
@ -105,6 +105,15 @@
</dependencies>
<build>
<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>
<artifactId>maven-jar-plugin</artifactId>
<configuration>

View File

@ -96,8 +96,7 @@ public class HttpPlugin extends ParameterBarn {
return nvPairs;
}
private static NameValuePair[] setInputParameters(
List<String> inputParameters) {
private NameValuePair[] setInputParameters(List<String> inputParameters) {
Set<NameValuePair> res = new HashSet<NameValuePair>();
Iterator<String> paramIter = inputParameters.iterator();
@ -148,38 +147,39 @@ public class HttpPlugin extends ParameterBarn {
}
private Callable<HttpReturn> buildTas(final Row row) {
switch (row.getCell(0).toLowerCase()) {
case "get":
return new Callable<HttpReturn>() {
@Override
public HttpReturn call() throws Exception {
return get(row.getCell(1), null, null, null);
}
};
case "post":
return new Callable<HttpReturn>() {
@Override
public HttpReturn call() throws Exception {
return post(row.getCell(1), null, null, null, null, null);
}
};
case "put":
return new Callable<HttpReturn>() {
@Override
public HttpReturn call() throws Exception {
return put(row.getCell(1), null, null, null, null, null);
}
};
case "delete":
return new Callable<HttpReturn>() {
@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<HttpReturn>() {
// @Override
// public HttpReturn call() throws Exception {
// return get(row.getCell(1), null, null, null);
// }
// };
// case "post":
// return new Callable<HttpReturn>() {
// @Override
// public HttpReturn call() throws Exception {
// return post(row.getCell(1), null, null, null, null, null);
// }
// };
// case "put":
// return new Callable<HttpReturn>() {
// @Override
// public HttpReturn call() throws Exception {
// return put(row.getCell(1), null, null, null, null, null);
// }
// };
// case "delete":
// return new Callable<HttpReturn>() {
// @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();

View File

@ -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<String> valueList = new ArrayList<String>();
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);
}

View File

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

View File

@ -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();
}
}

View File

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

View File

@ -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();

View File

@ -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;
}

View File

@ -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<Element> getChildElementsByName(Element element,
String elementName) {
List<Element> elements = new LinkedList<>();
List<Element> elements = new LinkedList<Element>();
for (Iterator iterator = element.elementIterator(); iterator.hasNext();) {
Element childElement = (Element) iterator.next();
if (childElement.getName().equals(elementName))

View File

@ -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;

View File

@ -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<Object> resultList = new ArrayList<>();
List<Object> resultList = new ArrayList<Object>();
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<Object> resultList = new ArrayList<>();
List<Object> resultList = new ArrayList<Object>();
resultList.addAll(testPlanScript.doAfterRun());
this.getTestResultSave().update(testPlan, resultList);
testPlanFromRepo = this.getTestPlanRepository().getTestPlanInDomainBy(

View File

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