-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add spring-trpc junit #36
Changes from 1 commit
5e4840e
77b1351
31c817c
0c47ac4
3704ad9
abd95f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Tencent is pleased to support the open source community by making tRPC available. | ||
* | ||
* Copyright (C) 2023 THL A29 Limited, a Tencent company. | ||
* All rights reserved. | ||
* | ||
* If you have downloaded a copy of the tRPC source code from Tencent, | ||
* please note that tRPC source code is licensed under the Apache 2.0 License, | ||
* A copy of the Apache 2.0 License can be found in the LICENSE file. | ||
*/ | ||
|
||
package com.tencent.trpc.spring.context.configuration; | ||
|
||
import com.tencent.trpc.core.common.ConfigManager; | ||
import com.tencent.trpc.core.common.config.ClientConfig; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
|
||
public class AddFilterTRpcConfigManagerCustomizerTest { | ||
|
||
|
||
private static final String KEY = "OrderKey:b131d74c753540db859c626c564aa8ce"; | ||
|
||
@Test | ||
public void testConstructor() { | ||
AddFilterTRpcConfigManagerCustomizer addFilterTRpcConfigManagerCustomizer = new AddFilterTRpcConfigManagerCustomizer(); | ||
Check warning on line 31 in trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java GitHub Actions / runner / checkstyle
|
||
Assert.assertNotNull(addFilterTRpcConfigManagerCustomizer); | ||
} | ||
|
||
@Test | ||
public void testAddClientFilters() { | ||
AddFilterTRpcConfigManagerCustomizer addFilterTRpcConfigManagerCustomizer = new AddFilterTRpcConfigManagerCustomizer(); | ||
Check warning on line 37 in trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java GitHub Actions / runner / checkstyle
|
||
AddFilterTRpcConfigManagerCustomizer customizer = addFilterTRpcConfigManagerCustomizer.addClientFilters( | ||
"filter1", "filter2"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 建议这些字符串也提取为常量 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i fix it |
||
Assert.assertEquals(addFilterTRpcConfigManagerCustomizer, customizer); | ||
} | ||
|
||
@Test | ||
public void testAddServerFilters() { | ||
AddFilterTRpcConfigManagerCustomizer addFilterTRpcConfigManagerCustomizer = new AddFilterTRpcConfigManagerCustomizer(); | ||
Check warning on line 45 in trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java GitHub Actions / runner / checkstyle
|
||
AddFilterTRpcConfigManagerCustomizer customizer = addFilterTRpcConfigManagerCustomizer.addServerFilters( | ||
"filter1", "filter2"); | ||
Assert.assertEquals(addFilterTRpcConfigManagerCustomizer, customizer); | ||
} | ||
|
||
@Test | ||
public void testCustomize() { | ||
AddFilterTRpcConfigManagerCustomizer addFilterTRpcConfigManagerCustomizer = new AddFilterTRpcConfigManagerCustomizer(); | ||
Check warning on line 53 in trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java GitHub Actions / runner / checkstyle
|
||
|
||
ConfigManager instance = ConfigManager.getInstance(); | ||
ClientConfig clientConfig = new ClientConfig(); | ||
List<String> list = Arrays.asList("filter1", "filter2"); | ||
clientConfig.setFilters(list); | ||
instance.setClientConfig(clientConfig); | ||
addFilterTRpcConfigManagerCustomizer.customize(instance); | ||
List<String> filters = instance.getClientConfig().getFilters(); | ||
|
||
Assert.assertEquals(filters.size(), list.size()); | ||
Assert.assertEquals(list, instance.getClientConfig().getFilters()); | ||
} | ||
|
||
@Test | ||
public void testGetOrder() { | ||
AddFilterTRpcConfigManagerCustomizer addFilterTRpcConfigManagerCustomizer = new AddFilterTRpcConfigManagerCustomizer2(); | ||
Check warning on line 69 in trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java GitHub Actions / runner / checkstyle
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new AddFilterTRpcConfigManagerCustomizer();然后断言 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i fix it |
||
Assert.assertEquals(Integer.MAX_VALUE, addFilterTRpcConfigManagerCustomizer.getOrder()); | ||
Integer value = 1024; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 把这个定义为常量,getOrder里面直接返回常量就行。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i fix it |
||
System.setProperty(KEY, String.valueOf(value)); | ||
Assert.assertEquals((long) value, addFilterTRpcConfigManagerCustomizer.getOrder()); | ||
} | ||
|
||
static final class AddFilterTRpcConfigManagerCustomizer2 extends AddFilterTRpcConfigManagerCustomizer { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 建议不用出现1 2 3这种数字的命名,可以命名为TestFilterConfigManagerCustomizer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i fix it |
||
|
||
@Override | ||
public int getOrder() { | ||
return System.getProperty(KEY) == null ? super.getOrder() : Integer.parseInt(System.getProperty(KEY)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 返回一个固定值就行 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i fix it |
||
} | ||
} | ||
|
||
@Test | ||
public void testEmptyIfNull() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个函数可以通过AddFilterTRpcConfigManagerCustomizer构造函数测,尽量减少使用setAccessible。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i fix it |
||
|
||
AddFilterTRpcConfigManagerCustomizer customizer = new AddFilterTRpcConfigManagerCustomizer(); | ||
|
||
Method method = customizer.getClass().getDeclaredMethod("emptyIfNull", List.class); | ||
boolean accessible = method.isAccessible(); | ||
method.setAccessible(true); | ||
List<String> list1 = Arrays.asList("filter1", "filter2", "filter3"); | ||
Object invoke = method.invoke(customizer, list1); | ||
if (invoke instanceof List) { | ||
List<String> resList = (List<String>) invoke; | ||
Assert.assertEquals(list1.size(), resList.size()); | ||
} | ||
method.setAccessible(accessible); | ||
} | ||
|
||
@Test | ||
public void testMerge() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个函数可以通过customize函数测,尽量减少使用setAccessible。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i fix it |
||
AddFilterTRpcConfigManagerCustomizer customizer = new AddFilterTRpcConfigManagerCustomizer(); | ||
List<String> list1 = Arrays.asList("filter1", "filter2", "filter3"); | ||
List<String> list2 = Arrays.asList("filter4", "filter5", "filter6"); | ||
Method method = customizer.getClass().getDeclaredMethod("merge", List.class, List.class); | ||
boolean accessible = method.isAccessible(); | ||
method.setAccessible(true); | ||
Object invoke = method.invoke(customizer, list1, list2); | ||
if (invoke instanceof List) { | ||
List<String> resList = (List<String>) invoke; | ||
Assert.assertEquals(list1.size() + list2.size(), resList.size()); | ||
} | ||
method.setAccessible(accessible); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
多余空行,下面也是,一般只有一个空行
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i fix it