Adjust integration tests for common modules in the core module to comply with checkstyle. (#12349)

* Fix exception code error.(#10925)

* [IT]Adjust integration tests for ability, base, and client modules in the core module to comply with checkstyle.

* [IT]Optimize the class naming.

* [IT]Adjust integration tests for common modules in the core module to comply with checkstyle.
This commit is contained in:
阿魁 2024-07-18 11:15:38 +08:00 committed by GitHub
parent c717538bd7
commit 4f827ca0d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 111 additions and 90 deletions

View File

@ -23,12 +23,17 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/**
* This class contains integration tests for the FileTypeEnum enumeration. It verifies the functionality of the
* getFileTypeEnumByFileExtensionOrFileType method. The tests ensure that the FileTypeEnum correctly maps file
* extensions and file types to their respective enum values.
*
* @author by jiangmin.wu on 2020/12/30
*/
class FileTypeEnum_ITCase {
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
class FileTypeEnumCoreITCase {
@Test
void fileTypeEnum_test1() {
void fileTypeEnumTest1() {
for (FileTypeEnum value : FileTypeEnum.values()) {
FileTypeEnum fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType(value.name());
assertEquals(fileTypeEnum, value);
@ -36,7 +41,7 @@ class FileTypeEnum_ITCase {
}
@Test
void fileTypeEnum_test2() {
void fileTypeEnumTest2() {
for (FileTypeEnum value : FileTypeEnum.values()) {
FileTypeEnum fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType(value.getFileType());
assertNotNull(fileTypeEnum);
@ -44,7 +49,7 @@ class FileTypeEnum_ITCase {
}
@Test
void fileTypeEnum_test3() {
void fileTypeEnumTest3() {
FileTypeEnum fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType("t");
assertEquals(FileTypeEnum.TEXT, fileTypeEnum);

View File

@ -48,49 +48,51 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* NacosAsyncRestTemplate_ITCase.
* This class provides integration tests for NacosAsyncRestTemplate. These tests cover various HTTP methods such as
* POST, GET, PUT, and DELETE to ensure the correct functioning of asynchronous HTTP requests in the context of Nacos.
*
* @author mai.jh
*/
@SuppressWarnings("all")
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
@TestMethodOrder(MethodName.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Nacos.class, properties = {
"server.servlet.context-path=/nacos"}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
class NacosAsyncRestTemplate_ITCase {
class NacosAsyncRestTemplateCoreITCase {
private static final HttpClientFactory PROCESSOR_ASYNC_HTTP_CLIENT_FACTORY = new ProcessorHttpClientFactory();
private final String CONFIG_INSTANCE_PATH = "/nacos/v1/ns";
private static final String CONFIG_INSTANCE_PATH = "/nacos/v1/ns";
private final NacosAsyncRestTemplate nacosRestTemplate = HttpClientBeanHolder.getNacosAsyncRestTemplate(
LoggerFactory.getLogger(NacosAsyncRestTemplateCoreITCase.class));
@SuppressWarnings("deprecation")
@LocalServerPort
private int port;
private NacosAsyncRestTemplate nacosRestTemplate = HttpClientBeanHolder.getNacosAsyncRestTemplate(
LoggerFactory.getLogger(NacosAsyncRestTemplate_ITCase.class));
private NacosAsyncRestTemplate processorRestTemplate = null;
private String IP = null;
private String ip = null;
@Autowired
private Environment environment;
@BeforeEach
void init() throws NacosException {
IP = String.format("http://localhost:%d", port);
ip = String.format("http://localhost:%d", port);
EnvUtil.setEnvironment((ConfigurableEnvironment) environment);
processorRestTemplate = HttpClientBeanHolder.getNacosAsyncRestTemplate(PROCESSOR_ASYNC_HTTP_CLIENT_FACTORY);
}
@Test
void test_url_post_form() throws Exception {
String url = IP + CONFIG_INSTANCE_PATH + "/instance";
void testUrlPostForm() throws Exception {
Map<String, String> param = new HashMap<>();
param.put("serviceName", "app-test");
param.put("port", "8080");
param.put("ip", "11.11.11.11");
CallbackMap<String> callbackMap = new CallbackMap<>();
String url = ip + CONFIG_INSTANCE_PATH + "/instance";
nacosRestTemplate.postForm(url, Header.newInstance(), Query.newInstance(), param, String.class, callbackMap);
Thread.sleep(2000);
HttpRestResult<String> restResult = callbackMap.getRestResult();
@ -100,14 +102,15 @@ class NacosAsyncRestTemplate_ITCase {
}
@Test
void test_url_post_form_by_processor() throws Exception {
String url = IP + CONFIG_INSTANCE_PATH + "/instance";
void testUrlPostFormByProcessor() throws Exception {
Map<String, String> param = new HashMap<>();
param.put("serviceName", "app-test2");
param.put("port", "8080");
param.put("ip", "11.11.11.11");
CallbackMap<String> callbackMap = new CallbackMap<>();
processorRestTemplate.postForm(url, Header.newInstance(), Query.newInstance(), param, String.class, callbackMap);
String url = ip + CONFIG_INSTANCE_PATH + "/instance";
processorRestTemplate.postForm(url, Header.newInstance(), Query.newInstance(), param, String.class,
callbackMap);
Thread.sleep(2000);
HttpRestResult<String> restResult = callbackMap.getRestResult();
System.out.println(restResult.getData());
@ -116,13 +119,13 @@ class NacosAsyncRestTemplate_ITCase {
}
@Test
void test_url_put_form() throws Exception {
String url = IP + CONFIG_INSTANCE_PATH + "/instance";
void testUrlPutForm() throws Exception {
Map<String, String> param = new HashMap<>();
param.put("serviceName", "app-test-change");
param.put("port", "8080");
param.put("ip", "11.11.11.11");
CallbackMap<String> callbackMap = new CallbackMap<>();
String url = ip + CONFIG_INSTANCE_PATH + "/instance";
nacosRestTemplate.postForm(url, Header.newInstance(), Query.newInstance(), param, String.class, callbackMap);
Thread.sleep(2000);
HttpRestResult<String> restResult = callbackMap.getRestResult();
@ -132,13 +135,13 @@ class NacosAsyncRestTemplate_ITCase {
}
@Test
void test_url_get() throws Exception {
String url = IP + CONFIG_INSTANCE_PATH + "/instance/list";
void testUrlGet() throws Exception {
String url = ip + CONFIG_INSTANCE_PATH + "/instance/list";
Query query = Query.newInstance().addParam("serviceName", "app-test");
CallbackMap<Map> callbackMap = new CallbackMap<>();
CallbackMap<Map<String, String>> callbackMap = new CallbackMap<>();
nacosRestTemplate.get(url, Header.newInstance(), query, Map.class, callbackMap);
Thread.sleep(2000);
HttpRestResult<Map> restResult = callbackMap.getRestResult();
HttpRestResult<Map<String, String>> restResult = callbackMap.getRestResult();
System.out.println(restResult.getData());
System.out.println(restResult.getHeader());
assertTrue(restResult.ok());
@ -146,14 +149,14 @@ class NacosAsyncRestTemplate_ITCase {
}
@Test
void test_url_by_map() throws Exception {
String url = IP + CONFIG_INSTANCE_PATH + "/instance/list";
void testUrlByMap() throws Exception {
String url = ip + CONFIG_INSTANCE_PATH + "/instance/list";
Map<String, String> param = new HashMap<>();
param.put("serviceName", "app-test");
CallbackMap<Map> callbackMap = new CallbackMap<>();
CallbackMap<Map<String, String>> callbackMap = new CallbackMap<>();
nacosRestTemplate.get(url, Header.newInstance(), Query.newInstance().initParams(param), Map.class, callbackMap);
Thread.sleep(2000);
HttpRestResult<Map> restResult = callbackMap.getRestResult();
HttpRestResult<Map<String, String>> restResult = callbackMap.getRestResult();
System.out.println(restResult.getData());
System.out.println(restResult.getHeader());
assertTrue(restResult.ok());
@ -161,9 +164,10 @@ class NacosAsyncRestTemplate_ITCase {
}
@Test
void test_url_delete() throws Exception {
String url = IP + CONFIG_INSTANCE_PATH + "/instance";
Query query = Query.newInstance().addParam("ip", "11.11.11.11").addParam("port", "8080").addParam("serviceName", "app-test");
void testUrlDelete() throws Exception {
String url = ip + CONFIG_INSTANCE_PATH + "/instance";
Query query = Query.newInstance().addParam("ip", "11.11.11.11").addParam("port", "8080")
.addParam("serviceName", "app-test");
CallbackMap<String> callbackMap = new CallbackMap<>();
nacosRestTemplate.delete(url, Header.newInstance(), query, String.class, callbackMap);
Thread.sleep(2000);
@ -173,7 +177,7 @@ class NacosAsyncRestTemplate_ITCase {
assertTrue(restResult.ok());
}
private class CallbackMap<T> implements Callback<T> {
private static class CallbackMap<T> implements Callback<T> {
private HttpRestResult<T> restResult;

View File

@ -44,36 +44,38 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* NacosRestTemplate_ITCase.
* Integration tests for NacosRestTemplate.This class contains integration tests for NacosRestTemplate using various
* HTTP methods.
*
* @author mai.jh
*/
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Nacos.class, properties = {
"server.servlet.context-path=/nacos"}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@TestMethodOrder(MethodName.class)
class NacosRestTemplate_ITCase {
class NacosRestTemplateCoreITCase {
private final String INSTANCE_PATH = "/nacos/v1/ns";
private static final String INSTANCE_PATH = "/nacos/v1/ns";
private final String CONFIG_PATH = "/nacos/v1/cs";
private static final String CONFIG_PATH = "/nacos/v1/cs";
private final NacosRestTemplate nacosRestTemplate = HttpClientBeanHolder.getNacosRestTemplate(
LoggerFactory.getLogger(NacosRestTemplateCoreITCase.class));
@LocalServerPort
private int port;
private NacosRestTemplate nacosRestTemplate = HttpClientBeanHolder.getNacosRestTemplate(
LoggerFactory.getLogger(NacosRestTemplate_ITCase.class));
private String IP = null;
private String ip = null;
@BeforeEach
void init() throws NacosException {
IP = String.format("http://localhost:%d", port);
ip = String.format("http://localhost:%d", port);
}
@Test
void test_url_post_config() throws Exception {
String url = IP + CONFIG_PATH + "/configs";
void testUrlPostConfig() throws Exception {
String url = ip + CONFIG_PATH + "/configs";
Map<String, String> param = new HashMap<>();
param.put("dataId", "test-1");
param.put("group", "DEFAULT_GROUP");
@ -85,9 +87,10 @@ class NacosRestTemplate_ITCase {
}
@Test
void test_url_get_return_restResult() throws Exception {
String url = IP + CONFIG_PATH + "/configs";
Query query = Query.newInstance().addParam("beta", true).addParam("dataId", "test-1").addParam("group", "DEFAULT_GROUP");
void testUrlGetReturnRestResult() throws Exception {
String url = ip + CONFIG_PATH + "/configs";
Query query = Query.newInstance().addParam("beta", true).addParam("dataId", "test-1")
.addParam("group", "DEFAULT_GROUP");
HttpRestResult<ConfigInfo4Beta> restResult = nacosRestTemplate.get(url, Header.newInstance(), query,
new TypeReference<RestResult<ConfigInfo4Beta>>() {
}.getType());
@ -96,10 +99,9 @@ class NacosRestTemplate_ITCase {
System.out.println(restResult.getHeader());
}
@Test
void test_url_post_form() throws Exception {
String url = IP + INSTANCE_PATH + "/instance";
void testUrlPostForm() throws Exception {
String url = ip + INSTANCE_PATH + "/instance";
Map<String, String> param = new HashMap<>();
param.put("serviceName", "app-test");
param.put("port", "8080");
@ -111,8 +113,8 @@ class NacosRestTemplate_ITCase {
@Test
@Disabled("new version can't update instance when service and instance is not exist")
void test_url_put_from() throws Exception {
String url = IP + INSTANCE_PATH + "/instance";
void testUrlPutFrom() throws Exception {
String url = ip + INSTANCE_PATH + "/instance";
Map<String, String> param = new HashMap<>();
param.put("serviceName", "app-test-change");
param.put("port", "8080");
@ -123,30 +125,33 @@ class NacosRestTemplate_ITCase {
}
@Test
void test_url_get() throws Exception {
String url = IP + INSTANCE_PATH + "/instance/list";
void testUrlGet() throws Exception {
String url = ip + INSTANCE_PATH + "/instance/list";
Query query = Query.newInstance().addParam("serviceName", "app-test");
HttpRestResult<Map> restResult = nacosRestTemplate.get(url, Header.newInstance(), query, Map.class);
HttpRestResult<Map<String, String>> restResult = nacosRestTemplate.get(url, Header.newInstance(), query,
Map.class);
assertTrue(restResult.ok());
assertEquals("DEFAULT_GROUP@@app-test", restResult.getData().get("name"));
System.out.println(restResult.getData());
}
@Test
void test_url_get_by_map() throws Exception {
String url = IP + INSTANCE_PATH + "/instance/list";
void testUrlGetByMap() throws Exception {
String url = ip + INSTANCE_PATH + "/instance/list";
Map<String, String> param = new HashMap<>();
param.put("serviceName", "app-test");
HttpRestResult<Map> restResult = nacosRestTemplate.get(url, Header.newInstance(), Query.newInstance().initParams(param), Map.class);
HttpRestResult<Map<String, String>> restResult = nacosRestTemplate.get(url, Header.newInstance(),
Query.newInstance().initParams(param), Map.class);
assertTrue(restResult.ok());
assertEquals("DEFAULT_GROUP@@app-test", restResult.getData().get("name"));
System.out.println(restResult.getData());
}
@Test
void test_url_delete() throws Exception {
String url = IP + INSTANCE_PATH + "/instance";
Query query = Query.newInstance().addParam("ip", "11.11.11.11").addParam("port", "8080").addParam("serviceName", "app-test");
void testUrlDelete() throws Exception {
String url = ip + INSTANCE_PATH + "/instance";
Query query = Query.newInstance().addParam("ip", "11.11.11.11").addParam("port", "8080")
.addParam("serviceName", "app-test");
HttpRestResult<String> restResult = nacosRestTemplate.delete(url, Header.newInstance(), query, String.class);
assertTrue(restResult.ok());
System.out.println(restResult);

View File

@ -39,42 +39,45 @@ import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* NacosRestTemplate_Interceptors_ITCase
* Integration tests for NacosRestTemplateInterceptorsCoreITCase.These tests verify the functionality of HTTP request
* interceptors in NacosRestTemplate.
*
* @author mai.jh
*/
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Nacos.class, properties = {
"server.servlet.context-path=/nacos"}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@TestMethodOrder(MethodName.class)
class NacosRestTemplate_Interceptors_ITCase {
class NacosRestTemplateInterceptorsCoreITCase {
private final String CONFIG_PATH = "/nacos/v1/cs";
private static final String CONFIG_PATH = "/nacos/v1/cs";
private final NacosRestTemplate nacosRestTemplate = HttpClientBeanHolder.getNacosRestTemplate(
LoggerFactory.getLogger(NacosRestTemplateInterceptorsCoreITCase.class));
@SuppressWarnings("deprecation")
@LocalServerPort
private int port;
private NacosRestTemplate nacosRestTemplate = HttpClientBeanHolder.getNacosRestTemplate(
LoggerFactory.getLogger(NacosRestTemplate_Interceptors_ITCase.class));
private String IP = null;
private String ip = null;
@BeforeEach
void init() throws NacosException {
nacosRestTemplate.setInterceptors(Arrays.asList(new TerminationInterceptor()));
IP = String.format("http://localhost:%d", port);
nacosRestTemplate.setInterceptors(Collections.singletonList(new TerminationInterceptor()));
ip = String.format("http://localhost:%d", port);
}
@Test
void test_url_post_config() throws Exception {
String url = IP + CONFIG_PATH + "/configs";
void testUrlPostConfig() throws Exception {
String url = ip + CONFIG_PATH + "/configs";
Map<String, String> param = new HashMap<>();
param.put("dataId", "test-1");
param.put("group", "DEFAULT_GROUP");
@ -86,7 +89,7 @@ class NacosRestTemplate_Interceptors_ITCase {
System.out.println(restResult.getHeader());
}
private class TerminationInterceptor implements HttpClientRequestInterceptor {
private static class TerminationInterceptor implements HttpClientRequestInterceptor {
@Override
public HttpClientResponse intercept() {

View File

@ -43,35 +43,39 @@ import java.util.function.Consumer;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Integration tests for file watching and modification handling using WatchFileCenter.These tests verify the
* concurrency and accuracy of file change notifications under high loads.
*
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
class WatchFileCenter_ITCase {
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
class WatchFileCenterCoreITCase {
static final String path = Paths.get(System.getProperty("user.home"), "/watch_file_change_test").toString();
private static final String PATH = Paths.get(System.getProperty("user.home"), "/watch_file_change_test").toString();
static final Executor executor = Executors.newFixedThreadPool(32);
private static final Executor EXECUTOR = Executors.newFixedThreadPool(32);
final Object monitor = new Object();
@BeforeAll
static void beforeCls() throws Exception {
DiskUtils.deleteDirThenMkdir(path);
DiskUtils.deleteDirThenMkdir(PATH);
}
@AfterAll
static void afterCls() throws Exception {
DiskUtils.deleteDirectory(path);
DiskUtils.deleteDirectory(PATH);
}
// The last file change must be notified
@Test
void test_high_concurrency_modify() throws Exception {
void testHighConcurrencyModify() throws Exception {
AtomicInteger count = new AtomicInteger(0);
Set<String> set = new ConcurrentHashSet<>();
final String fileName = "test2_file_change";
final File file = Paths.get(path, fileName).toFile();
final File file = Paths.get(PATH, fileName).toFile();
func(fileName, file, content -> {
set.add(content);
@ -82,14 +86,14 @@ class WatchFileCenter_ITCase {
}
@Test
void test_modify_file_much() throws Exception {
void testModifyFileMuch() throws Exception {
final String fileName = "modify_file_much";
final File file = Paths.get(path, fileName).toFile();
final File file = Paths.get(PATH, fileName).toFile();
CountDownLatch latch = new CountDownLatch(3);
AtomicInteger count = new AtomicInteger(0);
WatchFileCenter.registerWatcher(path, new FileWatcher() {
WatchFileCenter.registerWatcher(PATH, new FileWatcher() {
@Override
public void onChange(FileChangeEvent event) {
try {
@ -118,16 +122,16 @@ class WatchFileCenter_ITCase {
}
@Test
void test_multi_file_modify() throws Exception {
void testMultiFileModify() throws Exception {
CountDownLatch latch = new CountDownLatch(10);
for (int i = 0; i < 10; i++) {
AtomicInteger count = new AtomicInteger(0);
Set<String> set = new ConcurrentHashSet<>();
final String fileName = "test2_file_change_" + i;
final File file = Paths.get(path, fileName).toFile();
final File file = Paths.get(PATH, fileName).toFile();
executor.execute(() -> {
EXECUTOR.execute(() -> {
try {
func(fileName, file, content -> {
set.add(content);
@ -148,10 +152,10 @@ class WatchFileCenter_ITCase {
private void func(final String fileName, final File file, final Consumer<String> consumer) throws Exception {
CountDownLatch latch = new CountDownLatch(100);
DiskUtils.touch(file);
WatchFileCenter.registerWatcher(path, new FileWatcher() {
WatchFileCenter.registerWatcher(PATH, new FileWatcher() {
@Override
public void onChange(FileChangeEvent event) {
final File file = Paths.get(path, fileName).toFile();
final File file = Paths.get(PATH, fileName).toFile();
final String content = DiskUtils.readFile(file);
consumer.accept(content);
}
@ -165,10 +169,10 @@ class WatchFileCenter_ITCase {
final AtomicInteger id = new AtomicInteger(0);
final AtomicReference<String> finalContent = new AtomicReference<>(null);
for (int i = 0; i < 100; i++) {
executor.execute(() -> {
EXECUTOR.execute(() -> {
final String j = fileName + "_" + id.incrementAndGet();
try {
final File file1 = Paths.get(path, fileName).toFile();
final File file1 = Paths.get(PATH, fileName).toFile();
synchronized (monitor) {
finalContent.set(j);
DiskUtils.writeFile(file1, j.getBytes(StandardCharsets.UTF_8), false);