From 2ace04894fe36683f62dce41a49d86e18433d0bc Mon Sep 17 00:00:00 2001 From: ningyu1 Date: Wed, 14 Oct 2020 17:05:04 +0800 Subject: [PATCH 1/9] upgrade dubbo to v2.7.8 --- pom.xml | 91 +++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 62 insertions(+), 29 deletions(-) diff --git a/pom.xml b/pom.xml index 4f0880c..e321ecb 100644 --- a/pom.xml +++ b/pom.xml @@ -22,7 +22,7 @@ ${revision} - 2.7.7 + 2.7.8 1.8 1.8 1.8 @@ -31,9 +31,7 @@ 1.8 UTF-8 - 2.7.7 - 1.1.3 - + 2.7.8 3.0 4.1.4 3.0 @@ -51,10 +49,18 @@ pom import + + org.apache.dubbo + dubbo-bom + ${dubbo.version} + pom + import + + avalon-framework avalon-framework @@ -85,39 +91,70 @@ dubbo ${dubbo.version} - + + - org.apache.zookeeper - zookeeper + org.apache.dubbo + dubbo-configcenter-apollo + + + + org.apache.dubbo + dubbo-configcenter-zookeeper + - org.apache.curator - curator-framework - - - org.apache.curator - curator-recipes - - - de.ruedigermoeller - fst - + org.apache.dubbo + dubbo-configcenter-nacos + + org.apache.dubbo dubbo-registry-nacos - ${dubbo.version} + + + org.apache.dubbo + dubbo-registry-zookeeper + + + + org.apache.dubbo + dubbo-registry-redis + + + + + org.apache.dubbo + dubbo-serialization-hessian2 + + + + org.apache.dubbo + dubbo-serialization-fst + + - com.alibaba.nacos - nacos-api - ${nacos.version} + org.apache.dubbo + dubbo-serialization-kryo + - com.alibaba.nacos - nacos-client - ${nacos.version} + org.apache.dubbo + dubbo-serialization-fastjson + + + org.apache.dubbo + dubbo-serialization-gson + + + + org.apache.dubbo + dubbo-serialization-avro + + org.slf4j @@ -128,10 +165,6 @@ log4j-over-slf4j ${slf4j.version} - - com.google.code.gson - gson - From 5c33cf1e50bcb2ce1d425f36a42bb54bb1779bcc Mon Sep 17 00:00:00 2001 From: ningyu1 Date: Wed, 14 Oct 2020 17:13:18 +0800 Subject: [PATCH 2/9] #122 fixed ReferenceConfigCache get diff Service to same genericService from dubbo v2.7.7 --- .../ningyu/jmeter/plugin/dubbo/sample/DubboSample.java | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/main/java/io/github/ningyu/jmeter/plugin/dubbo/sample/DubboSample.java b/src/main/java/io/github/ningyu/jmeter/plugin/dubbo/sample/DubboSample.java index 516207c..e2c8936 100644 --- a/src/main/java/io/github/ningyu/jmeter/plugin/dubbo/sample/DubboSample.java +++ b/src/main/java/io/github/ningyu/jmeter/plugin/dubbo/sample/DubboSample.java @@ -24,7 +24,6 @@ import org.apache.dubbo.config.ApplicationConfig; import org.apache.dubbo.config.ConfigCenterConfig; import org.apache.dubbo.config.ReferenceConfig; -import org.apache.dubbo.config.ReferenceConfigBase; import org.apache.dubbo.config.RegistryConfig; import org.apache.dubbo.config.utils.ReferenceConfigCache; import org.apache.dubbo.rpc.RpcContext; @@ -270,13 +269,7 @@ private Object callDubbo(SampleResult res) { } // The registry's address is to generate the ReferenceConfigCache key - ReferenceConfigCache cache = ReferenceConfigCache.getCache(Constants.getAddress(this), new ReferenceConfigCache.KeyGenerator() { - - @Override - public String generateKey(ReferenceConfigBase referenceConfig) { - return referenceConfig.toString(); - } - }); + ReferenceConfigCache cache = ReferenceConfigCache.getCache(Constants.getAddress(this)); GenericService genericService = (GenericService) cache.get(reference); if (genericService == null) { setResponseError(res, ErrorCode.GENERIC_SERVICE_IS_NULL); From 8a7acd7d23a41f165ba752fa54aadc1fd249b492 Mon Sep 17 00:00:00 2001 From: ningyu1 Date: Thu, 15 Oct 2020 12:26:38 +0800 Subject: [PATCH 3/9] #122 fixed ReferenceConfigCache get diffsame genericService. --- pom.xml | 4 ++-- .../jmeter/plugin/dubbo/sample/ProviderService.java | 9 +-------- .../ningyu/jmeter/plugin/GenericServiceTest.java | 11 ++++++----- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/pom.xml b/pom.xml index e321ecb..6c6748b 100644 --- a/pom.xml +++ b/pom.xml @@ -31,7 +31,7 @@ 1.8 UTF-8 - 2.7.8 + 2.7.7 3.0 4.1.4 3.0 @@ -209,7 +209,7 @@ false false true - true + false diff --git a/src/main/java/io/github/ningyu/jmeter/plugin/dubbo/sample/ProviderService.java b/src/main/java/io/github/ningyu/jmeter/plugin/dubbo/sample/ProviderService.java index ffa7450..23865cc 100644 --- a/src/main/java/io/github/ningyu/jmeter/plugin/dubbo/sample/ProviderService.java +++ b/src/main/java/io/github/ningyu/jmeter/plugin/dubbo/sample/ProviderService.java @@ -20,7 +20,6 @@ import io.github.ningyu.jmeter.plugin.util.Constants; import org.apache.dubbo.common.URL; import org.apache.dubbo.config.ReferenceConfig; -import org.apache.dubbo.config.ReferenceConfigBase; import org.apache.dubbo.config.RegistryConfig; import org.apache.dubbo.config.utils.ReferenceConfigCache; import org.apache.dubbo.registry.RegistryService; @@ -103,13 +102,7 @@ private List executeRegistry(String protocol, String address, String gro } reference.setInterface("org.apache.dubbo.registry.RegistryService"); try { - ReferenceConfigCache cache = ReferenceConfigCache.getCache(address + "_" + group, new ReferenceConfigCache.KeyGenerator() { - - @Override - public String generateKey(ReferenceConfigBase referenceConfig) { - return referenceConfig.toString(); - } - }); + ReferenceConfigCache cache = ReferenceConfigCache.getCache(address + "_" + group); RegistryService registryService = (RegistryService) cache.get(reference); if (registryService == null) { throw new RuntimeException("Can't get the interface list, please check if the address is wrong!"); diff --git a/src/test/java/io/github/ningyu/jmeter/plugin/GenericServiceTest.java b/src/test/java/io/github/ningyu/jmeter/plugin/GenericServiceTest.java index eb8664b..33f5a6e 100644 --- a/src/test/java/io/github/ningyu/jmeter/plugin/GenericServiceTest.java +++ b/src/test/java/io/github/ningyu/jmeter/plugin/GenericServiceTest.java @@ -16,7 +16,7 @@ import java.util.List; public class GenericServiceTest { - @Test +// @Test public void test() { ApplicationConfig application = new ApplicationConfig(); application.setName("api-generic-consumer"); @@ -33,7 +33,7 @@ public void test() { System.out.println(json); } - @Test +// @Test public void testAttachment() { ApplicationConfig application = new ApplicationConfig(); application.setName("api-generic-consumer"); @@ -58,13 +58,14 @@ public void testZk() { application.setName("api-generic-consumer"); ReferenceConfig reference = new ReferenceConfig<>(); reference.setVersion("1.0.0"); + reference.setGroup(""); RegistryConfig registry = new RegistryConfig(); registry.setProtocol(Constants.REGISTRY_ZOOKEEPER); registry.setAddress("192.168.0.44:2181,192.168.0.44:2182,192.168.0.44:2183"); registry.setTimeout(10000); reference.setRegistry(registry); ConfigCenterConfig cc = new ConfigCenterConfig(); - cc.setAddress("192.168.0.58:2181,192.168.0.59:2181,192.168.0.60:2181"); + cc.setAddress("192.168.0.44:2181,192.168.0.44:2182,192.168.0.44:2183"); cc.setProtocol(Constants.REGISTRY_ZOOKEEPER); cc.setTimeout(Long.valueOf("10000")); cc.setGroup(""); @@ -81,7 +82,7 @@ public void testZk() { } } - @Test +// @Test public void testEnumA() { List paramterTypeList = new ArrayList<>(); List parameterValuesList = new ArrayList<>(); @@ -91,7 +92,7 @@ public void testEnumA() { System.out.println(parameterValuesList.toString()); } - @Test +// @Test public void testEnumB() { List paramterTypeList = new ArrayList<>(); List parameterValuesList = new ArrayList<>(); From bfb43f0b6d3a46299289b58e9edb1768eda58dc4 Mon Sep 17 00:00:00 2001 From: ningyu Date: Thu, 15 Oct 2020 12:33:34 +0800 Subject: [PATCH 4/9] 1111 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 6c6748b..58487cc 100644 --- a/pom.xml +++ b/pom.xml @@ -31,7 +31,7 @@ 1.8 UTF-8 - 2.7.7 + 2.7.8 3.0 4.1.4 3.0 From 91ec9c9757b133e6e33f3ce628a663b442346707 Mon Sep 17 00:00:00 2001 From: ningyu Date: Thu, 15 Oct 2020 12:41:23 +0800 Subject: [PATCH 5/9] remove Useless package --- .../jmeter/plugin/dubbo/sample/RegistryServerSync.java | 6 +----- .../io/github/ningyu/jmeter/plugin/util/JsonUtils.java | 9 ++++----- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/github/ningyu/jmeter/plugin/dubbo/sample/RegistryServerSync.java b/src/main/java/io/github/ningyu/jmeter/plugin/dubbo/sample/RegistryServerSync.java index 825d95b..9e35c53 100644 --- a/src/main/java/io/github/ningyu/jmeter/plugin/dubbo/sample/RegistryServerSync.java +++ b/src/main/java/io/github/ningyu/jmeter/plugin/dubbo/sample/RegistryServerSync.java @@ -27,11 +27,7 @@ import org.apache.dubbo.registry.NotifyListener; import java.io.Serializable; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; diff --git a/src/main/java/io/github/ningyu/jmeter/plugin/util/JsonUtils.java b/src/main/java/io/github/ningyu/jmeter/plugin/util/JsonUtils.java index e0c1afe..310db7b 100644 --- a/src/main/java/io/github/ningyu/jmeter/plugin/util/JsonUtils.java +++ b/src/main/java/io/github/ningyu/jmeter/plugin/util/JsonUtils.java @@ -16,14 +16,13 @@ */ package io.github.ningyu.jmeter.plugin.util; -import java.lang.reflect.Type; - -import org.apache.jorphan.logging.LoggingManager; -import org.apache.log.Logger; - import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonSyntaxException; +import org.apache.jorphan.logging.LoggingManager; +import org.apache.log.Logger; + +import java.lang.reflect.Type; /** * JsonUtils From 8edaff35498d1cc7f1b4e159c532e0b396f278c9 Mon Sep 17 00:00:00 2001 From: ningyu1 Date: Thu, 15 Oct 2020 12:41:23 +0800 Subject: [PATCH 6/9] remove Useless package --- .../jmeter/plugin/dubbo/sample/RegistryServerSync.java | 6 +----- .../io/github/ningyu/jmeter/plugin/util/JsonUtils.java | 9 ++++----- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/github/ningyu/jmeter/plugin/dubbo/sample/RegistryServerSync.java b/src/main/java/io/github/ningyu/jmeter/plugin/dubbo/sample/RegistryServerSync.java index 825d95b..9e35c53 100644 --- a/src/main/java/io/github/ningyu/jmeter/plugin/dubbo/sample/RegistryServerSync.java +++ b/src/main/java/io/github/ningyu/jmeter/plugin/dubbo/sample/RegistryServerSync.java @@ -27,11 +27,7 @@ import org.apache.dubbo.registry.NotifyListener; import java.io.Serializable; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; diff --git a/src/main/java/io/github/ningyu/jmeter/plugin/util/JsonUtils.java b/src/main/java/io/github/ningyu/jmeter/plugin/util/JsonUtils.java index e0c1afe..310db7b 100644 --- a/src/main/java/io/github/ningyu/jmeter/plugin/util/JsonUtils.java +++ b/src/main/java/io/github/ningyu/jmeter/plugin/util/JsonUtils.java @@ -16,14 +16,13 @@ */ package io.github.ningyu.jmeter.plugin.util; -import java.lang.reflect.Type; - -import org.apache.jorphan.logging.LoggingManager; -import org.apache.log.Logger; - import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonSyntaxException; +import org.apache.jorphan.logging.LoggingManager; +import org.apache.log.Logger; + +import java.lang.reflect.Type; /** * JsonUtils From 3e9d641775d66532822228999b78adbb40f86999 Mon Sep 17 00:00:00 2001 From: ningyu Date: Thu, 15 Oct 2020 19:46:52 +0800 Subject: [PATCH 7/9] =?UTF-8?q?#124=20fixed=20Locale=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ningyu/jmeter/plugin/util/ClassUtils.java | 62 +++++++++++++++++++ .../ningyu/jmeter/plugin/util/JsonUtils.java | 11 +++- .../ningyu/jmeter/plugin/ClassUtilsTest.java | 58 +++++++++++++++++ .../jmeter/plugin/GenericServiceTest.java | 2 +- .../ningyu/jmeter/plugin/LocaleTestBean.java | 17 +++++ .../github/ningyu/jmeter/plugin/TestBean.java | 26 ++++++++ 6 files changed, 172 insertions(+), 4 deletions(-) create mode 100644 src/test/java/io/github/ningyu/jmeter/plugin/ClassUtilsTest.java create mode 100644 src/test/java/io/github/ningyu/jmeter/plugin/LocaleTestBean.java create mode 100644 src/test/java/io/github/ningyu/jmeter/plugin/TestBean.java diff --git a/src/main/java/io/github/ningyu/jmeter/plugin/util/ClassUtils.java b/src/main/java/io/github/ningyu/jmeter/plugin/util/ClassUtils.java index 6c5ca0d..f12a7ab 100644 --- a/src/main/java/io/github/ningyu/jmeter/plugin/util/ClassUtils.java +++ b/src/main/java/io/github/ningyu/jmeter/plugin/util/ClassUtils.java @@ -23,8 +23,10 @@ import java.lang.reflect.Method; import java.lang.reflect.Type; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Locale; /** * ClassUtils @@ -194,6 +196,17 @@ public static void parseParameter(List paramterTypeList, || "Boolean[]".equals(className)) { paramterTypeList.add("java.lang.Boolean[]"); parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? null : JsonUtils.formJson(arg.getParamValue(), new TypeToken() {}.getType())); + } else if ("java.util.Locale".equals(className)) { + paramterTypeList.add("java.util.Locale"); + parameterValuesList.add(parseLocale(arg.getParamValue())); + } else if ("java.util.Locale[]".equals(className)) { + paramterTypeList.add("java.util.Locale[]"); + List list = JsonUtils.formJson(arg.getParamValue(), new TypeToken>() {}.getType()); + List localeList = new ArrayList<>(); + for (String locale : list) { + localeList.add(parseLocale(locale)); + } + parameterValuesList.add(localeList.toArray(new Locale[localeList.size()])); } else { if (className.endsWith("[]")) { List list = null; @@ -227,4 +240,53 @@ public static void parseParameter(List paramterTypeList, throw new IllegalArgumentException("Invalid parameter => [ParamType="+arg.getParamType()+",ParamValue="+arg.getParamValue()+"]", e); } } + + public static Locale parseLocale(String locale) { + switch (locale) { + case "ENGLISH": + return Locale.ENGLISH; + case "FRENCH": + return Locale.FRENCH; + case "GERMAN": + return Locale.GERMAN; + case "ITALIAN": + return Locale.ITALIAN; + case "JAPANESE": + return Locale.JAPANESE; + case "KOREAN": + return Locale.KOREAN; + case "CHINESE": + return Locale.CHINESE; + case "SIMPLIFIED_CHINESE": + return Locale.SIMPLIFIED_CHINESE; + case "TRADITIONAL_CHINESE": + return Locale.TRADITIONAL_CHINESE; + case "FRANCE": + return Locale.FRANCE; + case "GERMANY": + return Locale.GERMANY; + case "ITALY": + return Locale.ITALY; + case "JAPAN": + return Locale.JAPAN; + case "KOREA": + return Locale.KOREA; + case "CHINA": + return Locale.CHINA; + case "PRC": + return Locale.PRC; + case "TAIWAN": + return Locale.TAIWAN; + case "UK": + return Locale.UK; + case "US": + return Locale.US; + case "CANADA": + return Locale.CANADA; + case "CANADA_FRENCH": + return Locale.CANADA_FRENCH; + default : + return Locale.ROOT; + } + } } diff --git a/src/main/java/io/github/ningyu/jmeter/plugin/util/JsonUtils.java b/src/main/java/io/github/ningyu/jmeter/plugin/util/JsonUtils.java index 310db7b..58d334f 100644 --- a/src/main/java/io/github/ningyu/jmeter/plugin/util/JsonUtils.java +++ b/src/main/java/io/github/ningyu/jmeter/plugin/util/JsonUtils.java @@ -16,13 +16,12 @@ */ package io.github.ningyu.jmeter.plugin.util; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonSyntaxException; +import com.google.gson.*; import org.apache.jorphan.logging.LoggingManager; import org.apache.log.Logger; import java.lang.reflect.Type; +import java.util.Locale; /** * JsonUtils @@ -36,6 +35,12 @@ public class JsonUtils { .setPrettyPrinting() .disableHtmlEscaping() .serializeNulls() + .registerTypeAdapter(Locale.class, new JsonDeserializer() { + @Override + public Locale deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + return ClassUtils.parseLocale(json.getAsJsonPrimitive().getAsString()); + } + }) .create(); public static String toJson(Object obj) { diff --git a/src/test/java/io/github/ningyu/jmeter/plugin/ClassUtilsTest.java b/src/test/java/io/github/ningyu/jmeter/plugin/ClassUtilsTest.java new file mode 100644 index 0000000..45ed014 --- /dev/null +++ b/src/test/java/io/github/ningyu/jmeter/plugin/ClassUtilsTest.java @@ -0,0 +1,58 @@ +package io.github.ningyu.jmeter.plugin; + +import com.google.gson.internal.LinkedTreeMap; +import io.github.ningyu.jmeter.plugin.dubbo.sample.MethodArgument; +import io.github.ningyu.jmeter.plugin.util.ClassUtils; +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +public class ClassUtilsTest { + @Test + public void testLocale() { + List paramterTypeList = new ArrayList<>(); + List parameterValuesList = new ArrayList<>(); + MethodArgument arg = new MethodArgument("java.util.Locale","aaaaa"); + ClassUtils.parseParameter(paramterTypeList, parameterValuesList, arg); + Assert.assertEquals(paramterTypeList.get(0),"java.util.Locale"); + Assert.assertEquals(parameterValuesList.get(0), Locale.ROOT); + + arg = new MethodArgument("java.util.Locale","CHINESE"); + ClassUtils.parseParameter(paramterTypeList, parameterValuesList, arg); + Assert.assertEquals(paramterTypeList.get(1),"java.util.Locale"); + Assert.assertEquals(parameterValuesList.get(1), Locale.CHINESE); + } + + @Test + public void testLocaleArray() { + List paramterTypeList = new ArrayList<>(); + List parameterValuesList = new ArrayList<>(); + MethodArgument arg = new MethodArgument("java.util.Locale[]","[aaaaa,CHINESE]"); + ClassUtils.parseParameter(paramterTypeList, parameterValuesList, arg); + Assert.assertEquals(paramterTypeList.get(0),"java.util.Locale[]"); + Assert.assertEquals(parameterValuesList.get(0).getClass(), Locale[].class); + Assert.assertEquals(((Locale[])parameterValuesList.get(0))[0], Locale.ROOT); + Assert.assertEquals(((Locale[])parameterValuesList.get(0))[1], Locale.CHINESE); + } + + @Test + public void testGeneric() { + String paramValue = "{\"locale\":\"CHINESE\",\"name\":\"aaa\",\"item\":{\"class\":\"io.github.ningyu.jmeter.plugin.TestBean\",\"name\":\"bbb\"}}"; + String paramType = "io.github.ningyu.jmeter.plugin.LocaleTestBean"; + MethodArgument arg = new MethodArgument(paramType, paramValue); + List paramterTypeList = new ArrayList<>(); + List parameterValuesList = new ArrayList<>(); + ClassUtils.parseParameter(paramterTypeList, parameterValuesList, arg); + Assert.assertEquals(paramterTypeList.get(0),paramType); + Assert.assertEquals(parameterValuesList.get(0).getClass(), LocaleTestBean.class); + Assert.assertEquals(((LocaleTestBean)parameterValuesList.get(0)).getName(), "aaa"); + Assert.assertEquals(((LocaleTestBean)parameterValuesList.get(0)).getLocale(), Locale.CHINESE); + Assert.assertEquals(((LocaleTestBean)parameterValuesList.get(0)).getItem().getClass(), LinkedTreeMap.class); + Assert.assertEquals(((Map)((LocaleTestBean)parameterValuesList.get(0)).getItem()).get("name"), "bbb"); + Assert.assertEquals(((Map)((LocaleTestBean)parameterValuesList.get(0)).getItem()).get("item"), null); + } +} diff --git a/src/test/java/io/github/ningyu/jmeter/plugin/GenericServiceTest.java b/src/test/java/io/github/ningyu/jmeter/plugin/GenericServiceTest.java index 33f5a6e..c7e6807 100644 --- a/src/test/java/io/github/ningyu/jmeter/plugin/GenericServiceTest.java +++ b/src/test/java/io/github/ningyu/jmeter/plugin/GenericServiceTest.java @@ -51,7 +51,7 @@ public void testAttachment() { System.out.println(json); } - @Test +// @Test public void testZk() { for(int i=0;i<5;i++) { ApplicationConfig application = new ApplicationConfig(); diff --git a/src/test/java/io/github/ningyu/jmeter/plugin/LocaleTestBean.java b/src/test/java/io/github/ningyu/jmeter/plugin/LocaleTestBean.java new file mode 100644 index 0000000..46d894f --- /dev/null +++ b/src/test/java/io/github/ningyu/jmeter/plugin/LocaleTestBean.java @@ -0,0 +1,17 @@ +package io.github.ningyu.jmeter.plugin; + +import java.io.Serializable; +import java.util.Locale; + +public class LocaleTestBean extends TestBean implements Serializable { + private static final long serialVersionUID = -5300763291046460049L; + private Locale locale; + + public Locale getLocale() { + return locale; + } + + public void setLocale(Locale locale) { + this.locale = locale; + } +} diff --git a/src/test/java/io/github/ningyu/jmeter/plugin/TestBean.java b/src/test/java/io/github/ningyu/jmeter/plugin/TestBean.java new file mode 100644 index 0000000..557cfd6 --- /dev/null +++ b/src/test/java/io/github/ningyu/jmeter/plugin/TestBean.java @@ -0,0 +1,26 @@ +package io.github.ningyu.jmeter.plugin; + +import java.io.Serializable; + +public class TestBean implements Serializable { + private static final long serialVersionUID = 8364861022089141326L; + private String name; + + private T item; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public T getItem() { + return item; + } + + public void setItem(T item) { + this.item = item; + } +} From 3e9168cfdcacf270a6d382b46c9651f7a70ddff3 Mon Sep 17 00:00:00 2001 From: ningyu Date: Thu, 15 Oct 2020 20:17:06 +0800 Subject: [PATCH 8/9] =?UTF-8?q?#126=20fixed=20LocalDateTime=E3=80=81LocalD?= =?UTF-8?q?ate=E3=80=81LocalTime?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ningyu/jmeter/plugin/util/ClassUtils.java | 13 +++++ .../ningyu/jmeter/plugin/util/Constants.java | 2 + .../ningyu/jmeter/plugin/util/JsonUtils.java | 26 +++++++++- .../jmeter/plugin/{ => bean}/EnumA.java | 2 +- .../jmeter/plugin/{ => bean}/EnumB.java | 2 +- .../plugin/bean/LocalDateTimeTestBean.java | 37 ++++++++++++++ .../plugin/{ => bean}/LocaleTestBean.java | 2 +- .../jmeter/plugin/{ => bean}/TestBean.java | 2 +- .../plugin/{ => test}/ClassUtilsTest.java | 51 +++++++++++++++++-- .../plugin/{ => test}/GenericServiceTest.java | 12 ++--- 10 files changed, 135 insertions(+), 14 deletions(-) rename src/test/java/io/github/ningyu/jmeter/plugin/{ => bean}/EnumA.java (81%) rename src/test/java/io/github/ningyu/jmeter/plugin/{ => bean}/EnumB.java (92%) create mode 100644 src/test/java/io/github/ningyu/jmeter/plugin/bean/LocalDateTimeTestBean.java rename src/test/java/io/github/ningyu/jmeter/plugin/{ => bean}/LocaleTestBean.java (89%) rename src/test/java/io/github/ningyu/jmeter/plugin/{ => bean}/TestBean.java (90%) rename src/test/java/io/github/ningyu/jmeter/plugin/{ => test}/ClassUtilsTest.java (52%) rename src/test/java/io/github/ningyu/jmeter/plugin/{ => test}/GenericServiceTest.java (97%) diff --git a/src/main/java/io/github/ningyu/jmeter/plugin/util/ClassUtils.java b/src/main/java/io/github/ningyu/jmeter/plugin/util/ClassUtils.java index f12a7ab..158f6e6 100644 --- a/src/main/java/io/github/ningyu/jmeter/plugin/util/ClassUtils.java +++ b/src/main/java/io/github/ningyu/jmeter/plugin/util/ClassUtils.java @@ -23,6 +23,8 @@ import java.lang.reflect.Method; import java.lang.reflect.Type; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -207,6 +209,17 @@ public static void parseParameter(List paramterTypeList, localeList.add(parseLocale(locale)); } parameterValuesList.add(localeList.toArray(new Locale[localeList.size()])); + } else if ("java.time.LocalDateTime".equals(className)) { + paramterTypeList.add("java.time.LocalDateTime"); + parameterValuesList.add(LocalDateTime.parse(arg.getParamValue(), DateTimeFormatter.ofPattern(Constants.DATE_FORMAT))); + } else if ("java.time.LocalDate".equals(className)) { + paramterTypeList.add("java.time.LocalDate"); + LocalDateTime localDateTime = LocalDateTime.parse(arg.getParamValue(), DateTimeFormatter.ofPattern(Constants.DATE_FORMAT)); + parameterValuesList.add(localDateTime.toLocalDate()); + } else if ("java.time.LocalTime".equals(className)) { + paramterTypeList.add("java.time.LocalTime"); + LocalDateTime localDateTime = LocalDateTime.parse(arg.getParamValue(), DateTimeFormatter.ofPattern(Constants.DATE_FORMAT)); + parameterValuesList.add(localDateTime.toLocalTime()); } else { if (className.endsWith("[]")) { List list = null; diff --git a/src/main/java/io/github/ningyu/jmeter/plugin/util/Constants.java b/src/main/java/io/github/ningyu/jmeter/plugin/util/Constants.java index 136e0dc..ce4b6f5 100644 --- a/src/main/java/io/github/ningyu/jmeter/plugin/util/Constants.java +++ b/src/main/java/io/github/ningyu/jmeter/plugin/util/Constants.java @@ -102,6 +102,8 @@ public class Constants { public static final String DEFAULT_CLUSTER = "failfast"; public static final String DEFAULT_CONNECTIONS = "100"; + public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; + //冗余配置元件中的address、protocols、group,用于在sample gui获取配置元件中的默认值 public static String DEFAULT_PANEL_ADDRESS = ""; public static String DEFAULT_PANEL_PROTOCOLS = ""; diff --git a/src/main/java/io/github/ningyu/jmeter/plugin/util/JsonUtils.java b/src/main/java/io/github/ningyu/jmeter/plugin/util/JsonUtils.java index 58d334f..598da84 100644 --- a/src/main/java/io/github/ningyu/jmeter/plugin/util/JsonUtils.java +++ b/src/main/java/io/github/ningyu/jmeter/plugin/util/JsonUtils.java @@ -21,6 +21,10 @@ import org.apache.log.Logger; import java.lang.reflect.Type; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; import java.util.Locale; /** @@ -31,7 +35,7 @@ public class JsonUtils { private static final Logger logger = LoggingManager.getLoggerForClass(); private static final Gson gson = new GsonBuilder() - .setDateFormat("yyyy-MM-dd HH:mm:ss") + .setDateFormat(Constants.DATE_FORMAT) .setPrettyPrinting() .disableHtmlEscaping() .serializeNulls() @@ -41,6 +45,26 @@ public Locale deserialize(JsonElement json, Type typeOfT, JsonDeserializationCon return ClassUtils.parseLocale(json.getAsJsonPrimitive().getAsString()); } }) + .registerTypeAdapter(LocalDateTime.class, new JsonDeserializer() { + @Override + public LocalDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + return LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ofPattern(Constants.DATE_FORMAT)); + } + }) + .registerTypeAdapter(LocalDate.class, new JsonDeserializer() { + @Override + public LocalDate deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + LocalDateTime localDateTime = LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ofPattern(Constants.DATE_FORMAT)); + return localDateTime.toLocalDate(); + } + }) + .registerTypeAdapter(LocalTime.class, new JsonDeserializer() { + @Override + public LocalTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + LocalDateTime localDateTime = LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ofPattern(Constants.DATE_FORMAT)); + return localDateTime.toLocalTime(); + } + }) .create(); public static String toJson(Object obj) { diff --git a/src/test/java/io/github/ningyu/jmeter/plugin/EnumA.java b/src/test/java/io/github/ningyu/jmeter/plugin/bean/EnumA.java similarity index 81% rename from src/test/java/io/github/ningyu/jmeter/plugin/EnumA.java rename to src/test/java/io/github/ningyu/jmeter/plugin/bean/EnumA.java index 1e5f2e7..333d3ab 100644 --- a/src/test/java/io/github/ningyu/jmeter/plugin/EnumA.java +++ b/src/test/java/io/github/ningyu/jmeter/plugin/bean/EnumA.java @@ -2,7 +2,7 @@ * Copyright (c) 2018, Jiuye SCM and/or its affiliates. All rights reserved. */ -package io.github.ningyu.jmeter.plugin; +package io.github.ningyu.jmeter.plugin.bean; /** * <功能描述> diff --git a/src/test/java/io/github/ningyu/jmeter/plugin/EnumB.java b/src/test/java/io/github/ningyu/jmeter/plugin/bean/EnumB.java similarity index 92% rename from src/test/java/io/github/ningyu/jmeter/plugin/EnumB.java rename to src/test/java/io/github/ningyu/jmeter/plugin/bean/EnumB.java index 354c84a..a0ab547 100644 --- a/src/test/java/io/github/ningyu/jmeter/plugin/EnumB.java +++ b/src/test/java/io/github/ningyu/jmeter/plugin/bean/EnumB.java @@ -2,7 +2,7 @@ * Copyright (c) 2018, Jiuye SCM and/or its affiliates. All rights reserved. */ -package io.github.ningyu.jmeter.plugin; +package io.github.ningyu.jmeter.plugin.bean; /** * <功能描述> diff --git a/src/test/java/io/github/ningyu/jmeter/plugin/bean/LocalDateTimeTestBean.java b/src/test/java/io/github/ningyu/jmeter/plugin/bean/LocalDateTimeTestBean.java new file mode 100644 index 0000000..dd695b9 --- /dev/null +++ b/src/test/java/io/github/ningyu/jmeter/plugin/bean/LocalDateTimeTestBean.java @@ -0,0 +1,37 @@ +package io.github.ningyu.jmeter.plugin.bean; + +import java.io.Serializable; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; + +public class LocalDateTimeTestBean extends TestBean implements Serializable { + private static final long serialVersionUID = 3348012326910037863L; + private LocalDateTime localDateTime; + private LocalDate localDate; + private LocalTime localTime; + + public LocalDateTime getLocalDateTime() { + return localDateTime; + } + + public void setLocalDateTime(LocalDateTime localDateTime) { + this.localDateTime = localDateTime; + } + + public LocalDate getLocalDate() { + return localDate; + } + + public void setLocalDate(LocalDate localDate) { + this.localDate = localDate; + } + + public LocalTime getLocalTime() { + return localTime; + } + + public void setLocalTime(LocalTime localTime) { + this.localTime = localTime; + } +} diff --git a/src/test/java/io/github/ningyu/jmeter/plugin/LocaleTestBean.java b/src/test/java/io/github/ningyu/jmeter/plugin/bean/LocaleTestBean.java similarity index 89% rename from src/test/java/io/github/ningyu/jmeter/plugin/LocaleTestBean.java rename to src/test/java/io/github/ningyu/jmeter/plugin/bean/LocaleTestBean.java index 46d894f..d44ad4c 100644 --- a/src/test/java/io/github/ningyu/jmeter/plugin/LocaleTestBean.java +++ b/src/test/java/io/github/ningyu/jmeter/plugin/bean/LocaleTestBean.java @@ -1,4 +1,4 @@ -package io.github.ningyu.jmeter.plugin; +package io.github.ningyu.jmeter.plugin.bean; import java.io.Serializable; import java.util.Locale; diff --git a/src/test/java/io/github/ningyu/jmeter/plugin/TestBean.java b/src/test/java/io/github/ningyu/jmeter/plugin/bean/TestBean.java similarity index 90% rename from src/test/java/io/github/ningyu/jmeter/plugin/TestBean.java rename to src/test/java/io/github/ningyu/jmeter/plugin/bean/TestBean.java index 557cfd6..6c2da28 100644 --- a/src/test/java/io/github/ningyu/jmeter/plugin/TestBean.java +++ b/src/test/java/io/github/ningyu/jmeter/plugin/bean/TestBean.java @@ -1,4 +1,4 @@ -package io.github.ningyu.jmeter.plugin; +package io.github.ningyu.jmeter.plugin.bean; import java.io.Serializable; diff --git a/src/test/java/io/github/ningyu/jmeter/plugin/ClassUtilsTest.java b/src/test/java/io/github/ningyu/jmeter/plugin/test/ClassUtilsTest.java similarity index 52% rename from src/test/java/io/github/ningyu/jmeter/plugin/ClassUtilsTest.java rename to src/test/java/io/github/ningyu/jmeter/plugin/test/ClassUtilsTest.java index 45ed014..6154f5c 100644 --- a/src/test/java/io/github/ningyu/jmeter/plugin/ClassUtilsTest.java +++ b/src/test/java/io/github/ningyu/jmeter/plugin/test/ClassUtilsTest.java @@ -1,11 +1,17 @@ -package io.github.ningyu.jmeter.plugin; +package io.github.ningyu.jmeter.plugin.test; import com.google.gson.internal.LinkedTreeMap; +import io.github.ningyu.jmeter.plugin.bean.LocaleTestBean; import io.github.ningyu.jmeter.plugin.dubbo.sample.MethodArgument; import io.github.ningyu.jmeter.plugin.util.ClassUtils; +import io.github.ningyu.jmeter.plugin.util.Constants; import org.junit.Assert; import org.junit.Test; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -41,8 +47,8 @@ public void testLocaleArray() { @Test public void testGeneric() { - String paramValue = "{\"locale\":\"CHINESE\",\"name\":\"aaa\",\"item\":{\"class\":\"io.github.ningyu.jmeter.plugin.TestBean\",\"name\":\"bbb\"}}"; - String paramType = "io.github.ningyu.jmeter.plugin.LocaleTestBean"; + String paramValue = "{\"locale\":\"CHINESE\",\"name\":\"aaa\",\"item\":{\"class\":\"io.github.ningyu.jmeter.plugin.bean.TestBean\",\"name\":\"bbb\"}}"; + String paramType = "io.github.ningyu.jmeter.plugin.bean.LocaleTestBean"; MethodArgument arg = new MethodArgument(paramType, paramValue); List paramterTypeList = new ArrayList<>(); List parameterValuesList = new ArrayList<>(); @@ -55,4 +61,43 @@ public void testGeneric() { Assert.assertEquals(((Map)((LocaleTestBean)parameterValuesList.get(0)).getItem()).get("name"), "bbb"); Assert.assertEquals(((Map)((LocaleTestBean)parameterValuesList.get(0)).getItem()).get("item"), null); } + + @Test + public void testLocalDateTime() { + String paramValue = "2020-10-15 20:08:56"; + String paramType = "java.time.LocalDateTime"; + MethodArgument arg = new MethodArgument(paramType, paramValue); + List paramterTypeList = new ArrayList<>(); + List parameterValuesList = new ArrayList<>(); + ClassUtils.parseParameter(paramterTypeList, parameterValuesList, arg); + Assert.assertEquals(paramterTypeList.get(0),paramType); + Assert.assertEquals(parameterValuesList.get(0).getClass(), LocalDateTime.class); + Assert.assertEquals(((LocalDateTime)parameterValuesList.get(0)).format(DateTimeFormatter.ofPattern(Constants.DATE_FORMAT)), paramValue); + } + + @Test + public void testLocalDate() { + String paramValue = "2020-10-15 20:08:56"; + String paramType = "java.time.LocalDate"; + MethodArgument arg = new MethodArgument(paramType, paramValue); + List paramterTypeList = new ArrayList<>(); + List parameterValuesList = new ArrayList<>(); + ClassUtils.parseParameter(paramterTypeList, parameterValuesList, arg); + Assert.assertEquals(paramterTypeList.get(0),paramType); + Assert.assertEquals(parameterValuesList.get(0).getClass(), LocalDate.class); + Assert.assertEquals(((LocalDate)parameterValuesList.get(0)).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")), "2020-10-15"); + } + + @Test + public void testLocalTime() { + String paramValue = "2020-10-15 20:08:56"; + String paramType = "java.time.LocalTime"; + MethodArgument arg = new MethodArgument(paramType, paramValue); + List paramterTypeList = new ArrayList<>(); + List parameterValuesList = new ArrayList<>(); + ClassUtils.parseParameter(paramterTypeList, parameterValuesList, arg); + Assert.assertEquals(paramterTypeList.get(0),paramType); + Assert.assertEquals(parameterValuesList.get(0).getClass(), LocalTime.class); + Assert.assertEquals(((LocalTime)parameterValuesList.get(0)).format(DateTimeFormatter.ofPattern("HH:mm:ss")), "20:08:56"); + } } diff --git a/src/test/java/io/github/ningyu/jmeter/plugin/GenericServiceTest.java b/src/test/java/io/github/ningyu/jmeter/plugin/test/GenericServiceTest.java similarity index 97% rename from src/test/java/io/github/ningyu/jmeter/plugin/GenericServiceTest.java rename to src/test/java/io/github/ningyu/jmeter/plugin/test/GenericServiceTest.java index c7e6807..466f4ef 100644 --- a/src/test/java/io/github/ningyu/jmeter/plugin/GenericServiceTest.java +++ b/src/test/java/io/github/ningyu/jmeter/plugin/test/GenericServiceTest.java @@ -1,4 +1,4 @@ -package io.github.ningyu.jmeter.plugin; +package io.github.ningyu.jmeter.plugin.test; import io.github.ningyu.jmeter.plugin.dubbo.sample.MethodArgument; import io.github.ningyu.jmeter.plugin.util.ClassUtils; @@ -16,7 +16,7 @@ import java.util.List; public class GenericServiceTest { -// @Test + @Test public void test() { ApplicationConfig application = new ApplicationConfig(); application.setName("api-generic-consumer"); @@ -33,7 +33,7 @@ public void test() { System.out.println(json); } -// @Test + @Test public void testAttachment() { ApplicationConfig application = new ApplicationConfig(); application.setName("api-generic-consumer"); @@ -51,7 +51,7 @@ public void testAttachment() { System.out.println(json); } -// @Test + @Test public void testZk() { for(int i=0;i<5;i++) { ApplicationConfig application = new ApplicationConfig(); @@ -82,7 +82,7 @@ public void testZk() { } } -// @Test + @Test public void testEnumA() { List paramterTypeList = new ArrayList<>(); List parameterValuesList = new ArrayList<>(); @@ -92,7 +92,7 @@ public void testEnumA() { System.out.println(parameterValuesList.toString()); } -// @Test + @Test public void testEnumB() { List paramterTypeList = new ArrayList<>(); List parameterValuesList = new ArrayList<>(); From 84210e856841b0aaab96e0e0b913e426c7abf9b3 Mon Sep 17 00:00:00 2001 From: ningyu Date: Fri, 16 Oct 2020 15:14:05 +0800 Subject: [PATCH 9/9] =?UTF-8?q?fixed=20Locale=E3=80=81LocalDateTime?= =?UTF-8?q?=E3=80=81LocalDate=E3=80=81LocalTime?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ningyu/jmeter/plugin/util/ClassUtils.java | 42 +++++++++---------- .../ningyu/jmeter/plugin/util/JsonUtils.java | 41 +++++++----------- .../jmeter/plugin/bean/LongTestBean.java | 26 ++++++++++++ .../jmeter/plugin/test/ClassUtilsTest.java | 32 ++++++++++++-- 4 files changed, 91 insertions(+), 50 deletions(-) create mode 100644 src/test/java/io/github/ningyu/jmeter/plugin/bean/LongTestBean.java diff --git a/src/main/java/io/github/ningyu/jmeter/plugin/util/ClassUtils.java b/src/main/java/io/github/ningyu/jmeter/plugin/util/ClassUtils.java index 158f6e6..2e93c5a 100644 --- a/src/main/java/io/github/ningyu/jmeter/plugin/util/ClassUtils.java +++ b/src/main/java/io/github/ningyu/jmeter/plugin/util/ClassUtils.java @@ -87,49 +87,49 @@ public static void parseParameter(List paramterTypeList, parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.INT_DEFAULT : Integer.parseInt(arg.getParamValue())); } else if("int[]".equals(className)) { paramterTypeList.add(arg.getParamType()); - parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.INT_ARRAY_DEFAULT : JsonUtils.formJson(arg.getParamValue(), new TypeToken() {}.getType())); + parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.INT_ARRAY_DEFAULT : JsonUtils.fromJson(arg.getParamValue(), new TypeToken() {}.getType())); } else if ("double".equals(className)) { paramterTypeList.add(arg.getParamType()); parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.DOUBLE_DEFAULT : Double.parseDouble(arg.getParamValue())); } else if ("double[]".equals(className)) { paramterTypeList.add(arg.getParamType()); - parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.DOUBLE_ARRAY_DEFAULT : JsonUtils.formJson(arg.getParamValue(), new TypeToken() {}.getType())); + parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.DOUBLE_ARRAY_DEFAULT : JsonUtils.fromJson(arg.getParamValue(), new TypeToken() {}.getType())); } else if ("short".equals(className)) { paramterTypeList.add(arg.getParamType()); parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.SHORT_DEFAULT : Short.parseShort(arg.getParamValue())); } else if ("short[]".equals(className)) { paramterTypeList.add(arg.getParamType()); - parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.SHORT_ARRAY_DEFAULT : JsonUtils.formJson(arg.getParamValue(), new TypeToken() {}.getType())); + parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.SHORT_ARRAY_DEFAULT : JsonUtils.fromJson(arg.getParamValue(), new TypeToken() {}.getType())); } else if ("float".equals(className)) { paramterTypeList.add(arg.getParamType()); parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.FLOAT_DEFAULT : Float.parseFloat(arg.getParamValue())); } else if ("float[]".equals(className)) { paramterTypeList.add(arg.getParamType()); - parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.FLOAT_ARRAY_DEFAULT : JsonUtils.formJson(arg.getParamValue(), new TypeToken() {}.getType())); + parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.FLOAT_ARRAY_DEFAULT : JsonUtils.fromJson(arg.getParamValue(), new TypeToken() {}.getType())); } else if ("long".equals(className)) { paramterTypeList.add(arg.getParamType()); parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.LONG_DEFAULT : Long.parseLong(arg.getParamValue())); } else if ("long[]".equals(className)) { paramterTypeList.add(arg.getParamType()); - parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.LONG_ARRAY_DEFAULT : JsonUtils.formJson(arg.getParamValue(), new TypeToken() {}.getType())); + parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.LONG_ARRAY_DEFAULT : JsonUtils.fromJson(arg.getParamValue(), new TypeToken() {}.getType())); } else if ("byte".equals(className)) { paramterTypeList.add(arg.getParamType()); parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.BYTE_DEFAULT : Byte.parseByte(arg.getParamValue())); } else if ("byte[]".equals(className)) { paramterTypeList.add(arg.getParamType()); - parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.BYTE_ARRAY_DEFAULT : JsonUtils.formJson(arg.getParamValue(), new TypeToken() {}.getType())); + parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.BYTE_ARRAY_DEFAULT : JsonUtils.fromJson(arg.getParamValue(), new TypeToken() {}.getType())); } else if ("boolean".equals(className)) { paramterTypeList.add(arg.getParamType()); parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.BOOLEAN_DEFAULT : Boolean.parseBoolean(arg.getParamValue())); } else if ("boolean[]".equals(className)) { paramterTypeList.add(arg.getParamType()); - parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.BOOLEAN_ARRAY_DEFAULT : JsonUtils.formJson(arg.getParamValue(), new TypeToken() {}.getType())); + parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.BOOLEAN_ARRAY_DEFAULT : JsonUtils.fromJson(arg.getParamValue(), new TypeToken() {}.getType())); } else if ("char".equals(className)) { paramterTypeList.add(arg.getParamType()); parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.CHAR_DEFAULT : arg.getParamValue().charAt(0)); } else if ("char[]".equals(className)) { paramterTypeList.add(arg.getParamType()); - parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.CHAT_ARRAY_DEFAULT : JsonUtils.formJson(arg.getParamValue(), new TypeToken() {}.getType())); + parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? Constants.CHAT_ARRAY_DEFAULT : JsonUtils.fromJson(arg.getParamValue(), new TypeToken() {}.getType())); } else if ("java.lang.String".equals(className) || "String".equals(className) || "string".equals(className)) { @@ -139,7 +139,7 @@ public static void parseParameter(List paramterTypeList, || "String[]".equals(className) || "string[]".equals(className)) { paramterTypeList.add("java.lang.String[]"); - parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? null : JsonUtils.formJson(arg.getParamValue(), new TypeToken() {}.getType())); + parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? null : JsonUtils.fromJson(arg.getParamValue(), new TypeToken() {}.getType())); } else if ("java.lang.Integer".equals(className) || "Integer".equals(className) || "integer".equals(className)) { @@ -149,7 +149,7 @@ public static void parseParameter(List paramterTypeList, || "Integer[]".equals(className) || "integer[]".equals(className)) { paramterTypeList.add("java.lang.Integer[]"); - parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? null : JsonUtils.formJson(arg.getParamValue(), new TypeToken() {}.getType())); + parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? null : JsonUtils.fromJson(arg.getParamValue(), new TypeToken() {}.getType())); } else if ("java.lang.Double".equals(className) || "Double".equals(className)) { paramterTypeList.add("java.lang.Double"); @@ -157,7 +157,7 @@ public static void parseParameter(List paramterTypeList, } else if ("java.lang.Double[]".equals(className) || "Double[]".equals(className)) { paramterTypeList.add("java.lang.Double[]"); - parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? null : JsonUtils.formJson(arg.getParamValue(), new TypeToken() {}.getType())); + parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? null : JsonUtils.fromJson(arg.getParamValue(), new TypeToken() {}.getType())); } else if ("java.lang.Short".equals(className) || "Short".equals(className)) { paramterTypeList.add("java.lang.Short"); @@ -165,7 +165,7 @@ public static void parseParameter(List paramterTypeList, } else if ("java.lang.Short[]".equals(className) || "Short[]".equals(className)) { paramterTypeList.add("java.lang.Short[]"); - parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? null : JsonUtils.formJson(arg.getParamValue(), new TypeToken() {}.getType())); + parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? null : JsonUtils.fromJson(arg.getParamValue(), new TypeToken() {}.getType())); } else if ("java.lang.Long".equals(className) || "Long".equals(className)) { paramterTypeList.add("java.lang.Long"); @@ -173,7 +173,7 @@ public static void parseParameter(List paramterTypeList, } else if("java.lang.Long[]".equals(className) || "Long[]".equals(className)) { paramterTypeList.add("java.lang.Long[]"); - parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? null : JsonUtils.formJson(arg.getParamValue(), new TypeToken() {}.getType())); + parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? null : JsonUtils.fromJson(arg.getParamValue(), new TypeToken() {}.getType())); } else if ("java.lang.Float".equals(className) || "Float".equals(className)) { paramterTypeList.add("java.lang.Float"); @@ -181,7 +181,7 @@ public static void parseParameter(List paramterTypeList, } else if ("java.lang.Float[]".equals(className) || "Float[]".equals(className)) { paramterTypeList.add("java.lang.Float[]"); - parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? null : JsonUtils.formJson(arg.getParamValue(), new TypeToken() {}.getType())); + parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? null : JsonUtils.fromJson(arg.getParamValue(), new TypeToken() {}.getType())); } else if ("java.lang.Byte".equals(className) || "Byte".equals(className)) { paramterTypeList.add("java.lang.Byte"); @@ -189,7 +189,7 @@ public static void parseParameter(List paramterTypeList, } else if ("java.lang.Byte[]".equals(className) || "Byte[]".equals(className)) { paramterTypeList.add("java.lang.Byte[]"); - parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? null : JsonUtils.formJson(arg.getParamValue(), new TypeToken() {}.getType())); + parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? null : JsonUtils.fromJson(arg.getParamValue(), new TypeToken() {}.getType())); } else if ("java.lang.Boolean".equals(className) || "Boolean".equals(className)) { paramterTypeList.add("java.lang.Boolean"); @@ -197,13 +197,13 @@ public static void parseParameter(List paramterTypeList, } else if ("java.lang.Boolean[]".equals(className) || "Boolean[]".equals(className)) { paramterTypeList.add("java.lang.Boolean[]"); - parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? null : JsonUtils.formJson(arg.getParamValue(), new TypeToken() {}.getType())); + parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? null : JsonUtils.fromJson(arg.getParamValue(), new TypeToken() {}.getType())); } else if ("java.util.Locale".equals(className)) { paramterTypeList.add("java.util.Locale"); parameterValuesList.add(parseLocale(arg.getParamValue())); } else if ("java.util.Locale[]".equals(className)) { paramterTypeList.add("java.util.Locale[]"); - List list = JsonUtils.formJson(arg.getParamValue(), new TypeToken>() {}.getType()); + List list = JsonUtils.fromJson(arg.getParamValue(), new TypeToken>() {}.getType()); List localeList = new ArrayList<>(); for (String locale : list) { localeList.add(parseLocale(locale)); @@ -224,7 +224,7 @@ public static void parseParameter(List paramterTypeList, if (className.endsWith("[]")) { List list = null; if (!StringUtils.isBlank(arg.getParamValue())) { - list = JsonUtils.formJson(arg.getParamValue(), new TypeToken>() {}.getType()); + list = JsonUtils.fromJson(arg.getParamValue(), new TypeToken>() {}.getType()); } paramterTypeList.add(arg.getParamType()); parameterValuesList.add(list == null ? null : list.toArray()); @@ -232,17 +232,17 @@ public static void parseParameter(List paramterTypeList, try { Class clazz = Class.forName(className); paramterTypeList.add(arg.getParamType()); - parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? null : JsonUtils.formJson(arg.getParamValue(), clazz)); + parameterValuesList.add(StringUtils.isBlank(arg.getParamValue()) ? null : JsonUtils.fromJson(arg.getParamValue(), clazz)); } catch (ClassNotFoundException e) { //不是jdk或者lib下的类,使用通用map格式反序列化值 paramterTypeList.add(arg.getParamType()); Object obj = null; if (!StringUtils.isBlank(arg.getParamValue())) { //使用通用map格式反序列化值 - obj = JsonUtils.formJson(arg.getParamValue(), new TypeToken>() {}.getType()); + obj = JsonUtils.fromJson(arg.getParamValue(), new TypeToken>() {}.getType()); if (obj == null) { //枚举类型的类走字符串序列化 - obj = JsonUtils.formJson(arg.getParamValue(), String.class); + obj = JsonUtils.fromJson(arg.getParamValue(), String.class); } } parameterValuesList.add(obj); diff --git a/src/main/java/io/github/ningyu/jmeter/plugin/util/JsonUtils.java b/src/main/java/io/github/ningyu/jmeter/plugin/util/JsonUtils.java index 598da84..94b9cae 100644 --- a/src/main/java/io/github/ningyu/jmeter/plugin/util/JsonUtils.java +++ b/src/main/java/io/github/ningyu/jmeter/plugin/util/JsonUtils.java @@ -16,7 +16,10 @@ */ package io.github.ningyu.jmeter.plugin.util; -import com.google.gson.*; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonSyntaxException; import org.apache.jorphan.logging.LoggingManager; import org.apache.log.Logger; @@ -39,31 +42,19 @@ public class JsonUtils { .setPrettyPrinting() .disableHtmlEscaping() .serializeNulls() - .registerTypeAdapter(Locale.class, new JsonDeserializer() { - @Override - public Locale deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { - return ClassUtils.parseLocale(json.getAsJsonPrimitive().getAsString()); - } + .registerTypeAdapter(Locale.class, (JsonDeserializer) (json, typeOfT, context) -> { + return ClassUtils.parseLocale(json.getAsJsonPrimitive().getAsString()); }) - .registerTypeAdapter(LocalDateTime.class, new JsonDeserializer() { - @Override - public LocalDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { - return LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ofPattern(Constants.DATE_FORMAT)); - } + .registerTypeAdapter(LocalDateTime.class, (JsonDeserializer) (json, typeOfT, context) -> { + return LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ofPattern(Constants.DATE_FORMAT)); }) - .registerTypeAdapter(LocalDate.class, new JsonDeserializer() { - @Override - public LocalDate deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { - LocalDateTime localDateTime = LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ofPattern(Constants.DATE_FORMAT)); - return localDateTime.toLocalDate(); - } + .registerTypeAdapter(LocalDate.class, (JsonDeserializer) (json, typeOfT, context) -> { + LocalDateTime localDateTime = LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ofPattern(Constants.DATE_FORMAT)); + return localDateTime.toLocalDate(); }) - .registerTypeAdapter(LocalTime.class, new JsonDeserializer() { - @Override - public LocalTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { - LocalDateTime localDateTime = LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ofPattern(Constants.DATE_FORMAT)); - return localDateTime.toLocalTime(); - } + .registerTypeAdapter(LocalTime.class, (JsonDeserializer) (json, typeOfT, context) -> { + LocalDateTime localDateTime = LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ofPattern(Constants.DATE_FORMAT)); + return localDateTime.toLocalTime(); }) .create(); @@ -75,7 +66,7 @@ public static String toJson(Object obj, Type type) { return gson.toJson(obj, type); } - public static T formJson(String json, Class classOfT) { + public static T fromJson(String json, Class classOfT) { try { return gson.fromJson(json, classOfT); } catch (JsonSyntaxException e) { @@ -85,7 +76,7 @@ public static T formJson(String json, Class classOfT) { return null; } - public static T formJson(String json, Type type) { + public static T fromJson(String json, Type type) { try { return gson.fromJson(json, type); } catch (JsonSyntaxException e) { diff --git a/src/test/java/io/github/ningyu/jmeter/plugin/bean/LongTestBean.java b/src/test/java/io/github/ningyu/jmeter/plugin/bean/LongTestBean.java new file mode 100644 index 0000000..1e82ebf --- /dev/null +++ b/src/test/java/io/github/ningyu/jmeter/plugin/bean/LongTestBean.java @@ -0,0 +1,26 @@ +package io.github.ningyu.jmeter.plugin.bean; + +import java.io.Serializable; + +public class LongTestBean extends TestBean implements Serializable { + private static final long serialVersionUID = 4855342359618933272L; + + public Long getBigLong() { + return bigLong; + } + + public void setBigLong(Long bigLong) { + this.bigLong = bigLong; + } + + public long getSmallLong() { + return smallLong; + } + + public void setSmallLong(long smallLong) { + this.smallLong = smallLong; + } + + private Long bigLong; + private long smallLong; +} diff --git a/src/test/java/io/github/ningyu/jmeter/plugin/test/ClassUtilsTest.java b/src/test/java/io/github/ningyu/jmeter/plugin/test/ClassUtilsTest.java index 6154f5c..fb8de28 100644 --- a/src/test/java/io/github/ningyu/jmeter/plugin/test/ClassUtilsTest.java +++ b/src/test/java/io/github/ningyu/jmeter/plugin/test/ClassUtilsTest.java @@ -1,10 +1,14 @@ package io.github.ningyu.jmeter.plugin.test; +import com.google.common.reflect.TypeToken; import com.google.gson.internal.LinkedTreeMap; import io.github.ningyu.jmeter.plugin.bean.LocaleTestBean; +import io.github.ningyu.jmeter.plugin.bean.LongTestBean; +import io.github.ningyu.jmeter.plugin.bean.TestBean; import io.github.ningyu.jmeter.plugin.dubbo.sample.MethodArgument; import io.github.ningyu.jmeter.plugin.util.ClassUtils; import io.github.ningyu.jmeter.plugin.util.Constants; +import io.github.ningyu.jmeter.plugin.util.JsonUtils; import org.junit.Assert; import org.junit.Test; @@ -12,10 +16,7 @@ import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; -import java.util.Map; +import java.util.*; public class ClassUtilsTest { @Test @@ -100,4 +101,27 @@ public void testLocalTime() { Assert.assertEquals(parameterValuesList.get(0).getClass(), LocalTime.class); Assert.assertEquals(((LocalTime)parameterValuesList.get(0)).format(DateTimeFormatter.ofPattern("HH:mm:ss")), "20:08:56"); } + + @Test + public void testLongType() { + String paramValue = "{\"name\":\"tomcat\",\"bigLong\":6906764140092371368,\"smallLong\":6906764140092371368,\"item\":{\"class\":\"io.github.ningyu.jmeter.plugin.bean.TestBean\",\"name\":\"aaaa\"}}\n"; + String paramType = "io.github.ningyu.jmeter.plugin.bean.LongTestBean"; + MethodArgument arg = new MethodArgument(paramType, paramValue); + List paramterTypeList = new ArrayList<>(); + List parameterValuesList = new ArrayList<>(); + ClassUtils.parseParameter(paramterTypeList, parameterValuesList, arg); + Assert.assertEquals(paramterTypeList.get(0),paramType); + Assert.assertEquals(parameterValuesList.get(0).getClass(), LongTestBean.class); + Assert.assertEquals(((LongTestBean)parameterValuesList.get(0)).getBigLong(), Long.valueOf(6906764140092371368L)); + Assert.assertEquals(((LongTestBean)parameterValuesList.get(0)).getSmallLong(), 6906764140092371368L); + + + String jsonString = paramValue; + System.out.println(jsonString); // output ok: {"first":6906764140092371368} + Map mapFromJson = JsonUtils.fromJson(jsonString, new TypeToken>(){}.getType()); + System.out.println(mapFromJson.get("first")); + System.out.println(mapFromJson.get("aaa")); + } + + }