28 Commits

Author SHA1 Message Date
746603f939 1.0.1 2025-03-28 12:10:44 +08:00
73a99b630c doc: 修改 since 信息
1.0.0 之前新增的,其 since 修改为 1.0.0,统一以 1.0.0 作为初始版本

fix plusone/plusone-commons#30
2025-03-28 11:35:12 +08:00
f5b04a7ae8 docs: 改正 PredicateTools 的 javadoc (#39)
Reviewed-on: http://zhouxy.xyz:3000/plusone/plusone-commons/pulls/39
2025-03-28 10:39:56 +08:00
dde3d1d172 docs: 删除 IdWorker 的 author 信息
Reviewed-on: http://zhouxy.xyz:3000/plusone/plusone-commons/pulls/37
2025-03-28 09:31:02 +08:00
aadd1857de docs: 改正 OptionalTools 的 javadoc
Reviewed-on: http://zhouxy.xyz:3000/plusone/plusone-commons/pulls/35
2025-03-28 09:08:23 +08:00
e98fe66b65 docs: fix javadoc
fix issue plusone/plusone-commons#29
2025-03-28 08:55:35 +08:00
15cea5fb4b v1.0.0 2025-02-20 23:14:56 +08:00
ff3f80a447 更改 copyright。 2025-02-20 23:05:27 +08:00
e5a57e03b4 新增 UnifiedResponses
Reviewed-on: http://zhouxy.xyz:3000/plusone/plusone-commons/pulls/24
2025-02-20 20:51:29 +08:00
faab942e13 新增 UnifiedResponse 工厂 UnifiedResponses。 2025-02-20 20:26:21 +08:00
69377f3e67 1.0.0-RC3
Merge pull request '1.x.x' (#20) from ZhouXY108/plusone-commons:1.x.x into 1.x.x
2025-02-14 18:59:31 +08:00
ec4efe5f0f Merge branch '1.x.x' into 1.x.x 2025-02-14 18:58:17 +08:00
d92861284c 整理依赖版本;修改版本号 1.0.0-RC3。 2025-02-14 18:27:43 +08:00
cd88892762 IdGenerator#toSimpleString 添加 @Nonnull 注解 2025-02-14 16:58:33 +08:00
e3ff5a2ab3 CollectionTools#isNotEmpty 支持 guava 的 Table、Multimap、Multiset 和 RangeSet 2025-02-14 16:57:45 +08:00
d217e8b9ac 优化 TreeBuilder 的单元测试代码。 2025-01-30 19:23:25 +08:00
669506e499 删除 YearQuarter#max 和 YearQuarter#min,使用 guava 的 Comparators#max 和 Comparators#min 即可。 2025-01-30 19:21:21 +08:00
79b8b81220 test: 测试工具类的私有构造器 2025-01-22 21:57:53 +08:00
52a4011eb1 更新版本号
Reviewed-on: http://zhouxy.xyz:3000/plusone/plusone-commons/pulls/18
2025-01-07 17:15:45 +08:00
0bcc5b895a 新增 ThrowingFunction
Reviewed-on: http://zhouxy.xyz:3000/plusone/plusone-commons/pulls/16
2025-01-07 17:09:52 +08:00
4c0f5095fa 新增 base 和 function 包的 package-info
Reviewed-on: http://zhouxy.xyz:3000/plusone/plusone-commons/pulls/15
2025-01-07 17:06:33 +08:00
044320a2a9 优化 Ref API
Reviewed-on: http://zhouxy.xyz:3000/plusone/plusone-commons/pulls/14
2025-01-07 17:01:57 +08:00
1c940a9c7c IWithCode 相关接口修改方法名,区分重载方法
Reviewed-on: http://zhouxy.xyz:3000/plusone/plusone-commons/pulls/13
2025-01-07 16:52:48 +08:00
5c3923f8af 修改版本号
Reviewed-on: http://zhouxy.xyz:3000/plusone/plusone-commons/pulls/12
2025-01-01 21:10:35 +08:00
0eee05d46a Merge pull request '完成单元测试' (#11) from ZhouXY108/plusone-commons:1.x.x into 1.x.x
Reviewed-on: http://zhouxy.xyz:3000/plusone/plusone-commons/pulls/11
2025-01-01 20:47:38 +08:00
300a2436b1 CollectionTools 新增 nullToEmptyList、nullToEmptySet、nullToEmptyMap 方法
Reviewed-on: http://zhouxy.xyz:3000/plusone/plusone-commons/pulls/10
2024-12-29 22:22:21 +08:00
a6bb069f8a 完成 EnumTools 单元测试
Reviewed-on: http://zhouxy.xyz:3000/plusone/plusone-commons/pulls/9
2024-12-29 22:20:35 +08:00
dbb2ef8c50 修正 InvalidInputExceptionTests 和 ParsingFailureExceptionTests
Merge pull request '修正 InvalidInputExceptionTests 和 ParsingFailureExceptionTests' (#8) from ZhouXY108/plusone-commons:temp/complete-exception-tests into 1.x.x
Reviewed-on: http://zhouxy.xyz:3000/plusone/plusone-commons/pulls/8
2024-12-29 22:18:22 +08:00
48 changed files with 1224 additions and 232 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.1</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

@@ -28,7 +28,7 @@ import java.lang.annotation.Target;
* 标识方法是读方法,如 getter。 * 标识方法是读方法,如 getter。
* *
* @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.0
* @see WriterMethod * @see WriterMethod
*/ */
@Target(ElementType.METHOD) @Target(ElementType.METHOD)

View File

@@ -27,7 +27,7 @@ import java.lang.annotation.Target;
* <p>标识方法为静态工厂方法 * <p>标识方法为静态工厂方法
* *
* @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 1.0.0
*/ */
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)

View File

@@ -29,7 +29,7 @@ import java.lang.annotation.Documented;
* *
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a> * @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @version 1.0 * @version 1.0
* @since 1.0 * @since 1.0.0
* @see UnsupportedOperationException * @see UnsupportedOperationException
*/ */
@Documented @Documented

View File

@@ -26,7 +26,7 @@ import java.lang.annotation.Target;
* ValueObject - 值对象 * ValueObject - 值对象
* *
* @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 1.0.0
*/ */
@Inherited @Inherited
@Target(ElementType.TYPE) @Target(ElementType.TYPE)

View File

@@ -26,7 +26,7 @@ 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>
* @since 0.1.0 * @since 1.0.0
*/ */
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)

View File

@@ -28,7 +28,7 @@ import java.lang.annotation.Target;
* 标识方法是写方法,如 setter。 * 标识方法是写方法,如 setter。
* *
* @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.0
* @see ReaderMethod * @see ReaderMethod
*/ */
@Target(ElementType.METHOD) @Target(ElementType.METHOD)

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,17 +25,22 @@ 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;
/** /**
* 集合工具类 * 集合工具类
* *
* @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 1.0.0
*/ */
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

@@ -20,7 +20,7 @@ package xyz.zhouxy.plusone.commons.exception;
* 数据不存在异常 * 数据不存在异常
* *
* @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 1.0.0
*/ */
public final class DataNotExistsException extends Exception { public final class DataNotExistsException extends Exception {

View File

@@ -36,7 +36,7 @@ import xyz.zhouxy.plusone.commons.exception.MultiTypesException.ExceptionType;
* </p> * </p>
* *
* @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 1.0.0
*/ */
public final class ParsingFailureException public final class ParsingFailureException
extends RuntimeException extends RuntimeException

View File

@@ -27,7 +27,7 @@ package xyz.zhouxy.plusone.commons.exception.business;
* <b>NOTE: 通常表示业务中的意外情况。如:用户错误输入、缺失必填字段、用户余额不足等。</b> * <b>NOTE: 通常表示业务中的意外情况。如:用户错误输入、缺失必填字段、用户余额不足等。</b>
* </p> * </p>
* @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 1.0.0
*/ */
public class BizException extends RuntimeException { public class BizException extends RuntimeException {

View File

@@ -32,7 +32,7 @@ import xyz.zhouxy.plusone.commons.exception.MultiTypesException;
* <b>NOTE: 属业务异常</b> * <b>NOTE: 属业务异常</b>
* </p> * </p>
* @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 1.0.0
*/ */
public final class InvalidInputException public final class InvalidInputException
extends RequestParamsException extends RequestParamsException

View File

@@ -24,7 +24,7 @@ package xyz.zhouxy.plusone.commons.exception.business;
* </p> * </p>
* *
* @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 1.0.0
*/ */
public class RequestParamsException extends BizException { public class RequestParamsException extends BizException {

View File

@@ -29,7 +29,7 @@ package xyz.zhouxy.plusone.commons.exception.system;
* 后续需要排查原因。 * 后续需要排查原因。
* </p> * </p>
* @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 1.0.0
*/ */
public final class DataOperationResultException extends SysException { public final class DataOperationResultException extends SysException {

View File

@@ -24,7 +24,7 @@ package xyz.zhouxy.plusone.commons.exception.system;
* </p> * </p>
* *
* @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 1.0.0
*/ */
public class NoAvailableMacFoundException extends SysException { public class NoAvailableMacFoundException extends SysException {
private static final long serialVersionUID = 152827098461071551L; private static final long serialVersionUID = 152827098461071551L;

View File

@@ -24,7 +24,7 @@ package xyz.zhouxy.plusone.commons.exception.system;
* </p> * </p>
* *
* @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 1.0.0
*/ */
public class SysException extends RuntimeException { public class SysException extends RuntimeException {

View File

@@ -24,9 +24,9 @@ import java.util.function.Supplier;
* *
* <p> * <p>
* 返回 {@code Optional&lt;T&gt;} 对象。 * 返回 {@code Optional&lt;T&gt;} 对象。
* *
* @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 1.0.0
* @see Optional * @see Optional
* @see Supplier * @see Supplier
*/ */

View File

@@ -19,14 +19,14 @@ package xyz.zhouxy.plusone.commons.function;
import java.util.function.Predicate; import java.util.function.Predicate;
/** /**
* Predicates * PredicateTools
* *
* <p> * <p>
* {@link Predicate} 相关操作。 * {@link Predicate} 相关操作。
* </p> * </p>
* *
* @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 1.0.0
* @see Predicate * @see Predicate
*/ */
public class PredicateTools { public class PredicateTools {
@@ -36,12 +36,12 @@ public class PredicateTools {
* 如将 {@code Objects::nonNull} 明确地指定为 {@code Predicate&lt;String&gt;} * 如将 {@code Objects::nonNull} 明确地指定为 {@code Predicate&lt;String&gt;}
* 使之可以链式调用 {@link Predicate#and(Predicate)}、{@link Predicate#or(Predicate)} * 使之可以链式调用 {@link Predicate#and(Predicate)}、{@link Predicate#or(Predicate)}
* 等方法,连接其它 {@code Predicate<? super T>} 对象。 * 等方法,连接其它 {@code Predicate<? super T>} 对象。
* *
* <pre> * <pre>
* Predicate&lt;String&gt; predicate = PredicateTools.&lt;String&gt;from(Objects::nonNull) * Predicate&lt;String&gt; predicate = PredicateTools.&lt;String&gt;from(Objects::nonNull)
* .and(StringUtils::isNotEmpty); * .and(StringUtils::isNotEmpty);
* </pre> * </pre>
* *
* @param <T> 目标类型 * @param <T> 目标类型
* @param predicate Lambda 表达式 * @param predicate Lambda 表达式
* @return 传入的表达式自动成为 {@link Predicate} 实例 * @return 传入的表达式自动成为 {@link Predicate} 实例

View File

@@ -26,7 +26,7 @@ import java.util.function.BiFunction;
* 接受类型为 T 和 U 的两个参数,返回 {@code Optional&lt;R&gt;} 对象。 * 接受类型为 T 和 U 的两个参数,返回 {@code Optional&lt;R&gt;} 对象。
* *
* @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 1.0.0
* @see Optional * @see Optional
* @see BiFunction * @see BiFunction
*/ */

View File

@@ -26,7 +26,7 @@ import java.util.function.Function;
* 接受类型为 T 的参数,返回 {@code Optional&lt;R&gt;} 对象。 * 接受类型为 T 的参数,返回 {@code Optional&lt;R&gt;} 对象。
* *
* @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 1.0.0
* @see Optional * @see Optional
* @see Function * @see Function
*/ */

View File

@@ -39,7 +39,7 @@ import xyz.zhouxy.plusone.commons.util.StringTools;
* </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 * @since 1.0.0
* @see xyz.zhouxy.plusone.commons.constant.PatternConsts#CHINESE_2ND_ID_CARD_NUMBER * @see xyz.zhouxy.plusone.commons.constant.PatternConsts#CHINESE_2ND_ID_CARD_NUMBER
*/ */
@ValueObject @ValueObject

View File

@@ -30,7 +30,7 @@ import xyz.zhouxy.plusone.commons.util.AssertTools;
* 带校验的字符串值对象 * 带校验的字符串值对象
* *
* @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 1.0.0
*/ */
public abstract class ValidatableStringRecord<T extends ValidatableStringRecord<T>> public abstract class ValidatableStringRecord<T extends ValidatableStringRecord<T>>
implements Comparable<T> { implements Comparable<T> {

View File

@@ -22,10 +22,6 @@ import javax.annotation.Nullable;
/** /**
* 统一结果,对返回给前端的数据进行封装。 * 统一结果,对返回给前端的数据进行封装。
* *
* <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 class UnifiedResponse<T> { public class UnifiedResponse<T> {
@@ -39,11 +35,11 @@ public class UnifiedResponse<T> {
// #region - Constructors // #region - Constructors
// ================================ // ================================
private UnifiedResponse(String code, @Nullable String message) { UnifiedResponse(String code, @Nullable String message) {
this(code, message, null); this(code, message, null);
} }
private UnifiedResponse(String code, @Nullable String message, @Nullable T data) { UnifiedResponse(String code, @Nullable String message, @Nullable T data) {
this.code = Objects.requireNonNull(code); this.code = Objects.requireNonNull(code);
this.message = message == null ? "" : message; this.message = message == null ? "" : message;
this.data = data; this.data = data;
@@ -53,65 +49,6 @@ public class UnifiedResponse<T> {
// #endregion - Constructors // #endregion - Constructors
// ================================ // ================================
public static final String SUCCESS_CODE = "2000000";
private static final String DEFAULT_SUCCESS_MSG = "SUCCESS";
// ================================
// #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 // #region - Getters
// ================================ // ================================

View File

@@ -0,0 +1,91 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.zhouxy.plusone.commons.model.dto;
import javax.annotation.Nullable;
/**
* UnifiedResponse 工厂
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
* @see UnifiedResponse
*/
public class UnifiedResponses {
public static final String SUCCESS_CODE = "2000000";
public static final String DEFAULT_SUCCESS_MSG = "SUCCESS";
// ================================
// #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
// ================================
protected UnifiedResponses() {
}
}

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

@@ -37,7 +37,7 @@ import javax.annotation.Nullable;
* </p> * </p>
* *
* @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 1.0.0
*/ */
public class ArrayTools { public class ArrayTools {

View File

@@ -31,7 +31,7 @@ import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
* </p> * </p>
* *
* @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 1.0.0
*/ */
public class BigDecimals { public class BigDecimals {

View File

@@ -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 生成器
* *
@@ -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

@@ -26,13 +26,13 @@ import javax.annotation.Nullable;
import com.google.common.annotations.Beta; import com.google.common.annotations.Beta;
/** /**
* OptionalUtil * OptionalTools
* *
* <p> * <p>
* 提供一些 Optional 相关的方法 * 提供一些 Optional 相关的方法
* *
* @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 1.0.0
* @see Optional * @see Optional
* @see OptionalInt * @see OptionalInt
* @see OptionalLong * @see OptionalLong

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.
@@ -31,7 +31,7 @@ import javax.annotation.Nullable;
* TreeBuilder * TreeBuilder
* *
* @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.0
*/ */
public class TreeBuilder<T, TSubTree extends T, TIdentity> { public class TreeBuilder<T, TSubTree extends T, TIdentity> {
private final Function<T, TIdentity> identityGetter; private final Function<T, TIdentity> identityGetter;

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

@@ -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

@@ -0,0 +1,577 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.zhouxy.plusone.commons.model.dto;
import static org.junit.jupiter.api.Assertions.*;
import javax.annotation.Nullable;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.commons.exception.business.BizException;
@Slf4j
public
class CustomUnifiedResponseFactoryTests {
static final ObjectMapper jackson = new ObjectMapper();
static final Gson gson = new Gson();
static final PageResult<User> pageResult = PageResult.of(Lists.newArrayList(
new User("zhouxy1", "zhouxy1@gmail.com"),
new User("zhouxy2", "zhouxy2@gmail.com")
), 108);
static {
jackson.setSerializationInclusion(Include.NON_NULL);
}
@Test
void testSuccess_WithoutArgument() throws Exception {
// 1. success without argument
UnifiedResponse<Void> success = CustomUnifiedResponses.success();
assertEquals("0000000", success.getCode());
assertEquals("成功", success.getMessage());
assertNull(success.getData());
String jacksonSuccess = jackson.writeValueAsString(success);
log.info("jacksonSuccess: {}", jacksonSuccess);
assertEquals("{\"code\":\"0000000\",\"message\":\"成功\"}", jacksonSuccess);
}
@Test
void testSuccess_WithMessage() throws Exception {
// 2. success with message
UnifiedResponse<Void> successWithMessage = CustomUnifiedResponses.success("成功");
assertEquals("0000000", successWithMessage.getCode());
assertEquals("成功", successWithMessage.getMessage());
assertNull(successWithMessage.getData());
String jacksonSuccessWithMessage = jackson.writeValueAsString(successWithMessage);
log.info("jacksonSuccessWithMessage: {}", jacksonSuccessWithMessage);
assertEquals("{\"code\":\"0000000\",\"message\":\"成功\"}", jacksonSuccessWithMessage);
}
@Test
void testSuccess_WithMessageAndNullData() throws Exception {
// success with message and null data
final UnifiedResponse<Void> successWithMessageAndNullData = CustomUnifiedResponses.success("查询成功", null);
assertEquals("0000000", successWithMessageAndNullData.getCode());
assertEquals("查询成功", successWithMessageAndNullData.getMessage());
assertNull(successWithMessageAndNullData.getData());
final String jacksonSuccessWithMessageAndNullData = jackson.writeValueAsString(successWithMessageAndNullData);
log.info("jacksonSuccessWithMessageAndNullData: {}", jacksonSuccessWithMessageAndNullData);
assertEquals("{\"code\":\"0000000\",\"message\":\"查询成功\"}", jacksonSuccessWithMessageAndNullData);
assertEquals("{code: \"0000000\", message: \"查询成功\", data: null}", successWithMessageAndNullData.toString());
}
@Test
void testSuccess_WithMessageAndStringData() throws Exception {
UnifiedResponse<String> successWithStringData = CustomUnifiedResponses.success("查询成功", "zhouxy");
assertEquals("0000000", successWithStringData.getCode());
assertEquals("查询成功", successWithStringData.getMessage());
assertEquals("zhouxy", successWithStringData.getData());
String jacksonSuccessWithStringData = jackson.writeValueAsString(successWithStringData);
log.info("jacksonSuccessWithStringData: {}", jacksonSuccessWithStringData);
assertEquals("{\"code\":\"0000000\",\"message\":\"查询成功\",\"data\":\"zhouxy\"}", jacksonSuccessWithStringData);
assertEquals("{code: \"0000000\", message: \"查询成功\", data: \"zhouxy\"}", successWithStringData.toString());
}
@Test
void testSuccess_WithMessageAndIntegerData() throws Exception {
final UnifiedResponse<Integer> successWithIntegerData = CustomUnifiedResponses.success("查询成功", 1);
assertEquals("0000000", successWithIntegerData.getCode());
assertEquals("查询成功", successWithIntegerData.getMessage());
assertEquals(1, successWithIntegerData.getData());
final String jacksonSuccessWithIntegerData = jackson.writeValueAsString(successWithIntegerData);
log.info("jacksonSuccessWithIntegerData: {}", jacksonSuccessWithIntegerData);
assertEquals("{\"code\":\"0000000\",\"message\":\"查询成功\",\"data\":1}", jacksonSuccessWithIntegerData);
assertEquals("{code: \"0000000\", message: \"查询成功\", data: 1}", successWithIntegerData.toString());
}
@Test
void testSuccess_WithMessageAndData() throws Exception {
UnifiedResponse<PageResult<User>> successWithData = CustomUnifiedResponses.success("查询成功", pageResult);
assertEquals("0000000", successWithData.getCode());
assertEquals("查询成功", successWithData.getMessage());
assertNotNull(successWithData.getData());
assertEquals(pageResult, successWithData.getData());
String jacksonSuccessWithData = jackson.writeValueAsString(successWithData);
log.info("jacksonSuccessWithData: {}", jacksonSuccessWithData);
assertEquals("{\"code\":\"0000000\",\"message\":\"查询成功\",\"data\":{\"total\":108,\"content\":[{\"username\":\"zhouxy1\",\"email\":\"zhouxy1@gmail.com\"},{\"username\":\"zhouxy2\",\"email\":\"zhouxy2@gmail.com\"}]}}", jacksonSuccessWithData);
}
@Test
void testSuccess_WithNullMessage() throws Exception {
// 3. success with null message
UnifiedResponse<Void> successWithNullMessage = CustomUnifiedResponses.success(null);
assertEquals("0000000", successWithNullMessage.getCode());
assertEquals("", successWithNullMessage.getMessage());
assertNull(successWithNullMessage.getData());
String jacksonSuccessWithNullMessage = jackson.writeValueAsString(successWithNullMessage);
log.info("jacksonSuccessWithNullMessage: {}", jacksonSuccessWithNullMessage);
assertEquals("{\"code\":\"0000000\",\"message\":\"\"}", jacksonSuccessWithNullMessage);
}
// success with null message and null data
@Test
void testSuccess_WithNullMessageAndNullData() throws Exception {
final UnifiedResponse<Void> successWithNullMessageAndNullData = CustomUnifiedResponses.success(null, null);
assertEquals("0000000", successWithNullMessageAndNullData.getCode());
assertEquals("", successWithNullMessageAndNullData.getMessage());
assertNull(successWithNullMessageAndNullData.getData());
final String jacksonSuccessWithNullMessageAndNullData = jackson.writeValueAsString(successWithNullMessageAndNullData);
log.info("jacksonSuccessWithNullMessageAndNullData: {}", jacksonSuccessWithNullMessageAndNullData);
assertEquals("{\"code\":\"0000000\",\"message\":\"\"}", jacksonSuccessWithNullMessageAndNullData);
assertEquals("{code: \"0000000\", message: \"\", data: null}", successWithNullMessageAndNullData.toString());
}
@Test
void testSuccess_WithNullMessageAndData() throws Exception {
// success with null message and data
final User user = new User("zhouxy", "zhouxy@code108.cn");
final UnifiedResponse<User> successWithNullMessageAndData = CustomUnifiedResponses.success(null, user);
assertEquals("0000000", successWithNullMessageAndData.getCode());
assertEquals("", successWithNullMessageAndData.getMessage());
assertEquals(user, successWithNullMessageAndData.getData());
final String jacksonSuccessWithNullMessageAndData = jackson.writeValueAsString(successWithNullMessageAndData);
log.info("jacksonSuccessWithNullMessageAndData: {}", jacksonSuccessWithNullMessageAndData);
assertEquals("{\"code\":\"0000000\",\"message\":\"\",\"data\":{\"username\":\"zhouxy\",\"email\":\"zhouxy@code108.cn\"}}",
jacksonSuccessWithNullMessageAndData);
}
@Test
void testSuccess_WithEmptyMessage() throws Exception {
// 4. success with empty message
UnifiedResponse<Void> successWithEmptyMessage = CustomUnifiedResponses.success("");
assertEquals("0000000", successWithEmptyMessage.getCode());
assertEquals("", successWithEmptyMessage.getMessage());
assertNull(successWithEmptyMessage.getData());
String jacksonSuccessWithEmptyMessage = jackson.writeValueAsString(successWithEmptyMessage);
log.info("jacksonSuccessWithEmptyMessage: {}", jacksonSuccessWithEmptyMessage);
assertEquals("{\"code\":\"0000000\",\"message\":\"\"}", jacksonSuccessWithEmptyMessage);
}
// success with empty message and null data
@Test
void testSuccess_WithEmptyMessageAndNullData() throws Exception {
final UnifiedResponse<Void> successWithEmptyMessageAndNullData = CustomUnifiedResponses.success("", null);
assertEquals("0000000", successWithEmptyMessageAndNullData.getCode());
assertEquals("", successWithEmptyMessageAndNullData.getMessage());
assertNull(successWithEmptyMessageAndNullData.getData());
final String jacksonSuccessWithEmptyMessageAndNullData = jackson.writeValueAsString(successWithEmptyMessageAndNullData);
log.info("jacksonSuccessWithEmptyMessageAndNullData: {}", jacksonSuccessWithEmptyMessageAndNullData);
assertEquals("{\"code\":\"0000000\",\"message\":\"\"}", jacksonSuccessWithEmptyMessageAndNullData);
assertEquals("{code: \"0000000\", message: \"\", data: null}", successWithEmptyMessageAndNullData.toString());
}
// success with empty message and data
@Test
void testSuccess_WithEmptyMessageAndData() throws Exception {
final User user = new User("zhouxy", "zhouxy@gmail.com");
final UnifiedResponse<User> successWithEmptyMessageAndData = CustomUnifiedResponses.success("", user);
assertEquals("0000000", successWithEmptyMessageAndData.getCode());
assertEquals("", successWithEmptyMessageAndData.getMessage());
assertEquals(user, successWithEmptyMessageAndData.getData());
final String jacksonSuccessWithEmptyMessageAndData = jackson.writeValueAsString(successWithEmptyMessageAndData);
log.info("jacksonSuccessWithEmptyMessageAndData: {}", jacksonSuccessWithEmptyMessageAndData);
assertEquals("{\"code\":\"0000000\",\"message\":\"\",\"data\":{\"username\":\"zhouxy\",\"email\":\"zhouxy@gmail.com\"}}", jacksonSuccessWithEmptyMessageAndData);
}
@Test
void testError_WithStatusAndMessage() throws Exception {
final UnifiedResponse<Void> errorWithStatusAndMessage = CustomUnifiedResponses.error("108", "查询失败");
assertEquals("108", errorWithStatusAndMessage.getCode());
assertEquals("查询失败", errorWithStatusAndMessage.getMessage());
assertNull(errorWithStatusAndMessage.getData());
assertEquals("{code: \"108\", message: \"查询失败\", data: null}", errorWithStatusAndMessage.toString());
final String jacksonErrorWithStatusAndMessage = jackson.writeValueAsString(errorWithStatusAndMessage);
log.info("jacksonErrorWithStatusAndMessage: {}", jacksonErrorWithStatusAndMessage);
assertEquals("{\"code\":\"108\",\"message\":\"查询失败\"}", jacksonErrorWithStatusAndMessage);
final String gsonErrorWithStatusAndMessage = gson.toJson(errorWithStatusAndMessage);
assertEquals("{\"code\":\"108\",\"message\":\"查询失败\"}", gsonErrorWithStatusAndMessage);
}
@Test
void testError_WithStatusAndMessage_AndNullData() throws Exception {
final UnifiedResponse<Void> errorWithStatusAndMessageAndNullData = CustomUnifiedResponses.error("108", "查询失败", null);
assertEquals("108", errorWithStatusAndMessageAndNullData.getCode());
assertEquals("查询失败", errorWithStatusAndMessageAndNullData.getMessage());
assertNull(errorWithStatusAndMessageAndNullData.getData());
assertEquals("{code: \"108\", message: \"查询失败\", data: null}", errorWithStatusAndMessageAndNullData.toString());
final String jacksonErrorWithStatusAndMessageAndNullData = jackson.writeValueAsString(errorWithStatusAndMessageAndNullData);
log.info("jacksonErrorWithStatusAndMessage: {}", jacksonErrorWithStatusAndMessageAndNullData);
assertEquals("{\"code\":\"108\",\"message\":\"查询失败\"}", jacksonErrorWithStatusAndMessageAndNullData);
final String gsonErrorWithStatusAndMessageAndNullData = gson.toJson(errorWithStatusAndMessageAndNullData);
assertEquals("{\"code\":\"108\",\"message\":\"查询失败\"}", gsonErrorWithStatusAndMessageAndNullData);
}
@Test
void testError_WithStatusAndMessage_AndData() throws Exception {
final PageResult<User> emptyPageResult = PageResult.empty();
final UnifiedResponse<PageResult<User>> errorWithStatusAndMessageAndData = CustomUnifiedResponses.error("108", "查询失败", emptyPageResult);
assertEquals("108", errorWithStatusAndMessageAndData.getCode());
assertEquals("查询失败", errorWithStatusAndMessageAndData.getMessage());
assertEquals(emptyPageResult, errorWithStatusAndMessageAndData.getData());
assertEquals("{code: \"108\", message: \"查询失败\", data: PageResult [total=0, content=[]]}", errorWithStatusAndMessageAndData.toString());
final String jacksonErrorWithStatusAndMessageAndData = jackson.writeValueAsString(errorWithStatusAndMessageAndData);
assertEquals("{\"code\":\"108\",\"message\":\"查询失败\",\"data\":{\"total\":0,\"content\":[]}}",
jacksonErrorWithStatusAndMessageAndData);
final String gsonErrorWithStatusAndMessageAndData = gson.toJson(errorWithStatusAndMessageAndData);
assertEquals("{\"code\":\"108\",\"message\":\"查询失败\",\"data\":{\"total\":0,\"content\":[]}}",
gsonErrorWithStatusAndMessageAndData);
}
@Test
void testError_WithStatusAndNullMessage() throws Exception {
UnifiedResponse<Void> errorWithStatusAndNullMessage = CustomUnifiedResponses.error("500", (String) null);
assertEquals("500", errorWithStatusAndNullMessage.getCode());
assertEquals("", errorWithStatusAndNullMessage.getMessage());
assertNull(errorWithStatusAndNullMessage.getData());
final String jacksonErrorWithStatusAndNullMessage = jackson.writeValueAsString(errorWithStatusAndNullMessage);
assertEquals("{\"code\":\"500\",\"message\":\"\"}", jacksonErrorWithStatusAndNullMessage);
final String gsonErrorWithStatusAndNullMessage = gson.toJson(errorWithStatusAndNullMessage);
assertEquals("{\"code\":\"500\",\"message\":\"\"}", gsonErrorWithStatusAndNullMessage);
}
@Test
void testError_WithStatusAndNullMessage_AndNullData() throws Exception {
UnifiedResponse<Void> errorWithStatusAndNullMessageAndNullData = CustomUnifiedResponses.error("500", (String) null, null);
assertEquals("500", errorWithStatusAndNullMessageAndNullData.getCode());
assertEquals("", errorWithStatusAndNullMessageAndNullData.getMessage());
assertNull(errorWithStatusAndNullMessageAndNullData.getData());
final String jacksonErrorWithStatusAndNullMessageAndNullData = jackson.writeValueAsString(errorWithStatusAndNullMessageAndNullData);
assertEquals("{\"code\":\"500\",\"message\":\"\"}", jacksonErrorWithStatusAndNullMessageAndNullData);
final String gsonErrorWithStatusAndNullMessageAndNullData = gson.toJson(errorWithStatusAndNullMessageAndNullData);
assertEquals("{\"code\":\"500\",\"message\":\"\"}", gsonErrorWithStatusAndNullMessageAndNullData);
}
@Test
void testError_WithStatusAndNullMessage_AndData() throws Exception {
PageResult<User> emptyPageResult = PageResult.empty();
UnifiedResponse<PageResult<User>> errorWithStatusAndNullMessageAndData = CustomUnifiedResponses.error("500", (String) null, emptyPageResult);
assertEquals("500", errorWithStatusAndNullMessageAndData.getCode());
assertEquals("", errorWithStatusAndNullMessageAndData.getMessage());
assertEquals(emptyPageResult, errorWithStatusAndNullMessageAndData.getData());
final String jacksonErrorWithStatusAndNullMessageAndData = jackson.writeValueAsString(errorWithStatusAndNullMessageAndData);
assertEquals("{\"code\":\"500\",\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", jacksonErrorWithStatusAndNullMessageAndData);
final String gsonErrorWithStatusAndNullMessageAndData = gson.toJson(errorWithStatusAndNullMessageAndData);
assertEquals("{\"code\":\"500\",\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", gsonErrorWithStatusAndNullMessageAndData);
}
@Test
void testError_WithStatusAndEmptyMessage() throws Exception {
UnifiedResponse<Void> errorWithStatusAndEmptyMessage = CustomUnifiedResponses.error("500", "");
assertEquals("500", errorWithStatusAndEmptyMessage.getCode());
assertEquals("", errorWithStatusAndEmptyMessage.getMessage());
assertNull(errorWithStatusAndEmptyMessage.getData());
final String jacksonErrorWithStatusAndEmptyMessage = jackson.writeValueAsString(errorWithStatusAndEmptyMessage);
assertEquals("{\"code\":\"500\",\"message\":\"\"}", jacksonErrorWithStatusAndEmptyMessage);
final String gsonErrorWithStatusAndEmptyMessage = gson.toJson(errorWithStatusAndEmptyMessage);
assertEquals("{\"code\":\"500\",\"message\":\"\"}", gsonErrorWithStatusAndEmptyMessage);
}
@Test
void testError_WithStatusAndEmptyMessage_AndNullData() throws Exception {
UnifiedResponse<Void> errorWithStatusAndEmptyMessageAndNullData = CustomUnifiedResponses.error("500", "", null);
assertEquals("500", errorWithStatusAndEmptyMessageAndNullData.getCode());
assertEquals("", errorWithStatusAndEmptyMessageAndNullData.getMessage());
assertNull(errorWithStatusAndEmptyMessageAndNullData.getData());
final String jacksonErrorWithStatusAndEmptyMessageAndNullData = jackson.writeValueAsString(errorWithStatusAndEmptyMessageAndNullData);
assertEquals("{\"code\":\"500\",\"message\":\"\"}", jacksonErrorWithStatusAndEmptyMessageAndNullData);
final String gsonErrorWithStatusAndEmptyMessageAndNullData = gson.toJson(errorWithStatusAndEmptyMessageAndNullData);
assertEquals("{\"code\":\"500\",\"message\":\"\"}", gsonErrorWithStatusAndEmptyMessageAndNullData);
}
@Test
void testError_WithStatusAndEmptyMessage_AndData() throws Exception {
PageResult<User> emptyPageResult = PageResult.empty();
UnifiedResponse<PageResult<User>> errorWithStatusAndEmptyMessageAndData = CustomUnifiedResponses.error("500", "", emptyPageResult);
assertEquals("500", errorWithStatusAndEmptyMessageAndData.getCode());
assertEquals("", errorWithStatusAndEmptyMessageAndData.getMessage());
assertEquals(emptyPageResult, errorWithStatusAndEmptyMessageAndData.getData());
final String jacksonErrorWithStatusAndEmptyMessageAndData = jackson.writeValueAsString(errorWithStatusAndEmptyMessageAndData);
assertEquals("{\"code\":\"500\",\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", jacksonErrorWithStatusAndEmptyMessageAndData);
final String gsonErrorWithStatusAndEmptyMessageAndData = gson.toJson(errorWithStatusAndEmptyMessageAndData);
assertEquals("{\"code\":\"500\",\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", gsonErrorWithStatusAndEmptyMessageAndData);
}
@Test
void testError_WithStatusAndThrowable() throws Exception {
final IllegalArgumentException e = new IllegalArgumentException("ID cannot be null");
final UnifiedResponse<Void> errorWithStatusThrowable = CustomUnifiedResponses.error("500", e);
assertEquals("500", errorWithStatusThrowable.getCode());
assertEquals("ID cannot be null", errorWithStatusThrowable.getMessage());
assertNull(errorWithStatusThrowable.getData());
assertEquals("{\"code\":\"500\",\"message\":\"ID cannot be null\"}", jackson.writeValueAsString(errorWithStatusThrowable));
assertEquals("{\"code\":\"500\",\"message\":\"ID cannot be null\"}", gson.toJson(errorWithStatusThrowable));
}
@Test
void testError_WithStatusAndNullThrowable() {
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.error("500", (Throwable) null));
}
@Test
void testError_WithNullStatus() {
final String nullStatus = null;
final String nullMessage = null;
final User user = new User("zhouxy", "zhouxy@gmail.com");
// message
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.error(nullStatus, "查询失败"));
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.error(nullStatus, "查询失败", null));
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.error(nullStatus, "查询失败", user));
// empty message
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.error(nullStatus, ""));
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.error(nullStatus, "", null));
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.error(nullStatus, "", user));
// null message
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.error(nullStatus, nullMessage));
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.error(nullStatus, "查询失败", null));
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.error(nullStatus, "查询失败", user));
// Throwable
BizException bizException = new BizException("业务异常");
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.error(nullStatus, bizException));
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.error(nullStatus, (Throwable) null));
}
@Test
void testOf_WithStatusAndMessage() throws Exception {
final UnifiedResponse<Void> ofWithStatusAndMessage = CustomUnifiedResponses.of("108", "This is a message.");
assertEquals("108", ofWithStatusAndMessage.getCode());
assertEquals("This is a message.", ofWithStatusAndMessage.getMessage());
assertNull(ofWithStatusAndMessage.getData());
final String jacksonOfWithStatusAndMessage = jackson.writeValueAsString(ofWithStatusAndMessage);
log.info("jacksonOfWithStatusAndMessage: {}", jacksonOfWithStatusAndMessage);
assertEquals("{\"code\":\"108\",\"message\":\"This is a message.\"}", jacksonOfWithStatusAndMessage);
assertEquals("{code: \"108\", message: \"This is a message.\", data: null}", ofWithStatusAndMessage.toString());
final String gsonOfWithStatusAndMessage = gson.toJson(ofWithStatusAndMessage);
assertEquals("{\"code\":\"108\",\"message\":\"This is a message.\"}", gsonOfWithStatusAndMessage);
}
@Test
void testOf_WithStatusAndMessage_AndNullData() throws Exception {
final UnifiedResponse<Void> ofWithStatusAndMessageAndNullData = CustomUnifiedResponses.of("108", "This is a message.", null);
assertEquals("108", ofWithStatusAndMessageAndNullData.getCode());
assertEquals("This is a message.", ofWithStatusAndMessageAndNullData.getMessage());
assertNull(ofWithStatusAndMessageAndNullData.getData());
final String jacksonOfWithStatusAndMessageAndNullData = jackson.writeValueAsString(ofWithStatusAndMessageAndNullData);
log.info("jacksonOfWithStatusAndMessage: {}", jacksonOfWithStatusAndMessageAndNullData);
assertEquals("{\"code\":\"108\",\"message\":\"This is a message.\"}", jacksonOfWithStatusAndMessageAndNullData);
assertEquals("{code: \"108\", message: \"This is a message.\", data: null}", ofWithStatusAndMessageAndNullData.toString());
final String gsonOfWithStatusAndMessageAndNullData = gson.toJson(ofWithStatusAndMessageAndNullData);
assertEquals("{\"code\":\"108\",\"message\":\"This is a message.\"}", gsonOfWithStatusAndMessageAndNullData);
}
@Test
void testOf_WithStatusAndMessage_AndData() throws Exception {
final PageResult<User> emptyPageResult = PageResult.empty();
final UnifiedResponse<PageResult<User>> ofWithStatusAndMessageAndData
= CustomUnifiedResponses.of("108", "This is a message.", emptyPageResult);
assertEquals("{code: \"108\", message: \"This is a message.\", data: PageResult [total=0, content=[]]}",
ofWithStatusAndMessageAndData.toString());
assertEquals("108", ofWithStatusAndMessageAndData.getCode());
assertEquals("This is a message.", ofWithStatusAndMessageAndData.getMessage());
assertEquals(emptyPageResult, ofWithStatusAndMessageAndData.getData());
final String jacksonOfWithStatusAndMessageAndData = jackson.writeValueAsString(ofWithStatusAndMessageAndData);
assertEquals("{\"code\":\"108\",\"message\":\"This is a message.\",\"data\":{\"total\":0,\"content\":[]}}",
jacksonOfWithStatusAndMessageAndData);
final String gsonOfWithStatusAndMessageAndData = gson.toJson(ofWithStatusAndMessageAndData);
assertEquals("{\"code\":\"108\",\"message\":\"This is a message.\",\"data\":{\"total\":0,\"content\":[]}}",
gsonOfWithStatusAndMessageAndData);
}
@Test
void testOf_WithStatusAndNullMessage() throws Exception {
UnifiedResponse<Void> ofWithStatusAndNullMessage = CustomUnifiedResponses.of("108", (String) null);
assertEquals("108", ofWithStatusAndNullMessage.getCode());
assertEquals("", ofWithStatusAndNullMessage.getMessage());
assertNull(ofWithStatusAndNullMessage.getData());
final String jacksonOfWithStatusAndNullMessage = jackson.writeValueAsString(ofWithStatusAndNullMessage);
assertEquals("{\"code\":\"108\",\"message\":\"\"}", jacksonOfWithStatusAndNullMessage);
final String gsonOfWithStatusAndNullMessage = gson.toJson(ofWithStatusAndNullMessage);
assertEquals("{\"code\":\"108\",\"message\":\"\"}", gsonOfWithStatusAndNullMessage);
}
@Test
void testOf_WithStatusAndNullMessage_AndNullData() throws Exception {
UnifiedResponse<Void> ofWithStatusAndNullMessageAndNullData = CustomUnifiedResponses.of("108", (String) null, null);
assertEquals("108", ofWithStatusAndNullMessageAndNullData.getCode());
assertEquals("", ofWithStatusAndNullMessageAndNullData.getMessage());
assertNull(ofWithStatusAndNullMessageAndNullData.getData());
final String jacksonOfWithStatusAndNullMessageAndNullData = jackson.writeValueAsString(ofWithStatusAndNullMessageAndNullData);
assertEquals("{\"code\":\"108\",\"message\":\"\"}", jacksonOfWithStatusAndNullMessageAndNullData);
final String gsonOfWithStatusAndNullMessageAndNullData = gson.toJson(ofWithStatusAndNullMessageAndNullData);
assertEquals("{\"code\":\"108\",\"message\":\"\"}", gsonOfWithStatusAndNullMessageAndNullData);
}
@Test
void testOf_WithStatusAndNullMessage_AndData() throws Exception {
PageResult<User> emptyPageResult = PageResult.empty();
UnifiedResponse<PageResult<User>> ofWithStatusAndNullMessageAndData = CustomUnifiedResponses.of("108", (String) null, emptyPageResult);
assertEquals("108", ofWithStatusAndNullMessageAndData.getCode());
assertEquals("", ofWithStatusAndNullMessageAndData.getMessage());
assertEquals(emptyPageResult, ofWithStatusAndNullMessageAndData.getData());
final String jacksonOfWithStatusAndNullMessageAndData = jackson.writeValueAsString(ofWithStatusAndNullMessageAndData);
assertEquals("{\"code\":\"108\",\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", jacksonOfWithStatusAndNullMessageAndData);
final String gsonOfWithStatusAndNullMessageAndData = gson.toJson(ofWithStatusAndNullMessageAndData);
assertEquals("{\"code\":\"108\",\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", gsonOfWithStatusAndNullMessageAndData);
}
@Test
void testOf_WithStatusAndEmptyMessage() throws Exception {
UnifiedResponse<Void> ofWithStatusAndEmptyMessage = CustomUnifiedResponses.of("108", "");
assertEquals("108", ofWithStatusAndEmptyMessage.getCode());
assertEquals("", ofWithStatusAndEmptyMessage.getMessage());
assertNull(ofWithStatusAndEmptyMessage.getData());
final String jacksonOfWithStatusAndEmptyMessage = jackson.writeValueAsString(ofWithStatusAndEmptyMessage);
assertEquals("{\"code\":\"108\",\"message\":\"\"}", jacksonOfWithStatusAndEmptyMessage);
final String gsonOfWithStatusAndEmptyMessage = gson.toJson(ofWithStatusAndEmptyMessage);
assertEquals("{\"code\":\"108\",\"message\":\"\"}", gsonOfWithStatusAndEmptyMessage);
}
@Test
void testOf_WithStatusAndEmptyMessage_AndNullData() throws Exception {
UnifiedResponse<Void> ofWithStatusAndEmptyMessageAndNullData = CustomUnifiedResponses.of("108", "", null);
assertEquals("108", ofWithStatusAndEmptyMessageAndNullData.getCode());
assertEquals("", ofWithStatusAndEmptyMessageAndNullData.getMessage());
assertNull(ofWithStatusAndEmptyMessageAndNullData.getData());
final String jacksonOfWithStatusAndEmptyMessageAndNullData = jackson.writeValueAsString(ofWithStatusAndEmptyMessageAndNullData);
assertEquals("{\"code\":\"108\",\"message\":\"\"}", jacksonOfWithStatusAndEmptyMessageAndNullData);
final String gsonOfWithStatusAndEmptyMessageAndNullData = gson.toJson(ofWithStatusAndEmptyMessageAndNullData);
assertEquals("{\"code\":\"108\",\"message\":\"\"}", gsonOfWithStatusAndEmptyMessageAndNullData);
}
@Test
void testOf_WithStatusAndEmptyMessage_AndData() throws Exception {
PageResult<User> emptyPageResult = PageResult.empty();
UnifiedResponse<PageResult<User>> ofWithStatusAndEmptyMessageAndData = CustomUnifiedResponses.of("108", "", emptyPageResult);
assertEquals("108", ofWithStatusAndEmptyMessageAndData.getCode());
assertEquals("", ofWithStatusAndEmptyMessageAndData.getMessage());
assertEquals(emptyPageResult, ofWithStatusAndEmptyMessageAndData.getData());
final String jacksonOfWithStatusAndEmptyMessageAndData = jackson.writeValueAsString(ofWithStatusAndEmptyMessageAndData);
assertEquals("{\"code\":\"108\",\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", jacksonOfWithStatusAndEmptyMessageAndData);
final String gsonOfWithStatusAndEmptyMessageAndData = gson.toJson(ofWithStatusAndEmptyMessageAndData);
assertEquals("{\"code\":\"108\",\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", gsonOfWithStatusAndEmptyMessageAndData);
}
@Test
void testOf_WithNullStatus() {
final String nullStatus = null;
final String nullMessage = null;
final User user = new User("zhouxy", "zhouxy@gmail.com");
// message
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.of(nullStatus, "查询失败"));
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.of(nullStatus, "查询失败", null));
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.of(nullStatus, "查询失败", user));
// empty message
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.of(nullStatus, ""));
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.of(nullStatus, "", null));
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.of(nullStatus, "", user));
// null message
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.of(nullStatus, nullMessage));
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.of(nullStatus, "查询失败", null));
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.of(nullStatus, "查询失败", user));
}
@Data
@NoArgsConstructor
@AllArgsConstructor
private static class User {
private String username;
private String email;
}
public static class CustomUnifiedResponses extends UnifiedResponses {
public static final String SUCCESS_CODE = "0000000";
public static final String DEFAULT_SUCCESS_MSG = "成功";
public static UnifiedResponse<Void> success() {
return of(SUCCESS_CODE, DEFAULT_SUCCESS_MSG);
}
public static UnifiedResponse<Void> success(@Nullable String message) {
return of(SUCCESS_CODE, message);
}
public static <T> UnifiedResponse<T> success(@Nullable String message, @Nullable T data) {
return of(SUCCESS_CODE, message, data);
}
private CustomUnifiedResponses() {
super();
}
}
}

View File

@@ -51,7 +51,7 @@ class UnifiedResponseTests {
@Test @Test
void testSuccess_WithoutArgument() throws Exception { void testSuccess_WithoutArgument() throws Exception {
// 1. success without argument // 1. success without argument
UnifiedResponse<Void> success = UnifiedResponse.success(); UnifiedResponse<Void> success = UnifiedResponses.success();
assertEquals("2000000", success.getCode()); assertEquals("2000000", success.getCode());
assertEquals("SUCCESS", success.getMessage()); assertEquals("SUCCESS", success.getMessage());
assertNull(success.getData()); assertNull(success.getData());
@@ -63,7 +63,7 @@ class UnifiedResponseTests {
@Test @Test
void testSuccess_WithMessage() throws Exception { void testSuccess_WithMessage() throws Exception {
// 2. success with message // 2. success with message
UnifiedResponse<Void> successWithMessage = UnifiedResponse.success("成功"); UnifiedResponse<Void> successWithMessage = UnifiedResponses.success("成功");
assertEquals("2000000", successWithMessage.getCode()); assertEquals("2000000", successWithMessage.getCode());
assertEquals("成功", successWithMessage.getMessage()); assertEquals("成功", successWithMessage.getMessage());
assertNull(successWithMessage.getData()); assertNull(successWithMessage.getData());
@@ -75,7 +75,7 @@ class UnifiedResponseTests {
@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<Void> successWithMessageAndNullData = UnifiedResponse.success("查询成功", null); final UnifiedResponse<Void> successWithMessageAndNullData = UnifiedResponses.success("查询成功", null);
assertEquals("2000000", successWithMessageAndNullData.getCode()); assertEquals("2000000", successWithMessageAndNullData.getCode());
assertEquals("查询成功", successWithMessageAndNullData.getMessage()); assertEquals("查询成功", successWithMessageAndNullData.getMessage());
assertNull(successWithMessageAndNullData.getData()); assertNull(successWithMessageAndNullData.getData());
@@ -88,7 +88,7 @@ class UnifiedResponseTests {
@Test @Test
void testSuccess_WithMessageAndStringData() throws Exception { void testSuccess_WithMessageAndStringData() throws Exception {
UnifiedResponse<String> successWithStringData = UnifiedResponse.success("查询成功", "zhouxy"); UnifiedResponse<String> successWithStringData = UnifiedResponses.success("查询成功", "zhouxy");
assertEquals("2000000", successWithStringData.getCode()); assertEquals("2000000", successWithStringData.getCode());
assertEquals("查询成功", successWithStringData.getMessage()); assertEquals("查询成功", successWithStringData.getMessage());
assertEquals("zhouxy", successWithStringData.getData()); assertEquals("zhouxy", successWithStringData.getData());
@@ -101,7 +101,7 @@ class UnifiedResponseTests {
@Test @Test
void testSuccess_WithMessageAndIntegerData() throws Exception { void testSuccess_WithMessageAndIntegerData() throws Exception {
final UnifiedResponse<Integer> successWithIntegerData = UnifiedResponse.success("查询成功", 1); final UnifiedResponse<Integer> successWithIntegerData = UnifiedResponses.success("查询成功", 1);
assertEquals("2000000", successWithIntegerData.getCode()); assertEquals("2000000", successWithIntegerData.getCode());
assertEquals("查询成功", successWithIntegerData.getMessage()); assertEquals("查询成功", successWithIntegerData.getMessage());
assertEquals(1, successWithIntegerData.getData()); assertEquals(1, successWithIntegerData.getData());
@@ -114,7 +114,7 @@ class UnifiedResponseTests {
@Test @Test
void testSuccess_WithMessageAndData() throws Exception { void testSuccess_WithMessageAndData() throws Exception {
UnifiedResponse<PageResult<User>> successWithData = UnifiedResponse.success("查询成功", pageResult); UnifiedResponse<PageResult<User>> successWithData = UnifiedResponses.success("查询成功", pageResult);
assertEquals("2000000", successWithData.getCode()); assertEquals("2000000", successWithData.getCode());
assertEquals("查询成功", successWithData.getMessage()); assertEquals("查询成功", successWithData.getMessage());
assertNotNull(successWithData.getData()); assertNotNull(successWithData.getData());
@@ -127,7 +127,7 @@ class UnifiedResponseTests {
@Test @Test
void testSuccess_WithNullMessage() throws Exception { void testSuccess_WithNullMessage() throws Exception {
// 3. success with null message // 3. success with null message
UnifiedResponse<Void> successWithNullMessage = UnifiedResponse.success(null); UnifiedResponse<Void> successWithNullMessage = UnifiedResponses.success(null);
assertEquals("2000000", successWithNullMessage.getCode()); assertEquals("2000000", successWithNullMessage.getCode());
assertEquals("", successWithNullMessage.getMessage()); assertEquals("", successWithNullMessage.getMessage());
assertNull(successWithNullMessage.getData()); assertNull(successWithNullMessage.getData());
@@ -139,7 +139,7 @@ class UnifiedResponseTests {
// 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<Void> successWithNullMessageAndNullData = UnifiedResponse.success(null, null); final UnifiedResponse<Void> successWithNullMessageAndNullData = UnifiedResponses.success(null, null);
assertEquals("2000000", successWithNullMessageAndNullData.getCode()); assertEquals("2000000", successWithNullMessageAndNullData.getCode());
assertEquals("", successWithNullMessageAndNullData.getMessage()); assertEquals("", successWithNullMessageAndNullData.getMessage());
assertNull(successWithNullMessageAndNullData.getData()); assertNull(successWithNullMessageAndNullData.getData());
@@ -155,7 +155,7 @@ class UnifiedResponseTests {
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<User> successWithNullMessageAndData = UnifiedResponse.success(null, user); final UnifiedResponse<User> successWithNullMessageAndData = UnifiedResponses.success(null, user);
assertEquals("2000000", successWithNullMessageAndData.getCode()); assertEquals("2000000", successWithNullMessageAndData.getCode());
assertEquals("", successWithNullMessageAndData.getMessage()); assertEquals("", successWithNullMessageAndData.getMessage());
assertEquals(user, successWithNullMessageAndData.getData()); assertEquals(user, successWithNullMessageAndData.getData());
@@ -168,7 +168,7 @@ class UnifiedResponseTests {
@Test @Test
void testSuccess_WithEmptyMessage() throws Exception { void testSuccess_WithEmptyMessage() throws Exception {
// 4. success with empty message // 4. success with empty message
UnifiedResponse<Void> successWithEmptyMessage = UnifiedResponse.success(""); UnifiedResponse<Void> successWithEmptyMessage = UnifiedResponses.success("");
assertEquals("2000000", successWithEmptyMessage.getCode()); assertEquals("2000000", successWithEmptyMessage.getCode());
assertEquals("", successWithEmptyMessage.getMessage()); assertEquals("", successWithEmptyMessage.getMessage());
assertNull(successWithEmptyMessage.getData()); assertNull(successWithEmptyMessage.getData());
@@ -180,7 +180,7 @@ class UnifiedResponseTests {
// 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<Void> successWithEmptyMessageAndNullData = UnifiedResponse.success("", null); final UnifiedResponse<Void> successWithEmptyMessageAndNullData = UnifiedResponses.success("", null);
assertEquals("2000000", successWithEmptyMessageAndNullData.getCode()); assertEquals("2000000", successWithEmptyMessageAndNullData.getCode());
assertEquals("", successWithEmptyMessageAndNullData.getMessage()); assertEquals("", successWithEmptyMessageAndNullData.getMessage());
assertNull(successWithEmptyMessageAndNullData.getData()); assertNull(successWithEmptyMessageAndNullData.getData());
@@ -196,7 +196,7 @@ class UnifiedResponseTests {
@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<User> successWithEmptyMessageAndData = UnifiedResponse.success("", user); final UnifiedResponse<User> successWithEmptyMessageAndData = UnifiedResponses.success("", user);
assertEquals("2000000", successWithEmptyMessageAndData.getCode()); assertEquals("2000000", successWithEmptyMessageAndData.getCode());
assertEquals("", successWithEmptyMessageAndData.getMessage()); assertEquals("", successWithEmptyMessageAndData.getMessage());
assertEquals(user, successWithEmptyMessageAndData.getData()); assertEquals(user, successWithEmptyMessageAndData.getData());
@@ -208,7 +208,7 @@ class UnifiedResponseTests {
@Test @Test
void testError_WithStatusAndMessage() throws Exception { void testError_WithStatusAndMessage() throws Exception {
final UnifiedResponse<Void> errorWithStatusAndMessage = UnifiedResponse.error("108", "查询失败"); final UnifiedResponse<Void> errorWithStatusAndMessage = UnifiedResponses.error("108", "查询失败");
assertEquals("108", errorWithStatusAndMessage.getCode()); assertEquals("108", errorWithStatusAndMessage.getCode());
assertEquals("查询失败", errorWithStatusAndMessage.getMessage()); assertEquals("查询失败", errorWithStatusAndMessage.getMessage());
assertNull(errorWithStatusAndMessage.getData()); assertNull(errorWithStatusAndMessage.getData());
@@ -224,7 +224,7 @@ class UnifiedResponseTests {
@Test @Test
void testError_WithStatusAndMessage_AndNullData() throws Exception { void testError_WithStatusAndMessage_AndNullData() throws Exception {
final UnifiedResponse<Void> errorWithStatusAndMessageAndNullData = UnifiedResponse.error("108", "查询失败", null); final UnifiedResponse<Void> errorWithStatusAndMessageAndNullData = UnifiedResponses.error("108", "查询失败", null);
assertEquals("108", errorWithStatusAndMessageAndNullData.getCode()); assertEquals("108", errorWithStatusAndMessageAndNullData.getCode());
assertEquals("查询失败", errorWithStatusAndMessageAndNullData.getMessage()); assertEquals("查询失败", errorWithStatusAndMessageAndNullData.getMessage());
assertNull(errorWithStatusAndMessageAndNullData.getData()); assertNull(errorWithStatusAndMessageAndNullData.getData());
@@ -241,7 +241,7 @@ class UnifiedResponseTests {
@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<PageResult<User>> errorWithStatusAndMessageAndData = UnifiedResponse.error("108", "查询失败", emptyPageResult); final UnifiedResponse<PageResult<User>> errorWithStatusAndMessageAndData = UnifiedResponses.error("108", "查询失败", emptyPageResult);
assertEquals("108", errorWithStatusAndMessageAndData.getCode()); assertEquals("108", errorWithStatusAndMessageAndData.getCode());
assertEquals("查询失败", errorWithStatusAndMessageAndData.getMessage()); assertEquals("查询失败", errorWithStatusAndMessageAndData.getMessage());
assertEquals(emptyPageResult, errorWithStatusAndMessageAndData.getData()); assertEquals(emptyPageResult, errorWithStatusAndMessageAndData.getData());
@@ -258,7 +258,7 @@ class UnifiedResponseTests {
@Test @Test
void testError_WithStatusAndNullMessage() throws Exception { void testError_WithStatusAndNullMessage() throws Exception {
UnifiedResponse<Void> errorWithStatusAndNullMessage = UnifiedResponse.error("500", (String) null); UnifiedResponse<Void> errorWithStatusAndNullMessage = UnifiedResponses.error("500", (String) null);
assertEquals("500", errorWithStatusAndNullMessage.getCode()); assertEquals("500", errorWithStatusAndNullMessage.getCode());
assertEquals("", errorWithStatusAndNullMessage.getMessage()); assertEquals("", errorWithStatusAndNullMessage.getMessage());
assertNull(errorWithStatusAndNullMessage.getData()); assertNull(errorWithStatusAndNullMessage.getData());
@@ -272,7 +272,7 @@ class UnifiedResponseTests {
@Test @Test
void testError_WithStatusAndNullMessage_AndNullData() throws Exception { void testError_WithStatusAndNullMessage_AndNullData() throws Exception {
UnifiedResponse<Void> errorWithStatusAndNullMessageAndNullData = UnifiedResponse.error("500", (String) null, null); UnifiedResponse<Void> errorWithStatusAndNullMessageAndNullData = UnifiedResponses.error("500", (String) null, null);
assertEquals("500", errorWithStatusAndNullMessageAndNullData.getCode()); assertEquals("500", errorWithStatusAndNullMessageAndNullData.getCode());
assertEquals("", errorWithStatusAndNullMessageAndNullData.getMessage()); assertEquals("", errorWithStatusAndNullMessageAndNullData.getMessage());
@@ -288,7 +288,7 @@ class UnifiedResponseTests {
@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<PageResult<User>> errorWithStatusAndNullMessageAndData = UnifiedResponse.error("500", (String) null, emptyPageResult); UnifiedResponse<PageResult<User>> errorWithStatusAndNullMessageAndData = UnifiedResponses.error("500", (String) null, emptyPageResult);
assertEquals("500", errorWithStatusAndNullMessageAndData.getCode()); assertEquals("500", errorWithStatusAndNullMessageAndData.getCode());
assertEquals("", errorWithStatusAndNullMessageAndData.getMessage()); assertEquals("", errorWithStatusAndNullMessageAndData.getMessage());
assertEquals(emptyPageResult, errorWithStatusAndNullMessageAndData.getData()); assertEquals(emptyPageResult, errorWithStatusAndNullMessageAndData.getData());
@@ -300,7 +300,7 @@ class UnifiedResponseTests {
@Test @Test
void testError_WithStatusAndEmptyMessage() throws Exception { void testError_WithStatusAndEmptyMessage() throws Exception {
UnifiedResponse<Void> errorWithStatusAndEmptyMessage = UnifiedResponse.error("500", ""); UnifiedResponse<Void> errorWithStatusAndEmptyMessage = UnifiedResponses.error("500", "");
assertEquals("500", errorWithStatusAndEmptyMessage.getCode()); assertEquals("500", errorWithStatusAndEmptyMessage.getCode());
assertEquals("", errorWithStatusAndEmptyMessage.getMessage()); assertEquals("", errorWithStatusAndEmptyMessage.getMessage());
assertNull(errorWithStatusAndEmptyMessage.getData()); assertNull(errorWithStatusAndEmptyMessage.getData());
@@ -314,7 +314,7 @@ class UnifiedResponseTests {
@Test @Test
void testError_WithStatusAndEmptyMessage_AndNullData() throws Exception { void testError_WithStatusAndEmptyMessage_AndNullData() throws Exception {
UnifiedResponse<Void> errorWithStatusAndEmptyMessageAndNullData = UnifiedResponse.error("500", "", null); UnifiedResponse<Void> errorWithStatusAndEmptyMessageAndNullData = UnifiedResponses.error("500", "", null);
assertEquals("500", errorWithStatusAndEmptyMessageAndNullData.getCode()); assertEquals("500", errorWithStatusAndEmptyMessageAndNullData.getCode());
assertEquals("", errorWithStatusAndEmptyMessageAndNullData.getMessage()); assertEquals("", errorWithStatusAndEmptyMessageAndNullData.getMessage());
@@ -330,7 +330,7 @@ class UnifiedResponseTests {
@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<PageResult<User>> errorWithStatusAndEmptyMessageAndData = UnifiedResponse.error("500", "", emptyPageResult); UnifiedResponse<PageResult<User>> errorWithStatusAndEmptyMessageAndData = UnifiedResponses.error("500", "", emptyPageResult);
assertEquals("500", errorWithStatusAndEmptyMessageAndData.getCode()); assertEquals("500", errorWithStatusAndEmptyMessageAndData.getCode());
assertEquals("", errorWithStatusAndEmptyMessageAndData.getMessage()); assertEquals("", errorWithStatusAndEmptyMessageAndData.getMessage());
assertEquals(emptyPageResult, errorWithStatusAndEmptyMessageAndData.getData()); assertEquals(emptyPageResult, errorWithStatusAndEmptyMessageAndData.getData());
@@ -343,7 +343,7 @@ class UnifiedResponseTests {
@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<Void> errorWithStatusThrowable = UnifiedResponse.error("500", e); final UnifiedResponse<Void> errorWithStatusThrowable = UnifiedResponses.error("500", e);
assertEquals("500", errorWithStatusThrowable.getCode()); assertEquals("500", errorWithStatusThrowable.getCode());
assertEquals("ID cannot be null", errorWithStatusThrowable.getMessage()); assertEquals("ID cannot be null", errorWithStatusThrowable.getMessage());
assertNull(errorWithStatusThrowable.getData()); assertNull(errorWithStatusThrowable.getData());
@@ -353,7 +353,7 @@ class UnifiedResponseTests {
@Test @Test
void testError_WithStatusAndNullThrowable() { void testError_WithStatusAndNullThrowable() {
assertThrows(NullPointerException.class, () -> UnifiedResponse.error("500", (Throwable) null)); assertThrows(NullPointerException.class, () -> UnifiedResponses.error("500", (Throwable) null));
} }
@Test @Test
@@ -363,30 +363,30 @@ class UnifiedResponseTests {
final User user = new User("zhouxy", "zhouxy@gmail.com"); final User user = new User("zhouxy", "zhouxy@gmail.com");
// message // message
assertThrows(NullPointerException.class, () -> UnifiedResponse.error(nullStatus, "查询失败")); assertThrows(NullPointerException.class, () -> UnifiedResponses.error(nullStatus, "查询失败"));
assertThrows(NullPointerException.class, () -> UnifiedResponse.error(nullStatus, "查询失败", null)); assertThrows(NullPointerException.class, () -> UnifiedResponses.error(nullStatus, "查询失败", null));
assertThrows(NullPointerException.class, () -> UnifiedResponse.error(nullStatus, "查询失败", user)); assertThrows(NullPointerException.class, () -> UnifiedResponses.error(nullStatus, "查询失败", user));
// empty message // empty message
assertThrows(NullPointerException.class, () -> UnifiedResponse.error(nullStatus, "")); assertThrows(NullPointerException.class, () -> UnifiedResponses.error(nullStatus, ""));
assertThrows(NullPointerException.class, () -> UnifiedResponse.error(nullStatus, "", null)); assertThrows(NullPointerException.class, () -> UnifiedResponses.error(nullStatus, "", null));
assertThrows(NullPointerException.class, () -> UnifiedResponse.error(nullStatus, "", user)); assertThrows(NullPointerException.class, () -> UnifiedResponses.error(nullStatus, "", user));
// null message // null message
assertThrows(NullPointerException.class, () -> UnifiedResponse.error(nullStatus, nullMessage)); assertThrows(NullPointerException.class, () -> UnifiedResponses.error(nullStatus, nullMessage));
assertThrows(NullPointerException.class, () -> UnifiedResponse.error(nullStatus, "查询失败", null)); assertThrows(NullPointerException.class, () -> UnifiedResponses.error(nullStatus, "查询失败", null));
assertThrows(NullPointerException.class, () -> UnifiedResponse.error(nullStatus, "查询失败", user)); assertThrows(NullPointerException.class, () -> UnifiedResponses.error(nullStatus, "查询失败", user));
// Throwable // Throwable
BizException bizException = new BizException("业务异常"); BizException bizException = new BizException("业务异常");
assertThrows(NullPointerException.class, () -> UnifiedResponse.error(nullStatus, bizException)); assertThrows(NullPointerException.class, () -> UnifiedResponses.error(nullStatus, bizException));
assertThrows(NullPointerException.class, () -> UnifiedResponse.error(nullStatus, (Throwable) null)); assertThrows(NullPointerException.class, () -> UnifiedResponses.error(nullStatus, (Throwable) null));
} }
@Test @Test
void testOf_WithStatusAndMessage() throws Exception { void testOf_WithStatusAndMessage() throws Exception {
final UnifiedResponse<Void> ofWithStatusAndMessage = UnifiedResponse.of("108", "This is a message."); final UnifiedResponse<Void> ofWithStatusAndMessage = UnifiedResponses.of("108", "This is a message.");
assertEquals("108", ofWithStatusAndMessage.getCode()); assertEquals("108", ofWithStatusAndMessage.getCode());
assertEquals("This is a message.", ofWithStatusAndMessage.getMessage()); assertEquals("This is a message.", ofWithStatusAndMessage.getMessage());
assertNull(ofWithStatusAndMessage.getData()); assertNull(ofWithStatusAndMessage.getData());
@@ -403,7 +403,7 @@ class UnifiedResponseTests {
@Test @Test
void testOf_WithStatusAndMessage_AndNullData() throws Exception { void testOf_WithStatusAndMessage_AndNullData() throws Exception {
final UnifiedResponse<Void> ofWithStatusAndMessageAndNullData = UnifiedResponse.of("108", "This is a message.", null); final UnifiedResponse<Void> ofWithStatusAndMessageAndNullData = UnifiedResponses.of("108", "This is a message.", null);
assertEquals("108", ofWithStatusAndMessageAndNullData.getCode()); assertEquals("108", ofWithStatusAndMessageAndNullData.getCode());
assertEquals("This is a message.", ofWithStatusAndMessageAndNullData.getMessage()); assertEquals("This is a message.", ofWithStatusAndMessageAndNullData.getMessage());
assertNull(ofWithStatusAndMessageAndNullData.getData()); assertNull(ofWithStatusAndMessageAndNullData.getData());
@@ -422,7 +422,7 @@ class UnifiedResponseTests {
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<PageResult<User>> ofWithStatusAndMessageAndData final UnifiedResponse<PageResult<User>> ofWithStatusAndMessageAndData
= UnifiedResponse.of("108", "This is a message.", emptyPageResult); = UnifiedResponses.of("108", "This is a message.", emptyPageResult);
assertEquals("{code: \"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.getCode()); assertEquals("108", ofWithStatusAndMessageAndData.getCode());
@@ -438,7 +438,7 @@ class UnifiedResponseTests {
@Test @Test
void testOf_WithStatusAndNullMessage() throws Exception { void testOf_WithStatusAndNullMessage() throws Exception {
UnifiedResponse<Void> ofWithStatusAndNullMessage = UnifiedResponse.of("108", (String) null); UnifiedResponse<Void> ofWithStatusAndNullMessage = UnifiedResponses.of("108", (String) null);
assertEquals("108", ofWithStatusAndNullMessage.getCode()); assertEquals("108", ofWithStatusAndNullMessage.getCode());
assertEquals("", ofWithStatusAndNullMessage.getMessage()); assertEquals("", ofWithStatusAndNullMessage.getMessage());
assertNull(ofWithStatusAndNullMessage.getData()); assertNull(ofWithStatusAndNullMessage.getData());
@@ -452,7 +452,7 @@ class UnifiedResponseTests {
@Test @Test
void testOf_WithStatusAndNullMessage_AndNullData() throws Exception { void testOf_WithStatusAndNullMessage_AndNullData() throws Exception {
UnifiedResponse<Void> ofWithStatusAndNullMessageAndNullData = UnifiedResponse.of("108", (String) null, null); UnifiedResponse<Void> ofWithStatusAndNullMessageAndNullData = UnifiedResponses.of("108", (String) null, null);
assertEquals("108", ofWithStatusAndNullMessageAndNullData.getCode()); assertEquals("108", ofWithStatusAndNullMessageAndNullData.getCode());
assertEquals("", ofWithStatusAndNullMessageAndNullData.getMessage()); assertEquals("", ofWithStatusAndNullMessageAndNullData.getMessage());
@@ -468,7 +468,7 @@ class UnifiedResponseTests {
@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<PageResult<User>> ofWithStatusAndNullMessageAndData = UnifiedResponse.of("108", (String) null, emptyPageResult); UnifiedResponse<PageResult<User>> ofWithStatusAndNullMessageAndData = UnifiedResponses.of("108", (String) null, emptyPageResult);
assertEquals("108", ofWithStatusAndNullMessageAndData.getCode()); assertEquals("108", ofWithStatusAndNullMessageAndData.getCode());
assertEquals("", ofWithStatusAndNullMessageAndData.getMessage()); assertEquals("", ofWithStatusAndNullMessageAndData.getMessage());
assertEquals(emptyPageResult, ofWithStatusAndNullMessageAndData.getData()); assertEquals(emptyPageResult, ofWithStatusAndNullMessageAndData.getData());
@@ -480,7 +480,7 @@ class UnifiedResponseTests {
@Test @Test
void testOf_WithStatusAndEmptyMessage() throws Exception { void testOf_WithStatusAndEmptyMessage() throws Exception {
UnifiedResponse<Void> ofWithStatusAndEmptyMessage = UnifiedResponse.of("108", ""); UnifiedResponse<Void> ofWithStatusAndEmptyMessage = UnifiedResponses.of("108", "");
assertEquals("108", ofWithStatusAndEmptyMessage.getCode()); assertEquals("108", ofWithStatusAndEmptyMessage.getCode());
assertEquals("", ofWithStatusAndEmptyMessage.getMessage()); assertEquals("", ofWithStatusAndEmptyMessage.getMessage());
assertNull(ofWithStatusAndEmptyMessage.getData()); assertNull(ofWithStatusAndEmptyMessage.getData());
@@ -494,7 +494,7 @@ class UnifiedResponseTests {
@Test @Test
void testOf_WithStatusAndEmptyMessage_AndNullData() throws Exception { void testOf_WithStatusAndEmptyMessage_AndNullData() throws Exception {
UnifiedResponse<Void> ofWithStatusAndEmptyMessageAndNullData = UnifiedResponse.of("108", "", null); UnifiedResponse<Void> ofWithStatusAndEmptyMessageAndNullData = UnifiedResponses.of("108", "", null);
assertEquals("108", ofWithStatusAndEmptyMessageAndNullData.getCode()); assertEquals("108", ofWithStatusAndEmptyMessageAndNullData.getCode());
assertEquals("", ofWithStatusAndEmptyMessageAndNullData.getMessage()); assertEquals("", ofWithStatusAndEmptyMessageAndNullData.getMessage());
@@ -510,7 +510,7 @@ class UnifiedResponseTests {
@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<PageResult<User>> ofWithStatusAndEmptyMessageAndData = UnifiedResponse.of("108", "", emptyPageResult); UnifiedResponse<PageResult<User>> ofWithStatusAndEmptyMessageAndData = UnifiedResponses.of("108", "", emptyPageResult);
assertEquals("108", ofWithStatusAndEmptyMessageAndData.getCode()); assertEquals("108", ofWithStatusAndEmptyMessageAndData.getCode());
assertEquals("", ofWithStatusAndEmptyMessageAndData.getMessage()); assertEquals("", ofWithStatusAndEmptyMessageAndData.getMessage());
assertEquals(emptyPageResult, ofWithStatusAndEmptyMessageAndData.getData()); assertEquals(emptyPageResult, ofWithStatusAndEmptyMessageAndData.getData());
@@ -527,19 +527,19 @@ class UnifiedResponseTests {
final User user = new User("zhouxy", "zhouxy@gmail.com"); final User user = new User("zhouxy", "zhouxy@gmail.com");
// message // message
assertThrows(NullPointerException.class, () -> UnifiedResponse.of(nullStatus, "查询失败")); assertThrows(NullPointerException.class, () -> UnifiedResponses.of(nullStatus, "查询失败"));
assertThrows(NullPointerException.class, () -> UnifiedResponse.of(nullStatus, "查询失败", null)); assertThrows(NullPointerException.class, () -> UnifiedResponses.of(nullStatus, "查询失败", null));
assertThrows(NullPointerException.class, () -> UnifiedResponse.of(nullStatus, "查询失败", user)); assertThrows(NullPointerException.class, () -> UnifiedResponses.of(nullStatus, "查询失败", user));
// empty message // empty message
assertThrows(NullPointerException.class, () -> UnifiedResponse.of(nullStatus, "")); assertThrows(NullPointerException.class, () -> UnifiedResponses.of(nullStatus, ""));
assertThrows(NullPointerException.class, () -> UnifiedResponse.of(nullStatus, "", null)); assertThrows(NullPointerException.class, () -> UnifiedResponses.of(nullStatus, "", null));
assertThrows(NullPointerException.class, () -> UnifiedResponse.of(nullStatus, "", user)); assertThrows(NullPointerException.class, () -> UnifiedResponses.of(nullStatus, "", user));
// null message // null message
assertThrows(NullPointerException.class, () -> UnifiedResponse.of(nullStatus, nullMessage)); assertThrows(NullPointerException.class, () -> UnifiedResponses.of(nullStatus, nullMessage));
assertThrows(NullPointerException.class, () -> UnifiedResponse.of(nullStatus, "查询失败", null)); assertThrows(NullPointerException.class, () -> UnifiedResponses.of(nullStatus, "查询失败", null));
assertThrows(NullPointerException.class, () -> UnifiedResponse.of(nullStatus, "查询失败", user)); assertThrows(NullPointerException.class, () -> UnifiedResponses.of(nullStatus, "查询失败", user));
} }
@Data @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.
@@ -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,31 +70,35 @@ 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);
Map<String, Menu> menuMap = new HashMap<>(); Map<String, Menu> menuMap = new HashMap<>();
for (Menu element : clonedMenus) { for (Menu element : clonedMenus) {
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,16 +107,16 @@ 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);
Map<String, Menu> menuMap = new HashMap<>(); Map<String, Menu> menuMap = new HashMap<>();
for (Menu element : clonedMenus) { for (Menu element : clonedMenus) {
@@ -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;
} }