From f326a0c543c81da725c6ee7eedfae7053ceca949 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E7=BB=A7=E5=B3=B0?= Date: Mon, 12 Jul 2021 09:45:35 +0800 Subject: [PATCH] [ISSUE #5095] Add unit test for ClassUtils (#6327) * fix typo * add unit test for ClassUtils --- .../nacos/common/utils/ClassUtils.java | 4 +- .../nacos/common/utils/ClassUtilsTest.java | 53 +++++++++++++++++++ .../PersistentClientOperationServiceImpl.java | 4 +- 3 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 common/src/test/java/com/alibaba/nacos/common/utils/ClassUtilsTest.java diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/ClassUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/ClassUtils.java index 565f5ac43..008f69cbe 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/ClassUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/ClassUtils.java @@ -106,7 +106,7 @@ public final class ClassUtils { * @param cls Instances of the class represent classes and interfaces. * @return the simple name of the underlying class. */ - public static String getSimplaName(Class cls) { + public static String getSimpleName(Class cls) { Objects.requireNonNull(cls, "cls"); return cls.getSimpleName(); } @@ -117,7 +117,7 @@ public final class ClassUtils { * @param obj Object instance. * @return the simple name of the underlying class. */ - public static String getSimplaName(Object obj) { + public static String getSimpleName(Object obj) { Objects.requireNonNull(obj, "obj"); return obj.getClass().getSimpleName(); } diff --git a/common/src/test/java/com/alibaba/nacos/common/utils/ClassUtilsTest.java b/common/src/test/java/com/alibaba/nacos/common/utils/ClassUtilsTest.java new file mode 100644 index 000000000..4679b57e3 --- /dev/null +++ b/common/src/test/java/com/alibaba/nacos/common/utils/ClassUtilsTest.java @@ -0,0 +1,53 @@ +/* + * 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.api.exception.runtime.NacosRuntimeException; +import org.junit.Assert; +import org.junit.Test; + +public class ClassUtilsTest { + @Test + public void testFindClassByName1() { + Class clazz = ClassUtils.findClassByName("java.lang.Integer"); + Assert.assertEquals("java.lang.Integer", clazz.getName()); + } + + @Test(expected = NacosRuntimeException.class) + public void testFindClassByName2() { + ClassUtils.findClassByName("not.exist.Class"); + } + + @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.getSimpleName(val)); + Assert.assertEquals("Integer", ClassUtils.getSimpleName(Integer.class)); + } + + @Test + public void testIsAssignableFrom() { + Assert.assertTrue(ClassUtils.isAssignableFrom(Object.class, Integer.class)); + } +} \ No newline at end of file diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/service/impl/PersistentClientOperationServiceImpl.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/service/impl/PersistentClientOperationServiceImpl.java index 7c0a16671..1ad76b3ba 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/service/impl/PersistentClientOperationServiceImpl.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/service/impl/PersistentClientOperationServiceImpl.java @@ -240,9 +240,9 @@ public class PersistentClientOperationServiceImpl extends RequestProcessor4CP im private class PersistentInstanceSnapshotOperation extends AbstractSnapshotOperation { - private final String snapshotSaveTag = ClassUtils.getSimplaName(getClass()) + ".SAVE"; + private final String snapshotSaveTag = ClassUtils.getSimpleName(getClass()) + ".SAVE"; - private final String snapshotLoadTag = ClassUtils.getSimplaName(getClass()) + ".LOAD"; + private final String snapshotLoadTag = ClassUtils.getSimpleName(getClass()) + ".LOAD"; private static final String SNAPSHOT_ARCHIVE = "persistent_instance.zip";