2024-12-04 01:39:47 +08:00
|
|
|
package xyz.zhouxy.plusone.commons.base;
|
|
|
|
|
|
2024-12-24 10:44:05 +08:00
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
2024-12-04 01:39:47 +08:00
|
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
|
|
|
|
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
|
|
|
|
|
|
|
|
|
class IWithCodeTests {
|
|
|
|
|
|
2024-12-24 10:44:05 +08:00
|
|
|
@Test
|
|
|
|
|
void equalsCode_SameCode_ReturnsTrue() {
|
|
|
|
|
assertTrue(WithCode.INSTANCE.equalsCode("testCode"));
|
|
|
|
|
Integer intCode = 0;
|
|
|
|
|
Long longCode = 0L;
|
|
|
|
|
assertTrue(WithIntCode.INSTANCE.equalsCode(intCode));
|
|
|
|
|
assertTrue(WithLongCode.INSTANCE.equalsCode(intCode));
|
|
|
|
|
assertTrue(WithLongCode.INSTANCE.equalsCode(longCode));
|
|
|
|
|
|
|
|
|
|
assertTrue(WithCode.INSTANCE.equalsCode(WithCode.SAME_CODE_INSTANCE));
|
|
|
|
|
assertTrue(WithIntCode.INSTANCE.equalsCode(WithIntCode.SAME_CODE_INSTANCE));
|
|
|
|
|
assertTrue(WithIntCode.INSTANCE.equalsCode(WithLongCode.SAME_CODE_INSTANCE));
|
|
|
|
|
assertTrue(WithLongCode.INSTANCE.equalsCode(WithLongCode.SAME_CODE_INSTANCE));
|
|
|
|
|
assertTrue(WithLongCode.INSTANCE.equalsCode(WithIntCode.SAME_CODE_INSTANCE));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void equalsCode_DifferentCode_ReturnsFalse() {
|
|
|
|
|
assertFalse(WithCode.INSTANCE.equalsCode("wrongCode"));
|
|
|
|
|
Integer intCode = 108;
|
|
|
|
|
Long longCode = 108L;
|
|
|
|
|
assertFalse(WithIntCode.INSTANCE.equalsCode(intCode));
|
|
|
|
|
assertFalse(WithLongCode.INSTANCE.equalsCode(intCode));
|
|
|
|
|
assertFalse(WithLongCode.INSTANCE.equalsCode(longCode));
|
|
|
|
|
|
|
|
|
|
assertFalse(WithCode.INSTANCE.equalsCode(WithCode.WRONG_CODE_INSTANCE));
|
|
|
|
|
assertFalse(WithIntCode.INSTANCE.equalsCode(WithIntCode.WRONG_CODE_INSTANCE));
|
|
|
|
|
assertFalse(WithIntCode.INSTANCE.equalsCode(WithLongCode.WRONG_CODE_INSTANCE));
|
|
|
|
|
assertFalse(WithLongCode.INSTANCE.equalsCode(WithLongCode.WRONG_CODE_INSTANCE));
|
|
|
|
|
assertFalse(WithLongCode.INSTANCE.equalsCode(WithIntCode.WRONG_CODE_INSTANCE));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
@SuppressWarnings("null")
|
|
|
|
|
void equalsCode_NullCode_ReturnsFalse() {
|
|
|
|
|
assertFalse(WithCode.INSTANCE.equalsCode((WithCode) null));
|
|
|
|
|
assertFalse(WithCode.INSTANCE.equalsCode((WithIntCode) null));
|
|
|
|
|
assertFalse(WithCode.INSTANCE.equalsCode((WithLongCode) null));
|
|
|
|
|
|
|
|
|
|
assertFalse(WithIntCode.INSTANCE.equalsCode((WithCode) null));
|
|
|
|
|
assertFalse(WithIntCode.INSTANCE.equalsCode((WithIntCode) null));
|
|
|
|
|
assertFalse(WithIntCode.INSTANCE.equalsCode((WithLongCode) null));
|
|
|
|
|
|
|
|
|
|
assertFalse(WithLongCode.INSTANCE.equalsCode((WithCode) null));
|
|
|
|
|
assertFalse(WithLongCode.INSTANCE.equalsCode((WithIntCode) null));
|
|
|
|
|
assertFalse(WithLongCode.INSTANCE.equalsCode((WithLongCode) null));
|
|
|
|
|
|
|
|
|
|
assertFalse(WithCode.INSTANCE.equalsCode((String) null));
|
|
|
|
|
Integer intCode = null;
|
|
|
|
|
Long longCode = null;
|
|
|
|
|
assertThrows(NullPointerException.class, () -> WithIntCode.INSTANCE.equalsCode(intCode));
|
|
|
|
|
assertThrows(NullPointerException.class, () -> WithLongCode.INSTANCE.equalsCode(intCode));
|
|
|
|
|
assertThrows(NullPointerException.class, () -> WithLongCode.INSTANCE.equalsCode(longCode));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static enum WithCode implements IWithCode<String> {
|
|
|
|
|
INSTANCE("testCode"),
|
|
|
|
|
SAME_CODE_INSTANCE("testCode"),
|
|
|
|
|
WRONG_CODE_INSTANCE("wrongCode"),
|
|
|
|
|
;
|
|
|
|
|
|
2024-12-04 01:39:47 +08:00
|
|
|
@Nonnull
|
|
|
|
|
private final String code;
|
|
|
|
|
|
2024-12-24 10:44:05 +08:00
|
|
|
WithCode(String code) {
|
2024-12-04 01:39:47 +08:00
|
|
|
AssertTools.checkNotNull(code);
|
|
|
|
|
this.code = code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Nonnull
|
|
|
|
|
public String getCode() {
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-24 10:44:05 +08:00
|
|
|
private static enum WithIntCode implements IWithIntCode {
|
|
|
|
|
INSTANCE(0),
|
|
|
|
|
SAME_CODE_INSTANCE(0),
|
|
|
|
|
WRONG_CODE_INSTANCE(1),
|
|
|
|
|
;
|
2024-12-04 01:39:47 +08:00
|
|
|
|
2024-12-24 10:44:05 +08:00
|
|
|
private final int code;
|
2024-12-04 01:39:47 +08:00
|
|
|
|
2024-12-24 10:44:05 +08:00
|
|
|
WithIntCode(int code) {
|
|
|
|
|
this.code = code;
|
|
|
|
|
}
|
2024-12-04 01:39:47 +08:00
|
|
|
|
2024-12-24 10:44:05 +08:00
|
|
|
@Override
|
|
|
|
|
public int getCode() {
|
|
|
|
|
return code;
|
|
|
|
|
}
|
2024-12-04 01:39:47 +08:00
|
|
|
}
|
|
|
|
|
|
2024-12-24 10:44:05 +08:00
|
|
|
private static enum WithLongCode implements IWithLongCode {
|
|
|
|
|
INSTANCE(0L),
|
|
|
|
|
SAME_CODE_INSTANCE(0L),
|
|
|
|
|
WRONG_CODE_INSTANCE(108L),
|
|
|
|
|
;
|
2024-12-04 01:39:47 +08:00
|
|
|
|
2024-12-24 10:44:05 +08:00
|
|
|
private final long code;
|
|
|
|
|
|
|
|
|
|
WithLongCode(long code) {
|
|
|
|
|
this.code = code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public long getCode() {
|
|
|
|
|
return code;
|
|
|
|
|
}
|
2024-12-04 01:39:47 +08:00
|
|
|
}
|
|
|
|
|
}
|