pass test addPlugin

This commit is contained in:
fanfuxiaoran 2014-03-11 14:06:36 +08:00
parent 3c62882c5a
commit c9d020cffa
10 changed files with 238 additions and 147 deletions

View File

@ -13,8 +13,6 @@ import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
@Entity
@Table(name = "method")
@ -35,7 +33,6 @@ public class Method {
this.id = id;
}
@XmlElement(name="name")
@Column(name = "method", nullable = false)
public String getName() {
return name;
@ -55,8 +52,6 @@ public class Method {
this.plugin = plugin;
}
@XmlElementWrapper(name="params")
@XmlElement(name="param",type=MethodParam.class)
@OneToMany(mappedBy = "method", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
public Set<MethodParam> getMethodParams() {
return methodParams;

View File

@ -13,10 +13,7 @@ import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@Entity
@Table(name = "methodParam")
public class MethodParam {
@ -27,8 +24,9 @@ public class MethodParam {
private Method method;
private String lable;
private String name;
private String seperator;
private int editorType;
private List<ParamProperty> properties;
private List<ParamProperty> paramProperties;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ -41,7 +39,6 @@ public class MethodParam {
this.id = id;
}
@XmlElement(name = "name")
@Column(name = "name", nullable = false)
public String getName() {
return name;
@ -61,7 +58,6 @@ public class MethodParam {
this.method = method;
}
@XmlElement(name = "lable")
@Column(name = "lable")
public String getLable() {
return lable;
@ -71,6 +67,15 @@ public class MethodParam {
this.lable = lable;
}
@Column(name = "seperator")
public String getSeperator() {
return seperator;
}
public void setSeperator(String seperator) {
this.seperator = seperator;
}
@Column(name = "editorType")
public int getEditorType() {
return editorType;
@ -80,13 +85,13 @@ public class MethodParam {
this.editorType = editorType;
}
@OneToMany(mappedBy = "method", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
public List<ParamProperty> getProperties() {
return properties;
@OneToMany(mappedBy = "methodParam", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
public List<ParamProperty> getParamProperties() {
return paramProperties;
}
public void setProperties(List<ParamProperty> properties) {
this.properties = properties;
public void setParamProperties(List<ParamProperty> properties) {
this.paramProperties = properties;
}
}

View File

@ -8,17 +8,14 @@ import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "pararmProperty")
@Entity
@Table(name = "paramProperties")
@Table(name = "paramProperty")
public class ParamProperty {
private int id;
private String key;
private String value;
private MethodParam param;
private String propertyName;
private String propertyValue;
private MethodParam methodParam;
private String valueSeperator;
@Id
@ -32,38 +29,18 @@ public class ParamProperty {
this.id = id;
}
@XmlElement
@Column(name = "key")
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
@XmlElement
@Column(name = "value")
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@ManyToOne
@JoinColumn(name = "methodParamId")
public MethodParam getParam() {
return param;
public MethodParam getMethodParam() {
return methodParam;
}
public void setParam(MethodParam param) {
this.param = param;
public void setMethodParam(MethodParam methodParam) {
this.methodParam = methodParam;
}
@XmlElement
@Column(name = "valueSeperator")
@Column(name = "seperator")
public String getValueSeperator() {
return valueSeperator;
}
@ -71,5 +48,24 @@ public class ParamProperty {
public void setValueSeperator(String valueSeperator) {
this.valueSeperator = valueSeperator;
}
@Column(name="propertyName")
public String getPropertyName() {
return propertyName;
}
public void setPropertyName(String propertyName) {
this.propertyName = propertyName;
}
@Column(name="propertyValue")
public String getPropertyValue() {
return propertyValue;
}
public void setPropertyValue(String propertyValue) {
this.propertyValue = propertyValue;
}
}

View File

@ -11,11 +11,7 @@ import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "plugin")
@Entity
@Table(name = "plugin")
public class Plugin {
@ -34,7 +30,6 @@ public class Plugin {
this.id = id;
}
@XmlElement(name = "name")
@Column(name = "name", nullable = false)
public String getName() {
return name;
@ -44,8 +39,6 @@ public class Plugin {
this.name = name;
}
@XmlElementWrapper(name = "methods")
@XmlElement(name = "method", type = Method.class)
@OneToMany(mappedBy = "plugin", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
public Set<Method> getMethods() {
return methods;

View File

@ -0,0 +1,72 @@
package org.bench4q.master.domain.factory;
import org.bench4q.master.domain.entity.plugin.Method;
import org.bench4q.master.domain.entity.plugin.MethodParam;
import org.bench4q.master.domain.entity.plugin.ParamProperty;
import org.bench4q.master.domain.entity.plugin.Plugin;
import org.bench4q.share.models.master.plugin.MethodModel;
import org.bench4q.share.models.master.plugin.MethodParamModel;
import org.bench4q.share.models.master.plugin.ParamPropertyModel;
import org.bench4q.share.models.master.plugin.PluginModel;
public class PluginFactory {
public static Plugin createPluginEntity(PluginModel pluginModel) {
Plugin plugin = new Plugin();
if (plugin.getMethods() != null) {
for (MethodModel methodModel : pluginModel.getMethods()) {
plugin.getMethods()
.add(createMethodEntity(methodModel, plugin));
}
}
plugin.setName(pluginModel.getName());
return plugin;
}
private static Method createMethodEntity(MethodModel methodModel,
Plugin plugin) {
Method method = new Method();
method.setName(methodModel.getName());
if (methodModel.getMethodParams() != null) {
for (MethodParamModel methodParamModel : methodModel
.getMethodParams()) {
method.getMethodParams().add(
createMethodParamEntity(methodParamModel, method));
}
}
method.setPlugin(plugin);
return method;
}
private static MethodParam createMethodParamEntity(
MethodParamModel methodParamModel, Method method) {
MethodParam methodParam = new MethodParam();
methodParam.setEditorType(methodParamModel.getEditorType());
methodParam.setLable(methodParamModel.getLable());
methodParam.setName(methodParamModel.getName());
methodParam.setSeperator(methodParamModel.getSeperator());
if (methodParamModel.getParamProperties() != null) {
for (ParamPropertyModel paramPropertyModel : methodParamModel
.getParamProperties()) {
methodParam.getParamProperties().add(
createParamPropertyEntity(paramPropertyModel,
methodParam));
}
}
methodParam.setMethod(method);
return methodParam;
}
private static ParamProperty createParamPropertyEntity(
ParamPropertyModel paramPropertyModel, MethodParam methodParam) {
ParamProperty paramProperty = new ParamProperty();
paramProperty.setPropertyName(paramPropertyModel.getKey());
paramProperty.setPropertyValue(paramPropertyModel.getValue());
paramProperty.setValueSeperator(paramPropertyModel.getValueSeperator());
paramProperty.setMethodParam(methodParam);
return paramProperty;
}
}

View File

@ -17,8 +17,10 @@ public class PluginRepository extends AbstractRepositoty {
Transaction transaction = session.beginTransaction();
Plugin pluginExist = null;
try {
pluginExist = (Plugin) session.createCriteria(Plugin.class).add(
Restrictions.eq("name", plugin.getName()));
pluginExist = (Plugin) session.createCriteria(Plugin.class)
.add(Restrictions.eq("name", plugin.getName()))
.uniqueResult();
;
if (pluginExist != null) {
logger.info("the plugin exist");
return false;

View File

@ -14,7 +14,6 @@ public final class SessionHelper {
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

View File

@ -32,6 +32,6 @@
<mapping class="org.bench4q.master.domain.entity.plugin.Plugin" />
<mapping class="org.bench4q.master.domain.entity.plugin.Method" />
<mapping class="org.bench4q.master.domain.entity.plugin.MethodParam" />
<mapping class="org.bench4q.master.domain.entity.plugin.ParamType" />
<mapping class="org.bench4q.master.domain.entity.plugin.ParamProperty" />
</session-factory>
</hibernate-configuration>

View File

@ -12,12 +12,17 @@ import java.util.Set;
import javax.xml.bind.JAXBException;
import org.apache.commons.io.FileUtils;
import org.bench4q.master.domain.entity.plugin.Plugin;
import org.bench4q.master.domain.factory.PluginFactory;
import org.bench4q.master.domain.service.PluginService;
import org.bench4q.master.exception.Bench4QException;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.plugin.Method;
import org.bench4q.share.models.master.plugin.MethodParam;
import org.bench4q.share.models.master.plugin.ParamProperty;
import org.bench4q.share.models.master.plugin.Plugin;
import org.bench4q.share.models.master.plugin.MethodModel;
import org.bench4q.share.models.master.plugin.MethodParamModel;
import org.bench4q.share.models.master.plugin.ParamPropertyModel;
import org.bench4q.share.models.master.plugin.PluginModel;
import org.bench4q.share.models.master.plugin.PluginGUI;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -47,59 +52,83 @@ public class Test_PluginService {
@Test
public void testPluginModel() throws JAXBException, IOException {
File file = new File(buildSavePath()+System.getProperty("file.separator")+"plugin.xml");
Plugin plugin = new Plugin();
Method method = new Method();
MethodParam methodParamUrl = new MethodParam();
methodParamUrl.setEditorType(MethodParam.FIELD);
File file = new File(
buildSavePath()
+ System.getProperty("file.separator")
+ new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss")
.format(new Date()) + "plugin.xml");
FileUtils.writeStringToFile(file,
MarshalHelper.marshal(PluginModel.class, createOnePlugin()));
}
@Test
public void testAddPlugin() throws JAXBException, Bench4QException {
Plugin plugin=PluginFactory.createPluginEntity(createOnePlugin());
Assert.assertTrue(this.getPluginService().addPlugin(plugin));
}
private PluginModel createOnePlugin() {
PluginModel plugin = new PluginModel();
MethodModel method = new MethodModel();
MethodParamModel methodParamUrl = new MethodParamModel();
methodParamUrl.setEditorType(MethodParamModel.FIELD);
methodParamUrl.setName("url");
methodParamUrl.setSplitBy("");
methodParamUrl.setSeperator("");
methodParamUrl.setParamProperties(generateUrlProperties());
MethodParam methodParamParam = new MethodParam();
methodParamParam.setEditorType(MethodParam.TABLE);
MethodParamModel methodParamParam = new MethodParamModel();
methodParamParam.setEditorType(MethodParamModel.TABLE);
methodParamParam.setName("Param");
methodParamParam.setSplitBy("&");
methodParamParam.setSeperator("&");
methodParamParam.setParamProperties(generateParamProperties());
List<MethodParam> methodParams = new ArrayList<MethodParam>();
List<MethodParamModel> methodParams = new ArrayList<MethodParamModel>();
methodParams.add(methodParamParam);
methodParams.add(methodParamParam);
method.setName("get");
method.setParams(methodParams);
Set<Method> methods = new HashSet<Method>();
Set<MethodModel> methods = new HashSet<MethodModel>();
methods.add(method);
plugin.setMethods(methods);
FileUtils.writeStringToFile(file,
MarshalHelper.marshal(Plugin.class, plugin));
plugin.setName("http");
return plugin;
}
private List<ParamProperty> generateUrlProperties() {
ParamProperty paramPropertyUrl = new ParamProperty();
private PluginGUI generateOnePluginGUI() {
PluginGUI pluginGUI = new PluginGUI();
pluginGUI.setPlugin(createOnePlugin());
return pluginGUI;
}
private List<ParamPropertyModel> generateUrlProperties() {
ParamPropertyModel paramPropertyUrl = new ParamPropertyModel();
paramPropertyUrl.setKey("size");
paramPropertyUrl.setValue("20");
List<ParamProperty> paramProperties = new ArrayList<ParamProperty>();
List<ParamPropertyModel> paramProperties = new ArrayList<ParamPropertyModel>();
paramProperties.add(paramPropertyUrl);
return paramProperties;
}
private List<ParamProperty> generateParamProperties() {
ParamProperty paramProperty = new ParamProperty();
private List<ParamPropertyModel> generateParamProperties() {
ParamPropertyModel paramProperty = new ParamPropertyModel();
paramProperty.setKey("cols");
paramProperty.setValue("key,value");
paramProperty.setValueSeperator(",");
List<ParamProperty> paramProperties = new ArrayList<ParamProperty>();
List<ParamPropertyModel> paramProperties = new ArrayList<ParamPropertyModel>();
paramProperties.add(paramProperty);
return paramProperties;
}
private String buildSavePath() {
String dirString = "GUI" + System.getProperty("file.separator")
+ System.getProperty("file.separator")
+ new SimpleDateFormat("yyyyMMdd").format(new Date())
+ "plugin.xml";
+ System.getProperty("file.separator");
File dirFile = new File(dirString);
if (!dirFile.exists()) {
dirFile.mkdirs();