refactor
This commit is contained in:
coderfengyun 2014-03-24 11:20:04 +08:00
parent 23f5642b3b
commit 86ae689d2b
8 changed files with 71 additions and 116 deletions

View File

@ -11,7 +11,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.bench4q.agent.parameterization.SessionObject;
public class InstanceController implements SessionObject {
public class ParamCoordinator implements SessionObject {
private String userDefineParameterFolderPath = "/home/yxsh/git/Bench4Q-Agent/parameterClass/";
private UUID testRunId;
private UUID id = java.util.UUID.randomUUID();
@ -21,20 +21,45 @@ public class InstanceController implements SessionObject {
private Map<String, Object> cacheObjMap = new HashMap<String, Object>();
private ReentrantReadWriteLock mapRWLock = new ReentrantReadWriteLock();
private InstanceController() {
private ParamCoordinator() {
}
protected UUID getRunId() {
return testRunId;
}
public InstanceController(UUID testRunId, UUID controllerId) {
public ParamCoordinator(UUID testRunId, UUID controllerId) {
this();
this.id = controllerId;
this.testRunId = testRunId;
}
String instanceLevelGetParameter(String name, String className,
private String getParameter(ParamFormatParser paraFormat) {
String result;
result = this.getParameterByContext(paraFormat.getName());
if (result != null)
return result;
String[] argsArray = paraFormat.getArgsArray();
if (paraFormat.getParamType().equals("crossThread")) {
result = this.getParameter(
paraFormat.getName(),
getCurrentPackageFullName() + "."
+ paraFormat.getClassName(),
paraFormat.getMethodName(), argsArray);
} else if (paraFormat.getParamType().equals("inThread")) {
result = this.instanceLevelGetParameter(paraFormat.getName(),
paraFormat.getClassName(), paraFormat.getMethodName(),
argsArray);
}
return result;
}
private static String getCurrentPackageFullName() {
return ParamFormatParser.class.getName().substring(0,
ParamFormatParser.class.getName().lastIndexOf('.'));
}
private String instanceLevelGetParameter(String name, String className,
String functionName, Object[] args) {
boolean hasThisClass = false;
@ -70,7 +95,7 @@ public class InstanceController implements SessionObject {
}
String getParameterByContext(String name) {
private String getParameterByContext(String name) {
if (false == this.runtimeParamMap.containsKey(name)) {
return null;
}
@ -85,7 +110,6 @@ public class InstanceController implements SessionObject {
Object instance = cls.newInstance();
mapRWLock.writeLock().lock();
objMap.put(className, instance);
} catch (Exception ex) {
return false;
}
@ -101,8 +125,8 @@ public class InstanceController implements SessionObject {
return result;
}
String getParameter(String name, String className, String functionName,
Object[] args) {
private String getParameter(String name, String className,
String functionName, Object[] args) {
ParametersFactory paramFactor = ParametersFactory.getInstance();
boolean hasThisClass = paramFactor.containObj(className);
if (false == hasThisClass) {
@ -146,7 +170,7 @@ public class InstanceController implements SessionObject {
public String getParam(String name) {
if (!(name.startsWith("<parameters") && name.endsWith("/>")))
return name;
return ParameterizationParser.parse(name, this);
return this.getParameter(ParamFormatParser.parseInputParameter(name));
}
public void saveRuntimeParam(String name, String value) {

View File

@ -9,7 +9,7 @@ import org.bench4q.agent.utils.ParameterParser;
import org.bench4q.share.helper.MarshalHelper;
@XmlRootElement(name = "parameters")
public class ParameterFormat {
public class ParamFormatParser {
private String name;
private String className;
private String methodName;
@ -62,20 +62,24 @@ public class ParameterFormat {
this.paramType = paramType;
}
public List<String> getArgs() {
return args;
private List<String> getArgs() {
return this.args;
}
private void setArgs(List<String> args) {
this.args = args;
}
private ParameterFormat() {
public String[] getArgsArray() {
return this.getArgs().toArray(new String[] {});
}
public static ParameterFormat parseInputParameter(String inputContent) {
ParameterFormat result = (ParameterFormat) MarshalHelper.tryUnmarshal(
ParameterFormat.class, inputContent);
private ParamFormatParser() {
}
public static ParamFormatParser parseInputParameter(String inputContent) {
ParamFormatParser result = (ParamFormatParser) MarshalHelper.tryUnmarshal(
ParamFormatParser.class, inputContent);
result.setArgs(ParameterParser.buildNField(result.getArgsString()));
return result;
}

View File

@ -1,54 +0,0 @@
package org.bench4q.agent.parameterization.impl;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
public class ParameterizationParser {
public static String parse(String text, InstanceController insCon) {
// Pattern pattern = Pattern.compile("<parameter className=""/>");
String result = "";
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db
.parse(new InputSource(new StringReader(text)));
Element root = document.getDocumentElement();
String className = root.getAttribute("class");
String methodName = root.getAttribute("method");
String argString = root.getAttribute("args");
String type = root.getAttribute("type");
String[] args = argString.split(",");
if (argString.trim().equals(""))
args = new String[0];
String name = root.getAttribute("name");
result = insCon.getParameterByContext(name);
if (result != null)
return result;
if (type.equals("crossThread")) {
result = insCon.getParameter(name, getCurrentPackageFullName()
+ "." + className, methodName, args);
} else if (type.equals("inThread")) {
result = insCon.instanceLevelGetParameter(name, className,
methodName, args);
}
} catch (Exception ex) {
return text;
}
return result;
}
private static String getCurrentPackageFullName() {
return ParameterizationParser.class.getName().substring(0,
ParameterizationParser.class.getName().lastIndexOf('.'));
}
}

View File

@ -6,7 +6,7 @@ import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.log4j.Logger;
import org.bench4q.agent.parameterization.impl.InstanceController;
import org.bench4q.agent.parameterization.impl.ParamCoordinator;
import org.bench4q.agent.storage.StorageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -81,7 +81,7 @@ public class ScenarioEngine {
public void run() {
while (!scenarioContext.isFinished()) {
scenarioContext.getExecutor().execute(
new Worker(scenarioContext, new InstanceController(
new Worker(scenarioContext, new ParamCoordinator(
runId, UUID.randomUUID())));
}
}

View File

@ -2,14 +2,14 @@ package org.bench4q.agent.test.parameterization;
import java.util.UUID;
import org.bench4q.agent.parameterization.impl.InstanceController;
import org.bench4q.agent.parameterization.impl.ParamCoordinator;
import org.bench4q.share.helper.TestHelper;
public class TEST_HelloThread extends Thread {
InstanceController ic;
ParamCoordinator ic;
public TEST_HelloThread() {
ic = new InstanceController(UUID.randomUUID(), UUID.randomUUID());
ic = new ParamCoordinator(UUID.randomUUID(), UUID.randomUUID());
}
public void run() {

View File

@ -5,12 +5,19 @@ import static org.junit.Assert.*;
import java.util.HashMap;
import java.util.UUID;
import org.bench4q.agent.parameterization.impl.InstanceController;
import org.bench4q.agent.parameterization.impl.ParamCoordinator;
import org.bench4q.agent.parameterization.impl.Para_Table;
import org.bench4q.share.helper.TestHelper;
import org.junit.Test;
public class Test_InstanceCotroller {
/**
* The old test case is splited by ',', but it's not robust The new test
* case is splited by ParameterParser.parseNField, It's more robust
*/
// private static final String OLD_TEST_CASE =
// "<parameters name=\"useNamePassword\" class=\"Para_Table\" type=\"crossThread\" method=\"getTableColumnValue\" args=\"file,ScenarioParameters\\param1.txt,0,sequence,;,~,2\" />";
private static final String NEW_TEST_CASE = "<parameters name=\"useNamePassword\" class=\"Para_Table\" type=\"crossThread\" method=\"getTableColumnValue\" args=\"file;ScenarioParameters\\param1.txt;0;sequence;\\;;~;2\" />";
@Test
public void testGetParam() throws Exception {
@ -20,17 +27,20 @@ public class Test_InstanceCotroller {
"ScenarioParameters\\param1.txt", "0", "sequence", ";", "~",
"2");
System.out.println(ret);
assertEquals(11, Integer.parseInt(ret));
}
InstanceController ic = new InstanceController(UUID.randomUUID(),
@Test
public void testGetParam2() throws Exception {
ParamCoordinator ic = new ParamCoordinator(UUID.randomUUID(),
UUID.randomUUID());
String passwordName = ic
.getParam("<parameters name=\"useNamePassword\" class=\"Para_Table\" type=\"crossThread\" method=\"getTableColumnValue\" args=\"file,ScenarioParameters\\param1.txt,0,sequence,;,~,2\" />");
String passwordName = ic.getParam(NEW_TEST_CASE);
System.out.println(passwordName);
assertNotNull(passwordName);
InstanceController instanceControler = new InstanceController(
assertEquals(11, Integer.parseInt(passwordName));
ParamCoordinator instanceControler = new ParamCoordinator(
UUID.randomUUID(), UUID.randomUUID());
String password2 = instanceControler
.getParam("<parameters name=\"useNamePassword\" class=\"Para_Table\" type=\"crossThread\" method=\"getTableColumnValue\" args=\"file,ScenarioParameters\\param1.txt,0,sequence,;,~,2\" />");
String password2 = instanceControler.getParam(NEW_TEST_CASE);
System.out.println(password2);
assertNotNull(password2);
assertEquals(Integer.parseInt(passwordName) + 10,

View File

@ -2,7 +2,7 @@ package org.bench4q.agent.test.parameterization;
import static org.junit.Assert.*;
import org.bench4q.agent.parameterization.impl.ParameterFormat;
import org.bench4q.agent.parameterization.impl.ParamFormatParser;
import org.junit.After;
import org.junit.Test;
@ -15,7 +15,7 @@ public class Test_ParameterFormat {
@Test
public void test() {
ParameterFormat format = ParameterFormat.parseInputParameter(testcase);
ParamFormatParser format = ParamFormatParser.parseInputParameter(testcase);
assertNotNull(format);
assertEquals("useNamePassword", format.getName());
assertEquals("Para_Table", format.getClassName());
@ -23,8 +23,8 @@ public class Test_ParameterFormat {
assertEquals("crossThread", format.getParamType());
assertEquals("file;ScenarioParameters\\param1.txt;0;sequence;\\;;~;2",
format.getArgsString());
assertEquals(7, format.getArgs().size());
assertEquals("file", format.getArgs().get(0));
assertEquals(";", format.getArgs().get(4));
assertEquals(7, format.getArgsArray().length);
assertEquals("file", format.getArgsArray()[0]);
assertEquals(";", format.getArgsArray()[4]);
}
}

View File

@ -1,29 +0,0 @@
package org.bench4q.agent.test.parameterization;
import static org.junit.Assert.assertEquals;
import java.util.UUID;
import org.bench4q.agent.parameterization.impl.InstanceController;
import org.bench4q.agent.parameterization.impl.ParameterizationParser;
import org.bench4q.share.helper.TestHelper;
import org.junit.Test;
public class Test_ParameterizationParser {
@Test
public void testGetClassName() throws Exception {
String result = (String) TestHelper.invokePrivate(
new ParameterizationParser(), "getCurrentPackageFullName",
null, null);
assertEquals("org.bench4q.agent.parameterization.impl", result);
}
@Test
public void testParse() {
String result = ParameterizationParser.parse(
"<parameter class=\"Para_IteratorNumber\" method=\"getIteratorNumber\""
+ " type=\"crossThread\"/>", new InstanceController(
UUID.randomUUID(), UUID.randomUUID()));
System.out.println(result);
}
}