From 0e90956147bbeec6de7527dbbea26e52861b32f1 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 4 Dec 2024 01:39:47 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E8=BD=BD=20equalsCode=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95=EF=BC=9B=E6=B7=BB=E5=8A=A0=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/commons/base/IWithCode.java | 11 ++++ .../plusone/commons/base/IWithIntCode.java | 12 ++++ .../plusone/commons/base/IWithLongCode.java | 12 ++++ .../plusone/commons/base/IWithCodeTests.java | 57 +++++++++++++++++++ .../commons/base/IWithIntCodeTests.java | 38 +++++++++++++ 5 files changed, 130 insertions(+) create mode 100644 src/test/java/xyz/zhouxy/plusone/commons/base/IWithCodeTests.java create mode 100644 src/test/java/xyz/zhouxy/plusone/commons/base/IWithIntCodeTests.java diff --git a/src/main/java/xyz/zhouxy/plusone/commons/base/IWithCode.java b/src/main/java/xyz/zhouxy/plusone/commons/base/IWithCode.java index 015fdc3..b8bc1bb 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/base/IWithCode.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/base/IWithCode.java @@ -34,4 +34,15 @@ public interface IWithCode { default boolean equalsCode(T code) { return Objects.equals(getCode(), code); } + default boolean equalsCode(IWithCode obj) { // TODO 单元测试 + return obj != null && obj.getCode().equals(getCode()); + } + + default boolean equalsCode(IWithIntCode obj) { // TODO 单元测试 + return obj != null && getCode().equals(obj.getCode()); + } + + default boolean equalsCode(IWithLongCode obj) { // TODO 单元测试 + return obj != null && getCode().equals(obj.getCode()); + } } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/base/IWithIntCode.java b/src/main/java/xyz/zhouxy/plusone/commons/base/IWithIntCode.java index 7deac35..5a5f36c 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/base/IWithIntCode.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/base/IWithIntCode.java @@ -29,4 +29,16 @@ public interface IWithIntCode { default boolean equalsCode(int code) { return getCode() == code; } + + default boolean equalsCode(IWithCode obj) { // TODO 单元测试 + return obj != null && obj.getCode().equals(getCode()); + } + + default boolean equalsCode(IWithIntCode obj) { // TODO 单元测试 + return obj != null && getCode() == obj.getCode(); + } + + default boolean equalsCode(IWithLongCode obj) { // TODO 单元测试 + return obj != null && getCode() == obj.getCode(); + } } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/base/IWithLongCode.java b/src/main/java/xyz/zhouxy/plusone/commons/base/IWithLongCode.java index 1bd8d20..7550358 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/base/IWithLongCode.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/base/IWithLongCode.java @@ -29,4 +29,16 @@ public interface IWithLongCode { default boolean equalsCode(long code) { return getCode() == code; } + + default boolean equalsCode(IWithCode obj) { // TODO 单元测试 + return obj != null && obj.getCode().equals(getCode()); + } + + default boolean equalsCode(IWithIntCode obj) { // TODO 单元测试 + return obj != null && getCode() == obj.getCode(); + } + + default boolean equalsCode(IWithLongCode obj) { // TODO 单元测试 + return obj != null && getCode() == obj.getCode(); + } } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/base/IWithCodeTests.java b/src/test/java/xyz/zhouxy/plusone/commons/base/IWithCodeTests.java new file mode 100644 index 0000000..693d1b6 --- /dev/null +++ b/src/test/java/xyz/zhouxy/plusone/commons/base/IWithCodeTests.java @@ -0,0 +1,57 @@ +package xyz.zhouxy.plusone.commons.base; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import javax.annotation.Nonnull; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import xyz.zhouxy.plusone.commons.util.AssertTools; + +class IWithCodeTests { + + private static class WithCodeImpl implements IWithCode { + @Nonnull + private final String code; + + WithCodeImpl(String code) { + AssertTools.checkNotNull(code); + this.code = code; + } + + @Override + @Nonnull + public String getCode() { + return code; + } + } + + private WithCodeImpl instance; + + @BeforeEach + void setUp() { + instance = new WithCodeImpl("testCode"); + } + + @Test + void equalsCode_SameCode_ReturnsTrue() { + assertTrue(instance.equalsCode("testCode")); + } + + @Test + void equalsCode_DifferentCode_ReturnsFalse() { + assertFalse(instance.equalsCode("wrongCode")); + } + + @Test + void equalsCode_NullCode_ReturnsFalse() { + assertFalse(instance.equalsCode((String) null)); + } + + @Test + void equalsCode_NullObject_ReturnsFalse() { + assertFalse(instance.equalsCode((String) null)); + } +} diff --git a/src/test/java/xyz/zhouxy/plusone/commons/base/IWithIntCodeTests.java b/src/test/java/xyz/zhouxy/plusone/commons/base/IWithIntCodeTests.java new file mode 100644 index 0000000..60fa5ab --- /dev/null +++ b/src/test/java/xyz/zhouxy/plusone/commons/base/IWithIntCodeTests.java @@ -0,0 +1,38 @@ +package xyz.zhouxy.plusone.commons.base; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +class IWithIntCodeTests { + @Test + void test() { + IWithIntCode instance1 = new WithIntCodeImpl(10); + IWithIntCode instance2 = new WithIntCodeImpl(20); + IWithIntCode instance3 = new WithIntCodeImpl(10); + + // Test for equalsCode method + assertTrue(instance1.equalsCode(10)); + assertFalse(instance1.equalsCode(20)); + assertTrue(instance2.equalsCode(20)); + assertTrue(instance3.equalsCode(10)); + + // Test for distinct instances with same code + assertTrue(instance1.equalsCode(instance3.getCode())); // because they have the same code + assertFalse(instance1.equalsCode(instance2.getCode())); // because they have different codes + } +} + +class WithIntCodeImpl implements IWithIntCode { + private int code; + + public WithIntCodeImpl(int code) { + this.code = code; + } + + @Override + public int getCode() { + return code; + } +}