Skip to content

Commit

Permalink
[FOR 5094 utils] add unit tests in nacos-core. (alibaba#6037)
Browse files Browse the repository at this point in the history
* [Unit tests] [RemoteUtils] add unit tests.

* [Unit tests] [StringPool] add unit tests.

* [Unit tests] [OverrideParameterRequestWrapper] add unit tests.

* [Unit tests] [WebUtils] add unit tests.

* [Unit tests] [ClassUtils] add unit tests.
  • Loading branch information
brotherlu-xcq authored Jun 15, 2021
1 parent 6c8bbf2 commit e74560d
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

import java.util.List;

/**
*
* {@link ClassUtils} unit tests.
*/
public class ClassUtilsTest {

@Test
Expand All @@ -32,4 +36,23 @@ public void testGeneric() {
}.getType());
}

@Test
public void testFindClassByName() {
Class clazz = ClassUtils.findClassByName("java.lang.Integer");
Assert.assertEquals("java.lang.Integer", clazz.getName());
}

@Test
public void testGetName() {
final String name = "java.lang.Integer";
Integer val = 1;
Assert.assertEquals(name, ClassUtils.getName(val));
Assert.assertEquals(name, ClassUtils.getName(Integer.class));

Assert.assertEquals(name, ClassUtils.getCanonicalName(val));
Assert.assertEquals(name, ClassUtils.getCanonicalName(Integer.class));

Assert.assertEquals("Integer", ClassUtils.getSimplaName(val));
Assert.assertEquals("Integer", ClassUtils.getSimplaName(Integer.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 1999-2021 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.core.utils;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;

/**
* {@link OverrideParameterRequestWrapper} unit tests.
*
* @author chenglu
* @date 2021-06-10 14:11
*/
public class OverrideParameterRequestWrapperTest {

@Test
public void testOverrideParameterRequestWrapper() {
MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
httpServletRequest.addParameter("test1", "value1");
OverrideParameterRequestWrapper wrapper = OverrideParameterRequestWrapper.buildRequest(httpServletRequest);
String value1 = wrapper.getParameter("test1");
Assert.assertEquals("value1", value1);

wrapper.addParameter("test2", "value2");
Assert.assertEquals("value2", wrapper.getParameter("test2"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 1999-2021 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.core.utils;

import org.junit.Assert;
import org.junit.Test;

/**
* {@link RemoteUtils} unit tests.
*
* @author chenglu
* @date 2021-06-10 13:17
*/
public class RemoteUtilsTest {

@Test
public void testGetRemoteExecutorTimesOfProcessors() {
int defaultExpectVal = 1 << 4;
int defaultVal = RemoteUtils.getRemoteExecutorTimesOfProcessors();
Assert.assertEquals(defaultExpectVal, defaultVal);

System.setProperty("remote.executor.times.of.processors", "10");
int val1 = RemoteUtils.getRemoteExecutorTimesOfProcessors();
Assert.assertEquals(10, val1);

System.setProperty("remote.executor.times.of.processors", "-1");
int val2 = RemoteUtils.getRemoteExecutorTimesOfProcessors();
Assert.assertEquals(defaultExpectVal, val2);
}

@Test
public void testGetRemoteExecutorQueueSize() {
int defaultExpectVal = 1 << 14;
int defaultVal = RemoteUtils.getRemoteExecutorQueueSize();
Assert.assertEquals(defaultExpectVal, defaultVal);

System.setProperty("remote.executor.queue.size", "10");
int val1 = RemoteUtils.getRemoteExecutorQueueSize();
Assert.assertEquals(10, val1);

System.setProperty("remote.executor.queue.size", "-1");
int val2 = RemoteUtils.getRemoteExecutorQueueSize();
Assert.assertEquals(defaultExpectVal, val2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.core.utils;

import org.junit.Assert;
import org.junit.Test;

/**
* {@link StringPool} unit tests.
*
* @author chenglu
* @date 2021-06-10 13:52
*/
public class StringPoolTest {

@Test
public void testStringPool() {
String val1 = StringPool.get("test");
Assert.assertEquals("test", val1);

String val2 = StringPool.get(null);
Assert.assertEquals(null, val2);

long size1 = StringPool.size();
Assert.assertEquals(1, size1);

StringPool.remove("test");
long size2 = StringPool.size();
Assert.assertEquals(0, size2);
}
}
81 changes: 81 additions & 0 deletions core/src/test/java/com/alibaba/nacos/core/utils/WebUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 1999-2021 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.core.utils;

import com.alibaba.nacos.common.constant.HttpHeaderConsts;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;

import java.nio.charset.StandardCharsets;

/**
* {@link WebUtils} unit tests.
*
* @author chenglu
* @date 2021-06-10 13:33
*/
public class WebUtilsTest {

@Test
public void testRequired() {
final String key = "key";
MockHttpServletRequest servletRequest = new MockHttpServletRequest();
try {
WebUtils.required(servletRequest, key);
} catch (Exception e) {
Assert.assertTrue(e instanceof IllegalArgumentException);
}

servletRequest.addParameter(key, "value");
String val = WebUtils.required(servletRequest, key);
Assert.assertEquals("value", val);
}

@Test
public void testOptional() {
final String key = "key";
MockHttpServletRequest servletRequest = new MockHttpServletRequest();
String val1 = WebUtils.optional(servletRequest, key, "value");
Assert.assertEquals("value", val1);

servletRequest.addParameter(key, "value1");
Assert.assertEquals("value1", WebUtils.optional(servletRequest, key, "value"));
}

@Test
public void testGetUserAgent() {
MockHttpServletRequest servletRequest = new MockHttpServletRequest();
String userAgent = WebUtils.getUserAgent(servletRequest);
Assert.assertEquals("", userAgent);

servletRequest.addHeader(HttpHeaderConsts.CLIENT_VERSION_HEADER, "0");
Assert.assertEquals("0", WebUtils.getUserAgent(servletRequest));

servletRequest.addHeader(HttpHeaderConsts.USER_AGENT_HEADER, "1");
Assert.assertEquals("1", WebUtils.getUserAgent(servletRequest));
}

@Test
public void testGetAcceptEncoding() {
MockHttpServletRequest servletRequest = new MockHttpServletRequest();
Assert.assertEquals(StandardCharsets.UTF_8.name(), WebUtils.getAcceptEncoding(servletRequest));

servletRequest.addHeader(HttpHeaderConsts.ACCEPT_ENCODING, "gzip, deflate, br");
Assert.assertEquals("gzip", WebUtils.getAcceptEncoding(servletRequest));
}
}

0 comments on commit e74560d

Please sign in to comment.