Skip to content
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

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

多余空行,下面也是,一般只有一个空行

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i fix it

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

View workflow job for this annotation

GitHub Actions / runner / checkstyle

[checkstyle] reported by reviewdog 🐶 本行字符数 127个,最多:120个。 Raw Output: /github/workspace/./trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java:31:0: warning: 本行字符数 127个,最多:120个。 (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)
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

View workflow job for this annotation

GitHub Actions / runner / checkstyle

[checkstyle] reported by reviewdog 🐶 本行字符数 127个,最多:120个。 Raw Output: /github/workspace/./trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java:37:0: warning: 本行字符数 127个,最多:120个。 (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)
AddFilterTRpcConfigManagerCustomizer customizer = addFilterTRpcConfigManagerCustomizer.addClientFilters(
"filter1", "filter2");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议这些字符串也提取为常量

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

View workflow job for this annotation

GitHub Actions / runner / checkstyle

[checkstyle] reported by reviewdog 🐶 本行字符数 127个,最多:120个。 Raw Output: /github/workspace/./trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java:45:0: warning: 本行字符数 127个,最多:120个。 (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)
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

View workflow job for this annotation

GitHub Actions / runner / checkstyle

[checkstyle] reported by reviewdog 🐶 本行字符数 127个,最多:120个。 Raw Output: /github/workspace/./trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java:53:0: warning: 本行字符数 127个,最多:120个。 (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)

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

View workflow job for this annotation

GitHub Actions / runner / checkstyle

[checkstyle] reported by reviewdog 🐶 本行字符数 128个,最多:120个。 Raw Output: /github/workspace/./trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java:69:0: warning: 本行字符数 128个,最多:120个。 (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new AddFilterTRpcConfigManagerCustomizer();然后断言

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

把这个定义为常量,getOrder里面直接返回常量就行。

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议不用出现1 2 3这种数字的命名,可以命名为TestFilterConfigManagerCustomizer

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

返回一个固定值就行

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i fix it

}
}

@Test
public void testEmptyIfNull() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个函数可以通过AddFilterTRpcConfigManagerCustomizer构造函数测,尽量减少使用setAccessible。

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个函数可以通过customize函数测,尽量减少使用setAccessible。

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
}
}
Loading