1.0.0-RC3 #21

Merged
ZhouXY108 merged 11 commits from 1.x.x into dev 2025-02-14 19:03:05 +08:00
33 changed files with 819 additions and 601 deletions

48
pom.xml
View File

@ -6,19 +6,35 @@
<groupId>xyz.zhouxy.plusone</groupId> <groupId>xyz.zhouxy.plusone</groupId>
<artifactId>plusone-commons</artifactId> <artifactId>plusone-commons</artifactId>
<version>1.0.0-RC2</version> <version>1.0.0-RC3</version>
<properties> <properties>
<!-- Basic properties -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.target>1.8</maven.compiler.target>
<commons-lang3.version>3.17.0</commons-lang3.version>
<guava.version>33.3.1-jre</guava.version> <!-- Versions of compile dependencies -->
<guava.version>33.4.0-jre</guava.version>
<joda-time.version>2.13.0</joda-time.version> <joda-time.version>2.13.0</joda-time.version>
<!-- Versions of test dependencies -->
<commons-lang3.version>3.17.0</commons-lang3.version>
<logback.version>1.2.13</logback.version>
<junit.version>5.11.4</junit.version>
<lombok.version>1.18.36</lombok.version>
<hutool.version>5.8.35</hutool.version>
<mybatis.version>3.5.19</mybatis.version>
<h2.version>2.2.224</h2.version>
<jackson.version>2.18.2</jackson.version>
<gson.version>2.12.1</gson.version>
</properties> </properties>
<dependencies> <dependencies>
<!-- ========== Compile Dependencies ========== -->
<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
@ -32,7 +48,7 @@
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<!-- Test dependencies --> <!-- ========== Test Dependencies ========== -->
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
@ -44,55 +60,55 @@
<dependency> <dependency>
<groupId>ch.qos.logback</groupId> <groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId> <artifactId>logback-classic</artifactId>
<version>1.2.11</version> <version>${logback.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.junit.jupiter</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId> <artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version> <version>${junit.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.junit.jupiter</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId> <artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version> <version>${junit.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.junit.jupiter</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId> <artifactId>junit-jupiter-params</artifactId>
<version>5.9.2</version> <version>${junit.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>1.18.26</version> <version>${lombok.version}</version>
<optional>true</optional> <optional>true</optional>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>cn.hutool</groupId> <groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId> <artifactId>hutool-core</artifactId>
<version>5.8.25</version> <version>${hutool.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.mybatis</groupId> <groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId> <artifactId>mybatis</artifactId>
<version>3.5.17</version> <version>${mybatis.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.h2database</groupId> <groupId>com.h2database</groupId>
<artifactId>h2</artifactId> <artifactId>h2</artifactId>
<version>2.2.224</version> <version>${h2.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
@ -100,20 +116,20 @@
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>
<version>2.13.5</version> <version>${jackson.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.datatype</groupId> <groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId> <artifactId>jackson-datatype-jsr310</artifactId>
<version>2.13.5</version> <version>${jackson.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- Gson --> <!-- Gson -->
<dependency> <dependency>
<groupId>com.google.code.gson</groupId> <groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
<version>2.11.0</version> <version>${gson.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2023-2024 the original author or authors. * Copyright 2023-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -29,6 +29,7 @@ import java.lang.annotation.Target;
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0 * @since 1.0
* @see WriterMethod
*/ */
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2023-2024 the original author or authors. * Copyright 2023-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,7 +22,7 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
/** /**
* 标识该方法是虚方法 * 标识该方法是虚方法
* <p>该注解用于提醒强调父类虽然有默认实现但子类可以根据自己的需要覆写</p> * <p>该注解用于提醒强调父类虽然有默认实现但子类可以根据自己的需要覆写</p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2023-2024 the original author or authors. * Copyright 2023-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -29,6 +29,7 @@ import java.lang.annotation.Target;
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0 * @since 1.0
* @see ReaderMethod
*/ */
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2023-2024 the original author or authors. * Copyright 2023-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,11 @@ import java.util.Set;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;
import com.google.common.collect.RangeSet;
import com.google.common.collect.Table;
/** /**
* 集合工具类 * 集合工具类
* *
@ -33,9 +38,9 @@ import javax.annotation.Nullable;
*/ */
public class CollectionTools { public class CollectionTools {
// TODO [添加] 新增其它集合类型 guava 的扩展集合等 // ================================
// #region - isEmpty
// isEmpty // ================================
public static boolean isEmpty(@Nullable Collection<?> collection) { public static boolean isEmpty(@Nullable Collection<?> collection) {
return collection == null || collection.isEmpty(); return collection == null || collection.isEmpty();
@ -45,7 +50,29 @@ public class CollectionTools {
return map == null || map.isEmpty(); return map == null || map.isEmpty();
} }
// isNotEmpty public static boolean isEmpty(@Nullable Table<?, ?, ?> table) {
return table == null || table.isEmpty();
}
public static boolean isEmpty(@Nullable Multimap<?, ?> map) {
return map == null || map.isEmpty();
}
public static boolean isEmpty(@Nullable Multiset<?> set) {
return set == null || set.isEmpty();
}
public static boolean isEmpty(@Nullable RangeSet<?> set) {
return set == null || set.isEmpty();
}
// ================================
// #endregion - isEmpty
// ================================
// ================================
// #region - isNotEmpty
// ================================
public static boolean isNotEmpty(@Nullable Collection<?> collection) { public static boolean isNotEmpty(@Nullable Collection<?> collection) {
return collection != null && !collection.isEmpty(); return collection != null && !collection.isEmpty();
@ -55,6 +82,30 @@ public class CollectionTools {
return map != null && !map.isEmpty(); return map != null && !map.isEmpty();
} }
public static boolean isNotEmpty(@Nullable Table<?, ?, ?> table) {
return table != null && !table.isEmpty();
}
public static boolean isNotEmpty(@Nullable Multimap<?, ?> map) {
return map != null && !map.isEmpty();
}
public static boolean isNotEmpty(@Nullable Multiset<?> set) {
return set != null && !set.isEmpty();
}
public static boolean isNotEmpty(@Nullable RangeSet<?> set) {
return set != null && !set.isEmpty();
}
// ================================
// #endregion - isNotEmpty
// ================================
// ================================
// #region - nullToEmpty
// ================================
@Nonnull @Nonnull
public static <T> List<T> nullToEmptyList(@Nullable List<T> list) { public static <T> List<T> nullToEmptyList(@Nullable List<T> list) {
return list == null ? Collections.emptyList() : list; return list == null ? Collections.emptyList() : list;
@ -70,6 +121,10 @@ public class CollectionTools {
return map == null ? Collections.emptyMap() : map; return map == null ? Collections.emptyMap() : map;
} }
// ================================
// #endregion - nullToEmpty
// ================================
private CollectionTools() { private CollectionTools() {
throw new IllegalStateException("Utility class"); throw new IllegalStateException("Utility class");
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2024 the original author or authors. * Copyright 2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,30 +20,45 @@ import javax.annotation.Nonnull;
import xyz.zhouxy.plusone.commons.base.IWithCode; import xyz.zhouxy.plusone.commons.base.IWithCode;
/** /**
* 异常类型 * MultiTypesException
* *
* <p> * <p>
* 异常在不同场景下被抛出可以用不同的枚举值表示不同的异常类型 * 异常在不同场景下被抛出可以用不同的枚举值表示不同的场景类型
* 该枚举实现本接口用于基于不同类型创建异常 * </p>
* <p>
* 异常实现 {@link MultiTypesException} {@link #getType} 方法返回对应的场景类型
* </p>
* <p>
* 表示场景类型的枚举实现 {@link ExceptionType}其中的工厂方法用于创建类型对象
* </p>
* *
* <pre> * <pre>
* public final class LoginException extends RuntimeException { * public final class LoginException
* extends RuntimeException
* implements MultiTypesException&lt;LoginException, LoginException.Type&gt; {
* private final Type type; * private final Type type;
* private LoginException(Type type, String message) { * private LoginException(&#64;Nonnull Type type, &#64;Nonnull String message) {
* super(message); * super(message);
* this.type = type; * this.type = type;
* } * }
* *
* private LoginException(Type type, Throwable cause) { * private LoginException(&#64;Nonnull Type type, &#64;Nonnull Throwable cause) {
* super(cause); * super(cause);
* this.type = type; * this.type = type;
* } * }
* *
* private LoginException(Type type, String message, Throwable cause) { * private LoginException(&#64;Nonnull Type type,
* &#64;Nonnull String message,
* &#64;Nonnull Throwable cause) {
* super(message, cause); * super(message, cause);
* this.type = type; * this.type = type;
* } * }
* *
* &#64;Override
* public &#64;Nonnull Type getType() {
* return this.type;
* }
*
* // ... * // ...
* *
* public enum Type implements ExceptionType<LoginException> { * public enum Type implements ExceptionType<LoginException> {
@ -60,43 +75,38 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
* &#64;Nonnull * &#64;Nonnull
* private final String defaultMessage; * private final String defaultMessage;
* *
* Type(String code, String defaultMessage) { * Type(&#64;Nonnull String code, &#64;Nonnull String defaultMessage) {
* this.code = code; * this.code = code;
* this.defaultMessage = defaultMessage; * this.defaultMessage = defaultMessage;
* } * }
* *
* &#64;Override * &#64;Override
* &#64;Nonnull * public &#64;Nonnull String getCode() {
* public String getCode() {
* return code; * return code;
* } * }
* *
* &#64;Override * &#64;Override
* public String getDefaultMessage() { * public &#64;Nonnull String getDefaultMessage() {
* return defaultMessage; * return defaultMessage;
* } * }
* *
* &#64;Override * &#64;Override
* &#64;Nonnull * public &#64;Nonnull LoginException create() {
* public LoginException create() {
* return new LoginException(this, this.defaultMessage); * return new LoginException(this, this.defaultMessage);
* } * }
* *
* &#64;Override * &#64;Override
* &#64;Nonnull * public &#64;Nonnull LoginException create(String message) {
* public LoginException create(String message) {
* return new LoginException(this, message); * return new LoginException(this, message);
* } * }
* *
* &#64;Override * &#64;Override
* &#64;Nonnull * public &#64;Nonnull LoginException create(Throwable cause) {
* public LoginException create(Throwable cause) {
* return new LoginException(this, cause); * return new LoginException(this, cause);
* } * }
* *
* &#64;Override * &#64;Override
* &#64;Nonnull * public &#64;Nonnull LoginException create(String message, Throwable cause) {
* public LoginException create(String message, Throwable cause) {
* return new LoginException(this, message, cause); * return new LoginException(this, message, cause);
* } * }
* } * }
@ -109,9 +119,19 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
* </pre> * </pre>
* </p> * </p>
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108}">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/ */
public interface ExceptionType<E extends Exception> extends IWithCode<String> { public interface MultiTypesException<E extends Exception, T extends MultiTypesException.ExceptionType<E>> {
@Nonnull
T getType();
default @Nonnull String getTypeCode() {
return getType().getCode();
}
public static interface ExceptionType<E extends Exception> extends IWithCode<String> {
String getDefaultMessage(); String getDefaultMessage();
@ -128,3 +148,4 @@ public interface ExceptionType<E extends Exception> extends IWithCode<String> {
E create(String message, Throwable cause); E create(String message, Throwable cause);
} }
}

View File

@ -21,6 +21,7 @@ import java.time.format.DateTimeParseException;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import xyz.zhouxy.plusone.commons.exception.business.RequestParamsException; import xyz.zhouxy.plusone.commons.exception.business.RequestParamsException;
import xyz.zhouxy.plusone.commons.exception.MultiTypesException.ExceptionType;
/** /**
* 解析失败异常 * 解析失败异常
@ -37,7 +38,9 @@ import xyz.zhouxy.plusone.commons.exception.business.RequestParamsException;
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 0.1.0 * @since 0.1.0
*/ */
public final class ParsingFailureException extends RuntimeException { public final class ParsingFailureException
extends RuntimeException
implements MultiTypesException<ParsingFailureException, ParsingFailureException.Type> {
private final Type type; private final Type type;
@ -94,14 +97,12 @@ public final class ParsingFailureException extends RuntimeException {
return Type.NUMBER_PARSING_FAILURE.create(message, cause); return Type.NUMBER_PARSING_FAILURE.create(message, cause);
} }
@Override
@Nonnull
public Type getType() { public Type getType() {
return type; return type;
} }
public String getCode() {
return this.type.code;
}
public static final Type DEFAULT = Type.DEFAULT; public static final Type DEFAULT = Type.DEFAULT;
public static final Type NUMBER_PARSING_FAILURE = Type.NUMBER_PARSING_FAILURE; public static final Type NUMBER_PARSING_FAILURE = Type.NUMBER_PARSING_FAILURE;
public static final Type DATE_TIME_PARSING_FAILURE = Type.DATE_TIME_PARSING_FAILURE; public static final Type DATE_TIME_PARSING_FAILURE = Type.DATE_TIME_PARSING_FAILURE;
@ -127,37 +128,32 @@ public final class ParsingFailureException extends RuntimeException {
} }
@Override @Override
@Nonnull public @Nonnull String getCode() {
public String getCode() {
return code; return code;
} }
@Override @Override
public String getDefaultMessage() { public @Nonnull String getDefaultMessage() {
return defaultMessage; return defaultMessage;
} }
@Override @Override
@Nonnull public @Nonnull ParsingFailureException create() {
public ParsingFailureException create() {
return new ParsingFailureException(this, this.defaultMessage); return new ParsingFailureException(this, this.defaultMessage);
} }
@Override @Override
@Nonnull public @Nonnull ParsingFailureException create(String message) {
public ParsingFailureException create(String message) {
return new ParsingFailureException(this, message); return new ParsingFailureException(this, message);
} }
@Override @Override
@Nonnull public @Nonnull ParsingFailureException create(Throwable cause) {
public ParsingFailureException create(Throwable cause) {
return new ParsingFailureException(this, cause); return new ParsingFailureException(this, cause);
} }
@Override @Override
@Nonnull public @Nonnull ParsingFailureException create(String message, Throwable cause) {
public ParsingFailureException create(String message, Throwable cause) {
return new ParsingFailureException(this, message, cause); return new ParsingFailureException(this, message, cause);
} }
} }

View File

@ -18,7 +18,8 @@ package xyz.zhouxy.plusone.commons.exception.business;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import xyz.zhouxy.plusone.commons.exception.ExceptionType; import xyz.zhouxy.plusone.commons.exception.MultiTypesException.ExceptionType;
import xyz.zhouxy.plusone.commons.exception.MultiTypesException;
/** /**
* InvalidInputException * InvalidInputException
@ -33,7 +34,9 @@ import xyz.zhouxy.plusone.commons.exception.ExceptionType;
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 0.1.0 * @since 0.1.0
*/ */
public final class InvalidInputException extends RequestParamsException { public final class InvalidInputException
extends RequestParamsException
implements MultiTypesException<InvalidInputException, InvalidInputException.Type> {
private final Type type; private final Type type;
@ -73,14 +76,12 @@ public final class InvalidInputException extends RequestParamsException {
this(Type.DEFAULT, message, cause); this(Type.DEFAULT, message, cause);
} }
@Override
@Nonnull
public Type getType() { public Type getType() {
return this.type; return this.type;
} }
public Object getCode() {
return this.type.code;
}
public enum Type implements ExceptionType<InvalidInputException> { public enum Type implements ExceptionType<InvalidInputException> {
DEFAULT("00", "用户输入内容非法"), DEFAULT("00", "用户输入内容非法"),
CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS("01", "包含非法恶意跳转链接"), CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS("01", "包含非法恶意跳转链接"),
@ -90,9 +91,9 @@ public final class InvalidInputException extends RequestParamsException {
; ;
@Nonnull @Nonnull
final String code; private final String code;
@Nonnull @Nonnull
final String defaultMessage; private final String defaultMessage;
Type(String code, String defaultMsg) { Type(String code, String defaultMsg) {
this.code = code; this.code = code;
@ -100,37 +101,32 @@ public final class InvalidInputException extends RequestParamsException {
} }
@Override @Override
@Nonnull public @Nonnull String getCode() {
public String getCode() {
return code; return code;
} }
@Override @Override
public String getDefaultMessage() { public @Nonnull String getDefaultMessage() {
return defaultMessage; return defaultMessage;
} }
@Override @Override
@Nonnull public @Nonnull InvalidInputException create() {
public InvalidInputException create() {
return new InvalidInputException(this); return new InvalidInputException(this);
} }
@Override @Override
@Nonnull public @Nonnull InvalidInputException create(String message) {
public InvalidInputException create(String message) {
return new InvalidInputException(this, message); return new InvalidInputException(this, message);
} }
@Override @Override
@Nonnull public @Nonnull InvalidInputException create(Throwable cause) {
public InvalidInputException create(Throwable cause) {
return new InvalidInputException(this, cause); return new InvalidInputException(this, cause);
} }
@Override @Override
@Nonnull public @Nonnull InvalidInputException create(String message, Throwable cause) {
public InvalidInputException create(String message, Throwable cause) {
return new InvalidInputException(this, message, cause); return new InvalidInputException(this, message, cause);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2023-2024 the original author or authors. * Copyright 2023-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,109 +17,107 @@
package xyz.zhouxy.plusone.commons.model.dto; package xyz.zhouxy.plusone.commons.model.dto;
import java.util.Objects; import java.util.Objects;
import java.util.function.BooleanSupplier;
import java.util.function.Supplier;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import xyz.zhouxy.plusone.commons.util.AssertTools;
/** /**
* 统一结果对返回给前端的数据进行封装 * 统一结果对返回给前端的数据进行封装
* *
* <p>
* <b>SUCCESS: 2000000</b>
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/ */
public abstract class UnifiedResponse { public class UnifiedResponse<T> {
private Object status; private String code;
private String message; private String message;
private @Nullable Object data; private @Nullable T data;
public static UnifiedResponse success() { // ================================
return new SuccessResult(); // #region - Constructors
// ================================
private UnifiedResponse(String code, @Nullable String message) {
this(code, message, null);
} }
public static UnifiedResponse success(@Nullable String message) { private UnifiedResponse(String code, @Nullable String message, @Nullable T data) {
return new SuccessResult(message); this.code = Objects.requireNonNull(code);
}
public static UnifiedResponse success(@Nullable String message, @Nullable Object data) {
return new SuccessResult(message, data);
}
public static UnifiedResponse error(@Nullable String message) {
return new ErrorResult(message);
}
public static UnifiedResponse error(@Nullable String message, @Nullable Object data) {
return new ErrorResult(message, data);
}
public static UnifiedResponse error(Object status, @Nullable String message) {
return new ErrorResult(status, message);
}
public static UnifiedResponse error(Object status, @Nullable String message, @Nullable Object data) {
return new ErrorResult(status, message, data);
}
public static UnifiedResponse error(Object status, Throwable e) {
return new ErrorResult(status, e);
}
public static UnifiedResponse of(Object status, @Nullable String message) {
return new CustomResult(status, message);
}
public static UnifiedResponse of(Object status, @Nullable String message, @Nullable Object data) {
return new CustomResult(status, message, data);
}
public static UnifiedResponse of(final boolean isSuccess,
final Supplier<SuccessResult> successResult, final Supplier<ErrorResult> errorResult) {
AssertTools.checkNotNull(successResult, "Success supplier must not be null.");
AssertTools.checkNotNull(errorResult, "Error supplier must not be null.");
return isSuccess ? successResult.get() : errorResult.get();
}
public static UnifiedResponse of(final BooleanSupplier isSuccess,
final Supplier<SuccessResult> successResult, final Supplier<ErrorResult> errorResult) {
AssertTools.checkNotNull(isSuccess, "Conditions for success must not be null.");
AssertTools.checkNotNull(successResult, "Success supplier must not be null.");
AssertTools.checkNotNull(errorResult, "Error supplier must not be null.");
return isSuccess.getAsBoolean() ? successResult.get() : errorResult.get();
}
protected UnifiedResponse(Object status, @Nullable String message) {
setStatus(status);
setMessage(message);
}
protected UnifiedResponse(Object status, @Nullable String message, @Nullable Object data) {
setStatus(status);
setMessage(message);
setData(data);
}
private void setStatus(Object status) {
this.status = Objects.requireNonNull(status);
}
private void setMessage(@Nullable String message) {
this.message = message == null ? "" : message; this.message = message == null ? "" : message;
}
private void setData(@Nullable Object data) {
this.data = data; this.data = data;
} }
// Constructors end // ================================
// #endregion - Constructors
// ================================
// Getters public static final String SUCCESS_CODE = "2000000";
private static final String DEFAULT_SUCCESS_MSG = "SUCCESS";
public Object getStatus() { // ================================
return status; // #region - success
// ================================
public static UnifiedResponse<Void> success() {
return new UnifiedResponse<>(SUCCESS_CODE, DEFAULT_SUCCESS_MSG);
}
public static UnifiedResponse<Void> success(@Nullable String message) {
return new UnifiedResponse<>(SUCCESS_CODE, message);
}
public static <T> UnifiedResponse<T> success(@Nullable String message, @Nullable T data) {
return new UnifiedResponse<>(SUCCESS_CODE, message, data);
}
// ================================
// #endregion - success
// ================================
// ================================
// #region - error
// ================================
public static UnifiedResponse<Void> error(String code, @Nullable String message) {
return new UnifiedResponse<>(code, message);
}
public static <T> UnifiedResponse<T> error(String code, @Nullable String message, @Nullable T data) {
return new UnifiedResponse<>(code, message, data);
}
public static UnifiedResponse<Void> error(String code, Throwable e) {
return new UnifiedResponse<>(code, e.getMessage());
}
// ================================
// #endregion - error
// ================================
// ================================
// #region - of
// ================================
public static UnifiedResponse<Void> of(String code, @Nullable String message) {
return new UnifiedResponse<>(code, message);
}
public static <T> UnifiedResponse<T> of(String code, @Nullable String message, @Nullable T data) {
return new UnifiedResponse<>(code, message, data);
}
// ================================
// #endregion - of
// ================================
// ================================
// #region - Getters
// ================================
public String getCode() {
return code;
} }
public String getMessage() { public String getMessage() {
@ -127,16 +125,18 @@ public abstract class UnifiedResponse {
} }
@Nullable @Nullable
public Object getData() { public T getData() {
return data; return data;
} }
// Getters end // ================================
// #endregion - Getters
// ================================
@Override @Override
public String toString() { public String toString() {
return String.format("{status: %s, message: \"%s\", data: %s}", return String.format("{code: \"%s\", message: \"%s\", data: %s}",
transValue(this.status), this.message, transValue(this.data)); this.code, this.message, transValue(this.data));
} }
private static String transValue(Object value) { private static String transValue(Object value) {
@ -148,62 +148,4 @@ public abstract class UnifiedResponse {
} }
return String.valueOf(value); return String.valueOf(value);
} }
protected static class SuccessResult extends UnifiedResponse {
public static final String SUCCESS_STATUS = "2000000";
private static final String DEFAULT_SUCCESS_MSG = "SUCCESS";
SuccessResult() {
super(SUCCESS_STATUS, DEFAULT_SUCCESS_MSG);
}
SuccessResult(@Nullable String message) {
super(SUCCESS_STATUS, message);
}
SuccessResult(@Nullable String message, @Nullable Object data) {
super(SUCCESS_STATUS, message, data);
}
}
protected static class ErrorResult extends UnifiedResponse {
public static final String DEFAULT_ERROR_STATUS = "9999999";
ErrorResult(@Nullable String message) {
super(DEFAULT_ERROR_STATUS, message);
}
ErrorResult(@Nullable String message, @Nullable Object data) {
super(DEFAULT_ERROR_STATUS, message, data);
}
ErrorResult(Object status, @Nullable String message) {
super(status, message);
}
ErrorResult(Object status, @Nullable String message, @Nullable Object data) {
super(status, message, data);
}
ErrorResult(Object status, Throwable e) {
super(status, Objects.requireNonNull(e).getMessage());
}
}
/**
* 自定义结果
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
protected static class CustomResult extends UnifiedResponse {
CustomResult(Object status, @Nullable String message) {
super(status, message);
}
CustomResult(Object status, @Nullable String message, @Nullable Object data) {
super(status, message, data);
}
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2024 the original author or authors. * Copyright 2024-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -281,14 +281,6 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
return this.compareTo(other) > 0; return this.compareTo(other) > 0;
} }
public static YearQuarter min(YearQuarter yearQuarter1, YearQuarter yearQuarter2) {
return yearQuarter1.compareTo(yearQuarter2) <= 0 ? yearQuarter1 : yearQuarter2;
}
public static YearQuarter max(YearQuarter yearQuarter1, YearQuarter yearQuarter2) {
return yearQuarter1.compareTo(yearQuarter2) >= 0 ? yearQuarter1 : yearQuarter2;
}
// #endregion // #endregion
// #region - toString // #region - toString

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2023-2024 the original author or authors. * Copyright 2023-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -21,6 +21,8 @@ import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nonnull;
/** /**
* ID 生成器 * ID 生成器
* *
@ -30,7 +32,7 @@ import java.util.concurrent.ConcurrentHashMap;
* *
* @see UUID * @see UUID
* @see IdWorker * @see IdWorker
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108}">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/ */
public class IdGenerator { public class IdGenerator {
@ -48,7 +50,7 @@ public class IdGenerator {
return toSimpleString(UUID.randomUUID()); return toSimpleString(UUID.randomUUID());
} }
public static String toSimpleString(UUID uuid) { public static String toSimpleString(@Nonnull UUID uuid) {
AssertTools.checkArgument(Objects.nonNull(uuid)); AssertTools.checkArgument(Objects.nonNull(uuid));
return (uuidDigits(uuid.getMostSignificantBits() >> 32, 8) + return (uuidDigits(uuid.getMostSignificantBits() >> 32, 8) +
uuidDigits(uuid.getMostSignificantBits() >> 16, 4) + uuidDigits(uuid.getMostSignificantBits() >> 16, 4) +

View File

@ -42,7 +42,7 @@ import xyz.zhouxy.plusone.commons.exception.system.NoAvailableMacFoundException;
* <li><a href="https://juejin.cn/post/7265516484029743138">关于若干读者阅读改良版雪花算法后提出的几个共性问题的回复</a></li> * <li><a href="https://juejin.cn/post/7265516484029743138">关于若干读者阅读改良版雪花算法后提出的几个共性问题的回复</a></li>
* </ul> * </ul>
* </p> * </p>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108}">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/ */
public class IdWorker { public class IdWorker {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2023-2024 the original author or authors. * Copyright 2023-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -29,7 +29,7 @@ import javax.annotation.Nonnull;
* <p> * <p>
* 建议调用方自行维护 Random 对象 * 建议调用方自行维护 Random 对象
* </p> * </p>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108}">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/ */
public final class RandomTools { public final class RandomTools {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2023-2024 the original author or authors. * Copyright 2023-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2024 the original author or authors. * Copyright 2024-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,10 +18,14 @@ package xyz.zhouxy.plusone.commons.collection;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.reflect.Constructor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -30,14 +34,24 @@ import java.util.Set;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;
import com.google.common.collect.Range;
import com.google.common.collect.RangeSet;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.google.common.collect.Table;
import com.google.common.collect.TreeRangeSet;
public class CollectionToolsTests { public class CollectionToolsTests {
@Test @Test
void testIsEmpty() { void testIsEmpty() {
// Collection
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
assertTrue(CollectionTools.isEmpty(list)); assertTrue(CollectionTools.isEmpty(list));
assertFalse(CollectionTools.isNotEmpty(list)); assertFalse(CollectionTools.isNotEmpty(list));
@ -46,6 +60,7 @@ public class CollectionToolsTests {
assertFalse(CollectionTools.isEmpty(list)); assertFalse(CollectionTools.isEmpty(list));
assertTrue(CollectionTools.isNotEmpty(list)); assertTrue(CollectionTools.isNotEmpty(list));
// Map
Map<String, Integer> map = new HashMap<>(); Map<String, Integer> map = new HashMap<>();
assertTrue(CollectionTools.isEmpty(map)); assertTrue(CollectionTools.isEmpty(map));
assertFalse(CollectionTools.isNotEmpty(map)); assertFalse(CollectionTools.isNotEmpty(map));
@ -53,6 +68,43 @@ public class CollectionToolsTests {
map.put("2", 2); map.put("2", 2);
assertFalse(CollectionTools.isEmpty(map)); assertFalse(CollectionTools.isEmpty(map));
assertTrue(CollectionTools.isNotEmpty(map)); assertTrue(CollectionTools.isNotEmpty(map));
// Table
Table<String, String, Integer> table = HashBasedTable.create();
assertTrue(CollectionTools.isEmpty(table));
assertFalse(CollectionTools.isNotEmpty(table));
table.put("ABC", "d", 4);
assertFalse(CollectionTools.isEmpty(table));
assertTrue(CollectionTools.isNotEmpty(table));
// Multimap
Multimap<String, String> multimap = HashMultimap.create();
assertTrue(CollectionTools.isEmpty(multimap));
assertFalse(CollectionTools.isNotEmpty(multimap));
multimap.put("ABC", "d");
assertFalse(CollectionTools.isEmpty(multimap));
assertTrue(CollectionTools.isNotEmpty(multimap));
// Multiset
Multiset<String> multiset = HashMultiset.create();
assertTrue(CollectionTools.isEmpty(multiset));
assertFalse(CollectionTools.isNotEmpty(multiset));
multiset.add("ABC");
assertFalse(CollectionTools.isEmpty(multiset));
assertTrue(CollectionTools.isNotEmpty(multiset));
// RangeSet
RangeSet<Integer> rangeSet = TreeRangeSet.create();
assertTrue(CollectionTools.isEmpty(rangeSet));
assertFalse(CollectionTools.isNotEmpty(rangeSet));
rangeSet.add(Range.closed(0, 100));
rangeSet.add(Range.openClosed(100, 200));
assertFalse(CollectionTools.isEmpty(rangeSet));
assertTrue(CollectionTools.isNotEmpty(rangeSet));
} }
@Test @Test
@ -69,4 +121,18 @@ public class CollectionToolsTests {
assertSame(map, CollectionTools.nullToEmptyMap(map)); assertSame(map, CollectionTools.nullToEmptyMap(map));
assertEquals(Collections.emptyMap(), CollectionTools.nullToEmptyMap(null)); assertEquals(Collections.emptyMap(), CollectionTools.nullToEmptyMap(null));
} }
@Test
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
Constructor<?>[] constructors = CollectionTools.class.getDeclaredConstructors();
Arrays.stream(constructors)
.forEach(constructor -> {
assertFalse(constructor.isAccessible());
constructor.setAccessible(true);
Throwable cause = assertThrows(Exception.class, constructor::newInstance)
.getCause();
assertInstanceOf(IllegalStateException.class, cause);
assertEquals("Utility class", cause.getMessage());
});
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2024 the original author or authors. * Copyright 2024-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,6 +18,8 @@ package xyz.zhouxy.plusone.commons.constant;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -232,4 +234,38 @@ class PatternConstsTests {
// ================================ // ================================
// #endregion - Chinese2ndIdCardNumber // #endregion - Chinese2ndIdCardNumber
// ================================ // ================================
// ================================
// #region - invoke constructor
// ================================
@Test
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
Constructor<?>[] constructors;
constructors = RegexConsts.class.getDeclaredConstructors();
Arrays.stream(constructors)
.forEach(constructor -> {
assertFalse(constructor.isAccessible());
constructor.setAccessible(true);
Throwable cause = assertThrows(Exception.class, constructor::newInstance)
.getCause();
assertInstanceOf(IllegalStateException.class, cause);
assertEquals("Utility class", cause.getMessage());
});
constructors = PatternConsts.class.getDeclaredConstructors();
Arrays.stream(constructors)
.forEach(constructor -> {
assertFalse(constructor.isAccessible());
constructor.setAccessible(true);
Throwable cause = assertThrows(Exception.class, constructor::newInstance)
.getCause();
assertInstanceOf(IllegalStateException.class, cause);
assertEquals("Utility class", cause.getMessage());
});
}
// ================================
// #endregion - invoke constructor
// ================================
} }

View File

@ -38,7 +38,7 @@ public class InvalidInputExceptionTests {
throw InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS.create(); throw InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS.create();
}); });
assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS, e.getType()); assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS, e.getType());
assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS.getCode(), e.getCode()); assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS.getCode(), e.getTypeCode());
assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS.getDefaultMessage(), e.getMessage()); assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS.getDefaultMessage(), e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }
@ -50,7 +50,7 @@ public class InvalidInputExceptionTests {
throw InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.create(message); throw InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.create(message);
}); });
assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS, e.getType()); assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS, e.getType());
assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.getCode(), e.getCode()); assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.getCode(), e.getTypeCode());
assertEquals(message, e.getMessage()); assertEquals(message, e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }
@ -62,7 +62,7 @@ public class InvalidInputExceptionTests {
throw InvalidInputException.Type.PICTURE_CONTAINS_ILLEGAL_INFORMATION.create(message); throw InvalidInputException.Type.PICTURE_CONTAINS_ILLEGAL_INFORMATION.create(message);
}); });
assertSame(InvalidInputException.Type.PICTURE_CONTAINS_ILLEGAL_INFORMATION, e.getType()); assertSame(InvalidInputException.Type.PICTURE_CONTAINS_ILLEGAL_INFORMATION, e.getType());
assertEquals(InvalidInputException.Type.PICTURE_CONTAINS_ILLEGAL_INFORMATION.getCode(), e.getCode()); assertEquals(InvalidInputException.Type.PICTURE_CONTAINS_ILLEGAL_INFORMATION.getCode(), e.getTypeCode());
assertNull(e.getMessage()); assertNull(e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }
@ -77,7 +77,7 @@ public class InvalidInputExceptionTests {
}); });
assertSame(InvalidInputException.Type.INFRINGE_COPYRIGHT, e.getType()); assertSame(InvalidInputException.Type.INFRINGE_COPYRIGHT, e.getType());
assertEquals(InvalidInputException.Type.INFRINGE_COPYRIGHT.getCode(), e.getCode()); assertEquals(InvalidInputException.Type.INFRINGE_COPYRIGHT.getCode(), e.getTypeCode());
log.info("{}", e.getMessage()); log.info("{}", e.getMessage());
assertEquals(nfe.toString(), e.getMessage()); assertEquals(nfe.toString(), e.getMessage());
assertSame(nfe, e.getCause()); assertSame(nfe, e.getCause());
@ -92,7 +92,7 @@ public class InvalidInputExceptionTests {
}); });
assertSame(InvalidInputException.Type.DEFAULT, e.getType()); assertSame(InvalidInputException.Type.DEFAULT, e.getType());
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getCode()); assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
assertNull(e.getMessage()); assertNull(e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }
@ -106,7 +106,7 @@ public class InvalidInputExceptionTests {
throw InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS.create(message, nfe); throw InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS.create(message, nfe);
}); });
assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS, e.getType()); assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS, e.getType());
assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS.getCode(), e.getCode()); assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS.getCode(), e.getTypeCode());
assertEquals(message, e.getMessage()); assertEquals(message, e.getMessage());
assertSame(nfe, e.getCause()); assertSame(nfe, e.getCause());
} }
@ -120,7 +120,7 @@ public class InvalidInputExceptionTests {
throw InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.create(message, nfe); throw InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.create(message, nfe);
}); });
assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS, e.getType()); assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS, e.getType());
assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.getCode(), e.getCode()); assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.getCode(), e.getTypeCode());
assertNull(e.getMessage()); assertNull(e.getMessage());
assertSame(nfe, e.getCause()); assertSame(nfe, e.getCause());
} }
@ -134,7 +134,7 @@ public class InvalidInputExceptionTests {
throw InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.create(message, npe); throw InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.create(message, npe);
}); });
assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS, e.getType()); assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS, e.getType());
assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.getCode(), e.getCode()); assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.getCode(), e.getTypeCode());
assertEquals(message, e.getMessage()); assertEquals(message, e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }
@ -148,7 +148,7 @@ public class InvalidInputExceptionTests {
throw InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.create(message, nfe); throw InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.create(message, nfe);
}); });
assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS, e.getType()); assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS, e.getType());
assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.getCode(), e.getCode()); assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.getCode(), e.getTypeCode());
assertNull(e.getMessage()); assertNull(e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }
@ -167,7 +167,7 @@ public class InvalidInputExceptionTests {
throw new InvalidInputException(); throw new InvalidInputException();
}); });
assertSame(InvalidInputException.Type.DEFAULT, e.getType()); assertSame(InvalidInputException.Type.DEFAULT, e.getType());
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getCode()); assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
assertEquals(InvalidInputException.Type.DEFAULT.getDefaultMessage(), e.getMessage()); assertEquals(InvalidInputException.Type.DEFAULT.getDefaultMessage(), e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }
@ -179,7 +179,7 @@ public class InvalidInputExceptionTests {
throw new InvalidInputException(message); throw new InvalidInputException(message);
}); });
assertSame(InvalidInputException.Type.DEFAULT, e.getType()); assertSame(InvalidInputException.Type.DEFAULT, e.getType());
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getCode()); assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
assertEquals(message, e.getMessage()); assertEquals(message, e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }
@ -191,7 +191,7 @@ public class InvalidInputExceptionTests {
throw new InvalidInputException(message); throw new InvalidInputException(message);
}); });
assertSame(InvalidInputException.Type.DEFAULT, e.getType()); assertSame(InvalidInputException.Type.DEFAULT, e.getType());
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getCode()); assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
assertNull(e.getMessage()); assertNull(e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }
@ -206,7 +206,7 @@ public class InvalidInputExceptionTests {
}); });
assertSame(InvalidInputException.Type.DEFAULT, e.getType()); assertSame(InvalidInputException.Type.DEFAULT, e.getType());
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getCode()); assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
log.info("{}", e.getMessage()); log.info("{}", e.getMessage());
assertEquals(nfe.toString(), e.getMessage()); assertEquals(nfe.toString(), e.getMessage());
assertSame(nfe, e.getCause()); assertSame(nfe, e.getCause());
@ -221,7 +221,7 @@ public class InvalidInputExceptionTests {
}); });
assertSame(InvalidInputException.Type.DEFAULT, e.getType()); assertSame(InvalidInputException.Type.DEFAULT, e.getType());
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getCode()); assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
assertNull(e.getMessage()); assertNull(e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }
@ -235,7 +235,7 @@ public class InvalidInputExceptionTests {
throw new InvalidInputException(message, nfe); throw new InvalidInputException(message, nfe);
}); });
assertSame(InvalidInputException.Type.DEFAULT, e.getType()); assertSame(InvalidInputException.Type.DEFAULT, e.getType());
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getCode()); assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
assertEquals(message, e.getMessage()); assertEquals(message, e.getMessage());
assertSame(nfe, e.getCause()); assertSame(nfe, e.getCause());
} }
@ -249,7 +249,7 @@ public class InvalidInputExceptionTests {
throw new InvalidInputException(message, nfe); throw new InvalidInputException(message, nfe);
}); });
assertSame(InvalidInputException.Type.DEFAULT, e.getType()); assertSame(InvalidInputException.Type.DEFAULT, e.getType());
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getCode()); assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
assertNull(e.getMessage()); assertNull(e.getMessage());
assertSame(nfe, e.getCause()); assertSame(nfe, e.getCause());
} }
@ -263,7 +263,7 @@ public class InvalidInputExceptionTests {
throw new InvalidInputException(message, npe); throw new InvalidInputException(message, npe);
}); });
assertSame(InvalidInputException.Type.DEFAULT, e.getType()); assertSame(InvalidInputException.Type.DEFAULT, e.getType());
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getCode()); assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
assertEquals(message, e.getMessage()); assertEquals(message, e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }
@ -277,7 +277,7 @@ public class InvalidInputExceptionTests {
throw new InvalidInputException(message, nfe); throw new InvalidInputException(message, nfe);
}); });
assertSame(InvalidInputException.Type.DEFAULT, e.getType()); assertSame(InvalidInputException.Type.DEFAULT, e.getType());
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getCode()); assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
assertNull(e.getMessage()); assertNull(e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }

