diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ServiceInfo.java b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ServiceInfo.java
index 1b5281d42c3..4d8b13da19d 100644
--- a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ServiceInfo.java
+++ b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ServiceInfo.java
@@ -194,7 +194,7 @@ public boolean validate() {
continue;
}
- for (int i = 0; i < host.getWeight(); i++) {
+ if (host.getWeight() > 0) {
validHosts.add(host);
}
}
diff --git a/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/AntPathMatcher.java b/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/AntPathMatcher.java
index 6feb0ab09f2..93dbd1965e5 100644
--- a/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/AntPathMatcher.java
+++ b/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/AntPathMatcher.java
@@ -17,7 +17,7 @@
package com.alibaba.nacos.common.packagescan.resource;
import com.alibaba.nacos.common.packagescan.util.PathMatcher;
-import com.alibaba.nacos.common.utils.AbstractAssert;
+import com.alibaba.nacos.common.packagescan.util.AbstractAssert;
import com.alibaba.nacos.common.utils.StringUtils;
import java.util.ArrayList;
diff --git a/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/ByteArrayResource.java b/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/ByteArrayResource.java
index a602f1b6842..07d1e27f3d7 100644
--- a/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/ByteArrayResource.java
+++ b/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/ByteArrayResource.java
@@ -16,7 +16,7 @@
package com.alibaba.nacos.common.packagescan.resource;
-import com.alibaba.nacos.common.utils.AbstractAssert;
+import com.alibaba.nacos.common.packagescan.util.AbstractAssert;
import java.io.ByteArrayInputStream;
import java.io.IOException;
diff --git a/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/ClassPathResource.java b/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/ClassPathResource.java
index 098dd4f08fd..451450ca31f 100644
--- a/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/ClassPathResource.java
+++ b/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/ClassPathResource.java
@@ -16,8 +16,8 @@
package com.alibaba.nacos.common.packagescan.resource;
-import com.alibaba.nacos.common.utils.AbstractObjectUtils;
-import com.alibaba.nacos.common.utils.AbstractAssert;
+import com.alibaba.nacos.common.packagescan.util.AbstractObjectUtils;
+import com.alibaba.nacos.common.packagescan.util.AbstractAssert;
import com.alibaba.nacos.common.utils.ClassUtils;
import com.alibaba.nacos.common.utils.StringUtils;
diff --git a/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/DefaultResourceLoader.java b/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/DefaultResourceLoader.java
index f8e956ecb6c..5c88be58b3b 100644
--- a/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/DefaultResourceLoader.java
+++ b/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/DefaultResourceLoader.java
@@ -17,7 +17,7 @@
package com.alibaba.nacos.common.packagescan.resource;
import com.alibaba.nacos.common.packagescan.util.ResourceUtils;
-import com.alibaba.nacos.common.utils.AbstractAssert;
+import com.alibaba.nacos.common.packagescan.util.AbstractAssert;
import com.alibaba.nacos.common.utils.ClassUtils;
import com.alibaba.nacos.common.utils.StringUtils;
diff --git a/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/FileSystemResource.java b/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/FileSystemResource.java
index e61b977e449..a94a4ed3065 100644
--- a/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/FileSystemResource.java
+++ b/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/FileSystemResource.java
@@ -16,7 +16,7 @@
package com.alibaba.nacos.common.packagescan.resource;
-import com.alibaba.nacos.common.utils.AbstractAssert;
+import com.alibaba.nacos.common.packagescan.util.AbstractAssert;
import com.alibaba.nacos.common.utils.StringUtils;
import java.io.File;
diff --git a/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/InputStreamResource.java b/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/InputStreamResource.java
index ece35370644..d4ec787de00 100644
--- a/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/InputStreamResource.java
+++ b/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/InputStreamResource.java
@@ -16,7 +16,7 @@
package com.alibaba.nacos.common.packagescan.resource;
-import com.alibaba.nacos.common.utils.AbstractAssert;
+import com.alibaba.nacos.common.packagescan.util.AbstractAssert;
import java.io.IOException;
import java.io.InputStream;
diff --git a/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/PathMatchingResourcePatternResolver.java b/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/PathMatchingResourcePatternResolver.java
index 31dacb5de62..a2913ed6cf9 100644
--- a/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/PathMatchingResourcePatternResolver.java
+++ b/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/PathMatchingResourcePatternResolver.java
@@ -18,7 +18,7 @@
import com.alibaba.nacos.common.packagescan.util.PathMatcher;
import com.alibaba.nacos.common.packagescan.util.ResourceUtils;
-import com.alibaba.nacos.common.utils.AbstractAssert;
+import com.alibaba.nacos.common.packagescan.util.AbstractAssert;
import com.alibaba.nacos.common.utils.ClassUtils;
import com.alibaba.nacos.common.utils.ReflectUtils;
import com.alibaba.nacos.common.utils.StringUtils;
diff --git a/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/PathResource.java b/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/PathResource.java
index cd42fad2ae4..29c1d386091 100644
--- a/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/PathResource.java
+++ b/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/PathResource.java
@@ -16,7 +16,7 @@
package com.alibaba.nacos.common.packagescan.resource;
-import com.alibaba.nacos.common.utils.AbstractAssert;
+import com.alibaba.nacos.common.packagescan.util.AbstractAssert;
import java.io.File;
import java.io.FileNotFoundException;
diff --git a/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/UrlResource.java b/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/UrlResource.java
index a0a7572acff..2e634ebdda5 100644
--- a/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/UrlResource.java
+++ b/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/UrlResource.java
@@ -17,7 +17,7 @@
package com.alibaba.nacos.common.packagescan.resource;
import com.alibaba.nacos.common.packagescan.util.ResourceUtils;
-import com.alibaba.nacos.common.utils.AbstractAssert;
+import com.alibaba.nacos.common.packagescan.util.AbstractAssert;
import com.alibaba.nacos.common.utils.StringUtils;
import java.io.File;
diff --git a/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/VfsResource.java b/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/VfsResource.java
index 3996e0dc45d..9b0491c16f9 100644
--- a/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/VfsResource.java
+++ b/common/src/main/java/com/alibaba/nacos/common/packagescan/resource/VfsResource.java
@@ -16,7 +16,7 @@
package com.alibaba.nacos.common.packagescan.resource;
-import com.alibaba.nacos.common.utils.AbstractAssert;
+import com.alibaba.nacos.common.packagescan.util.AbstractAssert;
import com.alibaba.nacos.common.packagescan.util.NestedIoException;
import java.io.File;
diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/AbstractAssert.java b/common/src/main/java/com/alibaba/nacos/common/packagescan/util/AbstractAssert.java
similarity index 99%
rename from common/src/main/java/com/alibaba/nacos/common/utils/AbstractAssert.java
rename to common/src/main/java/com/alibaba/nacos/common/packagescan/util/AbstractAssert.java
index eadfe6fc952..e33db0b3bf8 100644
--- a/common/src/main/java/com/alibaba/nacos/common/utils/AbstractAssert.java
+++ b/common/src/main/java/com/alibaba/nacos/common/packagescan/util/AbstractAssert.java
@@ -1,11 +1,11 @@
/*
- * Copyright 2002-2020 the original author or authors.
+ * Copyright 1999-2023 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
*
- * https://www.apache.org/licenses/LICENSE-2.0
+ * 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,
@@ -14,7 +14,10 @@
* limitations under the License.
*/
-package com.alibaba.nacos.common.utils;
+package com.alibaba.nacos.common.packagescan.util;
+
+import com.alibaba.nacos.common.utils.CollectionUtils;
+import com.alibaba.nacos.common.utils.StringUtils;
import java.util.Collection;
import java.util.Map;
diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/AbstractObjectUtils.java b/common/src/main/java/com/alibaba/nacos/common/packagescan/util/AbstractObjectUtils.java
similarity index 99%
rename from common/src/main/java/com/alibaba/nacos/common/utils/AbstractObjectUtils.java
rename to common/src/main/java/com/alibaba/nacos/common/packagescan/util/AbstractObjectUtils.java
index 284fbcee305..6bdcf738148 100644
--- a/common/src/main/java/com/alibaba/nacos/common/utils/AbstractObjectUtils.java
+++ b/common/src/main/java/com/alibaba/nacos/common/packagescan/util/AbstractObjectUtils.java
@@ -1,11 +1,11 @@
/*
- * Copyright 2002-2021 the original author or authors.
+ * Copyright 1999-2023 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
*
- * https://www.apache.org/licenses/LICENSE-2.0
+ * 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,
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package com.alibaba.nacos.common.utils;
+package com.alibaba.nacos.common.packagescan.util;
import java.lang.reflect.Array;
import java.util.Arrays;
diff --git a/common/src/main/java/com/alibaba/nacos/common/packagescan/util/ResourceUtils.java b/common/src/main/java/com/alibaba/nacos/common/packagescan/util/ResourceUtils.java
index 05d74272fff..e11b1eeda7a 100644
--- a/common/src/main/java/com/alibaba/nacos/common/packagescan/util/ResourceUtils.java
+++ b/common/src/main/java/com/alibaba/nacos/common/packagescan/util/ResourceUtils.java
@@ -17,7 +17,6 @@
package com.alibaba.nacos.common.packagescan.util;
import com.alibaba.nacos.common.packagescan.resource.Resource;
-import com.alibaba.nacos.common.utils.AbstractAssert;
import com.alibaba.nacos.common.utils.ClassUtils;
import com.alibaba.nacos.common.utils.StringUtils;
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 52d9c459c5a..cba39b303cb 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
@@ -43,7 +43,7 @@
* @author liaochuntao
*/
public final class ClassUtils {
-
+
private ClassUtils() {
}
@@ -241,7 +241,7 @@ public static String getSimpleName(Object obj) {
*/
public static Class> forName(String name, ClassLoader classLoader) throws ClassNotFoundException, LinkageError {
- AbstractAssert.notNull(name, "Name must not be null");
+ Objects.requireNonNull(name, "Name must not be null");
Class> clazz = resolvePrimitiveClassName(name);
if (clazz == null) {
@@ -386,7 +386,7 @@ public static String classPackageAsResourcePath(Class> clazz) {
* @return the corresponding resource path, pointing to the class
*/
public static String convertClassNameToResourcePath(String className) {
- AbstractAssert.notNull(className, "Class name must not be null");
+ Objects.requireNonNull(className, "Class name must not be null");
return className.replace(PACKAGE_SEPARATOR, PATH_SEPARATOR);
}
@@ -397,7 +397,7 @@ public static String convertClassNameToResourcePath(String className) {
* @return the corresponding resource path, pointing to the class
*/
public static String resourcePathToConvertClassName(String className) {
- AbstractAssert.notNull(className, "Class name must not be null");
+ Objects.requireNonNull(className, "Class name must not be null");
return className.replace(PATH_SEPARATOR, PACKAGE_SEPARATOR);
}
}
diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/InetAddressValidator.java b/common/src/main/java/com/alibaba/nacos/common/utils/InetAddressValidator.java
index 2499e20f33d..993224403aa 100644
--- a/common/src/main/java/com/alibaba/nacos/common/utils/InetAddressValidator.java
+++ b/common/src/main/java/com/alibaba/nacos/common/utils/InetAddressValidator.java
@@ -1,14 +1,10 @@
/**
- * Copyright 2018-2021 Dynatrace LLC
- * 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.
+ * Copyright 2018-2021 Dynatrace LLC 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;
@@ -22,7 +18,7 @@
*/
@SuppressWarnings({"checkstyle:AbbreviationAsWordInName", "PMD.ClassNamingShouldBeCamelRule"})
public class InetAddressValidator {
-
+
private InetAddressValidator() {
}
@@ -59,9 +55,8 @@ private InetAddressValidator() {
.compile("^" + "(?:[0-9a-fA-F]{1,4}:){6}" + "$");
/**
- * Check if input
is a valid IPv4 address.
- * The format is 'xxx.xxx.xxx.xxx'. Four blocks of integer numbers ranging from 0 to 255
- * are required. Letters are not allowed.
+ * Check if input
is a valid IPv4 address. The format is 'xxx.xxx.xxx.xxx'. Four blocks of integer
+ * numbers ranging from 0 to 255 are required. Letters are not allowed.
*
* @param input ip-address to check
* @return true if input
is in correct IPv4 notation.
@@ -71,9 +66,8 @@ public static boolean isIPv4Address(final String input) {
}
/**
- * Check if the given address is a valid IPv6 address in the standard format
- * The format is 'xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx'. Eight blocks of hexadecimal digits
- * are required.
+ * Check if the given address is a valid IPv6 address in the standard format The format is
+ * 'xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx'. Eight blocks of hexadecimal digits are required.
*
* @param input ip-address to check
* @return true if input
is in correct IPv6 notation.
@@ -83,9 +77,8 @@ public static boolean isIPv6StdAddress(final String input) {
}
/**
- * Check if the given address is a valid IPv6 address in the hex-compressed notation
- * The format is 'xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx'. If all digits in a block are '0'
- * the block can be left empty.
+ * Check if the given address is a valid IPv6 address in the hex-compressed notation The format is
+ * 'xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx'. If all digits in a block are '0' the block can be left empty.
*
* @param input ip-address to check
* @return true if input
is in correct IPv6 (hex-compressed) notation.
@@ -95,13 +88,8 @@ public static boolean isIPv6HexCompressedAddress(final String input) {
}
/**
- * Check if input
is a IPv6 address.
- * Possible notations for valid IPv6 are:
- * - Standard IPv6 address
- * - Hex-compressed IPv6 address
- * - Link-local IPv6 address
- * - IPv4-mapped-to-IPV6 address
- * - IPv6 mixed address
+ * Check if input
is a IPv6 address. Possible notations for valid IPv6 are: - Standard IPv6 address -
+ * Hex-compressed IPv6 address - Link-local IPv6 address - IPv4-mapped-to-IPV6 address - IPv6 mixed address
*
* @param input ip-address to check
* @return true if input
is in correct IPv6 notation.
@@ -112,11 +100,9 @@ public static boolean isIPv6Address(final String input) {
}
/**
- * Check if the given address is a valid IPv6 address in the mixed-standard or mixed-compressed notation.
- * IPV6 Mixed mode consists of two parts, the first 96 bits (up to 6 blocks of 4 hex digits) are IPv6
- * the IPV6 part can be either compressed or uncompressed
- * the second block is a full IPv4 address
- * e.g. '0:0:0:0:0:0:172.12.55.18'
+ * Check if the given address is a valid IPv6 address in the mixed-standard or mixed-compressed notation. IPV6 Mixed
+ * mode consists of two parts, the first 96 bits (up to 6 blocks of 4 hex digits) are IPv6 the IPV6 part can be
+ * either compressed or uncompressed the second block is a full IPv4 address e.g. '0:0:0:0:0:0:172.12.55.18'
*
* @param input ip-address to check
* @return true if input
is in correct IPv6 (mixed-standard or mixed-compressed) notation.
@@ -143,9 +129,8 @@ public static boolean isIPv6MixedAddress(final String input) {
}
/**
- * Check if input
is an IPv4 address mapped into a IPv6 address. These are
- * starting with "::ffff:" followed by the IPv4 address in a dot-seperated notation.
- * The format is '::ffff:d.d.d.d'
+ * Check if input
is an IPv4 address mapped into a IPv6 address. These are starting with "::ffff:"
+ * followed by the IPv4 address in a dot-seperated notation. The format is '::ffff:d.d.d.d'
*
* @param input ip-address to check
* @return true if input
is in correct IPv6 notation containing an IPv4 address
@@ -159,8 +144,8 @@ public static boolean isIPv6IPv4MappedAddress(final String input) {
}
/**
- * Check if input
is a link local IPv6 address starting with "fe80:" and containing
- * a zone index with "%xxx". The zone index will not be checked.
+ * Check if input
is a link local IPv6 address starting with "fe80:" and containing a zone index with
+ * "%xxx". The zone index will not be checked.
*
* @param input ip-address to check
* @return true if address part of input
is in correct IPv6 notation.
@@ -176,27 +161,4 @@ public static boolean isLinkLocalIPv6WithZoneIndex(String input) {
return false;
}
- /**
- * Check if input
is a valid IPv4 or IPv6 address.
- *
- * @param ipAddress ip-address to check
- * @return true
if ipAddress
is a valid ip-address
- */
- public static boolean isValidIP(String ipAddress) {
- if (ipAddress == null || ipAddress.length() == 0) {
- return false;
- }
-
- return isIPv4Address(ipAddress) || isIPv6Address(ipAddress);
- }
-
- /**
- * get to ipv4 pattern.
- *
- * @return
- */
- public static Pattern getIpv4Pattern() {
- return IPV4_PATTERN;
- }
-
}
diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java
index b281007a6ce..87b155e9364 100644
--- a/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java
+++ b/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java
@@ -24,7 +24,6 @@
import java.io.CharArrayWriter;
import java.io.Closeable;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -33,7 +32,6 @@
import java.io.Reader;
import java.io.Writer;
import java.net.HttpURLConnection;
-import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -46,7 +44,7 @@
* @author nacos
*/
public class IoUtils {
-
+
private IoUtils() {
}
@@ -75,11 +73,7 @@ public static byte[] tryDecompress(byte[] raw) throws Exception {
if (!isGzipStream(raw)) {
return raw;
}
- try (GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(raw));
- ByteArrayOutputStream out = new ByteArrayOutputStream()) {
- copy(gis, out);
- return out.toByteArray();
- }
+ return tryDecompress(new ByteArrayInputStream(raw));
}
/**
@@ -276,31 +270,6 @@ public static void cleanDirectory(File directory) throws IOException {
}
}
- /**
- * Copy File.
- *
- * @param source source file path
- * @param target target file path
- * @throws IOException io exception
- */
- public static void copyFile(String source, String target) throws IOException {
- File sf = new File(source);
- if (!sf.exists()) {
- throw new IllegalArgumentException("source file does not exist.");
- }
- File tf = new File(target);
- if (!tf.getParentFile().mkdirs()) {
- throw new RuntimeException("failed to create parent directory.");
- }
- if (!tf.exists() && !tf.createNewFile()) {
- throw new RuntimeException("failed to create target file.");
- }
- try (FileChannel sc = new FileInputStream(sf).getChannel();
- FileChannel tc = new FileOutputStream(tf).getChannel()) {
- sc.transferTo(0, sc.size(), tc);
- }
- }
-
/**
* Judge whether is Gzip stream.
*
diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/StringUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/StringUtils.java
index a2b1f0e29ab..57e351ed26c 100644
--- a/common/src/main/java/com/alibaba/nacos/common/utils/StringUtils.java
+++ b/common/src/main/java/com/alibaba/nacos/common/utils/StringUtils.java
@@ -16,9 +16,6 @@
package com.alibaba.nacos.common.utils;
-import java.io.IOException;
-import java.io.StringWriter;
-import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayDeque;
import java.util.ArrayList;
@@ -26,7 +23,6 @@
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
-import java.util.Locale;
import java.util.StringTokenizer;
/**
@@ -36,7 +32,7 @@
* @author zzq
*/
public class StringUtils {
-
+
private StringUtils() {
}
@@ -226,108 +222,6 @@ public static String join(Collection collection, String separator) {
return stringBuilder.toString();
}
- public static String escapeJavaScript(String str) {
- return escapeJavaStyleString(str, true, true);
- }
-
- private static String escapeJavaStyleString(String str, boolean escapeSingleQuotes, boolean escapeForwardSlash) {
- if (str == null) {
- return null;
- }
- try {
- StringWriter writer = new StringWriter(str.length() * 2);
- escapeJavaStyleString(writer, str, escapeSingleQuotes, escapeForwardSlash);
- return writer.toString();
- } catch (IOException ioe) {
- // this should never ever happen while writing to a StringWriter
- return null;
- }
- }
-
- private static void escapeJavaStyleString(Writer out, String str, boolean escapeSingleQuote,
- boolean escapeForwardSlash) throws IOException {
- if (out == null) {
- throw new IllegalArgumentException("The Writer must not be null");
- }
- if (str == null) {
- return;
- }
- int sz;
- sz = str.length();
- for (int i = 0; i < sz; i++) {
- char ch = str.charAt(i);
-
- // handle unicode
- if (ch > 0xfff) {
- out.write("\\u" + hex(ch));
- } else if (ch > 0xff) {
- out.write("\\u0" + hex(ch));
- } else if (ch > 0x7f) {
- out.write("\\u00" + hex(ch));
- } else if (ch < 32) {
- switch (ch) {
- case '\b':
- out.write('\\');
- out.write('b');
- break;
- case '\n':
- out.write('\\');
- out.write('n');
- break;
- case '\t':
- out.write('\\');
- out.write('t');
- break;
- case '\f':
- out.write('\\');
- out.write('f');
- break;
- case '\r':
- out.write('\\');
- out.write('r');
- break;
- default:
- if (ch > 0xf) {
- out.write("\\u00" + hex(ch));
- } else {
- out.write("\\u000" + hex(ch));
- }
- break;
- }
- } else {
- switch (ch) {
- case '\'':
- if (escapeSingleQuote) {
- out.write('\\');
- }
- out.write('\'');
- break;
- case '"':
- out.write('\\');
- out.write('"');
- break;
- case '\\':
- out.write('\\');
- out.write('\\');
- break;
- case '/':
- if (escapeForwardSlash) {
- out.write('\\');
- }
- out.write('/');
- break;
- default:
- out.write(ch);
- break;
- }
- }
- }
- }
-
- private static String hex(char ch) {
- return Integer.toHexString(ch).toUpperCase(Locale.ENGLISH);
- }
-
/**
* Checks if CharSequence contains a search CharSequence irrespective of case, handling {@code null}.
* Case-insensitivity is defined as by {@link String#equalsIgnoreCase(String)}.
@@ -503,10 +397,6 @@ public static String[] split(final String str, String separatorChars) {
return str.split(separatorChars);
}
- private static String[] tokenizeLocaleSource(String localeSource) {
- return tokenizeToStringArray(localeSource, "_ ", false, false);
- }
-
/**
* Tokenize the given {@code String} into a {@code String} array via a {@link StringTokenizer}.
*
@@ -899,21 +789,17 @@ public static String getFilename(String path) {
* @return the capitalized {@code String}
*/
public static String capitalize(String str) {
- return changeFirstCharacterCase(str, true);
+ return changeFirstCharacterCase(str);
}
- private static String changeFirstCharacterCase(String str, boolean capitalize) {
+ private static String changeFirstCharacterCase(String str) {
if (!hasLength(str)) {
return str;
}
char baseChar = str.charAt(0);
char updatedChar;
- if (capitalize) {
- updatedChar = Character.toUpperCase(baseChar);
- } else {
- updatedChar = Character.toLowerCase(baseChar);
- }
+ updatedChar = Character.toUpperCase(baseChar);
if (baseChar == updatedChar) {
return str;
}
diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/ThreadUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/ThreadUtils.java
index da34ece684d..e96f912f097 100644
--- a/common/src/main/java/com/alibaba/nacos/common/utils/ThreadUtils.java
+++ b/common/src/main/java/com/alibaba/nacos/common/utils/ThreadUtils.java
@@ -29,25 +29,12 @@
* @author liaochuntao
*/
public final class ThreadUtils {
-
+
private ThreadUtils() {
}
private static final int THREAD_MULTIPLER = 2;
- /**
- * Wait.
- *
- * @param object load object
- */
- public static void objectWait(Object object) {
- try {
- object.wait();
- } catch (InterruptedException ignore) {
- Thread.interrupted();
- }
- }
-
/**
* Sleep.
*
diff --git a/common/src/test/java/ClassUtilsTestMockClass.java b/common/src/test/java/ClassUtilsTestMockClass.java
new file mode 100644
index 00000000000..5183078632f
--- /dev/null
+++ b/common/src/test/java/ClassUtilsTestMockClass.java
@@ -0,0 +1,18 @@
+/*
+ * Copyright 1999-2023 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.
+ */
+
+public class ClassUtilsTestMockClass {
+}
diff --git a/common/src/test/java/com/alibaba/nacos/common/utils/AbstractAssertTest.java b/common/src/test/java/com/alibaba/nacos/common/utils/AbstractAssertTest.java
deleted file mode 100644
index 9443428d46e..00000000000
--- a/common/src/test/java/com/alibaba/nacos/common/utils/AbstractAssertTest.java
+++ /dev/null
@@ -1,122 +0,0 @@
-/*
- * Copyright 1999-2022 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.Test;
-
-import java.util.Collections;
-import java.util.LinkedList;
-import java.util.List;
-
-import static org.junit.Assert.assertThrows;
-
-public class AbstractAssertTest {
-
- @Test
- public void testState() {
- assertThrows("error message", IllegalStateException.class, () -> AbstractAssert.state(false, "error message"));
- assertThrows("error", IllegalStateException.class, () -> AbstractAssert.state(false, () -> "error"));
- }
-
- @Test
- public void testIsTrue() {
- assertThrows("error", IllegalArgumentException.class, () -> AbstractAssert.isTrue(false, "error"));
- assertThrows("error", IllegalArgumentException.class, () -> AbstractAssert.isTrue(false, () -> "error"));
- }
-
- @Test
- public void testIsNull() {
- assertThrows("error", IllegalArgumentException.class, () -> AbstractAssert.isNull(new Object(), "error"));
- assertThrows("error", IllegalArgumentException.class, () -> AbstractAssert.isNull(new Object(), () -> "error"));
- }
-
- @Test
- public void testNotNull() {
- assertThrows("error", IllegalArgumentException.class, () -> AbstractAssert.notNull(null, "error"));
- assertThrows("error", IllegalArgumentException.class, () -> AbstractAssert.notNull(null, () -> "error"));
- }
-
- @Test
- public void testHasLength() {
- assertThrows("error", IllegalArgumentException.class, () -> AbstractAssert.hasLength(null, "error"));
- assertThrows("error", IllegalArgumentException.class, () -> AbstractAssert.hasLength(null, () -> "error"));
- }
-
- @Test
- public void testHasText() {
- assertThrows("error", IllegalArgumentException.class, () -> AbstractAssert.hasText(null, "error"));
- assertThrows("error", IllegalArgumentException.class, () -> AbstractAssert.hasText(null, () -> "error"));
- }
-
- @Test
- public void testDoesNotContain() {
- assertThrows(IllegalArgumentException.class, () -> AbstractAssert.doesNotContain("abcd", "bc"));
- assertThrows("error", IllegalArgumentException.class,
- () -> AbstractAssert.doesNotContain("abcd", "bc", () -> "error"));
- }
-
- @Test
- public void testNotEmpty() {
- assertThrows("error", IllegalArgumentException.class,
- () -> AbstractAssert.notEmpty(Collections.EMPTY_MAP, "error"));
- assertThrows("error", IllegalArgumentException.class,
- () -> AbstractAssert.notEmpty(Collections.EMPTY_MAP, () -> "error"));
-
- assertThrows("error", IllegalArgumentException.class, () -> AbstractAssert.notEmpty(new Object[] {}, "error"));
- assertThrows("error", IllegalArgumentException.class,
- () -> AbstractAssert.notEmpty(new Object[] {}, () -> "error"));
-
- assertThrows("error", IllegalArgumentException.class,
- () -> AbstractAssert.notEmpty(Collections.emptyList(), "error"));
- assertThrows("error", IllegalArgumentException.class,
- () -> AbstractAssert.notEmpty(Collections.emptyList(), () -> "error"));
- }
-
- @Test
- public void testNoNullElements() {
- List list = new LinkedList<>();
- list.add(null);
- assertThrows("error", IllegalArgumentException.class, () -> AbstractAssert.noNullElements(list, "error"));
- assertThrows("error", IllegalArgumentException.class, () -> AbstractAssert.noNullElements(list, () -> "error"));
-
- Object[] objects = new Object[1];
- assertThrows("error", IllegalArgumentException.class, () -> AbstractAssert.noNullElements(objects, "error"));
- assertThrows("error", IllegalArgumentException.class,
- () -> AbstractAssert.noNullElements(objects, () -> "error"));
- }
-
- @Test
- public void testIsInstanceOf() {
- assertThrows("", IllegalArgumentException.class, () -> AbstractAssert.isInstanceOf(String.class, new Object()));
- assertThrows("error", IllegalArgumentException.class,
- () -> AbstractAssert.isInstanceOf(String.class, new Object(), "error"));
- assertThrows("error", IllegalArgumentException.class,
- () -> AbstractAssert.isInstanceOf(String.class, new Object(), () -> "error"));
- }
-
- @Test
- public void testIsAssignable() {
- assertThrows("", IllegalArgumentException.class, () -> AbstractAssert.isAssignable(String.class, null));
- assertThrows("error", IllegalArgumentException.class,
- () -> AbstractAssert.isAssignable(String.class, null, "error"));
- assertThrows("error", IllegalArgumentException.class,
- () -> AbstractAssert.isAssignable(String.class, Integer.class, "error"));
- assertThrows("error", IllegalArgumentException.class,
- () -> AbstractAssert.isAssignable(String.class, Integer.class, () -> "error"));
- }
-
-}
diff --git a/common/src/test/java/com/alibaba/nacos/common/utils/AbstractObjectUtilsTest.java b/common/src/test/java/com/alibaba/nacos/common/utils/AbstractObjectUtilsTest.java
deleted file mode 100644
index 7ce85e29f94..00000000000
--- a/common/src/test/java/com/alibaba/nacos/common/utils/AbstractObjectUtilsTest.java
+++ /dev/null
@@ -1,203 +0,0 @@
-/*
- * Copyright 1999-2022 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.NacosException;
-import org.junit.Test;
-
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Optional;
-
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertThrows;
-import static org.junit.Assert.assertTrue;
-
-public class AbstractObjectUtilsTest {
-
- @Test
- public void testIsCheckedException() {
- assertTrue(AbstractObjectUtils.isCheckedException(new NacosException()));
- assertFalse(AbstractObjectUtils.isCheckedException(new RuntimeException()));
- assertFalse(AbstractObjectUtils.isCheckedException(new Error()));
- }
-
- @Test
- public void testIsCompatibleWithThrowsClause() {
- assertTrue(AbstractObjectUtils.isCompatibleWithThrowsClause(new NacosException(), Exception.class));
- assertFalse(AbstractObjectUtils.isCompatibleWithThrowsClause(new NacosException(), RuntimeException.class));
- }
-
- @Test
- public void testIsArray() {
- assertTrue(AbstractObjectUtils.isArray(new Object[] {}));
- assertFalse(AbstractObjectUtils.isArray(new Object()));
- }
-
- @Test
- public void testIsEmpty() {
- assertTrue(AbstractObjectUtils.isEmpty(null));
-
- assertTrue(AbstractObjectUtils.isEmpty(Optional.empty()));
- assertFalse(AbstractObjectUtils.isEmpty(Optional.of("1")));
-
- assertTrue(AbstractObjectUtils.isEmpty(""));
- assertFalse(AbstractObjectUtils.isEmpty("1"));
-
- assertTrue(AbstractObjectUtils.isEmpty(Collections.EMPTY_LIST));
- assertFalse(AbstractObjectUtils.isEmpty(Arrays.asList(1, 2)));
-
- assertTrue(AbstractObjectUtils.isEmpty(Collections.EMPTY_MAP));
- assertFalse(AbstractObjectUtils.isEmpty(new HashMap() {
- {
- put("k", "v");
- }
- }));
-
- assertFalse(AbstractObjectUtils.isEmpty(new Object()));
-
- assertTrue(AbstractObjectUtils.isEmpty(new Object[] {}));
- assertFalse(AbstractObjectUtils.isEmpty(new Object[] {""}));
- }
-
- @Test
- public void testUnwrapOptional() {
- Object o = new Object();
- assertNull(AbstractObjectUtils.unwrapOptional(null));
- assertNull(AbstractObjectUtils.unwrapOptional(Optional.empty()));
- assertEquals(o, AbstractObjectUtils.unwrapOptional(Optional.of(o)));
- assertEquals(o, AbstractObjectUtils.unwrapOptional(o));
- }
-
- @Test
- public void testContainsElement() {
- Object[] objects = new Object[] {1, 2, 3};
- assertTrue(AbstractObjectUtils.containsElement(objects, 1));
- assertFalse(AbstractObjectUtils.containsElement(objects, 4));
- assertFalse(AbstractObjectUtils.containsElement(null, 1));
- }
-
- private enum ETest {
- CASE1,
- CASE2
- }
-
- @Test
- public void testContainsConstant() {
- assertTrue(AbstractObjectUtils.containsConstant(ETest.values(), "case1"));
- assertFalse(AbstractObjectUtils.containsConstant(ETest.values(), "case3"));
-
- assertTrue(AbstractObjectUtils.containsConstant(ETest.values(), "CASE1", true));
- assertTrue(AbstractObjectUtils.containsConstant(ETest.values(), "CASE1", false));
- assertFalse(AbstractObjectUtils.containsConstant(ETest.values(), "case1", true));
- }
-
- @Test
- public void testCaseInsensitiveValueOf() {
- assertEquals(ETest.CASE1, AbstractObjectUtils.caseInsensitiveValueOf(ETest.values(), "case1"));
- assertThrows(IllegalArgumentException.class,
- () -> AbstractObjectUtils.caseInsensitiveValueOf(ETest.values(), "3"));
- }
-
- @Test
- public void testAddObjectToArray() {
- Object[] objects = new Object[0];
- Object[] objects1 = AbstractObjectUtils.addObjectToArray(objects, 1);
- assertEquals(1, objects1.length);
- }
-
- @Test
- public void testToObjectArray() {
- Object[] objects = new Object[0];
- assertArrayEquals(objects, AbstractObjectUtils.toObjectArray(objects));
- assertNotNull(AbstractObjectUtils.toObjectArray(null));
- assertThrows(IllegalArgumentException.class, () -> AbstractObjectUtils.toObjectArray(new Object()));
- }
-
- @Test
- public void testNullSafeEquals() {
- Integer o1 = new Integer(1111);
- Integer o2 = new Integer(1111);
- AbstractObjectUtils.nullSafeEquals(1, 1);
- assertTrue(AbstractObjectUtils.nullSafeEquals(o1, o2));
- assertTrue(AbstractObjectUtils.nullSafeEquals(null, null));
-
- assertFalse(AbstractObjectUtils.nullSafeEquals(o1, null));
- assertFalse(AbstractObjectUtils.nullSafeEquals(null, o2));
-
- assertTrue(AbstractObjectUtils.nullSafeEquals(1111, 1111));
-
- assertTrue(AbstractObjectUtils.nullSafeEquals(new Integer[] {1, 2}, new Integer[] {1, 2}));
- assertFalse(AbstractObjectUtils.nullSafeEquals(new Integer[] {1, 2}, new Integer[] {1, 2, 3}));
- }
-
- @Test
- public void testNullSafeHashCode() {
- Object o = null;
- assertEquals(0, AbstractObjectUtils.nullSafeHashCode(o));
- assertNotEquals(0, AbstractObjectUtils.nullSafeHashCode(new Object[] {}));
- }
-
- @Test
- public void testIdentityToString() {
- assertEquals("", AbstractObjectUtils.identityToString(null));
-
- String str = "1";
- assertEquals(str.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(str)),
- AbstractObjectUtils.identityToString(str));
- }
-
- @Test
- public void testGetIdentityHexString() {
- assertEquals(Integer.toHexString(System.identityHashCode("1")), AbstractObjectUtils.getIdentityHexString("1"));
- }
-
- @Test
- public void testGetDisplayString() {
- assertEquals("", AbstractObjectUtils.getDisplayString(null));
- }
-
- @Test
- public void testNullSafeClassName() {
- assertEquals("null", AbstractObjectUtils.nullSafeClassName(null));
- assertEquals(Integer.class.getName(), AbstractObjectUtils.nullSafeClassName(1));
- }
-
- @Test
- public void testNullSafeToString() {
- Object o = null;
- assertEquals("null", AbstractObjectUtils.nullSafeToString(o));
- assertSame("1", AbstractObjectUtils.nullSafeToString("1"));
- assertEquals("{1, 2}", AbstractObjectUtils.nullSafeToString(new Object[] {1, 2}));
- assertEquals("{true, false}", AbstractObjectUtils.nullSafeToString(new boolean[] {true, false}));
- assertEquals("{1}", AbstractObjectUtils.nullSafeToString(new byte[] {1}));
- assertEquals("{'a'}", AbstractObjectUtils.nullSafeToString(new char[] {'a'}));
- assertEquals("{1.1}", AbstractObjectUtils.nullSafeToString(new double[] {1.1d}));
- assertEquals("{1.1}", AbstractObjectUtils.nullSafeToString(new float[] {1.1f}));
- assertEquals("{11}", AbstractObjectUtils.nullSafeToString(new int[] {11}));
- assertEquals("{11}", AbstractObjectUtils.nullSafeToString(new long[] {11L}));
- assertEquals("{11}", AbstractObjectUtils.nullSafeToString(new short[] {11}));
- }
-
-}
diff --git a/common/src/test/java/com/alibaba/nacos/common/utils/ByteUtilsTest.java b/common/src/test/java/com/alibaba/nacos/common/utils/ByteUtilsTest.java
index 7d309a8c340..153291d6f8e 100644
--- a/common/src/test/java/com/alibaba/nacos/common/utils/ByteUtilsTest.java
+++ b/common/src/test/java/com/alibaba/nacos/common/utils/ByteUtilsTest.java
@@ -21,7 +21,8 @@
import org.junit.Test;
/**
- * ByteUtils Test.
+ * ByteUtils Test.
+ *
* @ClassName: ByteUtilsTest
* @Author: ChenHao26
* @Date: 2022/8/22 10:58
@@ -48,6 +49,13 @@ public void toStringTest() {
Assert.assertEquals(str, "google");
}
+ @Test
+ public void testForInputNull() {
+ Assert.assertEquals(0, ByteUtils.toBytes(null).length);
+ Assert.assertEquals(0, ByteUtils.toBytes((Object) null).length);
+ Assert.assertEquals("", ByteUtils.toString(null));
+ }
+
@Test
public void isEmpty() {
byte[] bytes = ByteUtils.toBytes("");
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
index 4679b57e3ff..b7068dacf4c 100644
--- a/common/src/test/java/com/alibaba/nacos/common/utils/ClassUtilsTest.java
+++ b/common/src/test/java/com/alibaba/nacos/common/utils/ClassUtilsTest.java
@@ -20,7 +20,11 @@
import org.junit.Assert;
import org.junit.Test;
+import java.lang.reflect.Field;
+import java.util.Map;
+
public class ClassUtilsTest {
+
@Test
public void testFindClassByName1() {
Class> clazz = ClassUtils.findClassByName("java.lang.Integer");
@@ -50,4 +54,57 @@ public void testGetName() {
public void testIsAssignableFrom() {
Assert.assertTrue(ClassUtils.isAssignableFrom(Object.class, Integer.class));
}
+
+ @Test
+ public void testForNameArray() throws ClassNotFoundException {
+ Class clazz = ClassUtils.forName("[Lcom.alibaba.nacos.common.utils.ClassUtilsTest;", null);
+ Assert.assertEquals("[Lcom.alibaba.nacos.common.utils.ClassUtilsTest;", clazz.getName());
+ clazz = ClassUtils.forName("java.lang.String[]", null);
+ Assert.assertEquals("[Ljava.lang.String;", clazz.getName());
+ clazz = ClassUtils.forName("[[Ljava.lang.String;", null);
+ Assert.assertEquals("[[Ljava.lang.String;", clazz.getName());
+ }
+
+ @Test(expected = ClassNotFoundException.class)
+ public void testForNameNonExist() throws ClassNotFoundException {
+ ClassUtils.forName("com.alibaba.nacos.common.NonExistClass", null);
+ }
+
+ @Test
+ public void testForNameFromPrimitive() throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException {
+ Field field = ClassUtils.class.getDeclaredField("PRIMITIVE_TYPE_NAME_MAP");
+ field.setAccessible(true);
+ Map> map = (Map>) field.get(null);
+ map.put("Test", ClassUtilsTest.class);
+ Assert.assertEquals(ClassUtilsTest.class, ClassUtils.forName("Test", null));
+ }
+
+ @Test
+ public void testGetDefaultClassLoader() {
+ ClassLoader cachedClassLoader = Thread.currentThread().getContextClassLoader();
+ try {
+ Thread.currentThread().setContextClassLoader(null);
+ Assert.assertNotNull(ClassUtils.getDefaultClassLoader());
+ } finally {
+ Thread.currentThread().setContextClassLoader(cachedClassLoader);
+ }
+ }
+
+ @Test
+ public void testClassPackageAsResourcePath() throws ClassNotFoundException {
+ Class noPackageClass = ClassUtils.forName("ClassUtilsTestMockClass", null);
+ Assert.assertEquals("", ClassUtils.classPackageAsResourcePath(null));
+ Assert.assertEquals("", ClassUtils.classPackageAsResourcePath(noPackageClass));
+ Assert.assertEquals("com/alibaba/nacos/common/utils",
+ ClassUtils.classPackageAsResourcePath(ClassUtilsTest.class));
+ }
+
+ @Test
+ public void testConvertClassNameAndClassPath() {
+ String name = ClassUtilsTest.class.getName();
+ Assert.assertEquals("com/alibaba/nacos/common/utils/ClassUtilsTest",
+ ClassUtils.convertClassNameToResourcePath(name));
+ Assert.assertEquals(name,
+ ClassUtils.resourcePathToConvertClassName("com/alibaba/nacos/common/utils/ClassUtilsTest"));
+ }
}
\ No newline at end of file
diff --git a/common/src/test/java/com/alibaba/nacos/common/utils/CollectionUtilsTest.java b/common/src/test/java/com/alibaba/nacos/common/utils/CollectionUtilsTest.java
index 0ced44ed378..8531c1c6138 100644
--- a/common/src/test/java/com/alibaba/nacos/common/utils/CollectionUtilsTest.java
+++ b/common/src/test/java/com/alibaba/nacos/common/utils/CollectionUtilsTest.java
@@ -22,14 +22,14 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
+import java.util.Enumeration;
import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
-import java.util.Enumeration;
-import java.util.Iterator;
-import java.util.HashSet;
-import java.util.LinkedHashSet;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.Vector;
@@ -38,7 +38,7 @@
/**
* Unit test of CollectionUtil.
- *
+ *
* @author sunjifeng
*/
public class CollectionUtilsTest {
@@ -189,7 +189,7 @@ public void testSize() {
Assert.assertEquals(0, CollectionUtils.size(Collections.emptyList()));
Assert.assertEquals(1, CollectionUtils.size(Collections.singletonList("")));
Assert.assertEquals(10, CollectionUtils.size(IntStream.range(0, 10).boxed().collect(Collectors.toList())));
-
+
// map
Map map = new HashMap<>();
map.put("key1", "value1");
@@ -197,8 +197,7 @@ public void testSize() {
map.put("key2", "value2");
Assert.assertEquals(2, CollectionUtils.size(map));
map.put("key3", "value3");
- Assert.assertEquals(3,
- CollectionUtils.size(map));
+ Assert.assertEquals(3, CollectionUtils.size(map));
// array
Assert.assertEquals(1, CollectionUtils.size(new Object[] {"1"}));
@@ -318,7 +317,7 @@ private Enumeration asEnumeration(final Iterator iterator) {
public boolean hasMoreElements() {
return iterator.hasNext();
}
-
+
public T nextElement() {
return iterator.next();
}
@@ -341,10 +340,15 @@ public void testSetNullPointerException() {
@Test(expected = IllegalArgumentException.class)
public void testGetOnlyElementIllegalArgumentException() {
- List list = Arrays.asList(1, 2, 3);
+ List list = Arrays.asList(1, 2, 3, 4, 5, 6);
CollectionUtils.getOnlyElement(list);
}
+ @Test(expected = IllegalArgumentException.class)
+ public void testGetOnlyElementIllegalArgumentException2() {
+ CollectionUtils.getOnlyElement(null);
+ }
+
@Test(expected = NoSuchElementException.class)
public void testGetOnlyElementNoSuchElementException() {
List