Skip to content

Commit

Permalink
[ISSUE alibaba#12227] upgrade module test from junit4 to junit5 (alib…
Browse files Browse the repository at this point in the history
…aba#12251)

* upgrade module naocs-test from junit4 to junit5

* fix test/core-test

* fix some test

* fix ci
  • Loading branch information
shalk committed Jun 24, 2024
1 parent 63dc87a commit 3b2f787
Show file tree
Hide file tree
Showing 69 changed files with 3,587 additions and 3,861 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@
import com.alibaba.nacos.core.cluster.ServerMemberManager;
import com.alibaba.nacos.core.utils.GenericType;
import com.alibaba.nacos.core.utils.Loggers;
import com.alibaba.nacos.sys.env.Constants;
import com.alibaba.nacos.sys.env.EnvUtil;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.util.ReflectionTestUtils;

Expand All @@ -48,6 +51,8 @@
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
// todo remove this
@MockitoSettings(strictness = Strictness.LENIENT)
class AddressServerMemberLookupTest {

private final GenericType<String> genericType = new GenericType<String>() {
Expand Down Expand Up @@ -85,6 +90,7 @@ void setUp() throws Exception {
when(environment.getProperty("address.server.domain", "jmenv.tbsite.net")).thenReturn("jmenv.tbsite.net");
when(environment.getProperty("address.server.port", "8080")).thenReturn("8080");
when(environment.getProperty(eq("address.server.url"), any(String.class))).thenReturn("/nacos/serverlist");
when(environment.getProperty(Constants.WEB_CONTEXT_PATH)).thenReturn("/nacos");
initAddressSys();
when(restTemplate.<String>get(eq(addressServerUrl), any(Header.EMPTY.getClass()), any(Query.EMPTY.getClass()),
any(Type.class))).thenReturn(result);
Expand Down Expand Up @@ -150,4 +156,4 @@ private void initAddressSys() {
Loggers.CORE.info("ServerListService address-server port:" + addressPort);
Loggers.CORE.info("ADDRESS_SERVER_URL:" + addressServerUrl);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
import com.alibaba.nacos.common.notify.Event;
import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.notify.listener.Subscriber;
import com.alibaba.nacos.persistence.model.event.RaftDbErrorEvent;
import com.alibaba.nacos.core.persistence.DistributedDatabaseOperateImpl;
import com.alibaba.nacos.consistency.ProtocolMetaData;
import com.alibaba.nacos.consistency.cp.CPProtocol;
import com.alibaba.nacos.consistency.cp.MetadataKey;
import com.alibaba.nacos.core.persistence.DistributedDatabaseOperateImpl;
import com.alibaba.nacos.persistence.model.event.RaftDbErrorEvent;
import com.alibaba.nacos.sys.env.EnvUtil;
import com.alibaba.nacos.sys.utils.DiskUtils;
import com.alibaba.nacos.sys.utils.InetUtils;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
Expand All @@ -57,21 +57,21 @@

public class BaseClusterTest extends HttpClient4Test {

protected static final String CONFIG_INFO_ID = "config-info-id";

protected static final AtomicBoolean[] FINISHED = new AtomicBoolean[] {new AtomicBoolean(false), new AtomicBoolean(false),
new AtomicBoolean(false)};

private static final Logger LOGGER = LoggerFactory.getLogger(BaseClusterTest.class);

protected static final String CONFIG_INFO_ID = "config-info-id";
protected static final NacosRestTemplate NACOS_REST_TEMPLATE = HttpClientBeanHolder.getNacosRestTemplate(LOGGER);

protected static ConfigService iconfig7;

protected static ConfigService iconfig8;

protected static ConfigService iconfig9;

protected static final NacosRestTemplate NACOS_REST_TEMPLATE = HttpClientBeanHolder.getNacosRestTemplate(LOGGER);

protected static final AtomicBoolean[] FINISHED = new AtomicBoolean[] {new AtomicBoolean(false),
new AtomicBoolean(false), new AtomicBoolean(false)};

protected static Map<String, ConfigurableApplicationContext> applications = new HashMap<>();

protected static String clusterInfo;
Expand All @@ -95,8 +95,8 @@ public Class<? extends Event> subscribeType() {
});
}

@BeforeClass
public static void before() throws Exception {
@BeforeAll
static void before() throws Exception {

CountDownLatch latch = new CountDownLatch(3);

Expand Down Expand Up @@ -144,8 +144,8 @@ public static void before() throws Exception {
TimeUnit.SECONDS.sleep(20L);
}

@AfterClass
public static void after() throws Exception {
@AfterAll
static void after() throws Exception {
CountDownLatch latch = new CountDownLatch(applications.size());
for (ConfigurableApplicationContext context : applications.values()) {
new Thread(() -> {
Expand Down Expand Up @@ -179,15 +179,14 @@ private static void run(final int index, final CountDownLatch latch, final Class
Map<String, Object> properties = new HashMap<>();
properties.put("server.port", "884" + (7 + index));
properties.put("nacos.home", path);
properties.put("nacos.logs.path",
Paths.get(System.getProperty("user.home"), "nacos-" + index, "/logs/").toString());
properties.put("nacos.logs.path", Paths.get(System.getProperty("user.home"), "nacos-" + index, "/logs/").toString());
properties.put("spring.jmx.enabled", false);
properties.put("nacos.core.snowflake.worker-id", index + 1);
MapPropertySource propertySource = new MapPropertySource("nacos_cluster_test", properties);
ConfigurableEnvironment environment = new StandardServletEnvironment();
environment.getPropertySources().addFirst(propertySource);
SpringApplication cluster = new SpringApplicationBuilder(cls).web(WebApplicationType.SERVLET)
.environment(environment).properties(clusterInfo).properties("embeddedStorage=true").build();
SpringApplication cluster = new SpringApplicationBuilder(cls).web(WebApplicationType.SERVLET).environment(environment)
.properties(clusterInfo).properties("embeddedStorage=true").build();

ConfigurableApplicationContext context = cluster.run();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.test.base;

import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -33,35 +34,31 @@
* @since 1.2.0
*/
public class HttpClient4Test {

protected URL base;

@Autowired
protected TestRestTemplate restTemplate;

protected <T> ResponseEntity<T> request(String path, MultiValueMap<String, String> params, Class<T> clazz) {

HttpHeaders headers = new HttpHeaders();

HttpEntity<?> entity = new HttpEntity<T>(headers);

UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(this.base.toString() + path)
.queryParams(params);

return this.restTemplate.exchange(
builder.toUriString(), HttpMethod.GET, entity, clazz);

UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(this.base.toString() + path).queryParams(params);

return this.restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, clazz);
}

protected <T> ResponseEntity<T> request(String path, MultiValueMap<String, String> params, Class<T> clazz, HttpMethod httpMethod) {

HttpHeaders headers = new HttpHeaders();

HttpEntity<?> entity = new HttpEntity<T>(headers);

UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(this.base.toString() + path)
.queryParams(params);

return this.restTemplate.exchange(
builder.toUriString(), httpMethod, entity, clazz);

UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(this.base.toString() + path).queryParams(params);

return this.restTemplate.exchange(builder.toUriString(), httpMethod, entity, clazz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.test.base;

import org.springframework.util.LinkedMultiValueMap;
Expand All @@ -22,20 +23,20 @@
* @author nkorange
*/
public class Params {

private MultiValueMap<String, String> paramMap;

public static Params newParams() {
Params params = new Params();
params.paramMap = new LinkedMultiValueMap<String, String>();
return params;
}

public Params appendParam(String name, String value) {
this.paramMap.add(name, value);
return this;
}

public MultiValueMap<String, String> done() {
return paramMap;
}
Expand Down
Loading

0 comments on commit 3b2f787

Please sign in to comment.