View File

@ -41,7 +41,7 @@ public class ParsingFailureExceptionTests {
throw ParsingFailureException.DATE_TIME_PARSING_FAILURE.create(); throw ParsingFailureException.DATE_TIME_PARSING_FAILURE.create();
}); });
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType()); assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getCode()); assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getDefaultMessage(), e.getMessage()); assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getDefaultMessage(), e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }
@ -53,7 +53,7 @@ public class ParsingFailureExceptionTests {
throw ParsingFailureException.JSON_PARSING_FAILURE.create(message); throw ParsingFailureException.JSON_PARSING_FAILURE.create(message);
}); });
assertSame(ParsingFailureException.Type.JSON_PARSING_FAILURE, e.getType()); assertSame(ParsingFailureException.Type.JSON_PARSING_FAILURE, e.getType());
assertEquals(ParsingFailureException.Type.JSON_PARSING_FAILURE.getCode(), e.getCode()); assertEquals(ParsingFailureException.Type.JSON_PARSING_FAILURE.getCode(), e.getTypeCode());
assertEquals(message, e.getMessage()); assertEquals(message, e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }
@ -65,7 +65,7 @@ public class ParsingFailureExceptionTests {
throw ParsingFailureException.XML_PARSING_FAILURE.create(message); throw ParsingFailureException.XML_PARSING_FAILURE.create(message);
}); });
assertSame(ParsingFailureException.XML_PARSING_FAILURE, e.getType()); assertSame(ParsingFailureException.XML_PARSING_FAILURE, e.getType());
assertEquals(ParsingFailureException.XML_PARSING_FAILURE.getCode(), e.getCode()); assertEquals(ParsingFailureException.XML_PARSING_FAILURE.getCode(), e.getTypeCode());
assertNull(e.getMessage()); assertNull(e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }
@ -80,7 +80,7 @@ public class ParsingFailureExceptionTests {
}); });
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType()); assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getCode()); assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
log.info("{}", e.getMessage()); log.info("{}", e.getMessage());
assertEquals(nfe.toString(), e.getMessage()); assertEquals(nfe.toString(), e.getMessage());
assertSame(nfe, e.getCause()); assertSame(nfe, e.getCause());
@ -95,7 +95,7 @@ public class ParsingFailureExceptionTests {
}); });
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType()); assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getCode()); assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
assertNull(e.getMessage()); assertNull(e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }
@ -109,7 +109,7 @@ public class ParsingFailureExceptionTests {
throw ParsingFailureException.NUMBER_PARSING_FAILURE.create(message, nfe); throw ParsingFailureException.NUMBER_PARSING_FAILURE.create(message, nfe);
}); });
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType()); assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getCode()); assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
assertEquals(message, e.getMessage()); assertEquals(message, e.getMessage());
assertSame(nfe, e.getCause()); assertSame(nfe, e.getCause());
} }
@ -123,7 +123,7 @@ public class ParsingFailureExceptionTests {
throw ParsingFailureException.DATE_TIME_PARSING_FAILURE.create(message, nfe); throw ParsingFailureException.DATE_TIME_PARSING_FAILURE.create(message, nfe);
}); });
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType()); assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getCode()); assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
assertNull(e.getMessage()); assertNull(e.getMessage());
assertSame(nfe, e.getCause()); assertSame(nfe, e.getCause());
} }
@ -137,7 +137,7 @@ public class ParsingFailureExceptionTests {
throw ParsingFailureException.DATE_TIME_PARSING_FAILURE.create(message, npe); throw ParsingFailureException.DATE_TIME_PARSING_FAILURE.create(message, npe);
}); });
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType()); assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getCode()); assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
assertEquals(message, e.getMessage()); assertEquals(message, e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }
@ -151,7 +151,7 @@ public class ParsingFailureExceptionTests {
throw ParsingFailureException.DATE_TIME_PARSING_FAILURE.create(message, nfe); throw ParsingFailureException.DATE_TIME_PARSING_FAILURE.create(message, nfe);
}); });
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType()); assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getCode()); assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
assertNull(e.getMessage()); assertNull(e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }
@ -175,7 +175,7 @@ public class ParsingFailureExceptionTests {
}); });
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType()); assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getCode()); assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
assertEquals(dtpe.getMessage(), e.getMessage()); assertEquals(dtpe.getMessage(), e.getMessage());
assertSame(dtpe, e.getCause()); assertSame(dtpe, e.getCause());
} }
@ -189,7 +189,7 @@ public class ParsingFailureExceptionTests {
}); });
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType()); assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getCode()); assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getDefaultMessage(), e.getMessage()); assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getDefaultMessage(), e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }
@ -206,7 +206,7 @@ public class ParsingFailureExceptionTests {
}); });
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType()); assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getCode()); assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
assertEquals(message, e.getMessage()); assertEquals(message, e.getMessage());
assertSame(dtpe, e.getCause()); assertSame(dtpe, e.getCause());
} }
@ -223,7 +223,7 @@ public class ParsingFailureExceptionTests {
}); });
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType()); assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getCode()); assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
assertNull(e.getMessage()); assertNull(e.getMessage());
assertSame(dtpe, e.getCause()); assertSame(dtpe, e.getCause());
} }
@ -238,7 +238,7 @@ public class ParsingFailureExceptionTests {
}); });
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType()); assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getCode()); assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
assertEquals(message, e.getMessage()); assertEquals(message, e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }
@ -253,7 +253,7 @@ public class ParsingFailureExceptionTests {
}); });
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType()); assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getCode()); assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
assertNull(e.getMessage()); assertNull(e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }
@ -277,7 +277,7 @@ public class ParsingFailureExceptionTests {
}); });
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType()); assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getCode()); assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
assertEquals(dtpe.getMessage(), e.getMessage()); assertEquals(dtpe.getMessage(), e.getMessage());
assertSame(dtpe, e.getCause()); assertSame(dtpe, e.getCause());
} }
@ -291,7 +291,7 @@ public class ParsingFailureExceptionTests {
}); });
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType()); assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getCode()); assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getDefaultMessage(), e.getMessage()); assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getDefaultMessage(), e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }
@ -308,7 +308,7 @@ public class ParsingFailureExceptionTests {
}); });
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType()); assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getCode()); assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
assertEquals(message, e.getMessage()); assertEquals(message, e.getMessage());
assertSame(dtpe, e.getCause()); assertSame(dtpe, e.getCause());
} }
@ -325,7 +325,7 @@ public class ParsingFailureExceptionTests {
}); });
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType()); assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getCode()); assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
assertNull(e.getMessage()); assertNull(e.getMessage());
assertSame(dtpe, e.getCause()); assertSame(dtpe, e.getCause());
} }
@ -340,7 +340,7 @@ public class ParsingFailureExceptionTests {
}); });
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType()); assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getCode()); assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
assertEquals(message, e.getMessage()); assertEquals(message, e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }
@ -355,7 +355,7 @@ public class ParsingFailureExceptionTests {
}); });
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType()); assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getCode()); assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
assertNull(e.getMessage()); assertNull(e.getMessage());
assertNull(e.getCause()); assertNull(e.getCause());
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2023-2024 the original author or authors. * Copyright 2023-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,8 +16,13 @@
package xyz.zhouxy.plusone.commons.function; package xyz.zhouxy.plusone.commons.function;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.Objects; import java.util.Objects;
import java.util.function.Predicate; import java.util.function.Predicate;
@ -33,4 +38,18 @@ class FunctionTests {
.and(StringUtils::isNotBlank); .and(StringUtils::isNotBlank);
assertFalse(predicate.test(str), "校验应是不通过"); assertFalse(predicate.test(str), "校验应是不通过");
} }
@Test
void test_constructorOfPredicateTools_isNotAccessible_ThrowsIllegalStateException() {
Constructor<?>[] constructors = PredicateTools.class.getDeclaredConstructors();
Arrays.stream(constructors)
.forEach(constructor -> {
assertFalse(constructor.isAccessible());
constructor.setAccessible(true);
Throwable cause = assertThrows(Exception.class, constructor::newInstance)
.getCause();
assertInstanceOf(IllegalStateException.class, cause);
assertEquals("Utility class", cause.getMessage());
});
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2024 the original author or authors. * Copyright 2024-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -30,7 +30,6 @@ import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.commons.exception.business.BizException; import xyz.zhouxy.plusone.commons.exception.business.BizException;
import xyz.zhouxy.plusone.commons.model.dto.UnifiedResponse.SuccessResult;
@Slf4j @Slf4j
public public
@ -52,439 +51,314 @@ class UnifiedResponseTests {
@Test @Test
void testSuccess_WithoutArgument() throws Exception { void testSuccess_WithoutArgument() throws Exception {
// 1. success without argument // 1. success without argument
UnifiedResponse success = UnifiedResponse.success(); UnifiedResponse<Void> success = UnifiedResponse.success();
assertEquals(SuccessResult.SUCCESS_STATUS, success.getStatus()); assertEquals("2000000", success.getCode());
assertEquals("SUCCESS", success.getMessage()); assertEquals("SUCCESS", success.getMessage());
assertNull(success.getData()); assertNull(success.getData());
String jacksonSuccess = jackson.writeValueAsString(success); String jacksonSuccess = jackson.writeValueAsString(success);
log.info("jacksonSuccess: {}", jacksonSuccess); log.info("jacksonSuccess: {}", jacksonSuccess);
assertEquals("{\"status\":\"2000000\",\"message\":\"SUCCESS\"}", jacksonSuccess); assertEquals("{\"code\":\"2000000\",\"message\":\"SUCCESS\"}", jacksonSuccess);
} }
@Test @Test
void testSuccess_WithMessage() throws Exception { void testSuccess_WithMessage() throws Exception {
// 2. success with message // 2. success with message
UnifiedResponse successWithMessage = UnifiedResponse.success("成功"); UnifiedResponse<Void> successWithMessage = UnifiedResponse.success("成功");
assertEquals(SuccessResult.SUCCESS_STATUS, successWithMessage.getStatus()); assertEquals("2000000", successWithMessage.getCode());
assertEquals("成功", successWithMessage.getMessage()); assertEquals("成功", successWithMessage.getMessage());
assertNull(successWithMessage.getData()); assertNull(successWithMessage.getData());
String jacksonSuccessWithMessage = jackson.writeValueAsString(successWithMessage); String jacksonSuccessWithMessage = jackson.writeValueAsString(successWithMessage);
log.info("jacksonSuccessWithMessage: {}", jacksonSuccessWithMessage); log.info("jacksonSuccessWithMessage: {}", jacksonSuccessWithMessage);
assertEquals("{\"status\":\"2000000\",\"message\":\"成功\"}", jacksonSuccessWithMessage); assertEquals("{\"code\":\"2000000\",\"message\":\"成功\"}", jacksonSuccessWithMessage);
} }
@Test @Test
void testSuccess_WithMessageAndNullData() throws Exception { void testSuccess_WithMessageAndNullData() throws Exception {
// success with message and null data // success with message and null data
final UnifiedResponse successWithMessageAndNullData = UnifiedResponse.success("查询成功", null); final UnifiedResponse<Void> successWithMessageAndNullData = UnifiedResponse.success("查询成功", null);
assertEquals(SuccessResult.SUCCESS_STATUS, successWithMessageAndNullData.getStatus()); assertEquals("2000000", successWithMessageAndNullData.getCode());
assertEquals("查询成功", successWithMessageAndNullData.getMessage()); assertEquals("查询成功", successWithMessageAndNullData.getMessage());
assertNull(successWithMessageAndNullData.getData()); assertNull(successWithMessageAndNullData.getData());
final String jacksonSuccessWithMessageAndNullData = jackson.writeValueAsString(successWithMessageAndNullData); final String jacksonSuccessWithMessageAndNullData = jackson.writeValueAsString(successWithMessageAndNullData);
log.info("jacksonSuccessWithMessageAndNullData: {}", jacksonSuccessWithMessageAndNullData); log.info("jacksonSuccessWithMessageAndNullData: {}", jacksonSuccessWithMessageAndNullData);
assertEquals("{\"status\":\"2000000\",\"message\":\"查询成功\"}", jacksonSuccessWithMessageAndNullData); assertEquals("{\"code\":\"2000000\",\"message\":\"查询成功\"}", jacksonSuccessWithMessageAndNullData);
assertEquals("{status: \"2000000\", message: \"查询成功\", data: null}", successWithMessageAndNullData.toString()); assertEquals("{code: \"2000000\", message: \"查询成功\", data: null}", successWithMessageAndNullData.toString());
} }
@Test @Test
void testSuccess_WithMessageAndStringData() throws Exception { void testSuccess_WithMessageAndStringData() throws Exception {
UnifiedResponse successWithStringData = UnifiedResponse.success("查询成功", "zhouxy"); UnifiedResponse<String> successWithStringData = UnifiedResponse.success("查询成功", "zhouxy");
assertEquals(SuccessResult.SUCCESS_STATUS, successWithStringData.getStatus()); assertEquals("2000000", successWithStringData.getCode());
assertEquals("查询成功", successWithStringData.getMessage()); assertEquals("查询成功", successWithStringData.getMessage());
assertEquals("zhouxy", successWithStringData.getData()); assertEquals("zhouxy", successWithStringData.getData());
String jacksonSuccessWithStringData = jackson.writeValueAsString(successWithStringData); String jacksonSuccessWithStringData = jackson.writeValueAsString(successWithStringData);
log.info("jacksonSuccessWithStringData: {}", jacksonSuccessWithStringData); log.info("jacksonSuccessWithStringData: {}", jacksonSuccessWithStringData);
assertEquals("{\"status\":\"2000000\",\"message\":\"查询成功\",\"data\":\"zhouxy\"}", jacksonSuccessWithStringData); assertEquals("{\"code\":\"2000000\",\"message\":\"查询成功\",\"data\":\"zhouxy\"}", jacksonSuccessWithStringData);
assertEquals("{status: \"2000000\", message: \"查询成功\", data: \"zhouxy\"}", successWithStringData.toString()); assertEquals("{code: \"2000000\", message: \"查询成功\", data: \"zhouxy\"}", successWithStringData.toString());
} }
@Test @Test
void testSuccess_WithMessageAndIntegerData() throws Exception { void testSuccess_WithMessageAndIntegerData() throws Exception {
final UnifiedResponse successWithIntegerData = UnifiedResponse.success("查询成功", 1); final UnifiedResponse<Integer> successWithIntegerData = UnifiedResponse.success("查询成功", 1);
assertEquals(SuccessResult.SUCCESS_STATUS, successWithIntegerData.getStatus()); assertEquals("2000000", successWithIntegerData.getCode());
assertEquals("查询成功", successWithIntegerData.getMessage()); assertEquals("查询成功", successWithIntegerData.getMessage());
assertEquals(1, successWithIntegerData.getData()); assertEquals(1, successWithIntegerData.getData());
final String jacksonSuccessWithIntegerData = jackson.writeValueAsString(successWithIntegerData); final String jacksonSuccessWithIntegerData = jackson.writeValueAsString(successWithIntegerData);
log.info("jacksonSuccessWithIntegerData: {}", jacksonSuccessWithIntegerData); log.info("jacksonSuccessWithIntegerData: {}", jacksonSuccessWithIntegerData);
assertEquals("{\"status\":\"2000000\",\"message\":\"查询成功\",\"data\":1}", jacksonSuccessWithIntegerData); assertEquals("{\"code\":\"2000000\",\"message\":\"查询成功\",\"data\":1}", jacksonSuccessWithIntegerData);
assertEquals("{status: \"2000000\", message: \"查询成功\", data: 1}", successWithIntegerData.toString()); assertEquals("{code: \"2000000\", message: \"查询成功\", data: 1}", successWithIntegerData.toString());
} }
@Test @Test
void testSuccess_WithMessageAndData() throws Exception { void testSuccess_WithMessageAndData() throws Exception {
UnifiedResponse successWithData = UnifiedResponse.success("查询成功", pageResult); UnifiedResponse<PageResult<User>> successWithData = UnifiedResponse.success("查询成功", pageResult);
assertEquals(SuccessResult.SUCCESS_STATUS, successWithData.getStatus()); assertEquals("2000000", successWithData.getCode());
assertEquals("查询成功", successWithData.getMessage()); assertEquals("查询成功", successWithData.getMessage());
assertNotNull(successWithData.getData()); assertNotNull(successWithData.getData());
assertEquals(pageResult, successWithData.getData()); assertEquals(pageResult, successWithData.getData());
String jacksonSuccessWithData = jackson.writeValueAsString(successWithData); String jacksonSuccessWithData = jackson.writeValueAsString(successWithData);
log.info("jacksonSuccessWithData: {}", jacksonSuccessWithData); log.info("jacksonSuccessWithData: {}", jacksonSuccessWithData);
assertEquals("{\"status\":\"2000000\",\"message\":\"查询成功\",\"data\":{\"total\":108,\"content\":[{\"username\":\"zhouxy1\",\"email\":\"zhouxy1@gmail.com\"},{\"username\":\"zhouxy2\",\"email\":\"zhouxy2@gmail.com\"}]}}", jacksonSuccessWithData); assertEquals("{\"code\":\"2000000\",\"message\":\"查询成功\",\"data\":{\"total\":108,\"content\":[{\"username\":\"zhouxy1\",\"email\":\"zhouxy1@gmail.com\"},{\"username\":\"zhouxy2\",\"email\":\"zhouxy2@gmail.com\"}]}}", jacksonSuccessWithData);
} }
@Test @Test
void testSuccess_WithNullMessage() throws Exception { void testSuccess_WithNullMessage() throws Exception {
// 3. success with null message // 3. success with null message
UnifiedResponse successWithNullMessage = UnifiedResponse.success(null); UnifiedResponse<Void> successWithNullMessage = UnifiedResponse.success(null);
assertEquals(SuccessResult.SUCCESS_STATUS, successWithNullMessage.getStatus()); assertEquals("2000000", successWithNullMessage.getCode());
assertEquals("", successWithNullMessage.getMessage()); assertEquals("", successWithNullMessage.getMessage());
assertNull(successWithNullMessage.getData()); assertNull(successWithNullMessage.getData());
String jacksonSuccessWithNullMessage = jackson.writeValueAsString(successWithNullMessage); String jacksonSuccessWithNullMessage = jackson.writeValueAsString(successWithNullMessage);
log.info("jacksonSuccessWithNullMessage: {}", jacksonSuccessWithNullMessage); log.info("jacksonSuccessWithNullMessage: {}", jacksonSuccessWithNullMessage);
assertEquals("{\"status\":\"2000000\",\"message\":\"\"}", jacksonSuccessWithNullMessage); assertEquals("{\"code\":\"2000000\",\"message\":\"\"}", jacksonSuccessWithNullMessage);
} }
// success with null message and null data // success with null message and null data
@Test @Test
void testSuccess_WithNullMessageAndNullData() throws Exception { void testSuccess_WithNullMessageAndNullData() throws Exception {
final UnifiedResponse successWithNullMessageAndNullData = UnifiedResponse.success(null, null); final UnifiedResponse<Void> successWithNullMessageAndNullData = UnifiedResponse.success(null, null);
assertEquals(SuccessResult.SUCCESS_STATUS, successWithNullMessageAndNullData.getStatus()); assertEquals("2000000", successWithNullMessageAndNullData.getCode());
assertEquals("", successWithNullMessageAndNullData.getMessage()); assertEquals("", successWithNullMessageAndNullData.getMessage());
assertNull(successWithNullMessageAndNullData.getData()); assertNull(successWithNullMessageAndNullData.getData());
final String jacksonSuccessWithNullMessageAndNullData = jackson.writeValueAsString(successWithNullMessageAndNullData); final String jacksonSuccessWithNullMessageAndNullData = jackson.writeValueAsString(successWithNullMessageAndNullData);
log.info("jacksonSuccessWithNullMessageAndNullData: {}", jacksonSuccessWithNullMessageAndNullData); log.info("jacksonSuccessWithNullMessageAndNullData: {}", jacksonSuccessWithNullMessageAndNullData);
assertEquals("{\"status\":\"2000000\",\"message\":\"\"}", jacksonSuccessWithNullMessageAndNullData); assertEquals("{\"code\":\"2000000\",\"message\":\"\"}", jacksonSuccessWithNullMessageAndNullData);
assertEquals("{status: \"2000000\", message: \"\", data: null}", successWithNullMessageAndNullData.toString()); assertEquals("{code: \"2000000\", message: \"\", data: null}", successWithNullMessageAndNullData.toString());
} }
@Test @Test
void testSuccess_WithNullMessageAndData() throws Exception { void testSuccess_WithNullMessageAndData() throws Exception {
// success with null message and data // success with null message and data
final User user = new User("zhouxy", "zhouxy@code108.cn"); final User user = new User("zhouxy", "zhouxy@code108.cn");
final UnifiedResponse successWithNullMessageAndData = UnifiedResponse.success(null, user); final UnifiedResponse<User> successWithNullMessageAndData = UnifiedResponse.success(null, user);
assertEquals(SuccessResult.SUCCESS_STATUS, successWithNullMessageAndData.getStatus()); assertEquals("2000000", successWithNullMessageAndData.getCode());
assertEquals("", successWithNullMessageAndData.getMessage()); assertEquals("", successWithNullMessageAndData.getMessage());
assertEquals(user, successWithNullMessageAndData.getData()); assertEquals(user, successWithNullMessageAndData.getData());
final String jacksonSuccessWithNullMessageAndData = jackson.writeValueAsString(successWithNullMessageAndData); final String jacksonSuccessWithNullMessageAndData = jackson.writeValueAsString(successWithNullMessageAndData);
log.info("jacksonSuccessWithNullMessageAndData: {}", jacksonSuccessWithNullMessageAndData); log.info("jacksonSuccessWithNullMessageAndData: {}", jacksonSuccessWithNullMessageAndData);
assertEquals("{\"status\":\"2000000\",\"message\":\"\",\"data\":{\"username\":\"zhouxy\",\"email\":\"zhouxy@code108.cn\"}}", assertEquals("{\"code\":\"2000000\",\"message\":\"\",\"data\":{\"username\":\"zhouxy\",\"email\":\"zhouxy@code108.cn\"}}",
jacksonSuccessWithNullMessageAndData); jacksonSuccessWithNullMessageAndData);
} }
@Test @Test
void testSuccess_WithEmptyMessage() throws Exception { void testSuccess_WithEmptyMessage() throws Exception {
// 4. success with empty message // 4. success with empty message
UnifiedResponse successWithEmptyMessage = UnifiedResponse.success(""); UnifiedResponse<Void> successWithEmptyMessage = UnifiedResponse.success("");
assertEquals(SuccessResult.SUCCESS_STATUS, successWithEmptyMessage.getStatus()); assertEquals("2000000", successWithEmptyMessage.getCode());
assertEquals("", successWithEmptyMessage.getMessage()); assertEquals("", successWithEmptyMessage.getMessage());
assertNull(successWithEmptyMessage.getData()); assertNull(successWithEmptyMessage.getData());
String jacksonSuccessWithEmptyMessage = jackson.writeValueAsString(successWithEmptyMessage); String jacksonSuccessWithEmptyMessage = jackson.writeValueAsString(successWithEmptyMessage);
log.info("jacksonSuccessWithEmptyMessage: {}", jacksonSuccessWithEmptyMessage); log.info("jacksonSuccessWithEmptyMessage: {}", jacksonSuccessWithEmptyMessage);
assertEquals("{\"status\":\"2000000\",\"message\":\"\"}", jacksonSuccessWithEmptyMessage); assertEquals("{\"code\":\"2000000\",\"message\":\"\"}", jacksonSuccessWithEmptyMessage);
} }
// success with empty message and null data // success with empty message and null data
@Test @Test
void testSuccess_WithEmptyMessageAndNullData() throws Exception { void testSuccess_WithEmptyMessageAndNullData() throws Exception {
final UnifiedResponse successWithEmptyMessageAndNullData = UnifiedResponse.success("", null); final UnifiedResponse<Void> successWithEmptyMessageAndNullData = UnifiedResponse.success("", null);
assertEquals(SuccessResult.SUCCESS_STATUS, successWithEmptyMessageAndNullData.getStatus()); assertEquals("2000000", successWithEmptyMessageAndNullData.getCode());
assertEquals("", successWithEmptyMessageAndNullData.getMessage()); assertEquals("", successWithEmptyMessageAndNullData.getMessage());
assertNull(successWithEmptyMessageAndNullData.getData()); assertNull(successWithEmptyMessageAndNullData.getData());
final String jacksonSuccessWithEmptyMessageAndNullData = jackson.writeValueAsString(successWithEmptyMessageAndNullData); final String jacksonSuccessWithEmptyMessageAndNullData = jackson.writeValueAsString(successWithEmptyMessageAndNullData);
log.info("jacksonSuccessWithEmptyMessageAndNullData: {}", jacksonSuccessWithEmptyMessageAndNullData); log.info("jacksonSuccessWithEmptyMessageAndNullData: {}", jacksonSuccessWithEmptyMessageAndNullData);
assertEquals("{\"status\":\"2000000\",\"message\":\"\"}", jacksonSuccessWithEmptyMessageAndNullData); assertEquals("{\"code\":\"2000000\",\"message\":\"\"}", jacksonSuccessWithEmptyMessageAndNullData);
assertEquals("{status: \"2000000\", message: \"\", data: null}", successWithEmptyMessageAndNullData.toString()); assertEquals("{code: \"2000000\", message: \"\", data: null}", successWithEmptyMessageAndNullData.toString());
} }
// success with empty message and data // success with empty message and data
@Test @Test
void testSuccess_WithEmptyMessageAndData() throws Exception { void testSuccess_WithEmptyMessageAndData() throws Exception {
final User user = new User("zhouxy", "zhouxy@gmail.com"); final User user = new User("zhouxy", "zhouxy@gmail.com");
final UnifiedResponse successWithEmptyMessageAndData = UnifiedResponse.success("", user); final UnifiedResponse<User> successWithEmptyMessageAndData = UnifiedResponse.success("", user);
assertEquals(SuccessResult.SUCCESS_STATUS, successWithEmptyMessageAndData.getStatus()); assertEquals("2000000", successWithEmptyMessageAndData.getCode());
assertEquals("", successWithEmptyMessageAndData.getMessage()); assertEquals("", successWithEmptyMessageAndData.getMessage());
assertEquals(user, successWithEmptyMessageAndData.getData()); assertEquals(user, successWithEmptyMessageAndData.getData());
final String jacksonSuccessWithEmptyMessageAndData = jackson.writeValueAsString(successWithEmptyMessageAndData); final String jacksonSuccessWithEmptyMessageAndData = jackson.writeValueAsString(successWithEmptyMessageAndData);
log.info("jacksonSuccessWithEmptyMessageAndData: {}", jacksonSuccessWithEmptyMessageAndData); log.info("jacksonSuccessWithEmptyMessageAndData: {}", jacksonSuccessWithEmptyMessageAndData);
assertEquals("{\"status\":\"2000000\",\"message\":\"\",\"data\":{\"username\":\"zhouxy\",\"email\":\"zhouxy@gmail.com\"}}", jacksonSuccessWithEmptyMessageAndData); assertEquals("{\"code\":\"2000000\",\"message\":\"\",\"data\":{\"username\":\"zhouxy\",\"email\":\"zhouxy@gmail.com\"}}", jacksonSuccessWithEmptyMessageAndData);
}
@Test
void testError_WithMessage() throws Exception {
UnifiedResponse errorWithMessage = UnifiedResponse.error("查询失败");
assertEquals("9999999", errorWithMessage.getStatus());
assertEquals("查询失败", errorWithMessage.getMessage());
assertNull(errorWithMessage.getData());
final String jacksonErrorWithMessage = jackson.writeValueAsString(errorWithMessage);
assertEquals("{\"status\":\"9999999\",\"message\":\"查询失败\"}", jacksonErrorWithMessage);
final String gsonErrorWithMessage = gson.toJson(errorWithMessage);
assertEquals("{\"status\":\"9999999\",\"message\":\"查询失败\"}", gsonErrorWithMessage);
}
@Test
void testError_WithMessage_AndNullData() throws Exception {
UnifiedResponse errorWithMessageAndNullData = UnifiedResponse.error("查询失败", (Object) null);
assertEquals("9999999", errorWithMessageAndNullData.getStatus());
assertEquals("查询失败", errorWithMessageAndNullData.getMessage());
assertNull(errorWithMessageAndNullData.getData());
final String jacksonErrorWithMessageAndNullData = jackson.writeValueAsString(errorWithMessageAndNullData);
assertEquals("{\"status\":\"9999999\",\"message\":\"查询失败\"}", jacksonErrorWithMessageAndNullData);
final String gsonErrorWithMessageAndNullData = gson.toJson(errorWithMessageAndNullData);
assertEquals("{\"status\":\"9999999\",\"message\":\"查询失败\"}", gsonErrorWithMessageAndNullData);
}
@Test
void testError_WithMessage_AndData() throws Exception {
final User user = new User("zhouxy", "zhouxy@gmail.com");
UnifiedResponse errorWithMessageAndData = UnifiedResponse.error("查询失败", user);
assertEquals("9999999", errorWithMessageAndData.getStatus());
assertEquals("查询失败", errorWithMessageAndData.getMessage());
assertEquals(user, errorWithMessageAndData.getData());
final String jacksonErrorWithMessageAndData = jackson.writeValueAsString(errorWithMessageAndData);
assertEquals("{\"status\":\"9999999\",\"message\":\"查询失败\",\"data\":{\"username\":\"zhouxy\",\"email\":\"zhouxy@gmail.com\"}}",
jacksonErrorWithMessageAndData);
final String gsonErrorWithMessageAndData = gson.toJson(errorWithMessageAndData);
assertEquals("{\"status\":\"9999999\",\"message\":\"查询失败\",\"data\":{\"username\":\"zhouxy\",\"email\":\"zhouxy@gmail.com\"}}",
gsonErrorWithMessageAndData);
}
@Test
void testError_WithNullMessage() throws Exception {
UnifiedResponse errorWithNullMessage = UnifiedResponse.error((String) null);
assertEquals("9999999", errorWithNullMessage.getStatus());
assertEquals("", errorWithNullMessage.getMessage());
assertNull(errorWithNullMessage.getData());
final String jacksonErrorWithNullMessage = jackson.writeValueAsString(errorWithNullMessage);
assertEquals("{\"status\":\"9999999\",\"message\":\"\"}", jacksonErrorWithNullMessage);
final String gsonErrorWithNullMessage = gson.toJson(errorWithNullMessage);
assertEquals("{\"status\":\"9999999\",\"message\":\"\"}", gsonErrorWithNullMessage);
}
@Test
void testError_WithNullMessage_AndNullData() throws Exception {
UnifiedResponse errorWithNullMessageAndNullData = UnifiedResponse.error((String) null, (User) null);
assertEquals("9999999", errorWithNullMessageAndNullData.getStatus());
assertEquals("", errorWithNullMessageAndNullData.getMessage());
assertNull(errorWithNullMessageAndNullData.getData());
final String jacksonErrorWithNullMessageAndNullData = jackson.writeValueAsString(errorWithNullMessageAndNullData);
assertEquals("{\"status\":\"9999999\",\"message\":\"\"}", jacksonErrorWithNullMessageAndNullData);
final String gsonErrorWithNullMessageAndNullData = gson.toJson(errorWithNullMessageAndNullData);
assertEquals("{\"status\":\"9999999\",\"message\":\"\"}", gsonErrorWithNullMessageAndNullData);
}
@Test
void testError_WithNullMessage_AndData() throws Exception {
final User user = new User("zhouxy1", "zhouxy1@gmail.com");
UnifiedResponse errorWithNullMessageAndData = UnifiedResponse.error((String) null, user);
assertEquals("9999999", errorWithNullMessageAndData.getStatus());
assertEquals("", errorWithNullMessageAndData.getMessage());
assertEquals(user, errorWithNullMessageAndData.getData());
final String jacksonErrorWithNullMessageAndData = jackson.writeValueAsString(errorWithNullMessageAndData);
assertEquals("{\"status\":\"9999999\",\"message\":\"\",\"data\":{\"username\":\"zhouxy1\",\"email\":\"zhouxy1@gmail.com\"}}",
jacksonErrorWithNullMessageAndData);
final String gsonErrorWithNullMessageAndData = gson.toJson(errorWithNullMessageAndData);
assertEquals("{\"status\":\"9999999\",\"message\":\"\",\"data\":{\"username\":\"zhouxy1\",\"email\":\"zhouxy1@gmail.com\"}}",
gsonErrorWithNullMessageAndData);
}
@Test
void testError_WithEmptyMessage() throws Exception {
UnifiedResponse errorWithEmptyMessage = UnifiedResponse.error("");
assertEquals("9999999", errorWithEmptyMessage.getStatus());
assertEquals("", errorWithEmptyMessage.getMessage());
assertNull(errorWithEmptyMessage.getData());
final String jacksonErrorWithEmptyMessage = jackson.writeValueAsString(errorWithEmptyMessage);
assertEquals("{\"status\":\"9999999\",\"message\":\"\"}", jacksonErrorWithEmptyMessage);
final String gsonErrorWithEmptyMessage = gson.toJson(errorWithEmptyMessage);
assertEquals("{\"status\":\"9999999\",\"message\":\"\"}", gsonErrorWithEmptyMessage);
}
@Test
void testError_WithEmptyMessage_AndNullData() throws Exception {
UnifiedResponse errorWithEmptyMessageAndNullData = UnifiedResponse.error("", (User) null);
assertEquals("9999999", errorWithEmptyMessageAndNullData.getStatus());
assertEquals("", errorWithEmptyMessageAndNullData.getMessage());
assertNull(errorWithEmptyMessageAndNullData.getData());
final String jacksonErrorEmptyNullMessageAndNullData = jackson.writeValueAsString(errorWithEmptyMessageAndNullData);
assertEquals("{\"status\":\"9999999\",\"message\":\"\"}", jacksonErrorEmptyNullMessageAndNullData);
final String gsonErrorWithEmptyMessageAndNullData = gson.toJson(errorWithEmptyMessageAndNullData);
assertEquals("{\"status\":\"9999999\",\"message\":\"\"}", gsonErrorWithEmptyMessageAndNullData);
}
@Test
void testError_WithEmptyMessage_AndData() throws Exception {
final User user = new User("zhouxy1", "zhouxy1@gmail.com");
UnifiedResponse errorWithEmptyMessageAndData = UnifiedResponse.error("", user);
assertEquals("9999999", errorWithEmptyMessageAndData.getStatus());
assertEquals("", errorWithEmptyMessageAndData.getMessage());
assertEquals(user, errorWithEmptyMessageAndData.getData());
final String jacksonErrorWithEmptyMessageAndData = jackson.writeValueAsString(errorWithEmptyMessageAndData);
assertEquals("{\"status\":\"9999999\",\"message\":\"\",\"data\":{\"username\":\"zhouxy1\",\"email\":\"zhouxy1@gmail.com\"}}",
jacksonErrorWithEmptyMessageAndData);
final String gsonErrorWithEmptyMessageAndData = gson.toJson(errorWithEmptyMessageAndData);
assertEquals("{\"status\":\"9999999\",\"message\":\"\",\"data\":{\"username\":\"zhouxy1\",\"email\":\"zhouxy1@gmail.com\"}}",
gsonErrorWithEmptyMessageAndData);
} }
@Test @Test
void testError_WithStatusAndMessage() throws Exception { void testError_WithStatusAndMessage() throws Exception {
final UnifiedResponse errorWithStatusAndMessage = UnifiedResponse.error(108, "查询失败"); final UnifiedResponse<Void> errorWithStatusAndMessage = UnifiedResponse.error("108", "查询失败");
assertEquals(108, errorWithStatusAndMessage.getStatus()); assertEquals("108", errorWithStatusAndMessage.getCode());
assertEquals("查询失败", errorWithStatusAndMessage.getMessage()); assertEquals("查询失败", errorWithStatusAndMessage.getMessage());
assertNull(errorWithStatusAndMessage.getData()); assertNull(errorWithStatusAndMessage.getData());
assertEquals("{status: 108, message: \"查询失败\", data: null}", errorWithStatusAndMessage.toString()); assertEquals("{code: \"108\", message: \"查询失败\", data: null}", errorWithStatusAndMessage.toString());
final String jacksonErrorWithStatusAndMessage = jackson.writeValueAsString(errorWithStatusAndMessage); final String jacksonErrorWithStatusAndMessage = jackson.writeValueAsString(errorWithStatusAndMessage);
log.info("jacksonErrorWithStatusAndMessage: {}", jacksonErrorWithStatusAndMessage); log.info("jacksonErrorWithStatusAndMessage: {}", jacksonErrorWithStatusAndMessage);
assertEquals("{\"status\":108,\"message\":\"查询失败\"}", jacksonErrorWithStatusAndMessage); assertEquals("{\"code\":\"108\",\"message\":\"查询失败\"}", jacksonErrorWithStatusAndMessage);
final String gsonErrorWithStatusAndMessage = gson.toJson(errorWithStatusAndMessage); final String gsonErrorWithStatusAndMessage = gson.toJson(errorWithStatusAndMessage);
assertEquals("{\"status\":108,\"message\":\"查询失败\"}", gsonErrorWithStatusAndMessage); assertEquals("{\"code\":\"108\",\"message\":\"查询失败\"}", gsonErrorWithStatusAndMessage);
} }
@Test @Test
void testError_WithStatusAndMessage_AndNullData() throws Exception { void testError_WithStatusAndMessage_AndNullData() throws Exception {
final UnifiedResponse errorWithStatusAndMessageAndNullData = UnifiedResponse.error(108, "查询失败", null); final UnifiedResponse<Void> errorWithStatusAndMessageAndNullData = UnifiedResponse.error("108", "查询失败", null);
assertEquals(108, errorWithStatusAndMessageAndNullData.getStatus()); assertEquals("108", errorWithStatusAndMessageAndNullData.getCode());
assertEquals("查询失败", errorWithStatusAndMessageAndNullData.getMessage()); assertEquals("查询失败", errorWithStatusAndMessageAndNullData.getMessage());
assertNull(errorWithStatusAndMessageAndNullData.getData()); assertNull(errorWithStatusAndMessageAndNullData.getData());
assertEquals("{status: 108, message: \"查询失败\", data: null}", errorWithStatusAndMessageAndNullData.toString()); assertEquals("{code: \"108\", message: \"查询失败\", data: null}", errorWithStatusAndMessageAndNullData.toString());
final String jacksonErrorWithStatusAndMessageAndNullData = jackson.writeValueAsString(errorWithStatusAndMessageAndNullData); final String jacksonErrorWithStatusAndMessageAndNullData = jackson.writeValueAsString(errorWithStatusAndMessageAndNullData);
log.info("jacksonErrorWithStatusAndMessage: {}", jacksonErrorWithStatusAndMessageAndNullData); log.info("jacksonErrorWithStatusAndMessage: {}", jacksonErrorWithStatusAndMessageAndNullData);
assertEquals("{\"status\":108,\"message\":\"查询失败\"}", jacksonErrorWithStatusAndMessageAndNullData); assertEquals("{\"code\":\"108\",\"message\":\"查询失败\"}", jacksonErrorWithStatusAndMessageAndNullData);
final String gsonErrorWithStatusAndMessageAndNullData = gson.toJson(errorWithStatusAndMessageAndNullData); final String gsonErrorWithStatusAndMessageAndNullData = gson.toJson(errorWithStatusAndMessageAndNullData);
assertEquals("{\"status\":108,\"message\":\"查询失败\"}", gsonErrorWithStatusAndMessageAndNullData); assertEquals("{\"code\":\"108\",\"message\":\"查询失败\"}", gsonErrorWithStatusAndMessageAndNullData);
} }
@Test @Test
void testError_WithStatusAndMessage_AndData() throws Exception { void testError_WithStatusAndMessage_AndData() throws Exception {
final PageResult<User> emptyPageResult = PageResult.empty(); final PageResult<User> emptyPageResult = PageResult.empty();
final UnifiedResponse errorWithStatusAndMessageAndData = UnifiedResponse.error(108, "查询失败", emptyPageResult); final UnifiedResponse<PageResult<User>> errorWithStatusAndMessageAndData = UnifiedResponse.error("108", "查询失败", emptyPageResult);
assertEquals(108, errorWithStatusAndMessageAndData.getStatus()); assertEquals("108", errorWithStatusAndMessageAndData.getCode());
assertEquals("查询失败", errorWithStatusAndMessageAndData.getMessage()); assertEquals("查询失败", errorWithStatusAndMessageAndData.getMessage());
assertEquals(emptyPageResult, errorWithStatusAndMessageAndData.getData()); assertEquals(emptyPageResult, errorWithStatusAndMessageAndData.getData());
assertEquals("{status: 108, message: \"查询失败\", data: PageResult [total=0, content=[]]}", errorWithStatusAndMessageAndData.toString()); assertEquals("{code: \"108\", message: \"查询失败\", data: PageResult [total=0, content=[]]}", errorWithStatusAndMessageAndData.toString());
final String jacksonErrorWithStatusAndMessageAndData = jackson.writeValueAsString(errorWithStatusAndMessageAndData); final String jacksonErrorWithStatusAndMessageAndData = jackson.writeValueAsString(errorWithStatusAndMessageAndData);
assertEquals("{\"status\":108,\"message\":\"查询失败\",\"data\":{\"total\":0,\"content\":[]}}", assertEquals("{\"code\":\"108\",\"message\":\"查询失败\",\"data\":{\"total\":0,\"content\":[]}}",
jacksonErrorWithStatusAndMessageAndData); jacksonErrorWithStatusAndMessageAndData);
final String gsonErrorWithStatusAndMessageAndData = gson.toJson(errorWithStatusAndMessageAndData); final String gsonErrorWithStatusAndMessageAndData = gson.toJson(errorWithStatusAndMessageAndData);
assertEquals("{\"status\":108,\"message\":\"查询失败\",\"data\":{\"total\":0,\"content\":[]}}", assertEquals("{\"code\":\"108\",\"message\":\"查询失败\",\"data\":{\"total\":0,\"content\":[]}}",
gsonErrorWithStatusAndMessageAndData); gsonErrorWithStatusAndMessageAndData);
} }
@Test @Test
void testError_WithStatusAndNullMessage() throws Exception { void testError_WithStatusAndNullMessage() throws Exception {
UnifiedResponse errorWithStatusAndNullMessage = UnifiedResponse.error(500, (String) null); UnifiedResponse<Void> errorWithStatusAndNullMessage = UnifiedResponse.error("500", (String) null);
assertEquals(500, errorWithStatusAndNullMessage.getStatus()); assertEquals("500", errorWithStatusAndNullMessage.getCode());
assertEquals("", errorWithStatusAndNullMessage.getMessage()); assertEquals("", errorWithStatusAndNullMessage.getMessage());
assertNull(errorWithStatusAndNullMessage.getData()); assertNull(errorWithStatusAndNullMessage.getData());
final String jacksonErrorWithStatusAndNullMessage = jackson.writeValueAsString(errorWithStatusAndNullMessage); final String jacksonErrorWithStatusAndNullMessage = jackson.writeValueAsString(errorWithStatusAndNullMessage);
assertEquals("{\"status\":500,\"message\":\"\"}", jacksonErrorWithStatusAndNullMessage); assertEquals("{\"code\":\"500\",\"message\":\"\"}", jacksonErrorWithStatusAndNullMessage);
final String gsonErrorWithStatusAndNullMessage = gson.toJson(errorWithStatusAndNullMessage); final String gsonErrorWithStatusAndNullMessage = gson.toJson(errorWithStatusAndNullMessage);
assertEquals("{\"status\":500,\"message\":\"\"}", gsonErrorWithStatusAndNullMessage); assertEquals("{\"code\":\"500\",\"message\":\"\"}", gsonErrorWithStatusAndNullMessage);
} }
@Test @Test
void testError_WithStatusAndNullMessage_AndNullData() throws Exception { void testError_WithStatusAndNullMessage_AndNullData() throws Exception {
UnifiedResponse errorWithStatusAndNullMessageAndNullData = UnifiedResponse.error(500, (String) null, null); UnifiedResponse<Void> errorWithStatusAndNullMessageAndNullData = UnifiedResponse.error("500", (String) null, null);
assertEquals(500, errorWithStatusAndNullMessageAndNullData.getStatus()); assertEquals("500", errorWithStatusAndNullMessageAndNullData.getCode());
assertEquals("", errorWithStatusAndNullMessageAndNullData.getMessage()); assertEquals("", errorWithStatusAndNullMessageAndNullData.getMessage());
assertNull(errorWithStatusAndNullMessageAndNullData.getData()); assertNull(errorWithStatusAndNullMessageAndNullData.getData());
final String jacksonErrorWithStatusAndNullMessageAndNullData = jackson.writeValueAsString(errorWithStatusAndNullMessageAndNullData); final String jacksonErrorWithStatusAndNullMessageAndNullData = jackson.writeValueAsString(errorWithStatusAndNullMessageAndNullData);
assertEquals("{\"status\":500,\"message\":\"\"}", jacksonErrorWithStatusAndNullMessageAndNullData); assertEquals("{\"code\":\"500\",\"message\":\"\"}", jacksonErrorWithStatusAndNullMessageAndNullData);
final String gsonErrorWithStatusAndNullMessageAndNullData = gson.toJson(errorWithStatusAndNullMessageAndNullData); final String gsonErrorWithStatusAndNullMessageAndNullData = gson.toJson(errorWithStatusAndNullMessageAndNullData);
assertEquals("{\"status\":500,\"message\":\"\"}", gsonErrorWithStatusAndNullMessageAndNullData); assertEquals("{\"code\":\"500\",\"message\":\"\"}", gsonErrorWithStatusAndNullMessageAndNullData);
} }
@Test @Test
void testError_WithStatusAndNullMessage_AndData() throws Exception { void testError_WithStatusAndNullMessage_AndData() throws Exception {
PageResult<User> emptyPageResult = PageResult.empty(); PageResult<User> emptyPageResult = PageResult.empty();
UnifiedResponse errorWithStatusAndNullMessageAndData = UnifiedResponse.error(500, (String) null, emptyPageResult); UnifiedResponse<PageResult<User>> errorWithStatusAndNullMessageAndData = UnifiedResponse.error("500", (String) null, emptyPageResult);
assertEquals(500, errorWithStatusAndNullMessageAndData.getStatus()); assertEquals("500", errorWithStatusAndNullMessageAndData.getCode());
assertEquals("", errorWithStatusAndNullMessageAndData.getMessage()); assertEquals("", errorWithStatusAndNullMessageAndData.getMessage());
assertEquals(emptyPageResult, errorWithStatusAndNullMessageAndData.getData()); assertEquals(emptyPageResult, errorWithStatusAndNullMessageAndData.getData());
final String jacksonErrorWithStatusAndNullMessageAndData = jackson.writeValueAsString(errorWithStatusAndNullMessageAndData); final String jacksonErrorWithStatusAndNullMessageAndData = jackson.writeValueAsString(errorWithStatusAndNullMessageAndData);
assertEquals("{\"status\":500,\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", jacksonErrorWithStatusAndNullMessageAndData); assertEquals("{\"code\":\"500\",\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", jacksonErrorWithStatusAndNullMessageAndData);
final String gsonErrorWithStatusAndNullMessageAndData = gson.toJson(errorWithStatusAndNullMessageAndData); final String gsonErrorWithStatusAndNullMessageAndData = gson.toJson(errorWithStatusAndNullMessageAndData);
assertEquals("{\"status\":500,\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", gsonErrorWithStatusAndNullMessageAndData); assertEquals("{\"code\":\"500\",\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", gsonErrorWithStatusAndNullMessageAndData);
} }
@Test @Test
void testError_WithStatusAndEmptyMessage() throws Exception { void testError_WithStatusAndEmptyMessage() throws Exception {
UnifiedResponse errorWithStatusAndEmptyMessage = UnifiedResponse.error(500, ""); UnifiedResponse<Void> errorWithStatusAndEmptyMessage = UnifiedResponse.error("500", "");
assertEquals(500, errorWithStatusAndEmptyMessage.getStatus()); assertEquals("500", errorWithStatusAndEmptyMessage.getCode());
assertEquals("", errorWithStatusAndEmptyMessage.getMessage()); assertEquals("", errorWithStatusAndEmptyMessage.getMessage());
assertNull(errorWithStatusAndEmptyMessage.getData()); assertNull(errorWithStatusAndEmptyMessage.getData());
final String jacksonErrorWithStatusAndEmptyMessage = jackson.writeValueAsString(errorWithStatusAndEmptyMessage); final String jacksonErrorWithStatusAndEmptyMessage = jackson.writeValueAsString(errorWithStatusAndEmptyMessage);
assertEquals("{\"status\":500,\"message\":\"\"}", jacksonErrorWithStatusAndEmptyMessage); assertEquals("{\"code\":\"500\",\"message\":\"\"}", jacksonErrorWithStatusAndEmptyMessage);
final String gsonErrorWithStatusAndEmptyMessage = gson.toJson(errorWithStatusAndEmptyMessage); final String gsonErrorWithStatusAndEmptyMessage = gson.toJson(errorWithStatusAndEmptyMessage);
assertEquals("{\"status\":500,\"message\":\"\"}", gsonErrorWithStatusAndEmptyMessage); assertEquals("{\"code\":\"500\",\"message\":\"\"}", gsonErrorWithStatusAndEmptyMessage);
} }
@Test @Test
void testError_WithStatusAndEmptyMessage_AndNullData() throws Exception { void testError_WithStatusAndEmptyMessage_AndNullData() throws Exception {
UnifiedResponse errorWithStatusAndEmptyMessageAndNullData = UnifiedResponse.error(500, "", null); UnifiedResponse<Void> errorWithStatusAndEmptyMessageAndNullData = UnifiedResponse.error("500", "", null);
assertEquals(500, errorWithStatusAndEmptyMessageAndNullData.getStatus()); assertEquals("500", errorWithStatusAndEmptyMessageAndNullData.getCode());
assertEquals("", errorWithStatusAndEmptyMessageAndNullData.getMessage()); assertEquals("", errorWithStatusAndEmptyMessageAndNullData.getMessage());
assertNull(errorWithStatusAndEmptyMessageAndNullData.getData()); assertNull(errorWithStatusAndEmptyMessageAndNullData.getData());
final String jacksonErrorWithStatusAndEmptyMessageAndNullData = jackson.writeValueAsString(errorWithStatusAndEmptyMessageAndNullData); final String jacksonErrorWithStatusAndEmptyMessageAndNullData = jackson.writeValueAsString(errorWithStatusAndEmptyMessageAndNullData);
assertEquals("{\"status\":500,\"message\":\"\"}", jacksonErrorWithStatusAndEmptyMessageAndNullData); assertEquals("{\"code\":\"500\",\"message\":\"\"}", jacksonErrorWithStatusAndEmptyMessageAndNullData);
final String gsonErrorWithStatusAndEmptyMessageAndNullData = gson.toJson(errorWithStatusAndEmptyMessageAndNullData); final String gsonErrorWithStatusAndEmptyMessageAndNullData = gson.toJson(errorWithStatusAndEmptyMessageAndNullData);
assertEquals("{\"status\":500,\"message\":\"\"}", gsonErrorWithStatusAndEmptyMessageAndNullData); assertEquals("{\"code\":\"500\",\"message\":\"\"}", gsonErrorWithStatusAndEmptyMessageAndNullData);
} }
@Test @Test
void testError_WithStatusAndEmptyMessage_AndData() throws Exception { void testError_WithStatusAndEmptyMessage_AndData() throws Exception {
PageResult<User> emptyPageResult = PageResult.empty(); PageResult<User> emptyPageResult = PageResult.empty();
UnifiedResponse errorWithStatusAndEmptyMessageAndData = UnifiedResponse.error(500, "", emptyPageResult); UnifiedResponse<PageResult<User>> errorWithStatusAndEmptyMessageAndData = UnifiedResponse.error("500", "", emptyPageResult);
assertEquals(500, errorWithStatusAndEmptyMessageAndData.getStatus()); assertEquals("500", errorWithStatusAndEmptyMessageAndData.getCode());
assertEquals("", errorWithStatusAndEmptyMessageAndData.getMessage()); assertEquals("", errorWithStatusAndEmptyMessageAndData.getMessage());
assertEquals(emptyPageResult, errorWithStatusAndEmptyMessageAndData.getData()); assertEquals(emptyPageResult, errorWithStatusAndEmptyMessageAndData.getData());
final String jacksonErrorWithStatusAndEmptyMessageAndData = jackson.writeValueAsString(errorWithStatusAndEmptyMessageAndData); final String jacksonErrorWithStatusAndEmptyMessageAndData = jackson.writeValueAsString(errorWithStatusAndEmptyMessageAndData);
assertEquals("{\"status\":500,\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", jacksonErrorWithStatusAndEmptyMessageAndData); assertEquals("{\"code\":\"500\",\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", jacksonErrorWithStatusAndEmptyMessageAndData);
final String gsonErrorWithStatusAndEmptyMessageAndData = gson.toJson(errorWithStatusAndEmptyMessageAndData); final String gsonErrorWithStatusAndEmptyMessageAndData = gson.toJson(errorWithStatusAndEmptyMessageAndData);
assertEquals("{\"status\":500,\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", gsonErrorWithStatusAndEmptyMessageAndData); assertEquals("{\"code\":\"500\",\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", gsonErrorWithStatusAndEmptyMessageAndData);
} }
@Test @Test
void testError_WithStatusAndThrowable() throws Exception { void testError_WithStatusAndThrowable() throws Exception {
final IllegalArgumentException e = new IllegalArgumentException("ID cannot be null"); final IllegalArgumentException e = new IllegalArgumentException("ID cannot be null");
final UnifiedResponse errorWithStatusThrowable = UnifiedResponse.error(500, e); final UnifiedResponse<Void> errorWithStatusThrowable = UnifiedResponse.error("500", e);
assertEquals(500, errorWithStatusThrowable.getStatus()); assertEquals("500", errorWithStatusThrowable.getCode());
assertEquals("ID cannot be null", errorWithStatusThrowable.getMessage()); assertEquals("ID cannot be null", errorWithStatusThrowable.getMessage());
assertNull(errorWithStatusThrowable.getData()); assertNull(errorWithStatusThrowable.getData());
assertEquals("{\"status\":500,\"message\":\"ID cannot be null\"}", jackson.writeValueAsString(errorWithStatusThrowable)); assertEquals("{\"code\":\"500\",\"message\":\"ID cannot be null\"}", jackson.writeValueAsString(errorWithStatusThrowable));
assertEquals("{\"status\":500,\"message\":\"ID cannot be null\"}", gson.toJson(errorWithStatusThrowable)); assertEquals("{\"code\":\"500\",\"message\":\"ID cannot be null\"}", gson.toJson(errorWithStatusThrowable));
} }
@Test @Test
void testError_WithStatusAndNullThrowable() { void testError_WithStatusAndNullThrowable() {
assertThrows(NullPointerException.class, () -> UnifiedResponse.error(500, (Throwable) null)); assertThrows(NullPointerException.class, () -> UnifiedResponse.error("500", (Throwable) null));
} }
@Test @Test
void testError_WithNullStatus() { void testError_WithNullStatus() {
final Object nullStatus = null; final String nullStatus = null;
final String nullMessage = null; final String nullMessage = null;
final User user = new User("zhouxy", "zhouxy@gmail.com"); final User user = new User("zhouxy", "zhouxy@gmail.com");
@ -512,143 +386,143 @@ class UnifiedResponseTests {
@Test @Test
void testOf_WithStatusAndMessage() throws Exception { void testOf_WithStatusAndMessage() throws Exception {
final UnifiedResponse ofWithStatusAndMessage = UnifiedResponse.of(108, "This is a message."); final UnifiedResponse<Void> ofWithStatusAndMessage = UnifiedResponse.of("108", "This is a message.");
assertEquals(108, ofWithStatusAndMessage.getStatus()); assertEquals("108", ofWithStatusAndMessage.getCode());
assertEquals("This is a message.", ofWithStatusAndMessage.getMessage()); assertEquals("This is a message.", ofWithStatusAndMessage.getMessage());
assertNull(ofWithStatusAndMessage.getData()); assertNull(ofWithStatusAndMessage.getData());
final String jacksonOfWithStatusAndMessage = jackson.writeValueAsString(ofWithStatusAndMessage); final String jacksonOfWithStatusAndMessage = jackson.writeValueAsString(ofWithStatusAndMessage);
log.info("jacksonOfWithStatusAndMessage: {}", jacksonOfWithStatusAndMessage); log.info("jacksonOfWithStatusAndMessage: {}", jacksonOfWithStatusAndMessage);
assertEquals("{\"status\":108,\"message\":\"This is a message.\"}", jacksonOfWithStatusAndMessage); assertEquals("{\"code\":\"108\",\"message\":\"This is a message.\"}", jacksonOfWithStatusAndMessage);
assertEquals("{status: 108, message: \"This is a message.\", data: null}", ofWithStatusAndMessage.toString()); assertEquals("{code: \"108\", message: \"This is a message.\", data: null}", ofWithStatusAndMessage.toString());
final String gsonOfWithStatusAndMessage = gson.toJson(ofWithStatusAndMessage); final String gsonOfWithStatusAndMessage = gson.toJson(ofWithStatusAndMessage);
assertEquals("{\"status\":108,\"message\":\"This is a message.\"}", gsonOfWithStatusAndMessage); assertEquals("{\"code\":\"108\",\"message\":\"This is a message.\"}", gsonOfWithStatusAndMessage);
} }
@Test @Test
void testOf_WithStatusAndMessage_AndNullData() throws Exception { void testOf_WithStatusAndMessage_AndNullData() throws Exception {
final UnifiedResponse ofWithStatusAndMessageAndNullData = UnifiedResponse.of(108, "This is a message.", null); final UnifiedResponse<Void> ofWithStatusAndMessageAndNullData = UnifiedResponse.of("108", "This is a message.", null);
assertEquals(108, ofWithStatusAndMessageAndNullData.getStatus()); assertEquals("108", ofWithStatusAndMessageAndNullData.getCode());
assertEquals("This is a message.", ofWithStatusAndMessageAndNullData.getMessage()); assertEquals("This is a message.", ofWithStatusAndMessageAndNullData.getMessage());
assertNull(ofWithStatusAndMessageAndNullData.getData()); assertNull(ofWithStatusAndMessageAndNullData.getData());
final String jacksonOfWithStatusAndMessageAndNullData = jackson.writeValueAsString(ofWithStatusAndMessageAndNullData); final String jacksonOfWithStatusAndMessageAndNullData = jackson.writeValueAsString(ofWithStatusAndMessageAndNullData);
log.info("jacksonOfWithStatusAndMessage: {}", jacksonOfWithStatusAndMessageAndNullData); log.info("jacksonOfWithStatusAndMessage: {}", jacksonOfWithStatusAndMessageAndNullData);
assertEquals("{\"status\":108,\"message\":\"This is a message.\"}", jacksonOfWithStatusAndMessageAndNullData); assertEquals("{\"code\":\"108\",\"message\":\"This is a message.\"}", jacksonOfWithStatusAndMessageAndNullData);
assertEquals("{status: 108, message: \"This is a message.\", data: null}", ofWithStatusAndMessageAndNullData.toString()); assertEquals("{code: \"108\", message: \"This is a message.\", data: null}", ofWithStatusAndMessageAndNullData.toString());
final String gsonOfWithStatusAndMessageAndNullData = gson.toJson(ofWithStatusAndMessageAndNullData); final String gsonOfWithStatusAndMessageAndNullData = gson.toJson(ofWithStatusAndMessageAndNullData);
assertEquals("{\"status\":108,\"message\":\"This is a message.\"}", gsonOfWithStatusAndMessageAndNullData); assertEquals("{\"code\":\"108\",\"message\":\"This is a message.\"}", gsonOfWithStatusAndMessageAndNullData);
} }
@Test @Test
void testOf_WithStatusAndMessage_AndData() throws Exception { void testOf_WithStatusAndMessage_AndData() throws Exception {
final PageResult<User> emptyPageResult = PageResult.empty(); final PageResult<User> emptyPageResult = PageResult.empty();
final UnifiedResponse ofWithStatusAndMessageAndData final UnifiedResponse<PageResult<User>> ofWithStatusAndMessageAndData
= UnifiedResponse.of(108, "This is a message.", emptyPageResult); = UnifiedResponse.of("108", "This is a message.", emptyPageResult);
assertEquals("{status: 108, message: \"This is a message.\", data: PageResult [total=0, content=[]]}", assertEquals("{code: \"108\", message: \"This is a message.\", data: PageResult [total=0, content=[]]}",
ofWithStatusAndMessageAndData.toString()); ofWithStatusAndMessageAndData.toString());
assertEquals(108, ofWithStatusAndMessageAndData.getStatus()); assertEquals("108", ofWithStatusAndMessageAndData.getCode());
assertEquals("This is a message.", ofWithStatusAndMessageAndData.getMessage()); assertEquals("This is a message.", ofWithStatusAndMessageAndData.getMessage());
assertEquals(emptyPageResult, ofWithStatusAndMessageAndData.getData()); assertEquals(emptyPageResult, ofWithStatusAndMessageAndData.getData());
final String jacksonOfWithStatusAndMessageAndData = jackson.writeValueAsString(ofWithStatusAndMessageAndData); final String jacksonOfWithStatusAndMessageAndData = jackson.writeValueAsString(ofWithStatusAndMessageAndData);
assertEquals("{\"status\":108,\"message\":\"This is a message.\",\"data\":{\"total\":0,\"content\":[]}}", assertEquals("{\"code\":\"108\",\"message\":\"This is a message.\",\"data\":{\"total\":0,\"content\":[]}}",
jacksonOfWithStatusAndMessageAndData); jacksonOfWithStatusAndMessageAndData);
final String gsonOfWithStatusAndMessageAndData = gson.toJson(ofWithStatusAndMessageAndData); final String gsonOfWithStatusAndMessageAndData = gson.toJson(ofWithStatusAndMessageAndData);
assertEquals("{\"status\":108,\"message\":\"This is a message.\",\"data\":{\"total\":0,\"content\":[]}}", assertEquals("{\"code\":\"108\",\"message\":\"This is a message.\",\"data\":{\"total\":0,\"content\":[]}}",
gsonOfWithStatusAndMessageAndData); gsonOfWithStatusAndMessageAndData);
} }
@Test @Test
void testOf_WithStatusAndNullMessage() throws Exception { void testOf_WithStatusAndNullMessage() throws Exception {
UnifiedResponse ofWithStatusAndNullMessage = UnifiedResponse.of(108, (String) null); UnifiedResponse<Void> ofWithStatusAndNullMessage = UnifiedResponse.of("108", (String) null);
assertEquals(108, ofWithStatusAndNullMessage.getStatus()); assertEquals("108", ofWithStatusAndNullMessage.getCode());
assertEquals("", ofWithStatusAndNullMessage.getMessage()); assertEquals("", ofWithStatusAndNullMessage.getMessage());
assertNull(ofWithStatusAndNullMessage.getData()); assertNull(ofWithStatusAndNullMessage.getData());
final String jacksonOfWithStatusAndNullMessage = jackson.writeValueAsString(ofWithStatusAndNullMessage); final String jacksonOfWithStatusAndNullMessage = jackson.writeValueAsString(ofWithStatusAndNullMessage);
assertEquals("{\"status\":108,\"message\":\"\"}", jacksonOfWithStatusAndNullMessage); assertEquals("{\"code\":\"108\",\"message\":\"\"}", jacksonOfWithStatusAndNullMessage);
final String gsonOfWithStatusAndNullMessage = gson.toJson(ofWithStatusAndNullMessage); final String gsonOfWithStatusAndNullMessage = gson.toJson(ofWithStatusAndNullMessage);
assertEquals("{\"status\":108,\"message\":\"\"}", gsonOfWithStatusAndNullMessage); assertEquals("{\"code\":\"108\",\"message\":\"\"}", gsonOfWithStatusAndNullMessage);
} }
@Test @Test
void testOf_WithStatusAndNullMessage_AndNullData() throws Exception { void testOf_WithStatusAndNullMessage_AndNullData() throws Exception {
UnifiedResponse ofWithStatusAndNullMessageAndNullData = UnifiedResponse.of(108, (String) null, null); UnifiedResponse<Void> ofWithStatusAndNullMessageAndNullData = UnifiedResponse.of("108", (String) null, null);
assertEquals(108, ofWithStatusAndNullMessageAndNullData.getStatus()); assertEquals("108", ofWithStatusAndNullMessageAndNullData.getCode());
assertEquals("", ofWithStatusAndNullMessageAndNullData.getMessage()); assertEquals("", ofWithStatusAndNullMessageAndNullData.getMessage());
assertNull(ofWithStatusAndNullMessageAndNullData.getData()); assertNull(ofWithStatusAndNullMessageAndNullData.getData());
final String jacksonOfWithStatusAndNullMessageAndNullData = jackson.writeValueAsString(ofWithStatusAndNullMessageAndNullData); final String jacksonOfWithStatusAndNullMessageAndNullData = jackson.writeValueAsString(ofWithStatusAndNullMessageAndNullData);
assertEquals("{\"status\":108,\"message\":\"\"}", jacksonOfWithStatusAndNullMessageAndNullData); assertEquals("{\"code\":\"108\",\"message\":\"\"}", jacksonOfWithStatusAndNullMessageAndNullData);
final String gsonOfWithStatusAndNullMessageAndNullData = gson.toJson(ofWithStatusAndNullMessageAndNullData); final String gsonOfWithStatusAndNullMessageAndNullData = gson.toJson(ofWithStatusAndNullMessageAndNullData);
assertEquals("{\"status\":108,\"message\":\"\"}", gsonOfWithStatusAndNullMessageAndNullData); assertEquals("{\"code\":\"108\",\"message\":\"\"}", gsonOfWithStatusAndNullMessageAndNullData);
} }
@Test @Test
void testOf_WithStatusAndNullMessage_AndData() throws Exception { void testOf_WithStatusAndNullMessage_AndData() throws Exception {
PageResult<User> emptyPageResult = PageResult.empty(); PageResult<User> emptyPageResult = PageResult.empty();
UnifiedResponse ofWithStatusAndNullMessageAndData = UnifiedResponse.of(108, (String) null, emptyPageResult); UnifiedResponse<PageResult<User>> ofWithStatusAndNullMessageAndData = UnifiedResponse.of("108", (String) null, emptyPageResult);
assertEquals(108, ofWithStatusAndNullMessageAndData.getStatus()); assertEquals("108", ofWithStatusAndNullMessageAndData.getCode());
assertEquals("", ofWithStatusAndNullMessageAndData.getMessage()); assertEquals("", ofWithStatusAndNullMessageAndData.getMessage());
assertEquals(emptyPageResult, ofWithStatusAndNullMessageAndData.getData()); assertEquals(emptyPageResult, ofWithStatusAndNullMessageAndData.getData());
final String jacksonOfWithStatusAndNullMessageAndData = jackson.writeValueAsString(ofWithStatusAndNullMessageAndData); final String jacksonOfWithStatusAndNullMessageAndData = jackson.writeValueAsString(ofWithStatusAndNullMessageAndData);
assertEquals("{\"status\":108,\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", jacksonOfWithStatusAndNullMessageAndData); assertEquals("{\"code\":\"108\",\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", jacksonOfWithStatusAndNullMessageAndData);
final String gsonOfWithStatusAndNullMessageAndData = gson.toJson(ofWithStatusAndNullMessageAndData); final String gsonOfWithStatusAndNullMessageAndData = gson.toJson(ofWithStatusAndNullMessageAndData);
assertEquals("{\"status\":108,\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", gsonOfWithStatusAndNullMessageAndData); assertEquals("{\"code\":\"108\",\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", gsonOfWithStatusAndNullMessageAndData);
} }
@Test @Test
void testOf_WithStatusAndEmptyMessage() throws Exception { void testOf_WithStatusAndEmptyMessage() throws Exception {
UnifiedResponse ofWithStatusAndEmptyMessage = UnifiedResponse.of(108, ""); UnifiedResponse<Void> ofWithStatusAndEmptyMessage = UnifiedResponse.of("108", "");
assertEquals(108, ofWithStatusAndEmptyMessage.getStatus()); assertEquals("108", ofWithStatusAndEmptyMessage.getCode());
assertEquals("", ofWithStatusAndEmptyMessage.getMessage()); assertEquals("", ofWithStatusAndEmptyMessage.getMessage());
assertNull(ofWithStatusAndEmptyMessage.getData()); assertNull(ofWithStatusAndEmptyMessage.getData());
final String jacksonOfWithStatusAndEmptyMessage = jackson.writeValueAsString(ofWithStatusAndEmptyMessage); final String jacksonOfWithStatusAndEmptyMessage = jackson.writeValueAsString(ofWithStatusAndEmptyMessage);
assertEquals("{\"status\":108,\"message\":\"\"}", jacksonOfWithStatusAndEmptyMessage); assertEquals("{\"code\":\"108\",\"message\":\"\"}", jacksonOfWithStatusAndEmptyMessage);
final String gsonOfWithStatusAndEmptyMessage = gson.toJson(ofWithStatusAndEmptyMessage); final String gsonOfWithStatusAndEmptyMessage = gson.toJson(ofWithStatusAndEmptyMessage);
assertEquals("{\"status\":108,\"message\":\"\"}", gsonOfWithStatusAndEmptyMessage); assertEquals("{\"code\":\"108\",\"message\":\"\"}", gsonOfWithStatusAndEmptyMessage);
} }
@Test @Test
void testOf_WithStatusAndEmptyMessage_AndNullData() throws Exception { void testOf_WithStatusAndEmptyMessage_AndNullData() throws Exception {
UnifiedResponse ofWithStatusAndEmptyMessageAndNullData = UnifiedResponse.of(108, "", null); UnifiedResponse<Void> ofWithStatusAndEmptyMessageAndNullData = UnifiedResponse.of("108", "", null);
assertEquals(108, ofWithStatusAndEmptyMessageAndNullData.getStatus()); assertEquals("108", ofWithStatusAndEmptyMessageAndNullData.getCode());
assertEquals("", ofWithStatusAndEmptyMessageAndNullData.getMessage()); assertEquals("", ofWithStatusAndEmptyMessageAndNullData.getMessage());
assertNull(ofWithStatusAndEmptyMessageAndNullData.getData()); assertNull(ofWithStatusAndEmptyMessageAndNullData.getData());
final String jacksonOfWithStatusAndEmptyMessageAndNullData = jackson.writeValueAsString(ofWithStatusAndEmptyMessageAndNullData); final String jacksonOfWithStatusAndEmptyMessageAndNullData = jackson.writeValueAsString(ofWithStatusAndEmptyMessageAndNullData);
assertEquals("{\"status\":108,\"message\":\"\"}", jacksonOfWithStatusAndEmptyMessageAndNullData); assertEquals("{\"code\":\"108\",\"message\":\"\"}", jacksonOfWithStatusAndEmptyMessageAndNullData);
final String gsonOfWithStatusAndEmptyMessageAndNullData = gson.toJson(ofWithStatusAndEmptyMessageAndNullData); final String gsonOfWithStatusAndEmptyMessageAndNullData = gson.toJson(ofWithStatusAndEmptyMessageAndNullData);
assertEquals("{\"status\":108,\"message\":\"\"}", gsonOfWithStatusAndEmptyMessageAndNullData); assertEquals("{\"code\":\"108\",\"message\":\"\"}", gsonOfWithStatusAndEmptyMessageAndNullData);
} }
@Test @Test
void testOf_WithStatusAndEmptyMessage_AndData() throws Exception { void testOf_WithStatusAndEmptyMessage_AndData() throws Exception {
PageResult<User> emptyPageResult = PageResult.empty(); PageResult<User> emptyPageResult = PageResult.empty();
UnifiedResponse ofWithStatusAndEmptyMessageAndData = UnifiedResponse.of(108, "", emptyPageResult); UnifiedResponse<PageResult<User>> ofWithStatusAndEmptyMessageAndData = UnifiedResponse.of("108", "", emptyPageResult);
assertEquals(108, ofWithStatusAndEmptyMessageAndData.getStatus()); assertEquals("108", ofWithStatusAndEmptyMessageAndData.getCode());
assertEquals("", ofWithStatusAndEmptyMessageAndData.getMessage()); assertEquals("", ofWithStatusAndEmptyMessageAndData.getMessage());
assertEquals(emptyPageResult, ofWithStatusAndEmptyMessageAndData.getData()); assertEquals(emptyPageResult, ofWithStatusAndEmptyMessageAndData.getData());
final String jacksonOfWithStatusAndEmptyMessageAndData = jackson.writeValueAsString(ofWithStatusAndEmptyMessageAndData); final String jacksonOfWithStatusAndEmptyMessageAndData = jackson.writeValueAsString(ofWithStatusAndEmptyMessageAndData);
assertEquals("{\"status\":108,\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", jacksonOfWithStatusAndEmptyMessageAndData); assertEquals("{\"code\":\"108\",\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", jacksonOfWithStatusAndEmptyMessageAndData);
final String gsonOfWithStatusAndEmptyMessageAndData = gson.toJson(ofWithStatusAndEmptyMessageAndData); final String gsonOfWithStatusAndEmptyMessageAndData = gson.toJson(ofWithStatusAndEmptyMessageAndData);
assertEquals("{\"status\":108,\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", gsonOfWithStatusAndEmptyMessageAndData); assertEquals("{\"code\":\"108\",\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", gsonOfWithStatusAndEmptyMessageAndData);
} }
@Test @Test
void testOf_WithNullStatus() { void testOf_WithNullStatus() {
final Object nullStatus = null; final String nullStatus = null;
final String nullMessage = null; final String nullMessage = null;
final User user = new User("zhouxy", "zhouxy@gmail.com"); final User user = new User("zhouxy", "zhouxy@gmail.com");

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2024 the original author or authors. * Copyright 2024-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -1173,19 +1173,4 @@ public class YearQuarterTests {
} }
} }
} }
@ParameterizedTest
@ValueSource(ints = { -1, 0, 1, 1900, 2000, 2023, 2024, Year.MAX_VALUE, Year.MIN_VALUE })
void test_min_And_max_sameYear(int year) {
YearQuarter yq1 = YearQuarter.of(year, 1);
YearQuarter anotherYq1 = YearQuarter.of(year, 1);
assertEquals(yq1, YearQuarter.max(yq1, anotherYq1));
assertEquals(yq1, YearQuarter.min(yq1, anotherYq1));
YearQuarter yq2 = YearQuarter.of(year, 2);
assertEquals(yq2, YearQuarter.max(yq1, yq2));
assertEquals(yq1, YearQuarter.min(yq1, yq2));
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2024 the original author or authors. * Copyright 2024-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,9 +20,11 @@ import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.reflect.Constructor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -1087,4 +1089,26 @@ public class ArrayToolsTests {
// #endregion // #endregion
// ================================ // ================================
// ================================
// #region - invoke constructor
// ================================
@Test
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
Constructor<?>[] constructors = ArrayTools.class.getDeclaredConstructors();
Arrays.stream(constructors)
.forEach(constructor -> {
assertFalse(constructor.isAccessible());
constructor.setAccessible(true);
Throwable cause = assertThrows(Exception.class, constructor::newInstance)
.getCause();
assertInstanceOf(IllegalStateException.class, cause);
assertEquals("Utility class", cause.getMessage());
});
}
// ================================
// #endregion - invoke constructor
// ================================
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2024 the original author or authors. * Copyright 2024-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,7 +18,9 @@ package xyz.zhouxy.plusone.commons.util;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import java.lang.reflect.Constructor;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.Arrays;
import java.util.Optional; import java.util.Optional;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -939,4 +941,25 @@ public class AssertToolsTests {
// #endregion - Condition // #endregion - Condition
// ================================
// #region - invoke constructor
// ================================
@Test
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
Constructor<?>[] constructors = AssertTools.class.getDeclaredConstructors();
Arrays.stream(constructors)
.forEach(constructor -> {
assertFalse(constructor.isAccessible());
constructor.setAccessible(true);
Throwable cause = assertThrows(Exception.class, constructor::newInstance)
.getCause();
assertInstanceOf(IllegalStateException.class, cause);
assertEquals("Utility class", cause.getMessage());
});
}
// ================================
// #endregion - invoke constructor
// ================================
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2024 the original author or authors. * Copyright 2024-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,7 +18,9 @@ package xyz.zhouxy.plusone.commons.util;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import java.lang.reflect.Constructor;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Arrays;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -202,4 +204,17 @@ public class BigDecimalsTests {
assertEquals(bd, BigDecimals.of("10")); assertEquals(bd, BigDecimals.of("10"));
} }
@Test
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
Constructor<?>[] constructors = BigDecimals.class.getDeclaredConstructors();
Arrays.stream(constructors)
.forEach(constructor -> {
assertFalse(constructor.isAccessible());
constructor.setAccessible(true);
Throwable cause = assertThrows(Exception.class, constructor::newInstance)
.getCause();
assertInstanceOf(IllegalStateException.class, cause);
assertEquals("Utility class", cause.getMessage());
});
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2023-2024 the original author or authors. * Copyright 2023-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,10 +18,12 @@ package xyz.zhouxy.plusone.commons.util;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.reflect.Constructor;
import java.time.DateTimeException; import java.time.DateTimeException;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDate; import java.time.LocalDate;
@ -32,6 +34,7 @@ import java.time.Year;
import java.time.YearMonth; import java.time.YearMonth;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.TimeZone; import java.util.TimeZone;
@ -493,4 +496,27 @@ class DateTimeToolsTests {
// ================================ // ================================
// #endregion - toString // #endregion - toString
// ================================ // ================================
// ================================
// #region - invoke constructor
// ================================
@Test
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
Constructor<?>[] constructors = DateTimeTools.class.getDeclaredConstructors();
Arrays.stream(constructors)
.forEach(constructor -> {
assertFalse(constructor.isAccessible());
constructor.setAccessible(true);
Throwable cause = assertThrows(Exception.class, constructor::newInstance)
.getCause();
assertInstanceOf(IllegalStateException.class, cause);
assertEquals("Utility class", cause.getMessage());
});
}
// ================================
// #endregion - invoke constructor
// ================================
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2024 the original author or authors. * Copyright 2024-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,10 +17,15 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import java.lang.reflect.Constructor;
import java.util.Arrays;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource; import org.junit.jupiter.params.provider.ValueSource;
@ -222,4 +227,18 @@ class EnumToolsTests {
assertSame(MyEnum.VALUE_0, EnumTools.valueOf(MyEnum.class, null, MyEnum.VALUE_0)); assertSame(MyEnum.VALUE_0, EnumTools.valueOf(MyEnum.class, null, MyEnum.VALUE_0));
assertNull(EnumTools.valueOf(MyEnum.class, null, null)); assertNull(EnumTools.valueOf(MyEnum.class, null, null));
} }
@Test
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
Constructor<?>[] constructors = EnumTools.class.getDeclaredConstructors();
Arrays.stream(constructors)
.forEach(constructor -> {
assertFalse(constructor.isAccessible());
constructor.setAccessible(true);
Throwable cause = assertThrows(Exception.class, constructor::newInstance)
.getCause();
assertInstanceOf(IllegalStateException.class, cause);
assertEquals("Utility class", cause.getMessage());
});
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2024 the original author or authors. * Copyright 2024-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,7 +17,12 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
@ -83,4 +88,17 @@ public class IdGeneratorTests {
IdGenerator.toSimpleString(id)); IdGenerator.toSimpleString(id));
} }
@Test
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
Constructor<?>[] constructors = IdGenerator.class.getDeclaredConstructors();
Arrays.stream(constructors)
.forEach(constructor -> {
assertFalse(constructor.isAccessible());
constructor.setAccessible(true);
Throwable cause = assertThrows(Exception.class, constructor::newInstance)
.getCause();
assertInstanceOf(IllegalStateException.class, cause);
assertEquals("Utility class", cause.getMessage());
});
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2024 the original author or authors. * Copyright 2024-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,9 +17,16 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.lang.reflect.Constructor;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
public public
class NumbersTests { class NumbersTests {
@ -184,4 +191,18 @@ class NumbersTests {
BigDecimal result = Numbers.nullToZero(value); BigDecimal result = Numbers.nullToZero(value);
assertEquals(BigDecimal.ZERO, result); assertEquals(BigDecimal.ZERO, result);
} }
@Test
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
Constructor<?>[] constructors = Numbers.class.getDeclaredConstructors();
Arrays.stream(constructors)
.forEach(constructor -> {
assertFalse(constructor.isAccessible());
constructor.setAccessible(true);
Throwable cause = assertThrows(Exception.class, constructor::newInstance)
.getCause();
assertInstanceOf(IllegalStateException.class, cause);
assertEquals("Utility class", cause.getMessage());
});
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2024 the original author or authors. * Copyright 2024-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,10 +18,13 @@ package xyz.zhouxy.plusone.commons.util;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.Optional; import java.util.Optional;
import java.util.OptionalDouble; import java.util.OptionalDouble;
import java.util.OptionalInt; import java.util.OptionalInt;
@ -184,4 +187,18 @@ class OptionalToolsTests {
Double result = OptionalTools.toDouble(OptionalDouble.of(10.0)); Double result = OptionalTools.toDouble(OptionalDouble.of(10.0));
assertEquals(10.0, result, 0.0001); assertEquals(10.0, result, 0.0001);
} }
@Test
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
Constructor<?>[] constructors = OptionalTools.class.getDeclaredConstructors();
Arrays.stream(constructors)
.forEach(constructor -> {
assertFalse(constructor.isAccessible());
constructor.setAccessible(true);
Throwable cause = assertThrows(Exception.class, constructor::newInstance)
.getCause();
assertInstanceOf(IllegalStateException.class, cause);
assertEquals("Utility class", cause.getMessage());
});
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2024 the original author or authors. * Copyright 2024-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,9 +18,13 @@ package xyz.zhouxy.plusone.commons.util;
import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import java.lang.reflect.Constructor;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Random; import java.util.Random;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
@ -102,4 +106,18 @@ public class RandomToolsTests {
String result = RandomTools.secureRandomStr(sourceCharactersString, 5); String result = RandomTools.secureRandomStr(sourceCharactersString, 5);
assertEquals(5, result.length()); assertEquals(5, result.length());
} }
@Test
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
Constructor<?>[] constructors = RandomTools.class.getDeclaredConstructors();
Arrays.stream(constructors)
.forEach(constructor -> {
assertFalse(constructor.isAccessible());
constructor.setAccessible(true);
Throwable cause = assertThrows(Exception.class, constructor::newInstance)
.getCause();
assertInstanceOf(IllegalStateException.class, cause);
assertEquals("Utility class", cause.getMessage());
});
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2024 the original author or authors. * Copyright 2024-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,6 +18,8 @@ package xyz.zhouxy.plusone.commons.util;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -162,4 +164,18 @@ class RegexToolsTests {
RegexTools.getMatcher("abc", pattern); RegexTools.getMatcher("abc", pattern);
}); });
} }
@Test
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
Constructor<?>[] constructors = RegexTools.class.getDeclaredConstructors();
Arrays.stream(constructors)
.forEach(constructor -> {
assertFalse(constructor.isAccessible());
constructor.setAccessible(true);
Throwable cause = assertThrows(Exception.class, constructor::newInstance)
.getCause();
assertInstanceOf(IllegalStateException.class, cause);
assertEquals("Utility class", cause.getMessage());
});
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2024 the original author or authors. * Copyright 2024-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,10 +18,14 @@ package xyz.zhouxy.plusone.commons.util;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.reflect.Constructor;
import java.util.Arrays;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public public
@ -84,4 +88,18 @@ class StringToolsTests {
void repeat_ZeroTimes_ReturnsEmptyString() { void repeat_ZeroTimes_ReturnsEmptyString() {
assertEquals("", StringTools.repeat("Hello", 0)); assertEquals("", StringTools.repeat("Hello", 0));
} }
@Test
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
Constructor<?>[] constructors = StringTools.class.getDeclaredConstructors();
Arrays.stream(constructors)
.forEach(constructor -> {
assertFalse(constructor.isAccessible());
constructor.setAccessible(true);
Throwable cause = assertThrows(Exception.class, constructor::newInstance)
.getCause();
assertInstanceOf(IllegalStateException.class, cause);
assertEquals("Utility class", cause.getMessage());
});
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2023-2024 the original author or authors. * Copyright 2023-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -62,7 +62,7 @@ class TreeBuilderTests {
Menu::getMenuCode, Menu::getMenuCode,
menu -> Optional.ofNullable(menu.parentMenuCode), menu -> Optional.ofNullable(menu.parentMenuCode),
MenuList::addChild, MenuList::addChild,
Menu.orderNumComparator); Comparator.comparing(Menu::getOrderNum));
@Test @Test
void testBuildTreeAndSortedByOrderNum() { void testBuildTreeAndSortedByOrderNum() {
@ -70,9 +70,10 @@ class TreeBuilderTests {
List<Menu> menuTreeSortedByOrderNum = treeBuilder.buildTree(clonedMenus); List<Menu> menuTreeSortedByOrderNum = treeBuilder.buildTree(clonedMenus);
log.info("menuTreeSortedByOrderNum: {}", new Gson().toJson(menuTreeSortedByOrderNum)); log.info("menuTreeSortedByOrderNum: {}", new Gson().toJson(menuTreeSortedByOrderNum));
assertEquals(clonedMenus.stream() assertEquals(
clonedMenus.stream()
.filter(menu -> menu.getParentMenuCode() == null) .filter(menu -> menu.getParentMenuCode() == null)
.sorted(Menu.orderNumComparator) .sorted(Comparator.comparing(Menu::getOrderNum))
.collect(Collectors.toList()), .collect(Collectors.toList()),
menuTreeSortedByOrderNum); menuTreeSortedByOrderNum);
@ -81,18 +82,21 @@ class TreeBuilderTests {
menuMap.put(element.getMenuCode(), element); menuMap.put(element.getMenuCode(), element);
} }
assertEquals(Arrays.stream(new Menu[] { B001, B002, B003, B004 }) assertEquals(
.sorted(Menu.orderNumComparator) Arrays.stream(new Menu[] { B001, B002, B003, B004 })
.sorted(Comparator.comparing(Menu::getOrderNum))
.collect(Collectors.toList()), .collect(Collectors.toList()),
MenuList.class.cast(menuMap.get("B")).children); MenuList.class.cast(menuMap.get("B")).children);
assertEquals(Arrays.stream(new Menu[] { C1, C2, C3 }) assertEquals(
.sorted(Menu.orderNumComparator) Arrays.stream(new Menu[] { C1, C2, C3 })
.sorted(Comparator.comparing(Menu::getOrderNum))
.collect(Collectors.toList()), .collect(Collectors.toList()),
MenuList.class.cast(menuMap.get("C")).children); MenuList.class.cast(menuMap.get("C")).children);
assertEquals(Arrays.stream(new Menu[] { C1001, C1002 }) assertEquals(
.sorted(Menu.orderNumComparator) Arrays.stream(new Menu[] { C1001, C1002 })
.sorted(Comparator.comparing(Menu::getOrderNum))
.collect(Collectors.toList()), .collect(Collectors.toList()),
MenuList.class.cast(menuMap.get("C1")).children); MenuList.class.cast(menuMap.get("C1")).children);
@ -103,14 +107,14 @@ class TreeBuilderTests {
List<Menu> clonedMenus; List<Menu> clonedMenus;
clonedMenus = menus.stream().map(ObjectUtil::clone).collect(Collectors.toList()); clonedMenus = menus.stream().map(ObjectUtil::clone).collect(Collectors.toList());
List<Menu> menuTreeSortedByMenuCode = treeBuilder.buildTree( List<Menu> menuTreeSortedByMenuCode = treeBuilder
clonedMenus, .buildTree(clonedMenus, Comparator.comparing(Menu::getMenuCode));
(a, b) -> a.getMenuCode().compareTo(b.getMenuCode()));
log.info("menuTreeSortedByMenuCode: {}", new Gson().toJson(menuTreeSortedByMenuCode)); log.info("menuTreeSortedByMenuCode: {}", new Gson().toJson(menuTreeSortedByMenuCode));
assertEquals(clonedMenus.stream() assertEquals(
clonedMenus.stream()
.filter(menu -> menu.getParentMenuCode() == null) .filter(menu -> menu.getParentMenuCode() == null)
.sorted((a, b) -> a.getMenuCode().compareTo(b.getMenuCode())) .sorted(Comparator.comparing(Menu::getMenuCode))
.collect(Collectors.toList()), .collect(Collectors.toList()),
menuTreeSortedByMenuCode); menuTreeSortedByMenuCode);
@ -160,9 +164,6 @@ class TreeBuilderTests {
return orderNum; return orderNum;
} }
public static Comparator<Menu> orderNumComparator =
(a, b) -> Integer.compare(a.getOrderNum(), b.getOrderNum());
private static final long serialVersionUID = 20240917181424L; private static final long serialVersionUID = 20240917181424L;
} }