refactor the parameterization and parameterparer part

refactor the parameterization and parameterparer part
This commit is contained in:
coderfengyun 2014-03-21 18:33:59 +08:00
parent 98525d2c7d
commit b1cc057f4d
32 changed files with 302 additions and 129 deletions

View File

@ -1,3 +1,4 @@
isToSaveDetailResult=false isToSaveDetailResult=false
servePort=6565 servePort=6565
userDefinedParamFolder=userDefinedParams userDefinedParamFolder=userDefinedParams
scenarioParamFolder=ScenarioParameters

View File

@ -9,6 +9,7 @@ import java.util.Properties;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
public class Main { public class Main {
private static String LINE_SEPARATOR = System.getProperty("line.separator");
private static final String CONFIG_FILE_NAME = "agent-config.properties"; private static final String CONFIG_FILE_NAME = "agent-config.properties";
private static String DIR_PATH = "configure" private static String DIR_PATH = "configure"
+ System.getProperty("file.separator"); + System.getProperty("file.separator");
@ -16,6 +17,7 @@ public class Main {
private static int PORT_TO_SERVE; private static int PORT_TO_SERVE;
public static boolean IS_TO_SAVE_DETAIL; public static boolean IS_TO_SAVE_DETAIL;
public static String USER_DEFINED_PARAMS_FOLDER; public static String USER_DEFINED_PARAMS_FOLDER;
public static String SCENARIO_PARAMETERS_FOLDER;
static { static {
init(); init();
@ -46,9 +48,11 @@ public class Main {
try { try {
if (configFile.createNewFile()) { if (configFile.createNewFile()) {
FileOutputStream outputStream = new FileOutputStream(configFile); FileOutputStream outputStream = new FileOutputStream(configFile);
String content = "isToSaveDetailResult=false" + "\n" String content = "isToSaveDetailResult=false" + LINE_SEPARATOR
+ "servePort=6565" + "\n" + "servePort=6565" + LINE_SEPARATOR
+ "userDefinedParamFolder=userDefinedParams"; + "userDefinedParamFolder=userDefinedParams"
+ LINE_SEPARATOR
+ "scenarioParamFolder=ScenarioParameters";
outputStream.write(content.getBytes()); outputStream.write(content.getBytes());
outputStream.flush(); outputStream.flush();
outputStream.close(); outputStream.close();
@ -69,6 +73,8 @@ public class Main {
.get("isToSaveDetailResult")); .get("isToSaveDetailResult"));
USER_DEFINED_PARAMS_FOLDER = properties USER_DEFINED_PARAMS_FOLDER = properties
.getProperty("userDefinedParamFolder"); .getProperty("userDefinedParamFolder");
SCENARIO_PARAMETERS_FOLDER = properties
.getProperty("scenarioParamFolder");
guardUserDefinedFolderExists(); guardUserDefinedFolderExists();
} catch (IOException e) { } catch (IOException e) {
logger.error("There is an error when getPortToServe!"); logger.error("There is an error when getPortToServe!");

View File

@ -11,15 +11,29 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.bench4q.agent.parameterization.SessionObject; import org.bench4q.agent.parameterization.SessionObject;
public class InstanceControler implements SessionObject { public class InstanceController implements SessionObject {
private String userDefineParameterFolderPath = "/home/yxsh/git/Bench4Q-Agent/parameterClass/"; private String userDefineParameterFolderPath = "/home/yxsh/git/Bench4Q-Agent/parameterClass/";
private UUID instandid = java.util.UUID.randomUUID(); private UUID testRunId;
private UUID id = java.util.UUID.randomUUID();
private Set<String> usedClassName = new HashSet<String>(); private Set<String> usedClassName = new HashSet<String>();
private Map<String, Object> objMap = new HashMap<String, Object>(); private Map<String, Object> objMap = new HashMap<String, Object>();
private Map<String, String> runtimeParaMap = new HashMap<String, String>(); private Map<String, String> runtimeParamMap = new HashMap<String, String>();
private Map<String, Object> cacheObjMap = new HashMap<String, Object>(); private Map<String, Object> cacheObjMap = new HashMap<String, Object>();
private ReentrantReadWriteLock mapRWLock = new ReentrantReadWriteLock(); private ReentrantReadWriteLock mapRWLock = new ReentrantReadWriteLock();
private InstanceController() {
}
protected UUID getRunId() {
return testRunId;
}
public InstanceController(UUID testRunId, UUID controllerId) {
this();
this.id = controllerId;
this.testRunId = testRunId;
}
String instanceLevelGetParameter(String name, String className, String instanceLevelGetParameter(String name, String className,
String functionName, Object[] args) { String functionName, Object[] args) {
@ -34,10 +48,10 @@ public class InstanceControler implements SessionObject {
Object result = null; Object result = null;
try { try {
Class<?>[] argTypeArr = new Class<?>[args.length + 2]; Class<?>[] argTypeArr = new Class<?>[args.length + 2];
argTypeArr[0] = instandid.getClass(); argTypeArr[0] = id.getClass();
argTypeArr[1] = this.cacheObjMap.getClass(); argTypeArr[1] = this.cacheObjMap.getClass();
Object[] totalArgs = new Object[args.length + 2]; Object[] totalArgs = new Object[args.length + 2];
totalArgs[0] = instandid; totalArgs[0] = id;
totalArgs[1] = this.cacheObjMap; totalArgs[1] = this.cacheObjMap;
for (int i = 2; i < args.length + 2; i++) { for (int i = 2; i < args.length + 2; i++) {
argTypeArr[i] = args[i - 2].getClass(); argTypeArr[i] = args[i - 2].getClass();
@ -51,23 +65,23 @@ public class InstanceControler implements SessionObject {
.getTargetException().getMessage()); .getTargetException().getMessage());
return null; return null;
} }
runtimeParaMap.put(name, (String) result); runtimeParamMap.put(name, (String) result);
return (String) result; return (String) result;
} }
String getParameterByContext(String name) { String getParameterByContext(String name) {
if (false == this.runtimeParaMap.containsKey(name)) { if (false == this.runtimeParamMap.containsKey(name)) {
return null; return null;
} }
return runtimeParaMap.get(name); return runtimeParamMap.get(name);
} }
private boolean createObj(String className) { private boolean createObj(String className) {
try { try {
MyFileClassLoader cl = new MyFileClassLoader(); MyFileClassLoader classLoader = new MyFileClassLoader(
cl.setClassPath(userDefineParameterFolderPath); userDefineParameterFolderPath);
Class<?> cls = cl.loadClass(className); Class<?> cls = classLoader.loadClass(className);
Object instance = cls.newInstance(); Object instance = cls.newInstance();
mapRWLock.writeLock().lock(); mapRWLock.writeLock().lock();
objMap.put(className, instance); objMap.put(className, instance);
@ -89,7 +103,6 @@ public class InstanceControler implements SessionObject {
String getParameter(String name, String className, String functionName, String getParameter(String name, String className, String functionName,
Object[] args) { Object[] args) {
ParametersFactory paramFactor = ParametersFactory.getInstance(); ParametersFactory paramFactor = ParametersFactory.getInstance();
boolean hasThisClass = paramFactor.containObj(className); boolean hasThisClass = paramFactor.containObj(className);
if (false == hasThisClass) { if (false == hasThisClass) {
@ -100,10 +113,10 @@ public class InstanceControler implements SessionObject {
Object result = null; Object result = null;
try { try {
Class<?>[] argTypeArr = new Class<?>[args.length + 2]; Class<?>[] argTypeArr = new Class<?>[args.length + 2];
argTypeArr[0] = instandid.getClass(); argTypeArr[0] = id.getClass();
argTypeArr[1] = this.cacheObjMap.getClass(); argTypeArr[1] = this.cacheObjMap.getClass();
Object[] totalArgs = new Object[args.length + 2]; Object[] totalArgs = new Object[args.length + 2];
totalArgs[0] = instandid; totalArgs[0] = id;
totalArgs[1] = this.cacheObjMap; totalArgs[1] = this.cacheObjMap;
for (int i = 2; i < args.length + 2; i++) { for (int i = 2; i < args.length + 2; i++) {
argTypeArr[i] = args[i - 2].getClass(); argTypeArr[i] = args[i - 2].getClass();
@ -122,7 +135,7 @@ public class InstanceControler implements SessionObject {
} }
usedClassName.add(className); usedClassName.add(className);
runtimeParaMap.put(name, (String) result); runtimeParamMap.put(name, (String) result);
return (String) result; return (String) result;
} }
@ -137,11 +150,11 @@ public class InstanceControler implements SessionObject {
} }
public void saveRuntimeParam(String name, String value) { public void saveRuntimeParam(String name, String value) {
runtimeParaMap.put(name, value); runtimeParamMap.put(name, value);
} }
public void saveRuntimeParams(Map<String, String> runTimeParams) { public void saveRuntimeParams(Map<String, String> runTimeParams) {
runtimeParaMap.putAll(runTimeParams); runtimeParamMap.putAll(runTimeParams);
} }
public void doCleanUp() { public void doCleanUp() {
@ -169,10 +182,9 @@ public class InstanceControler implements SessionObject {
try { try {
Method m = instance.getClass().getMethod("unreg", Method m = instance.getClass().getMethod("unreg", id.getClass());
instandid.getClass());
m.invoke(instance, instandid); m.invoke(instance, id);
} catch (Exception ex) { } catch (Exception ex) {
System.out.println("realse failed"); System.out.println("realse failed");

View File

@ -4,35 +4,25 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class MyFileClassLoader extends ClassLoader { public class MyFileClassLoader extends ClassLoader {
private String classPath; private String classPath;
public static void main(String[] args) throws ClassNotFoundException, private void setClassPath(String classPath) {
InstantiationException, IllegalAccessException, this.classPath = classPath;
IllegalArgumentException, InvocationTargetException { }
MyFileClassLoader fileClsLoader = new MyFileClassLoader();
fileClsLoader private MyFileClassLoader() {
.setClassPath("E:\\j2ee_proj\\skythink\\WebContent\\WEB-INF\\classes\\"); }
Class<?> cls = fileClsLoader
.loadClass("com.cmw.entity.sys.AccordionEntity"); public MyFileClassLoader(String classPath) {
Object obj = cls.newInstance(); this();
Method[] mthds = cls.getMethods(); this.setClassPath(classPath);
for (Method mthd : mthds) {
String methodName = mthd.getName();
System.out.println("mthd.name=" + methodName);
}
System.out.println("obj.class=" + obj.getClass().getName());
System.out.println("obj.class=" + cls.getClassLoader().toString());
System.out.println("obj.class="
+ cls.getClassLoader().getParent().toString());
} }
/** /**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> * 锟斤拷锟斤拷锟斤拷锟斤拷址锟斤拷指锟斤拷锟斤拷目录锟斤拷锟斤拷锟洁锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟<EFBFBD>
*/ */
@Override @Override
protected Class<?> findClass(String name) throws ClassNotFoundException { protected Class<?> findClass(String name) throws ClassNotFoundException {
@ -46,11 +36,11 @@ public class MyFileClassLoader extends ClassLoader {
} }
/** /**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> byte <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> * 锟斤拷锟斤拷锟斤拷锟斤拷址锟斤拷锟斤拷锟斤拷 byte 锟斤拷锟斤拷锟<EFBFBD>
* *
* @param name * @param name
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD> <EFBFBD><EFBFBD><EFBFBD> com.cmw.entity.SysEntity * 锟斤拷锟斤拷锟街凤拷 锟斤拷锟界 com.cmw.entity.SysEntity
* @return <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD> byte <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> * @return 锟斤拷锟斤拷锟斤拷锟侥硷拷 byte 锟斤拷锟斤拷锟<EFBFBD>
* @throws IOException * @throws IOException
*/ */
private byte[] loadClassData(String name) throws IOException { private byte[] loadClassData(String name) throws IOException {
@ -63,17 +53,17 @@ public class MyFileClassLoader extends ClassLoader {
} }
/** /**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD> File <EFBFBD><EFBFBD><EFBFBD><EFBFBD> * 锟斤拷锟斤拷锟斤拷锟斤拷址锟斤拷锟揭伙拷锟<EFBFBD>File 锟斤拷锟斤拷
* *
* @param name * @param name
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD> * 锟斤拷锟斤拷锟街凤拷
* @return File <EFBFBD><EFBFBD><EFBFBD><EFBFBD> * @return File 锟斤拷锟斤拷
* @throws FileNotFoundException * @throws FileNotFoundException
*/ */
private File getFile(String name) throws FileNotFoundException { private File getFile(String name) throws FileNotFoundException {
File dir = new File(classPath); File dir = new File(classPath);
if (!dir.exists()) if (!dir.exists())
throw new FileNotFoundException(classPath + " Ŀ¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD>"); throw new FileNotFoundException(classPath + " 目录锟斤拷锟斤拷锟节o拷");
String _classPath = classPath.replaceAll("[\\\\]", "/"); String _classPath = classPath.replaceAll("[\\\\]", "/");
int offset = _classPath.lastIndexOf("/"); int offset = _classPath.lastIndexOf("/");
name = name.replaceAll("[.]", "/"); name = name.replaceAll("[.]", "/");
@ -83,16 +73,8 @@ public class MyFileClassLoader extends ClassLoader {
_classPath += name + ".class"; _classPath += name + ".class";
dir = new File(_classPath); dir = new File(_classPath);
if (!dir.exists()) if (!dir.exists())
throw new FileNotFoundException(dir + " <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD>"); throw new FileNotFoundException(dir + " 锟斤拷锟斤拷锟节o拷");
return dir; return dir;
} }
public String getClassPath() {
return classPath;
}
public void setClassPath(String classPath) {
this.classPath = classPath;
}
} }

View File

@ -0,0 +1,82 @@
package org.bench4q.agent.parameterization.impl;
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import org.bench4q.agent.utils.ParameterParser;
import org.bench4q.share.helper.MarshalHelper;
@XmlRootElement(name = "parameters")
public class ParameterFormat {
private String name;
private String className;
private String methodName;
private String argsString;
private String paramType;
private List<String> args;
@XmlAttribute(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlAttribute(name = "class")
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
@XmlAttribute(name = "method")
public String getMethodName() {
return methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
@XmlAttribute(name = "args")
public String getArgsString() {
return argsString;
}
public void setArgsString(String argsString) {
this.argsString = argsString;
}
@XmlAttribute(name = "type")
public String getParamType() {
return paramType;
}
public void setParamType(String paramType) {
this.paramType = paramType;
}
public List<String> getArgs() {
return args;
}
private void setArgs(List<String> args) {
this.args = args;
}
private ParameterFormat() {
}
public static ParameterFormat parseInputParameter(String inputContent) {
ParameterFormat result = (ParameterFormat) MarshalHelper.tryUnmarshal(
ParameterFormat.class, inputContent);
result.setArgs(ParameterParser.buildNField(result.getArgsString()));
return result;
}
}

View File

@ -11,7 +11,7 @@ import org.xml.sax.InputSource;
public class ParameterizationParser { public class ParameterizationParser {
public static String parse(String text, InstanceControler insCon) { public static String parse(String text, InstanceController insCon) {
// Pattern pattern = Pattern.compile("<parameter className=""/>"); // Pattern pattern = Pattern.compile("<parameter className=""/>");
String result = ""; String result = "";
try { try {
@ -23,6 +23,7 @@ public class ParameterizationParser {
String className = root.getAttribute("class"); String className = root.getAttribute("class");
String methodName = root.getAttribute("method"); String methodName = root.getAttribute("method");
String argString = root.getAttribute("args"); String argString = root.getAttribute("args");
String type = root.getAttribute("type"); String type = root.getAttribute("type");
String[] args = argString.split(","); String[] args = argString.split(",");
if (argString.trim().equals("")) if (argString.trim().equals(""))

View File

@ -35,8 +35,7 @@ public class ParametersFactory {
public boolean createObj(String className) { public boolean createObj(String className) {
try { try {
MyFileClassLoader cl = new MyFileClassLoader(); MyFileClassLoader cl = new MyFileClassLoader(rootFilePath);
cl.setClassPath(rootFilePath);
Class<?> cls = cl.loadClass(className); Class<?> cls = cl.loadClass(className);
Object instance = cls.newInstance(); Object instance = cls.newInstance();
mapRWLock.writeLock().lock(); mapRWLock.writeLock().lock();

View File

@ -5,7 +5,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import org.bench4q.agent.scenario.util.Type.SupportTypes; import org.bench4q.agent.utils.Type.SupportTypes;
@Target(ElementType.PARAMETER) @Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)

View File

@ -7,7 +7,7 @@ import org.bench4q.agent.plugin.Behavior;
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.result.CommandLineReturn; import org.bench4q.agent.plugin.result.CommandLineReturn;
import org.bench4q.agent.scenario.util.Type.SupportTypes; import org.bench4q.agent.utils.Type.SupportTypes;
@Plugin("CommandLine") @Plugin("CommandLine")
public class CommandLinePlugin { public class CommandLinePlugin {

View File

@ -5,7 +5,7 @@ import org.bench4q.agent.plugin.Behavior;
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.result.TimerReturn; import org.bench4q.agent.plugin.result.TimerReturn;
import org.bench4q.agent.scenario.util.Type.SupportTypes; import org.bench4q.agent.utils.Type.SupportTypes;
@Plugin("ConstantTimer") @Plugin("ConstantTimer")
public class ConstantTimerPlugin { public class ConstantTimerPlugin {

View File

@ -4,7 +4,7 @@ import org.bench4q.agent.plugin.Behavior;
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.result.LogReturn; import org.bench4q.agent.plugin.result.LogReturn;
import org.bench4q.agent.scenario.util.Type.SupportTypes; import org.bench4q.agent.utils.Type.SupportTypes;
@Plugin("Log") @Plugin("Log")
public class LogPlugin { public class LogPlugin {

View File

@ -33,9 +33,9 @@ import org.bench4q.agent.plugin.Behavior;
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.result.HttpReturn; import org.bench4q.agent.plugin.result.HttpReturn;
import org.bench4q.agent.scenario.util.ParameterParser; import org.bench4q.agent.utils.ParameterParser;
import org.bench4q.agent.scenario.util.Type.SupportTypes; import org.bench4q.agent.utils.Type.SupportTypes;
import org.bench4q.agent.scenario.util.types.Table; import org.bench4q.agent.utils.types.Table;
@Plugin("Http") @Plugin("Http")
public class HttpPlugin { public class HttpPlugin {

View File

@ -6,7 +6,7 @@ import java.util.UUID;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.bench4q.agent.parameterization.impl.InstanceControler; import org.bench4q.agent.parameterization.impl.InstanceController;
import org.bench4q.agent.storage.StorageHelper; import org.bench4q.agent.storage.StorageHelper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -66,10 +66,11 @@ public class ScenarioEngine {
return false; return false;
} }
final ScenarioContext context = this.getRunningTests().get(runId); final ScenarioContext context = this.getRunningTests().get(runId);
return runWith(context); return runWith(context, runId);
} }
private boolean runWith(final ScenarioContext scenarioContext) { private boolean runWith(final ScenarioContext scenarioContext,
final UUID runId) {
if (scenarioContext == null) { if (scenarioContext == null) {
logger.error("The context required is null"); logger.error("The context required is null");
return false; return false;
@ -79,10 +80,9 @@ public class ScenarioEngine {
taskMaker.execute(new Runnable() { taskMaker.execute(new Runnable() {
public void run() { public void run() {
while (!scenarioContext.isFinished()) { while (!scenarioContext.isFinished()) {
scenarioContext.getExecutor() scenarioContext.getExecutor().execute(
.execute( new Worker(scenarioContext, new InstanceController(
new Worker(scenarioContext, runId, UUID.randomUUID())));
new InstanceControler()));
} }
} }
}); });

View File

@ -1,5 +0,0 @@
package org.bench4q.agent.scenario.util.types;
public class CheckBox {
}

View File

@ -1,7 +0,0 @@
package org.bench4q.agent.scenario.util.types;
import org.bench4q.agent.scenario.util.Type;
public class Filed extends Type {
}

View File

@ -1,5 +0,0 @@
package org.bench4q.agent.scenario.util.types;
public class NField {
}

View File

@ -1,5 +0,0 @@
package org.bench4q.agent.scenario.util.types;
public class Radio {
}

View File

@ -1,4 +1,4 @@
package org.bench4q.agent.plugin.basic.http; package org.bench4q.agent.utils;
public class ParameterConstant { public class ParameterConstant {
public static final String escape = "\\"; public static final String escape = "\\";

View File

@ -1,11 +1,10 @@
package org.bench4q.agent.scenario.util; package org.bench4q.agent.utils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.StringTokenizer;
import org.bench4q.agent.plugin.basic.http.ParameterConstant; import org.bench4q.agent.utils.types.Table;
import org.bench4q.agent.scenario.util.types.Table;
/** /**
* *
@ -17,10 +16,9 @@ public abstract class ParameterParser {
private static final String NFIELD_SEPARATOR = RadioGroup_SEPARATOR; private static final String NFIELD_SEPARATOR = RadioGroup_SEPARATOR;
static public List<String> buildNField(String value) { static public List<String> buildNField(String value) {
StringTokenizer st = new StringTokenizer(value, NFIELD_SEPARATOR, false); List<String> result = new LinkedList<String>();
List<String> result = new ArrayList<String>(); List<String> realTokens = getRealTokens(NFIELD_SEPARATOR, value, false);
while (st.hasMoreTokens()) { for (String token : realTokens) {
String token = st.nextToken();
result.add(token.trim()); result.add(token.trim());
} }
return result; return result;
@ -89,6 +87,8 @@ public abstract class ParameterParser {
entry = entry.substring(0, entry.length() - 1) + separator; entry = entry.substring(0, entry.length() - 1) + separator;
} }
} }
if (entry.isEmpty())
continue;
result.add(entry); result.add(entry);
} }
return result; return result;

View File

@ -1,4 +1,4 @@
package org.bench4q.agent.scenario.util; package org.bench4q.agent.utils;
public class Type { public class Type {
public static enum SupportTypes { public static enum SupportTypes {

View File

@ -0,0 +1,5 @@
package org.bench4q.agent.utils.types;
public class CheckBox {
}

View File

@ -0,0 +1,7 @@
package org.bench4q.agent.utils.types;
import org.bench4q.agent.utils.Type;
public class Filed extends Type {
}

View File

@ -0,0 +1,5 @@
package org.bench4q.agent.utils.types;
public class NField {
}

View File

@ -0,0 +1,5 @@
package org.bench4q.agent.utils.types;
public class Radio {
}

View File

@ -1,12 +1,12 @@
package org.bench4q.agent.scenario.util.types; package org.bench4q.agent.utils.types;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import org.bench4q.agent.plugin.basic.http.ParameterConstant; import org.bench4q.agent.utils.ParameterConstant;
import org.bench4q.agent.scenario.util.ParameterParser; import org.bench4q.agent.utils.ParameterParser;
import org.bench4q.agent.scenario.util.Type; import org.bench4q.agent.utils.Type;
public class Table extends Type { public class Table extends Type {
public static final String ROW_SEPARATOR = "|;"; public static final String ROW_SEPARATOR = "|;";

View File

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

View File

@ -3,13 +3,14 @@ package org.bench4q.agent.test.parameterization;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.util.HashMap; import java.util.HashMap;
import java.util.UUID;
import org.bench4q.agent.parameterization.impl.InstanceControler; import org.bench4q.agent.parameterization.impl.InstanceController;
import org.bench4q.agent.parameterization.impl.Para_Table; import org.bench4q.agent.parameterization.impl.Para_Table;
import org.bench4q.share.helper.TestHelper; import org.bench4q.share.helper.TestHelper;
import org.junit.Test; import org.junit.Test;
public class TEST_UserName { public class Test_InstanceCotroller {
@Test @Test
public void testGetParam() throws Exception { public void testGetParam() throws Exception {
@ -20,12 +21,14 @@ public class TEST_UserName {
"2"); "2");
System.out.println(ret); System.out.println(ret);
InstanceControler ic = new InstanceControler(); InstanceController ic = new InstanceController(UUID.randomUUID(),
UUID.randomUUID());
String passwordName = ic String passwordName = ic
.getParam("<parameters name=\"useNamePassword\" class=\"Para_Table\" type=\"crossThread\" method=\"getTableColumnValue\" args=\"file,ScenarioParameters\\param1.txt,0,sequence,;,~,2\" />"); .getParam("<parameters name=\"useNamePassword\" class=\"Para_Table\" type=\"crossThread\" method=\"getTableColumnValue\" args=\"file,ScenarioParameters\\param1.txt,0,sequence,;,~,2\" />");
System.out.println(passwordName); System.out.println(passwordName);
assertNotNull(passwordName); assertNotNull(passwordName);
InstanceControler instanceControler = new InstanceControler(); InstanceController instanceControler = new InstanceController(
UUID.randomUUID(), UUID.randomUUID());
String password2 = instanceControler String password2 = instanceControler
.getParam("<parameters name=\"useNamePassword\" class=\"Para_Table\" type=\"crossThread\" method=\"getTableColumnValue\" args=\"file,ScenarioParameters\\param1.txt,0,sequence,;,~,2\" />"); .getParam("<parameters name=\"useNamePassword\" class=\"Para_Table\" type=\"crossThread\" method=\"getTableColumnValue\" args=\"file,ScenarioParameters\\param1.txt,0,sequence,;,~,2\" />");
System.out.println(password2); System.out.println(password2);

View File

@ -0,0 +1,33 @@
package org.bench4q.agent.test.parameterization;
import static org.junit.Assert.*;
import java.lang.reflect.Method;
import org.bench4q.agent.parameterization.impl.MyFileClassLoader;
import org.junit.Test;
public class Test_MyFileClassLoader {
private String Test_ClassPath = "E:\\j2ee_proj\\skythink\\WebContent\\WEB-INF\\classes\\";
@Test
public void test() throws ClassNotFoundException, InstantiationException,
IllegalAccessException {
MyFileClassLoader fileClsLoader = new MyFileClassLoader(Test_ClassPath);
Class<?> cls = fileClsLoader
.loadClass("com.cmw.entity.sys.AccordionEntity");
Object obj = cls.newInstance();
Method[] mthds = cls.getMethods();
for (Method mthd : mthds) {
String methodName = mthd.getName();
System.out.println("mthd.name=" + methodName);
}
System.out.println("obj.class=" + obj.getClass().getName());
System.out.println("obj.class=" + cls.getClassLoader().toString());
System.out.println("obj.class="
+ cls.getClassLoader().getParent().toString());
assertTrue(true);
}
}

View File

@ -0,0 +1,30 @@
package org.bench4q.agent.test.parameterization;
import static org.junit.Assert.*;
import org.bench4q.agent.parameterization.impl.ParameterFormat;
import org.junit.After;
import org.junit.Test;
public class Test_ParameterFormat {
private static String testcase = "<parameters name=\"useNamePassword\" class=\"Para_Table\" type=\"crossThread\" method=\"getTableColumnValue\" args=\"file;ScenarioParameters\\param1.txt;0;sequence;\\;;~;2\" />";
@After
public void tearDown() throws Exception {
}
@Test
public void test() {
ParameterFormat format = ParameterFormat.parseInputParameter(testcase);
assertNotNull(format);
assertEquals("useNamePassword", format.getName());
assertEquals("Para_Table", format.getClassName());
assertEquals("getTableColumnValue", format.getMethodName());
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));
}
}

View File

@ -2,7 +2,9 @@ package org.bench4q.agent.test.parameterization;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import org.bench4q.agent.parameterization.impl.InstanceControler; import java.util.UUID;
import org.bench4q.agent.parameterization.impl.InstanceController;
import org.bench4q.agent.parameterization.impl.ParameterizationParser; import org.bench4q.agent.parameterization.impl.ParameterizationParser;
import org.bench4q.share.helper.TestHelper; import org.bench4q.share.helper.TestHelper;
import org.junit.Test; import org.junit.Test;
@ -20,7 +22,8 @@ public class Test_ParameterizationParser {
public void testParse() { public void testParse() {
String result = ParameterizationParser.parse( String result = ParameterizationParser.parse(
"<parameter class=\"Para_IteratorNumber\" method=\"getIteratorNumber\"" "<parameter class=\"Para_IteratorNumber\" method=\"getIteratorNumber\""
+ " type=\"crossThread\"/>", new InstanceControler()); + " type=\"crossThread\"/>", new InstanceController(
UUID.randomUUID(), UUID.randomUUID()));
System.out.println(result); System.out.println(result);
} }
} }

View File

@ -8,8 +8,8 @@ import java.util.List;
import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.NameValuePair;
import org.bench4q.agent.plugin.basic.http.HttpPlugin; import org.bench4q.agent.plugin.basic.http.HttpPlugin;
import org.bench4q.agent.scenario.util.ParameterParser; import org.bench4q.agent.utils.ParameterParser;
import org.bench4q.agent.scenario.util.types.Table; import org.bench4q.agent.utils.types.Table;
import org.bench4q.share.helper.TestHelper; import org.bench4q.share.helper.TestHelper;
import org.junit.Test; import org.junit.Test;
@ -40,7 +40,7 @@ public class Test_ParameterParser {
@Test @Test
public void testGetNumberOfEscapeCharacter() throws Exception { public void testGetNumberOfEscapeCharacter() throws Exception {
assertEquals(3, "\\\\\\".length()); assertEquals(3, "\\\\\\".length());
assertEquals(2, TestHelper.invokePrivate(null, assertEquals(2, TestHelper.invokeStaticPrivate(ParameterParser.class,
"getNumberOfEscapeCharacter", new Class[] { String.class }, "getNumberOfEscapeCharacter", new Class[] { String.class },
new Object[] { "\\\\" })); new Object[] { "\\\\" }));
} }
@ -69,7 +69,8 @@ public class Test_ParameterParser {
columnList.get(1)); columnList.get(1));
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<List<String>> result = (List<List<String>>) TestHelper List<List<String>> result = (List<List<String>>) TestHelper
.invokePrivate(null, "extractValueFromWellFormedTable", .invokeStaticPrivate(Table.class,
"extractValueFromWellFormedTable",
new Class<?>[] { List.class }, new Class<?>[] { List.class },
new Object[] { new LinkedList<List<String>>() { new Object[] { new LinkedList<List<String>>() {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@ -93,8 +94,9 @@ public class Test_ParameterParser {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private List<String> invokeGetRealTokens(String separator, String value, private List<String> invokeGetRealTokens(String separator, String value,
boolean toUnescapeSeparator) throws Exception { boolean toUnescapeSeparator) throws Exception {
return (List<String>) TestHelper.invokePrivate(null, "getRealTokens", return (List<String>) TestHelper.invokeStaticPrivate(
new Class[] { String.class, String.class, boolean.class }, ParameterParser.class, "getRealTokens", new Class[] {
String.class, String.class, boolean.class },
new Object[] { separator, value, true }); new Object[] { separator, value, true });
} }
@ -199,4 +201,12 @@ public class Test_ParameterParser {
public void testParseResponseVariables() { public void testParseResponseVariables() {
} }
@Test
public void testParseNFields() {
List<String> result = ParameterParser
.buildNField("file;ScenarioParameters\\\\param1.txt;0;sequence;\\;;~;2");
assertEquals(7, result.size());
assertEquals(";", result.get(4));
}
} }

View File

@ -13,6 +13,15 @@ public class TestHelper {
return method.invoke(targetObject, params); return method.invoke(targetObject, params);
} }
public static Object invokeStaticPrivate(Class<?> containerClass,
String methodName, Class<?>[] paramClasses, Object[] params)
throws Exception {
Method method = containerClass.getDeclaredMethod(methodName,
paramClasses);
method.setAccessible(true);
return method.invoke(null, params);
}
public static void createFileIfNotExist(File file) { public static void createFileIfNotExist(File file) {
if (file.exists()) { if (file.exists()) {
return; return;