加强UT
This commit is contained in:
@@ -8,6 +8,7 @@ package org.springside.modules.utils.base;
|
||||
import org.apache.commons.lang3.ClassUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
import org.springside.modules.utils.base.annotation.Nullable;
|
||||
|
||||
import com.google.common.base.Throwables;
|
||||
|
||||
@@ -62,7 +63,8 @@ public abstract class ExceptionUtil {
|
||||
* from Quasar and Tomcat's ExceptionUtils
|
||||
*/
|
||||
public static Throwable unwrap(Throwable t) {
|
||||
if (t instanceof java.util.concurrent.ExecutionException|| t instanceof java.lang.reflect.InvocationTargetException||t instanceof UncheckedException) {
|
||||
if (t instanceof java.util.concurrent.ExecutionException
|
||||
|| t instanceof java.lang.reflect.InvocationTargetException || t instanceof UncheckedException) {
|
||||
return t.getCause();
|
||||
}
|
||||
|
||||
@@ -81,6 +83,8 @@ public abstract class ExceptionUtil {
|
||||
/**
|
||||
* 获取异常的Root Cause.
|
||||
*
|
||||
* 如无底层Cause, 则返回自身
|
||||
*
|
||||
* @see Throwables#getRootCause(Throwable)
|
||||
*/
|
||||
public static Throwable getRootCause(Throwable t) {
|
||||
@@ -112,14 +116,14 @@ public abstract class ExceptionUtil {
|
||||
*
|
||||
* @see ExceptionUtils#getMessage(Throwable)
|
||||
*/
|
||||
public static String buildMessageWithShortName(Throwable t) {
|
||||
public static String toStringWithShortName(@Nullable Throwable t) {
|
||||
return ExceptionUtils.getMessage(t);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼装 短异常类名: 异常信息 <-- RootCause的短异常类名: 异常信息
|
||||
*/
|
||||
public static String buildMessageWithRootCause(Throwable t) {
|
||||
public static String toStringWithRootCause(@Nullable Throwable t) {
|
||||
if (t == null) {
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
@@ -128,8 +132,10 @@ public abstract class ExceptionUtil {
|
||||
final String message = StringUtils.defaultString(t.getMessage());
|
||||
Throwable cause = getRootCause(t);
|
||||
|
||||
StringBuilder sb = new StringBuilder(128).append(clsName).append(":").append(message).append("; <---")
|
||||
.append(buildMessageWithShortName(cause));
|
||||
StringBuilder sb = new StringBuilder(128).append(clsName).append(": ").append(message);
|
||||
if (cause != t) {
|
||||
sb.append("; <---").append(toStringWithShortName(cause));
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package org.springside.modules.utils.base;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
import org.springside.modules.utils.base.annotation.Nullable;
|
||||
|
||||
/**
|
||||
@@ -60,9 +58,9 @@ public class MoreValidate {
|
||||
/**
|
||||
* 校验为正数则返回该数字,否则抛出异常.
|
||||
*/
|
||||
static BigInteger positive(@Nullable String role, BigInteger x) {
|
||||
if (x.signum() <= 0) {
|
||||
throw new IllegalArgumentException(role + " (" + x + ") must be > 0");
|
||||
public static double positive(@Nullable String role, double x) {
|
||||
if (!(x > 0)) { // not x < 0, to work with NaN.
|
||||
throw new IllegalArgumentException(role + " (" + x + ") must be >= 0");
|
||||
}
|
||||
return x;
|
||||
}
|
||||
@@ -107,16 +105,6 @@ public class MoreValidate {
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验为正数则返回该数字,否则抛出异常.
|
||||
*/
|
||||
public static BigInteger nonNegative(@Nullable String role, BigInteger x) {
|
||||
if (x.signum() < 0) {
|
||||
throw new IllegalArgumentException(role + " (" + x + ") must be >= 0");
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验为正数则返回该数字,否则抛出异常.
|
||||
*/
|
||||
|
||||
@@ -44,7 +44,7 @@ public class ObjectUtil {
|
||||
} else if (componentType == byte.class) {
|
||||
sb.append(Arrays.toString((byte[]) value));
|
||||
} else {
|
||||
throw new IllegalArgumentException();
|
||||
throw new IllegalArgumentException("unsupport array type");
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
|
||||
@@ -44,12 +44,19 @@ public class ArrayUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加元素到数组末尾,没有银弹,复制扩容.
|
||||
* 添加元素到数组头,没有银弹,复制扩容.
|
||||
*/
|
||||
public static <T> T[] concat(@Nullable T element, T[] array) {
|
||||
return ObjectArrays.concat(element, array);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加元素到数组末尾,没有银弹,复制扩容.
|
||||
*/
|
||||
public static <T> T[] concat(T[] array, @Nullable T element) {
|
||||
return ObjectArrays.concat(array, element);
|
||||
}
|
||||
|
||||
/**
|
||||
* 传入类型与大小创建数组.
|
||||
*/
|
||||
|
||||
+23
-23
@@ -27,30 +27,7 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
*/
|
||||
public class ThreadPoolUtil {
|
||||
|
||||
/**
|
||||
* 保证不会有Exception抛出到线程池的Runnable包裹类,防止用户没有捕捉异常导致中断了线程池中的线程, 使得SchedulerService无法执行. 在无法控制第三方包的Runnalbe实现时,使用本类进行包裹.
|
||||
*/
|
||||
public static class WrapExceptionRunnable implements Runnable {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(WrapExceptionRunnable.class);
|
||||
|
||||
private Runnable runnable;
|
||||
|
||||
public WrapExceptionRunnable(Runnable runnable) {
|
||||
Validate.notNull(runnable);
|
||||
this.runnable = runnable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
runnable.run();
|
||||
} catch (Throwable e) {
|
||||
// catch any exception, because the scheduled thread will break if the exception thrown to outside.
|
||||
logger.error("Unexpected error occurred in task", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 按照ExecutorService JavaDoc示例代码编写的Graceful Shutdown方法.
|
||||
@@ -112,5 +89,28 @@ public class ThreadPoolUtil {
|
||||
return new WrapExceptionRunnable(runnable);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保证不会有Exception抛出到线程池的Runnable包裹类,防止用户没有捕捉异常导致中断了线程池中的线程, 使得SchedulerService无法执行. 在无法控制第三方包的Runnalbe实现时,使用本类进行包裹.
|
||||
*/
|
||||
public static class WrapExceptionRunnable implements Runnable {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(WrapExceptionRunnable.class);
|
||||
|
||||
private Runnable runnable;
|
||||
|
||||
public WrapExceptionRunnable(Runnable runnable) {
|
||||
Validate.notNull(runnable);
|
||||
this.runnable = runnable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
runnable.run();
|
||||
} catch (Throwable e) {
|
||||
// catch any exception, because the scheduled thread will break if the exception thrown to outside.
|
||||
logger.error("Unexpected error occurred in task", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,14 +104,18 @@ public abstract class MathUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 保证结果为正数的取模
|
||||
* 保证结果为正数的取模.
|
||||
*
|
||||
* 如果(v = x/m) <0,v+=m.
|
||||
*/
|
||||
public static int mod(int x, int m) {
|
||||
return IntMath.mod(x, m);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保证结果为正数的取模
|
||||
* 保证结果为正数的取模.
|
||||
*
|
||||
* 如果(v = x/m) <0,v+=m.
|
||||
*/
|
||||
public static long mod(long x, long m) {
|
||||
return LongMath.mod(x, m);
|
||||
@@ -135,6 +139,8 @@ public abstract class MathUtil {
|
||||
|
||||
/**
|
||||
* 能控制rounding方向的相除
|
||||
*
|
||||
* jdk的'/'运算符,直接向下取整
|
||||
*/
|
||||
public static long divide(long p, long q, RoundingMode mode) {
|
||||
return LongMath.divide(p, q, mode);
|
||||
@@ -142,6 +148,8 @@ public abstract class MathUtil {
|
||||
|
||||
/**
|
||||
* 平方
|
||||
*
|
||||
* @param k 平方次数,不能为负数, k=0时返回1.
|
||||
*/
|
||||
public static int pow(int b, int k) {
|
||||
return IntMath.pow(b, k);
|
||||
@@ -149,6 +157,8 @@ public abstract class MathUtil {
|
||||
|
||||
/**
|
||||
* 平方
|
||||
*
|
||||
* @param k 平方次数,不能为负数, k=0时返回1.
|
||||
*/
|
||||
public static long pow(long b, int k) {
|
||||
return LongMath.pow(b, k);
|
||||
|
||||
@@ -13,6 +13,8 @@ import java.security.SecureRandom;
|
||||
import java.util.zip.CRC32;
|
||||
|
||||
import org.apache.commons.lang3.Validate;
|
||||
import org.springside.modules.utils.base.annotation.NotNull;
|
||||
import org.springside.modules.utils.base.annotation.Nullable;
|
||||
|
||||
import com.google.common.hash.Hashing;
|
||||
|
||||
@@ -63,28 +65,28 @@ public abstract class HashUtil {
|
||||
/**
|
||||
* 对输入字符串进行sha1散列.
|
||||
*/
|
||||
public static byte[] sha1(byte[] input) {
|
||||
public static byte[] sha1(@NotNull byte[] input) {
|
||||
return digest(input, get(SHA_1_DIGEST), null, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行sha1散列, 编码默认为UTF8.
|
||||
*/
|
||||
public static byte[] sha1(String input) {
|
||||
public static byte[] sha1(@NotNull String input) {
|
||||
return digest(input.getBytes(Charsets.UTF_8), get(SHA_1_DIGEST), null, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行sha1散列,带salt达到更高的安全性.
|
||||
*/
|
||||
public static byte[] sha1(byte[] input, byte[] salt) {
|
||||
public static byte[] sha1(@NotNull byte[] input, @Nullable byte[] salt) {
|
||||
return digest(input, get(SHA_1_DIGEST), salt, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行sha1散列,带salt达到更高的安全性.
|
||||
*/
|
||||
public static byte[] sha1(String input, byte[] salt) {
|
||||
public static byte[] sha1(@NotNull String input, @Nullable byte[] salt) {
|
||||
return digest(input.getBytes(Charsets.UTF_8), get(SHA_1_DIGEST), salt, 1);
|
||||
}
|
||||
|
||||
@@ -93,7 +95,7 @@ public abstract class HashUtil {
|
||||
*
|
||||
* @see #generateSalt(int)
|
||||
*/
|
||||
public static byte[] sha1(byte[] input, byte[] salt, int iterations) {
|
||||
public static byte[] sha1(@NotNull byte[] input, @Nullable byte[] salt, int iterations) {
|
||||
return digest(input, get(SHA_1_DIGEST), salt, iterations);
|
||||
}
|
||||
|
||||
@@ -102,7 +104,7 @@ public abstract class HashUtil {
|
||||
*
|
||||
* @see #generateSalt(int)
|
||||
*/
|
||||
public static byte[] sha1(String input, byte[] salt, int iterations) {
|
||||
public static byte[] sha1(@NotNull String input, @Nullable byte[] salt, int iterations) {
|
||||
return digest(input.getBytes(Charsets.UTF_8), get(SHA_1_DIGEST), salt, iterations);
|
||||
}
|
||||
|
||||
@@ -115,7 +117,7 @@ public abstract class HashUtil {
|
||||
/**
|
||||
* 对字符串进行散列, 支持md5与sha1算法.
|
||||
*/
|
||||
private static byte[] digest(byte[] input, MessageDigest digest, byte[] salt, int iterations) {
|
||||
private static byte[] digest(@NotNull byte[] input, MessageDigest digest, byte[] salt, int iterations) {
|
||||
// 带盐
|
||||
if (salt != null) {
|
||||
digest.update(salt);
|
||||
@@ -180,7 +182,7 @@ public abstract class HashUtil {
|
||||
*
|
||||
* Guava也有crc32实现, 但返回值无法返回long,所以统一使用JDK默认实现
|
||||
*/
|
||||
public static int crc32AsInt(String input) {
|
||||
public static int crc32AsInt(@NotNull String input) {
|
||||
return crc32AsInt(input.getBytes(Charsets.UTF_8));
|
||||
}
|
||||
|
||||
@@ -189,7 +191,7 @@ public abstract class HashUtil {
|
||||
*
|
||||
* Guava也有crc32实现, 但返回值无法返回long,所以统一使用JDK默认实现
|
||||
*/
|
||||
public static int crc32AsInt(byte[] input) {
|
||||
public static int crc32AsInt(@NotNull byte[] input) {
|
||||
CRC32 crc32 = new CRC32();
|
||||
crc32.update(input);
|
||||
// CRC32 只是 32bit int,为了CheckSum接口强转成long,此处再次转回来
|
||||
@@ -201,7 +203,7 @@ public abstract class HashUtil {
|
||||
*
|
||||
* Guava也有crc32实现, 但返回值无法返回long,所以统一使用JDK默认实现
|
||||
*/
|
||||
public static long crc32AsLong(String input) {
|
||||
public static long crc32AsLong(@NotNull String input) {
|
||||
return crc32AsLong(input.getBytes(Charsets.UTF_8));
|
||||
}
|
||||
|
||||
@@ -210,7 +212,7 @@ public abstract class HashUtil {
|
||||
*
|
||||
* Guava也有crc32实现, 但返回值无法返回long,所以统一使用JDK默认实现
|
||||
*/
|
||||
public static long crc32AsLong(byte[] input) {
|
||||
public static long crc32AsLong(@NotNull byte[] input) {
|
||||
CRC32 crc32 = new CRC32();
|
||||
crc32.update(input);
|
||||
return crc32.getValue();
|
||||
@@ -220,28 +222,28 @@ public abstract class HashUtil {
|
||||
/**
|
||||
* 对输入字符串进行murmur32散列, 返回值可能是负数
|
||||
*/
|
||||
public static int murmur32AsInt(byte[] input) {
|
||||
public static int murmur32AsInt(@NotNull byte[] input) {
|
||||
return Hashing.murmur3_32(MURMUR_SEED).hashBytes(input).asInt();
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行murmur32散列, 返回值可能是负数
|
||||
*/
|
||||
public static int murmur32AsInt(String input) {
|
||||
public static int murmur32AsInt(@NotNull String input) {
|
||||
return Hashing.murmur3_32(MURMUR_SEED).hashString(input, Charsets.UTF_8).asInt();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 对输入字符串进行murmur128散列, 返回值可能是负数
|
||||
*/
|
||||
public static long murmur128AsLong(byte[] input) {
|
||||
public static long murmur128AsLong(@NotNull byte[] input) {
|
||||
return Hashing.murmur3_128(MURMUR_SEED).hashBytes(input).asLong();
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行murmur128散列, 返回值可能是负数
|
||||
*/
|
||||
public static long murmur128AsLong(String input) {
|
||||
public static long murmur128AsLong(@NotNull String input) {
|
||||
return Hashing.murmur3_128(MURMUR_SEED).hashString(input, Charsets.UTF_8).asLong();
|
||||
}
|
||||
|
||||
@@ -251,8 +253,8 @@ public abstract class HashUtil {
|
||||
* 一致性哈希算法,无需构造一致性哈希环的
|
||||
*
|
||||
* @param input 输入值
|
||||
* @param buckets 桶的数量
|
||||
* @return 桶的index,从0开始
|
||||
* @param buckets 桶的数量, 必须>0
|
||||
* @return 分配到的桶的index,从0开始
|
||||
*/
|
||||
public static int consistentHash(long input, int buckets) {
|
||||
return Hashing.consistentHash(input, buckets);
|
||||
|
||||
@@ -13,31 +13,35 @@ public abstract class ClockUtil {
|
||||
|
||||
private static Clock INSTANCE = new DefaultClock();
|
||||
|
||||
/**
|
||||
* 计算流逝的时间
|
||||
*/
|
||||
public static long elapsedTime(long beginTime) {
|
||||
return currentTimeMillis() - beginTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换为DummyClock,使用系统时间为初始时间, 单个测试完成后需要调用useDefaultClock()切换回去.
|
||||
*/
|
||||
public static synchronized DummyClock useDummyClock() {
|
||||
DummyClock clock = new DummyClock();
|
||||
INSTANCE = clock;
|
||||
return clock;
|
||||
INSTANCE = new DummyClock();
|
||||
return (DummyClock) INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换为DummyClock,单个测试完成后需要调用useDefaultClock()切换回去.
|
||||
*/
|
||||
public static synchronized DummyClock useDummyClock(long timeStampMills) {
|
||||
DummyClock clock = new DummyClock(timeStampMills);
|
||||
INSTANCE = clock;
|
||||
return clock;
|
||||
INSTANCE = new DummyClock(timeStampMills);
|
||||
return (DummyClock) INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换为DummyClock,单个测试完成后需要调用useDefaultClock()切换回去.
|
||||
*/
|
||||
public static synchronized DummyClock useDummyClock(Date date) {
|
||||
DummyClock clock = new DummyClock(date);
|
||||
INSTANCE = clock;
|
||||
return clock;
|
||||
INSTANCE = new DummyClock(date);
|
||||
return (DummyClock) INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,13 +51,6 @@ public abstract class ClockUtil {
|
||||
INSTANCE = new DefaultClock();
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算流逝的时间
|
||||
*/
|
||||
public static long elapsedTime(long beginTime) {
|
||||
return currentTimeMillis() - beginTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统当前时间
|
||||
*/
|
||||
|
||||
+19
-3
@@ -37,6 +37,15 @@ public class ExceptionUtilTest {
|
||||
assertThat(t.getCause()).isSameAs(exception);
|
||||
}
|
||||
|
||||
// do nothing of Error
|
||||
Error error = new LinkageError();
|
||||
try {
|
||||
ExceptionUtil.unchecked(error);
|
||||
fail("should fail before");
|
||||
} catch (Throwable t) {
|
||||
assertThat(t).isSameAs(error);
|
||||
}
|
||||
|
||||
// do nothing of RuntimeException
|
||||
RuntimeException runtimeException = new RuntimeException("haha");
|
||||
try {
|
||||
@@ -88,16 +97,23 @@ public class ExceptionUtilTest {
|
||||
RuntimeException runtimeException = new RuntimeException(illegalStateException);
|
||||
|
||||
assertThat(ExceptionUtil.getRootCause(runtimeException)).isSameAs(ioexception);
|
||||
//无cause
|
||||
assertThat(ExceptionUtil.getRootCause(ioexception)).isSameAs(ioexception);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildMessage() {
|
||||
IOException ioexception = new IOException("my exception");
|
||||
assertThat(ExceptionUtil.buildMessageWithShortName(ioexception)).isEqualTo("IOException: my exception");
|
||||
assertThat(ExceptionUtil.toStringWithShortName(ioexception)).isEqualTo("IOException: my exception");
|
||||
assertThat(ExceptionUtil.toStringWithShortName(null)).isEqualTo("");
|
||||
|
||||
RuntimeException runtimeExcetpion = new RuntimeException("my runtimeException", ioexception);
|
||||
assertThat(ExceptionUtil.buildMessageWithRootCause(runtimeExcetpion))
|
||||
.isEqualTo("RuntimeException:my runtimeException; <---IOException: my exception");
|
||||
assertThat(ExceptionUtil.toStringWithRootCause(runtimeExcetpion))
|
||||
.isEqualTo("RuntimeException: my runtimeException; <---IOException: my exception");
|
||||
|
||||
assertThat(ExceptionUtil.toStringWithRootCause(null)).isEqualTo("");
|
||||
//无cause
|
||||
assertThat(ExceptionUtil.toStringWithRootCause(ioexception)).isEqualTo("IOException: my exception");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+62
-31
@@ -27,14 +27,23 @@ public class MoreValidateTest {
|
||||
assertThat(c).isEqualTo(1);
|
||||
|
||||
// long
|
||||
long b = MoreValidate.nonNegative("x", 0);
|
||||
long b = MoreValidate.nonNegative("x", 0l);
|
||||
assertThat(b).isEqualTo(0);
|
||||
|
||||
b = MoreValidate.positive("x", 1);
|
||||
b = MoreValidate.nonNegative("x", 11l);
|
||||
assertThat(b).isEqualTo(11);
|
||||
|
||||
b = MoreValidate.positive("x", 1l);
|
||||
assertThat(b).isEqualTo(1);
|
||||
|
||||
b = MoreValidate.nonNegative("x", 11);
|
||||
assertThat(b).isEqualTo(11);
|
||||
double e = MoreValidate.nonNegative("x", 0l);
|
||||
assertThat(e).isEqualTo(0);
|
||||
|
||||
e = MoreValidate.nonNegative("x", 11d);
|
||||
assertThat(e).isEqualTo(11);
|
||||
|
||||
e = MoreValidate.positive("x", 1.1d);
|
||||
assertThat(e).isEqualTo(1.1);
|
||||
|
||||
// Long
|
||||
Long d = MoreValidate.nonNegative("x", Long.valueOf(0));
|
||||
@@ -46,36 +55,13 @@ public class MoreValidateTest {
|
||||
d = MoreValidate.nonNegative("x", Long.valueOf(11));
|
||||
assertThat(d).isEqualTo(11);
|
||||
|
||||
// int
|
||||
try {
|
||||
MoreValidate.nonNegative("x", -1);
|
||||
fail("fail");
|
||||
} catch (Throwable t) {
|
||||
assertThat(t).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
try {
|
||||
MoreValidate.nonNegative("x", -1l);
|
||||
fail("fail");
|
||||
} catch (Throwable t) {
|
||||
assertThat(t).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
try {
|
||||
MoreValidate.nonNegative("x", Long.valueOf(-1));
|
||||
fail("fail");
|
||||
} catch (Throwable t) {
|
||||
assertThat(t).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
try {
|
||||
MoreValidate.nonNegative("x", Integer.valueOf(-1));
|
||||
fail("fail");
|
||||
} catch (Throwable t) {
|
||||
assertThat(t).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
//////
|
||||
|
||||
try {
|
||||
MoreValidate.positive("x", -1);
|
||||
fail("fail");
|
||||
@@ -90,6 +76,14 @@ public class MoreValidateTest {
|
||||
assertThat(t).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
// long
|
||||
try {
|
||||
MoreValidate.nonNegative("x", -1l);
|
||||
fail("fail");
|
||||
} catch (Throwable t) {
|
||||
assertThat(t).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
try {
|
||||
MoreValidate.positive("x", -1l);
|
||||
fail("fail");
|
||||
@@ -97,7 +91,36 @@ public class MoreValidateTest {
|
||||
assertThat(t).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
try {
|
||||
MoreValidate.positive("x", 0);
|
||||
MoreValidate.positive("x", 0l);
|
||||
fail("fail");
|
||||
} catch (Throwable t) {
|
||||
assertThat(t).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
// Long
|
||||
try {
|
||||
MoreValidate.nonNegative("x", Long.valueOf(-1));
|
||||
fail("fail");
|
||||
} catch (Throwable t) {
|
||||
assertThat(t).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
try {
|
||||
MoreValidate.positive("x", Long.valueOf(-1));
|
||||
fail("fail");
|
||||
} catch (Throwable t) {
|
||||
assertThat(t).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
try {
|
||||
MoreValidate.positive("x", Long.valueOf(0));
|
||||
fail("fail");
|
||||
} catch (Throwable t) {
|
||||
assertThat(t).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
// Integer
|
||||
try {
|
||||
MoreValidate.nonNegative("x", Integer.valueOf(-1));
|
||||
fail("fail");
|
||||
} catch (Throwable t) {
|
||||
assertThat(t).isInstanceOf(IllegalArgumentException.class);
|
||||
@@ -117,19 +140,27 @@ public class MoreValidateTest {
|
||||
assertThat(t).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
// double
|
||||
try {
|
||||
MoreValidate.positive("x", Long.valueOf(-1));
|
||||
MoreValidate.nonNegative("x", -9999.2d);
|
||||
fail("fail");
|
||||
} catch (Throwable t) {
|
||||
assertThat(t).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
try {
|
||||
MoreValidate.positive("x", Long.valueOf(0));
|
||||
MoreValidate.positive("x", -1.2d);
|
||||
fail("fail");
|
||||
} catch (Throwable t) {
|
||||
assertThat(t).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
try {
|
||||
MoreValidate.positive("x", 0d);
|
||||
fail("fail");
|
||||
} catch (Throwable t) {
|
||||
assertThat(t).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,10 @@ public class PropertiesUtilTest {
|
||||
assertThat(PropertiesUtil.getLong(p2, "springside.max", 0L)).isEqualTo(10);
|
||||
assertThat(PropertiesUtil.getLong(p2, "springside.maxA", 0L)).isEqualTo(0);
|
||||
|
||||
assertThat(PropertiesUtil.getDouble(p2, "springside.min", 0d)).isEqualTo(1);
|
||||
assertThat(PropertiesUtil.getDouble(p2, "springside.max", 0d)).isEqualTo(10);
|
||||
assertThat(PropertiesUtil.getDouble(p2, "springside.maxA", 0d)).isEqualTo(0);
|
||||
|
||||
assertThat(PropertiesUtil.getString(p2, "springside.min", "")).isEqualTo("1");
|
||||
assertThat(PropertiesUtil.getString(p2, "springside.max", "")).isEqualTo("10");
|
||||
assertThat(PropertiesUtil.getString(p2, "springside.maxA", "")).isEqualTo("");
|
||||
|
||||
+14
-3
@@ -6,16 +6,20 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springside.modules.utils.number.RandomUtil;
|
||||
|
||||
public class ArrayUtilTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void shuffle() {
|
||||
String[] arrays=new String[]{"d", "a", "c", "b", "e", "i", "g"};
|
||||
String[] arrays = new String[] { "d", "a", "c", "b", "e", "i", "g" };
|
||||
Arrays.sort(arrays);
|
||||
assertThat(arrays).containsExactly("a","b","c","d","e","g","i");
|
||||
assertThat(arrays).containsExactly("a", "b", "c", "d", "e", "g", "i");
|
||||
ArrayUtil.shuffle(arrays);
|
||||
System.out.println(Arrays.toString(arrays));
|
||||
Arrays.sort(arrays);
|
||||
|
||||
ArrayUtil.shuffle(arrays, RandomUtil.secureRandom());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -50,4 +54,11 @@ public class ArrayUtilTest {
|
||||
assertThat(list5).hasSize(3).containsExactly(1.1d, 2.2d, 3.3d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contact() {
|
||||
String[] array = new String[] { "d", "a", "c" };
|
||||
assertThat(ArrayUtil.concat("z", array)).containsExactly("z", "d", "a", "c");
|
||||
assertThat(ArrayUtil.concat(array, "z")).containsExactly("d", "a", "c", "z");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+10
-8
@@ -3,7 +3,7 @@
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
*******************************************************************************/
|
||||
package org.springside.modules.utils.concurrent;
|
||||
package org.springside.modules.utils.concurrent.threadpool;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
@@ -19,10 +19,9 @@ import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springside.modules.test.log.LogbackListAppender;
|
||||
import org.springside.modules.utils.concurrent.threadpool.ThreadPoolBuilder;
|
||||
import org.springside.modules.utils.concurrent.threadpool.ThreadPoolUtil;
|
||||
import org.springside.modules.utils.concurrent.ThreadUtil;
|
||||
|
||||
public class ThreadUtilTest {
|
||||
public class ThreadPoolUtilTest {
|
||||
@Test
|
||||
public void buildThreadFactory() {
|
||||
|
||||
@@ -91,10 +90,13 @@ public class ThreadUtilTest {
|
||||
thread.interrupt();
|
||||
ThreadUtil.sleep(500);
|
||||
assertThat(appender.getFirstLog().getMessage()).isEqualTo("InterruptedException");
|
||||
|
||||
ThreadPoolUtil.gracefulShutdown(null, 1000);
|
||||
ThreadPoolUtil.gracefulShutdown(null, 1000, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exception() {
|
||||
public void wrapException() {
|
||||
ScheduledThreadPoolExecutor executor = ThreadPoolBuilder.scheduledPool().build();
|
||||
ExceptionTask task = new ExceptionTask();
|
||||
executor.scheduleAtFixedRate(task, 0, 100, TimeUnit.MILLISECONDS);
|
||||
@@ -107,12 +109,12 @@ public class ThreadUtilTest {
|
||||
|
||||
////////
|
||||
executor = ThreadPoolBuilder.scheduledPool().build();
|
||||
task = new ExceptionTask();
|
||||
Runnable wrapTask = ThreadPoolUtil.wrapException(task);
|
||||
ExceptionTask newTask = new ExceptionTask();
|
||||
Runnable wrapTask = ThreadPoolUtil.wrapException(newTask);
|
||||
executor.scheduleAtFixedRate(wrapTask, 0, 100, TimeUnit.MILLISECONDS);
|
||||
|
||||
ThreadUtil.sleep(500);
|
||||
assertThat(task.counter.get()).isGreaterThan(1);
|
||||
assertThat(newTask.counter.get()).isGreaterThan(2);
|
||||
System.out.println("-------actual run:" + task.counter.get());
|
||||
ThreadPoolUtil.gracefulShutdown(executor, 1000);
|
||||
|
||||
@@ -41,7 +41,7 @@ public class MathUtilTest {
|
||||
assertThat(MathUtil.modByPowerOfTwo(32, 16)).isEqualTo(0);
|
||||
assertThat(MathUtil.modByPowerOfTwo(65, 16)).isEqualTo(1);
|
||||
assertThat(MathUtil.modByPowerOfTwo(-1, 16)).isEqualTo(15);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -56,9 +56,35 @@ public class MathUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cal() {
|
||||
public void caculate() {
|
||||
assertThat(MathUtil.mod(15, 10)).isEqualTo(5);
|
||||
assertThat(MathUtil.mod(-15, 10)).isEqualTo(5);
|
||||
assertThat(MathUtil.mod(-5, 3)).isEqualTo(1);
|
||||
|
||||
assertThat(MathUtil.mod(15l, 10l)).isEqualTo(5);
|
||||
assertThat(MathUtil.mod(-15l, 10l)).isEqualTo(5);
|
||||
assertThat(MathUtil.mod(-5l, 3l)).isEqualTo(1);
|
||||
|
||||
assertThat(MathUtil.mod(15l, 10)).isEqualTo(5);
|
||||
assertThat(MathUtil.mod(-15l, 10)).isEqualTo(5);
|
||||
assertThat(MathUtil.mod(-5l, 3)).isEqualTo(1);
|
||||
|
||||
assertThat(MathUtil.pow(2, 3)).isEqualTo(8);
|
||||
assertThat(MathUtil.pow(2, 0)).isEqualTo(1);
|
||||
|
||||
assertThat(MathUtil.pow(2l, 3)).isEqualTo(8);
|
||||
assertThat(MathUtil.pow(2l, 0)).isEqualTo(1);
|
||||
|
||||
assertThat(MathUtil.sqrt(15, RoundingMode.HALF_UP)).isEqualTo(4);
|
||||
assertThat(MathUtil.sqrt(16, RoundingMode.HALF_UP)).isEqualTo(4);
|
||||
assertThat(MathUtil.sqrt(10l, RoundingMode.HALF_UP)).isEqualTo(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void divide() {
|
||||
assertThat(10 / 4).isEqualTo(2);
|
||||
assertThat(MathUtil.divide(10, 4, RoundingMode.HALF_UP)).isEqualTo(3);
|
||||
assertThat(MathUtil.divide(10L, 4L, RoundingMode.HALF_DOWN)).isEqualTo(2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+19
-3
@@ -15,12 +15,28 @@ public class NumberUtilTest {
|
||||
assertThat(bytes).containsSequence((byte) 0, (byte) 0, (byte) 1, (byte) 1);
|
||||
assertThat(NumberUtil.toInt(bytes)).isEqualTo(257);
|
||||
|
||||
//long
|
||||
byte[] bytes2 = NumberUtil.toBytes(1L);
|
||||
assertThat(bytes2).hasSize(8);
|
||||
|
||||
bytes = NumberUtil.toBytes(257L);
|
||||
assertThat(bytes).containsSequence((byte) 0, (byte) 0, (byte) 1, (byte) 1);
|
||||
assertThat(NumberUtil.toLong(bytes)).isEqualTo(257L);
|
||||
|
||||
bytes2 = NumberUtil.toBytes(257L);
|
||||
assertThat(bytes2).containsSequence((byte) 0, (byte) 0, (byte) 1, (byte) 1);
|
||||
assertThat(NumberUtil.toLong(bytes2)).isEqualTo(257L);
|
||||
|
||||
//dobule
|
||||
byte[] bytes3 = NumberUtil.toBytes(1.123d);
|
||||
assertThat(NumberUtil.toDouble(bytes3)).isEqualTo(1.123d);
|
||||
|
||||
//toInt32
|
||||
assertThat(NumberUtil.toInt32(123l)).isEqualTo(123);
|
||||
|
||||
try{
|
||||
NumberUtil.toInt32(Long.valueOf(Integer.MAX_VALUE+1l));
|
||||
fail("should fail here");
|
||||
}catch(Exception e){
|
||||
assertThat(e).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+14
@@ -22,6 +22,13 @@ public class UnitConverterTest {
|
||||
} catch (Throwable t) {
|
||||
assertThat(t).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
try {
|
||||
assertThat(UnitConverter.convertDurationMillis("a12")).isEqualTo(12 * 60 * 1000);
|
||||
fail("should fail");
|
||||
} catch (Throwable t) {
|
||||
assertThat(t).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -40,6 +47,13 @@ public class UnitConverterTest {
|
||||
} catch (Throwable t) {
|
||||
assertThat(t).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
try {
|
||||
UnitConverter.convertSizeBytes("a12");
|
||||
fail("should fail");
|
||||
} catch (Throwable t) {
|
||||
assertThat(t).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -74,6 +74,7 @@ public class HashUtilTest {
|
||||
assertThat(HashUtil.murmur32AsInt("hahhha6")).isEqualTo(-446760132);
|
||||
|
||||
assertThat(HashUtil.murmur128AsLong("hahhha6")).isEqualTo(-5203515929515563680L);
|
||||
assertThat(HashUtil.murmur128AsLong("hahhha6".getBytes(Charsets.UTF_8))).isEqualTo(-5203515929515563680L);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -28,4 +28,15 @@ public class ClockUtilTest {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void elapsedTime() {
|
||||
try {
|
||||
DummyClock clock = ClockUtil.useDummyClock(2000);
|
||||
clock.increaseTime(1000);
|
||||
assertThat(ClockUtil.elapsedTime(2000)).isEqualTo(1000);
|
||||
} finally {
|
||||
ClockUtil.useDefaultClock();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -44,8 +44,17 @@ public class DateFormatUtilTest {
|
||||
public void formatDuration() {
|
||||
assertThat(DateFormatUtil.formatDuration(100)).isEqualTo("00:00:00.100");
|
||||
|
||||
assertThat(DateFormatUtil.formatDuration(new Date(100), new Date(3000))).isEqualTo("00:00:02.900");
|
||||
|
||||
assertThat(DateFormatUtil.formatDuration(DateUtil.MILLIS_PER_DAY * 2 + DateUtil.MILLIS_PER_HOUR * 4))
|
||||
.isEqualTo("52:00:00.000");
|
||||
|
||||
assertThat(DateFormatUtil.formatDurationOnSecond(new Date(100), new Date(3000))).isEqualTo("00:00:02");
|
||||
|
||||
assertThat(DateFormatUtil.formatDurationOnSecond(2000)).isEqualTo("00:00:02");
|
||||
|
||||
assertThat(DateFormatUtil.formatDurationOnSecond(DateUtil.MILLIS_PER_DAY * 2 + DateUtil.MILLIS_PER_HOUR * 4))
|
||||
.isEqualTo("52:00:00");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -17,11 +17,29 @@ public class DateUtilTest {
|
||||
|
||||
Date date3 = new Date(106, 10, 1);
|
||||
assertThat(DateUtil.isSameTime(date1, date3)).isTrue();
|
||||
|
||||
Date date4 = new Date(106, 10, 1, 12, 23, 43);
|
||||
assertThat(DateUtil.isBetween(date3, date1, date2));
|
||||
assertThat(DateUtil.isBetween(date4, date1, date2));
|
||||
|
||||
Date date5 = new Date(106, 10, 2);
|
||||
assertThat(DateUtil.isSameTime(date1, date5)).isFalse();
|
||||
|
||||
Date date4 = new Date(106, 10, 1, 12, 23, 43);
|
||||
assertThat(DateUtil.isBetween(date3, date1, date2)).isTrue();
|
||||
assertThat(DateUtil.isBetween(date4, date1, date2)).isTrue();
|
||||
|
||||
try {
|
||||
DateUtil.isBetween(null, date1, date2);
|
||||
fail("should fail before");
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
|
||||
try {
|
||||
DateUtil.isBetween(date3, date2, date1);
|
||||
fail("should fail before");
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
|
||||
assertThat(DateUtil.isBetween(date5, date1, date2)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user