From a022c51949e28436b1a4e99b980a7096126910bb Mon Sep 17 00:00:00 2001 From: fanfuxiaoran <495538672@qq.com> Date: Thu, 13 Mar 2014 17:47:31 +0800 Subject: [PATCH] change the loadPlugin list to set --- src/main/java/org/bench4q/master/Main.java | 5 -- .../org/bench4q/master/TestPlunginHelper.java | 59 +++++++++++++++++++ .../bench4q/master/api/PluginController.java | 4 ++ .../master/domain/entity/plugin/Method.java | 2 +- .../domain/entity/plugin/MethodParam.java | 10 +--- .../master/domain/entity/plugin/Plugin.java | 3 +- .../master/domain/factory/PluginFactory.java | 8 +-- .../domain/repository/PluginRepository.java | 1 + .../master/domain/service/PluginService.java | 13 +++- .../test/service/Test_PluginService.java | 50 +++++++++------- 10 files changed, 111 insertions(+), 44 deletions(-) create mode 100644 src/main/java/org/bench4q/master/TestPlunginHelper.java diff --git a/src/main/java/org/bench4q/master/Main.java b/src/main/java/org/bench4q/master/Main.java index 1ae0d49f..6901197d 100644 --- a/src/main/java/org/bench4q/master/Main.java +++ b/src/main/java/org/bench4q/master/Main.java @@ -2,7 +2,6 @@ package org.bench4q.master; import java.io.InputStream; import java.util.Properties; - import org.apache.log4j.Logger; /** @@ -21,14 +20,10 @@ public class Main { try { MasterServer masterServer = new MasterServer(getPortToServe()); masterServer.start(); - - - } catch (Exception e) { e.printStackTrace(); return; } - } public static int getPortToServe() { diff --git a/src/main/java/org/bench4q/master/TestPlunginHelper.java b/src/main/java/org/bench4q/master/TestPlunginHelper.java new file mode 100644 index 00000000..4f181f97 --- /dev/null +++ b/src/main/java/org/bench4q/master/TestPlunginHelper.java @@ -0,0 +1,59 @@ +package org.bench4q.master; + +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; + +import org.bench4q.share.models.master.plugin.FieldModel; +import org.bench4q.share.models.master.plugin.MethodModel; +import org.bench4q.share.models.master.plugin.MethodParamModel; +import org.bench4q.share.models.master.plugin.MultiFieldModel; +import org.bench4q.share.models.master.plugin.ParamTypeModel; +import org.bench4q.share.models.master.plugin.ParamTypeString; +import org.bench4q.share.models.master.plugin.PluginModel; + +public class TestPlunginHelper { + + public static PluginModel createOnePlugin() { + PluginModel plugin = new PluginModel(); + MethodModel method = new MethodModel(); + MethodParamModel methodParamModel = new MethodParamModel(); + methodParamModel.setLable("input url"); + methodParamModel.setName("url"); + methodParamModel.setParamTypeModel(generateUrlParamTypeModel()); + + MethodParamModel methodParamsParamModel = new MethodParamModel(); + methodParamsParamModel.setName("queryparams"); + methodParamsParamModel.setLable("key=value"); + methodParamsParamModel.setParamTypeModel(generateParamsParamTypeModel()); + Set methodParams = new HashSet(); + methodParams.add(methodParamModel); + methodParams.add(methodParamsParamModel); + + method.setName("get"); + method.setMethodParams(methodParams); + + Set methods = new HashSet(); + methods.add(method); + plugin.setMethods(methods); + plugin.setName("http" + UUID.randomUUID()); + return plugin; + + } + + private static ParamTypeModel generateUrlParamTypeModel() { + FieldModel paramPropertyUrl = new FieldModel(); + paramPropertyUrl.setType(ParamTypeString.FIELD); + paramPropertyUrl.setSize(20); + return paramPropertyUrl; + } + + private static ParamTypeModel generateParamsParamTypeModel() { + MultiFieldModel multiFieldModel = new MultiFieldModel(); + multiFieldModel.setSize(20); + multiFieldModel.setType(ParamTypeString.MULTIFIELD); + return multiFieldModel; + } + + +} diff --git a/src/main/java/org/bench4q/master/api/PluginController.java b/src/main/java/org/bench4q/master/api/PluginController.java index 59e291ed..974ae4a9 100644 --- a/src/main/java/org/bench4q/master/api/PluginController.java +++ b/src/main/java/org/bench4q/master/api/PluginController.java @@ -37,6 +37,8 @@ public class PluginController extends BaseController { PluginResponseModel pluginResponseModel = new PluginResponseModel(); pluginResponseModel.setPluginList(this.getPluginService() .getPluginNameList()); + + pluginResponseModel.setSuccess(true); return pluginResponseModel; } @@ -53,6 +55,7 @@ public class PluginController extends BaseController { PluginResponseModel pluginResponseModel = new PluginResponseModel(); pluginResponseModel.setMethodList(this.getPluginService() .getMethodNameInPlugin(pluginName)); + pluginResponseModel.setSuccess(true); return pluginResponseModel; } @@ -70,6 +73,7 @@ public class PluginController extends BaseController { PluginResponseModel pluginResponseModel = new PluginResponseModel(); pluginResponseModel.setMethosMethodParamModels(this.getPluginService() .getMethodParamModelsInPlugin(pluginName, methodName)); + pluginResponseModel.setSuccess(true); return pluginResponseModel; } diff --git a/src/main/java/org/bench4q/master/domain/entity/plugin/Method.java b/src/main/java/org/bench4q/master/domain/entity/plugin/Method.java index 7063bec6..c2d6464b 100644 --- a/src/main/java/org/bench4q/master/domain/entity/plugin/Method.java +++ b/src/main/java/org/bench4q/master/domain/entity/plugin/Method.java @@ -43,7 +43,7 @@ public class Method { } @ManyToOne - @JoinColumn(name = "pluginId") + @JoinColumn(name = "pluginId",nullable = false) public Plugin getPlugin() { return plugin; } diff --git a/src/main/java/org/bench4q/master/domain/entity/plugin/MethodParam.java b/src/main/java/org/bench4q/master/domain/entity/plugin/MethodParam.java index 0a0d308c..c54b71f4 100644 --- a/src/main/java/org/bench4q/master/domain/entity/plugin/MethodParam.java +++ b/src/main/java/org/bench4q/master/domain/entity/plugin/MethodParam.java @@ -16,15 +16,11 @@ import javax.persistence.Table; @Entity @Table(name = "methodParam") public class MethodParam { - public static final int FIELD = 1; - public static final int NFIELD = 2; - public static final int TABLE = 3; - public static final int CHECKBOX = 4; private int id; private Method method; private String lable; private String name; - private ParamType paramType; + /*private ParamType paramType;*/ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -64,7 +60,7 @@ public class MethodParam { public void setLable(String lable) { this.lable = lable; } - @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER) +/* @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER) @JoinColumn(name = "paramTypeId",nullable = false) public ParamType getParamType() { return paramType; @@ -72,6 +68,6 @@ public class MethodParam { public void setParamType(ParamType paramType) { this.paramType = paramType; - } + }*/ } diff --git a/src/main/java/org/bench4q/master/domain/entity/plugin/Plugin.java b/src/main/java/org/bench4q/master/domain/entity/plugin/Plugin.java index 437e0916..9582fbe8 100644 --- a/src/main/java/org/bench4q/master/domain/entity/plugin/Plugin.java +++ b/src/main/java/org/bench4q/master/domain/entity/plugin/Plugin.java @@ -39,8 +39,7 @@ public class Plugin { this.name = name; } - - @OneToMany(mappedBy = "plugin", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER) + @OneToMany(mappedBy = "plugin", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) public Set getPluginMethods() { return pluginMethods; } diff --git a/src/main/java/org/bench4q/master/domain/factory/PluginFactory.java b/src/main/java/org/bench4q/master/domain/factory/PluginFactory.java index 67ff70b8..aaa3ca34 100644 --- a/src/main/java/org/bench4q/master/domain/factory/PluginFactory.java +++ b/src/main/java/org/bench4q/master/domain/factory/PluginFactory.java @@ -59,8 +59,8 @@ public class PluginFactory { methodParam.setLable(methodParamModel.getLable()); methodParam.setName(methodParamModel.getName()); methodParam.setMethod(method); - methodParam.setParamType(createParamTypeEntity( - methodParamModel.getParamTypeModel(), methodParam)); + /*methodParam.setParamType(createParamTypeEntity( + methodParamModel.getParamTypeModel(), methodParam));*/ return methodParam; } @@ -91,8 +91,8 @@ public class PluginFactory { MethodParamModel methodParamModel = new MethodParamModel(); methodParamModel.setLable(methodParam.getLable()); methodParamModel.setName(methodParam.getName()); - methodParamModel.setParamTypeModel(extractParamTypeModel(methodParam - .getParamType())); +// methodParamModel.setParamTypeModel(extractParamTypeModel(methodParam +// .getParamType())); return methodParamModel; } diff --git a/src/main/java/org/bench4q/master/domain/repository/PluginRepository.java b/src/main/java/org/bench4q/master/domain/repository/PluginRepository.java index 6d32fdac..1270dee1 100644 --- a/src/main/java/org/bench4q/master/domain/repository/PluginRepository.java +++ b/src/main/java/org/bench4q/master/domain/repository/PluginRepository.java @@ -7,6 +7,7 @@ import org.bench4q.master.exception.Bench4QException; import org.bench4q.master.exception.ExceptionUtils.EntityUniqueAlReadyExistException; import org.hibernate.Session; import org.hibernate.Transaction; +import org.hibernate.criterion.Order; import org.hibernate.criterion.Restrictions; import org.springframework.stereotype.Component; diff --git a/src/main/java/org/bench4q/master/domain/service/PluginService.java b/src/main/java/org/bench4q/master/domain/service/PluginService.java index 442c93bb..929e0d67 100644 --- a/src/main/java/org/bench4q/master/domain/service/PluginService.java +++ b/src/main/java/org/bench4q/master/domain/service/PluginService.java @@ -1,6 +1,7 @@ package org.bench4q.master.domain.service; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Set; @@ -17,7 +18,7 @@ import org.springframework.stereotype.Component; @Component public class PluginService { - private List plugins; + private Set plugins; private List pluginNameList; private PluginRepository pluginRepository; @@ -30,8 +31,14 @@ public class PluginService { this.pluginRepository = pluginRepository; } - private List getPlugins() throws Bench4QException { - this.plugins = this.getPluginRepository().loadPlugins(); + private Set getPlugins() throws Bench4QException { + List loadPluginList = this.getPluginRepository().loadPlugins(); + if (loadPluginList != null) { + this.plugins = new HashSet(); + for (Plugin plugin : loadPluginList) { + this.plugins.add(plugin); + } + } return this.plugins; } diff --git a/src/test/java/org/bench4q/master/test/service/Test_PluginService.java b/src/test/java/org/bench4q/master/test/service/Test_PluginService.java index ede3d325..3bf710b4 100644 --- a/src/test/java/org/bench4q/master/test/service/Test_PluginService.java +++ b/src/test/java/org/bench4q/master/test/service/Test_PluginService.java @@ -5,14 +5,14 @@ import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; - +import java.util.UUID; import javax.xml.bind.JAXBException; - import org.apache.commons.io.FileUtils; 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.PluginModel; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -20,7 +20,6 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - import TestHelper.TestPlunginHelper; @RunWith(SpringJUnit4ClassRunner.class) @@ -29,6 +28,8 @@ public class Test_PluginService { private PluginService pluginService; + private String nameForPlugin; + public PluginService getPluginService() { return pluginService; } @@ -39,8 +40,8 @@ public class Test_PluginService { } @Before - public void setUp() { - + public void setUp() throws Bench4QException { + nameForPlugin = addPlugin(); } @Test @@ -50,37 +51,37 @@ public class Test_PluginService { + System.getProperty("file.separator") + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss") .format(new Date()) + "plugin.xml"); - FileUtils.writeStringToFile(file, - MarshalHelper.marshal(PluginModel.class,TestPlunginHelper. createOnePlugin())); + FileUtils.writeStringToFile( + file, + MarshalHelper.marshal(PluginModel.class, + TestPlunginHelper.createOnePlugin())); } @Test - public void testAddPlugin() throws JAXBException, Bench4QException { + public void testLoadPluginList() throws Bench4QException { + int pluginCountBefore = this.getPluginService().getPluginNameList() + .size(); + String pluginName = addPlugin(); + System.out.println(this.getPluginService().getPluginNameList().size() + - pluginCountBefore); + Assert.assertTrue(this.getPluginService().getPluginNameList().size() == (pluginCountBefore + 1)); - Assert.assertTrue(this.getPluginService().addPlugin(TestPlunginHelper.createOnePlugin())); + Assert.assertTrue(this.getPluginService().deletePlugin(pluginName)); } @Test public void testDeletePlugin() throws Bench4QException { - PluginModel plugin =TestPlunginHelper. createOnePlugin(); - plugin.setName("ppp"); - Assert.assertTrue(this.getPluginService().addPlugin(plugin)); - Assert.assertTrue(this.getPluginService() - .deletePlugin(plugin.getName())); + String pluginName = addPlugin(); + Assert.assertTrue(this.getPluginService().deletePlugin(pluginName)); } - @Test - public void testLoadPluginNameList() throws Bench4QException { + private String addPlugin() throws Bench4QException { PluginModel plugin = TestPlunginHelper.createOnePlugin(); - plugin.setName("ppp"); + plugin.setName("test" + UUID.randomUUID()); Assert.assertTrue(this.getPluginService().addPlugin(plugin)); - List pluginNameList = this.getPluginService() - .getPluginNameList(); - Assert.assertTrue(pluginNameList.size() > 0); - Assert.assertTrue(this.getPluginService() - .deletePlugin(plugin.getName())); + return plugin.getName(); } @Test @@ -95,6 +96,11 @@ public class Test_PluginService { .deletePlugin(plugin.getName())); } + @After + public void clear() throws Bench4QException { + this.getPluginService().deletePlugin(nameForPlugin); + } + private String buildSavePath() { String dirString = "GUI" + System.getProperty("file.separator") + System.getProperty("file.separator");