add some utils UT (#9006)

add some utils UT
This commit is contained in:
chenhao26 2022-08-22 15:43:22 +08:00 committed by GitHub
parent 3f75b3a2c5
commit 3c8c6a7a14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 354 additions and 0 deletions

View File

@ -0,0 +1,69 @@
/*
* 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.common.utils;
import com.alibaba.nacos.common.utils.to.User;
import org.junit.Assert;
import org.junit.Test;
/**
* ByteUtils Test.
* @ClassName: ByteUtilsTest
* @Author: ChenHao26
* @Date: 2022/8/22 10:58
*/
public class ByteUtilsTest {
@Test
public void objectToByte() {
User user = new User(1, "google");
byte[] bytes = ByteUtils.toBytes(user);
Assert.assertNotNull(bytes);
}
@Test
public void stringToByte() {
byte[] bytes = ByteUtils.toBytes("google");
Assert.assertNotNull(bytes);
}
@Test
public void toStringTest() {
byte[] bytes = ByteUtils.toBytes("google");
String str = ByteUtils.toString(bytes);
Assert.assertEquals(str, "google");
}
@Test
public void isEmpty() {
byte[] bytes = ByteUtils.toBytes("");
Assert.assertTrue(ByteUtils.isEmpty(bytes));
byte[] byte2 = new byte[1024];
Assert.assertFalse(ByteUtils.isEmpty(byte2));
byte[] byte3 = null;
Assert.assertTrue(ByteUtils.isEmpty(byte3));
}
@Test
public void isNotEmpty() {
byte[] bytes = ByteUtils.toBytes("google");
Assert.assertTrue(ByteUtils.isNotEmpty(bytes));
byte[] bytes2 = ByteUtils.toBytes("");
Assert.assertFalse(ByteUtils.isNotEmpty(bytes2));
}
}

View File

@ -0,0 +1,139 @@
/*
* 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.common.utils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.ConcurrentModificationException;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
/**
* ConcurrentHashSet Test.
* @ClassName: ConcurrentHashSetTest
* @Author: ChenHao26
* @Date: 2022/8/22 11:21
*/
public class ConcurrentHashSetTest {
Set<Integer> concurrentHashSet;
@Before
public void setUp() {
concurrentHashSet = new ConcurrentHashSet<>();
concurrentHashSet.add(1);
concurrentHashSet.add(2);
concurrentHashSet.add(3);
concurrentHashSet.add(4);
concurrentHashSet.add(5);
}
@Test
public void size() {
Assert.assertEquals(concurrentHashSet.size(), 5);
}
@Test
public void contains() {
Assert.assertTrue(concurrentHashSet.contains(1));
}
@Test
public void testMultithreaded() {
try {
concurrentHashSet = new HashSet<>();
executeThread();
} catch (Exception e) {
Assert.assertTrue(e instanceof ConcurrentModificationException);
}
try {
concurrentHashSet = new ConcurrentHashSet<>();
executeThread();
} catch (Exception e) {
Assert.assertNull(e);
}
}
/**
* execute muti thread.
*/
public void executeThread() throws Exception {
for (int i = 0; i < 1000; i++) {
concurrentHashSet.add(i);
}
new Thread(new AddDataThread(concurrentHashSet)).start();
new Thread(new DeleteDataThread(concurrentHashSet)).start();
new Thread(new IteratorThread(concurrentHashSet)).start();
}
//add data thread
static class AddDataThread implements Runnable {
Set<Integer> hashSet;
public AddDataThread(Set<Integer> hashSet) {
this.hashSet = hashSet;
}
@Override
public void run() {
while (true) {
int random = new Random().nextInt();
hashSet.add(random);
}
}
}
// delete data thread
static class DeleteDataThread implements Runnable {
Set<Integer> hashSet;
public DeleteDataThread(Set<Integer> hashSet) {
this.hashSet = hashSet;
}
@Override
public void run() {
int random = new Random().nextInt(1000);
while (true) {
hashSet.remove(random);
}
}
}
static class IteratorThread implements Runnable {
Set<Integer> hashSet;
public IteratorThread(Set<Integer> hashSet) {
this.hashSet = hashSet;
}
@Override
public void run() {
System.out.println("start -- hashSet.size() : " + hashSet.size());
for (Integer str : hashSet) {
System.out.println("value : " + str);
}
System.out.println("end -- hashSet.size() : " + hashSet.size());
}
}
}

View File

@ -0,0 +1,54 @@
/*
* 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.common.utils;
import org.junit.Assert;
import org.junit.Test;
/**
* InetAddressValidator Test.
* @ClassName: InetAddressValidatorTest
* @Author: ChenHao26
* @Date: 2022/8/22 13:07
*/
public class InetAddressValidatorTest {
@Test
public void isIPv6Address() {
Assert.assertTrue(InetAddressValidator.isIPv6Address("2000:0000:0000:0000:0001:2345:6789:abcd"));
Assert.assertTrue(InetAddressValidator.isIPv6Address("2001:DB8:0:0:8:800:200C:417A"));
Assert.assertTrue(InetAddressValidator.isIPv6Address("2001:DB8::8:800:200C:417A"));
Assert.assertFalse(InetAddressValidator.isIPv6Address("2001:DB8::8:800:200C141aA"));
}
@Test
public void isIPv6MixedAddress() {
Assert.assertTrue(InetAddressValidator.isIPv6MixedAddress("1:0:0:0:0:0:172.12.55.18"));
Assert.assertFalse(InetAddressValidator.isIPv6MixedAddress("2001:DB8::8:800:200C141aA"));
}
@Test
public void isIPv6IPv4MappedAddress() {
Assert.assertFalse(InetAddressValidator.isIPv6IPv4MappedAddress(":ffff:1.1.1.1"));
Assert.assertTrue(InetAddressValidator.isIPv6IPv4MappedAddress("::FFFF:192.168.1.2"));
}
@Test
public void isIPv4Address() {
Assert.assertTrue(InetAddressValidator.isIPv4Address("192.168.1.2"));
}
}

View File

@ -0,0 +1,48 @@
/*
* 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.common.utils;
import org.junit.Assert;
import org.junit.Test;
/**
* PropertyUtils Test.
* @ClassName: PropertyUtilsTest
* @Author: ChenHao26
* @Date: 2022/8/22 13:28
*/
public class PropertyUtilsTest {
@Test
public void getProperty() {
System.setProperty("nacos.test", "google");
String property = PropertyUtils.getProperty("nacos.test", "xx");
Assert.assertEquals(property, "google");
}
@Test
public void getPropertyWithDefaultValue() {
String property = PropertyUtils.getProperty("nacos.test", "xx", "test001");
Assert.assertEquals(property, "test001");
}
@Test
public void getProcessorsCount() {
int processorsCount = PropertyUtils.getProcessorsCount();
Assert.assertNotNull(processorsCount);
}
}

View File

@ -0,0 +1,44 @@
/*
* 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.common.utils.to;
public class User {
private Integer id;
private String name;
public User(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}