From b60393d9b2f3f45f06079355c14f3ba4505adb2e Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 3 May 2023 21:35:04 +0800 Subject: [PATCH] add test --- .../hutool/core/exception/ExceptionUtil.java | 7 +- .../dromara/hutool/json/Issue3086Test.java | 70 +++++++++++++++++++ 2 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 hutool-json/src/test/java/org/dromara/hutool/json/Issue3086Test.java diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/exception/ExceptionUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/exception/ExceptionUtil.java index 01fbc5bd5..f8fad0f9c 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/exception/ExceptionUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/exception/ExceptionUtil.java @@ -421,12 +421,11 @@ public class ExceptionUtil { * @return 最尾端异常,传入null参数返回也为null */ public static Throwable getRootCause(final Throwable throwable) { - Throwable cause = throwable.getCause(); - if (cause != null) { - return getRootCause(cause); - }else{ + final Throwable cause = throwable.getCause(); + if (null == cause) { return throwable; } + return getRootCause(cause); } /** diff --git a/hutool-json/src/test/java/org/dromara/hutool/json/Issue3086Test.java b/hutool-json/src/test/java/org/dromara/hutool/json/Issue3086Test.java new file mode 100644 index 000000000..656ee6547 --- /dev/null +++ b/hutool-json/src/test/java/org/dromara/hutool/json/Issue3086Test.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2023 looly(loolly@aliyun.com) + * Hutool is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package org.dromara.hutool.json; + +import lombok.Data; +import org.dromara.hutool.core.collection.ListUtil; +import org.dromara.hutool.json.serialize.JSONObjectSerializer; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.stream.Collectors; + +public class Issue3086Test { + + @Test + public void serializeTest() { + JSONUtil.putSerializer(TestBean.class, new TestBean()); + + final List strings = ListUtil.of( + new SimpleGrantedAuthority("ROLE_admin"), + new SimpleGrantedAuthority("ROLE_normal") + ); + final TestBean testBean = new TestBean(); + testBean.setAuthorities(strings); + + Assertions.assertEquals("{\"authorities\":[\"ROLE_admin\",\"ROLE_normal\"]}", + JSONUtil.toJsonStr(testBean)); + } + + static class SimpleGrantedAuthority { + private final String role; + + public SimpleGrantedAuthority(final String role) { + this.role = role; + } + + public String getAuthority() { + return this.role; + } + + public String toString() { + return this.role; + } + } + + @Data + static class TestBean implements JSONObjectSerializer { + private Collection authorities = new ArrayList<>(); + + @Override + public void serialize(final JSONObject json, final TestBean testBean) { + final List strings = testBean.getAuthorities() + .stream().map(SimpleGrantedAuthority::getAuthority).collect(Collectors.toList()); + json.set("authorities",strings); + } + } +}