Skip to content

Commit

Permalink
upgrade module naocs-sys from junit4 to junit5 (#12107)
Browse files Browse the repository at this point in the history
  • Loading branch information
shalk committed May 16, 2024
1 parent 5169f06 commit 67b6722
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,32 @@

import com.alibaba.nacos.common.utils.VersionUtils;
import com.alibaba.nacos.sys.module.ModuleState;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.mock.env.MockEnvironment;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

public class EnvModuleStateBuilderTest {
class EnvModuleStateBuilderTest {

private static ConfigurableEnvironment environment;

@BeforeClass
public static void setUp() throws Exception {
@BeforeAll
static void setUp() throws Exception {
environment = new MockEnvironment();
EnvUtil.setEnvironment(environment);
System.setProperty(Constants.STANDALONE_MODE_PROPERTY_NAME, "true");
EnvUtil.setIsStandalone(null);
}

@Test
public void testBuild() {
void testBuild() {
ModuleState actual = new EnvModuleStateBuilder().build();
assertEquals(Constants.SYS_MODULE, actual.getModuleName());
assertEquals(EnvUtil.STANDALONE_MODE_ALONE, actual.getStates().get(Constants.STARTUP_MODE_STATE));
assertNull(EnvUtil.FUNCTION_MODE_NAMING, actual.getStates().get(Constants.FUNCTION_MODE_STATE));
assertNull(actual.getStates().get(Constants.FUNCTION_MODE_STATE), EnvUtil.FUNCTION_MODE_NAMING);
assertEquals(VersionUtils.version, actual.getStates().get(Constants.NACOS_VERSION));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.alibaba.nacos.sys.env;

import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -26,37 +26,37 @@
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@SpringBootTest(classes = EnvUtilWithConfigTest.class)
public class EnvUtilWithConfigTest {
class EnvUtilWithConfigTest {

private static final int SETTING_PROCESSORS = 10;

@Autowired
private Environment environment;

@Before
public void setUp() {
@BeforeEach
void setUp() {
EnvUtil.setEnvironment((ConfigurableEnvironment) environment);
}

@Test
public void testGetAvailableProcessors() {
void testGetAvailableProcessors() {
int actual = EnvUtil.getAvailableProcessors();
assertEquals(SETTING_PROCESSORS, actual);
}

@Test
public void testGetAvailableProcessorsWithMultiple() {
void testGetAvailableProcessorsWithMultiple() {
int actual = EnvUtil.getAvailableProcessors(2);
assertEquals(SETTING_PROCESSORS * 2, actual);
}

@Test
public void testGetAvailableProcessorsWithScale() {
void testGetAvailableProcessorsWithScale() {
int actual = EnvUtil.getAvailableProcessors(0.5);
assertEquals((int) (SETTING_PROCESSORS * 0.5), actual);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package com.alibaba.nacos.sys.env;

import com.alibaba.nacos.common.utils.ThreadUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -27,37 +27,37 @@
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

@RunWith(SpringRunner.class)
@ActiveProfiles("empty")
@SpringBootTest(classes = EnvUtilWithConfigTest.class)
public class EnvUtilWithoutConfigTest {
class EnvUtilWithoutConfigTest {

@Autowired
private Environment environment;

@Before
public void setUp() {
@BeforeEach
void setUp() {
EnvUtil.setEnvironment((ConfigurableEnvironment) environment);
}

@Test
public void testGetAvailableProcessors() {
void testGetAvailableProcessors() {
int expected = ThreadUtils.getSuitableThreadCount(1);
int actual = EnvUtil.getAvailableProcessors();
assertEquals(expected, actual);
}

@Test
public void testGetAvailableProcessorsWithMultiple() {
void testGetAvailableProcessorsWithMultiple() {
int expected = ThreadUtils.getSuitableThreadCount(2);
int actual = EnvUtil.getAvailableProcessors(2);
assertEquals(expected, actual);
}

@Test
public void testGetAvailableProcessorsWithScale() {
void testGetAvailableProcessorsWithScale() {
int expected = ThreadUtils.getSuitableThreadCount(1);
int actual = EnvUtil.getAvailableProcessors(0.5);
assertEquals((int) (expected * 0.5), actual);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,66 +17,66 @@
package com.alibaba.nacos.sys.module;

import com.alibaba.nacos.sys.env.EnvUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.util.ReflectionTestUtils;

import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class ModuleStateHolderTest {
class ModuleStateHolderTest {

private Map<String, ModuleState> moduleStateMap;

private ConfigurableEnvironment environment;

@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
environment = new MockEnvironment();
EnvUtil.setEnvironment(environment);
moduleStateMap = (Map<String, ModuleState>) ReflectionTestUtils
.getField(ModuleStateHolder.getInstance(), ModuleStateHolder.class, "moduleStates");
moduleStateMap = (Map<String, ModuleState>) ReflectionTestUtils.getField(ModuleStateHolder.getInstance(),
ModuleStateHolder.class, "moduleStates");
}

@After
public void tearDown() throws Exception {
@AfterEach
void tearDown() throws Exception {
}

@Test
public void testGetModuleState() {
void testGetModuleState() {
assertNotNull(ModuleStateHolder.getInstance().getModuleState("mock"));
}

@Test
public void testGetAllModuleStates() {
void testGetAllModuleStates() {
assertEquals(2, ModuleStateHolder.getInstance().getAllModuleStates().size());
}

@Test
public void testGetStateValueByNameFound() {
void testGetStateValueByNameFound() {
assertEquals("test", ModuleStateHolder.getInstance().getStateValueByName("mock", "test"));
assertEquals("test", ModuleStateHolder.getInstance().getStateValueByName("mock", "test", "aaa"));
}

@Test
public void testGetStateValueByNameWithoutModuleState() {
void testGetStateValueByNameWithoutModuleState() {
assertEquals("", ModuleStateHolder.getInstance().getStateValueByName("non-exist", "test"));
assertEquals("aaa", ModuleStateHolder.getInstance().getStateValueByName("non-exist", "test", "aaa"));
}

@Test
public void testGetStateValueByNameWithoutStateName() {
void testGetStateValueByNameWithoutStateName() {
assertEquals("", ModuleStateHolder.getInstance().getStateValueByName("mock", "non-exist"));
assertEquals("aaa", ModuleStateHolder.getInstance().getStateValueByName("mock", "non-exist", "aaa"));
}

@Test
public void testSearchStateValue() {
void testSearchStateValue() {
assertEquals("test", ModuleStateHolder.getInstance().searchStateValue("test", "aaa"));
assertEquals("aaa", ModuleStateHolder.getInstance().searchStateValue("non-exist", "aaa"));
}
Expand Down

0 comments on commit 67b6722

Please sign in to comment.