重载 equalsCode 方法;添加单元测试
parent
75b39de99f
commit
0e90956147
|
@ -34,4 +34,15 @@ public interface IWithCode<T> {
|
||||||
default boolean equalsCode(T code) {
|
default boolean equalsCode(T code) {
|
||||||
return Objects.equals(getCode(), 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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,4 +29,16 @@ public interface IWithIntCode {
|
||||||
default boolean equalsCode(int code) {
|
default boolean equalsCode(int code) {
|
||||||
return getCode() == 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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,4 +29,16 @@ public interface IWithLongCode {
|
||||||
default boolean equalsCode(long code) {
|
default boolean equalsCode(long code) {
|
||||||
return getCode() == 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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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<String> {
|
||||||
|
@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));
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue