Skip to content

Commit

Permalink
Add UT for common module utils package.
Browse files Browse the repository at this point in the history
  • Loading branch information
KomachiSion committed Nov 6, 2023
1 parent f9e97cd commit 564b085
Show file tree
Hide file tree
Showing 19 changed files with 1,212 additions and 209 deletions.
35 changes: 2 additions & 33 deletions common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -46,7 +44,7 @@
* @author nacos
*/
public class IoUtils {

private IoUtils() {
}

Expand Down Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
122 changes: 4 additions & 118 deletions common/src/main/java/com/alibaba/nacos/common/utils/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@

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;
import java.util.Collection;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.StringTokenizer;

/**
Expand All @@ -36,7 +32,7 @@
* @author zzq
*/
public class StringUtils {

private StringUtils() {
}

Expand Down Expand Up @@ -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)}.
Expand Down Expand Up @@ -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}.
*
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,12 @@
* @author <a href="mailto:[email protected]">liaochuntao</a>
*/
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.
*
Expand Down
Loading

0 comments on commit 564b085

Please sign in to comment.