From 469de71f5897d61541a875777874a9f684ee06db Mon Sep 17 00:00:00 2001 From: Ahoo Wang Date: Sat, 7 Dec 2024 14:51:06 +0800 Subject: [PATCH] feat(test): Improve unit test coverage --- .../ahoo/cosid/cosid/RadixCosIdGenerator.java | 8 +------ .../cosid/Radix62CosIdGeneratorTest.java | 22 ++++++++++++++----- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/cosid-core/src/main/java/me/ahoo/cosid/cosid/RadixCosIdGenerator.java b/cosid-core/src/main/java/me/ahoo/cosid/cosid/RadixCosIdGenerator.java index f7cfc1cc36..ee777fe701 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/cosid/RadixCosIdGenerator.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/cosid/RadixCosIdGenerator.java @@ -118,11 +118,5 @@ public synchronized CosIdState generateAsState() { lastTimestamp = currentTimestamp; return new CosIdState(lastTimestamp, machineId, sequence); } - - @Nonnull - @Override - public String generateAsString() { - CosIdState state = generateAsState(); - return stateParser.asString(state); - } + } diff --git a/cosid-core/src/test/java/me/ahoo/cosid/cosid/Radix62CosIdGeneratorTest.java b/cosid-core/src/test/java/me/ahoo/cosid/cosid/Radix62CosIdGeneratorTest.java index cc1826ea69..f26247baf3 100644 --- a/cosid-core/src/test/java/me/ahoo/cosid/cosid/Radix62CosIdGeneratorTest.java +++ b/cosid-core/src/test/java/me/ahoo/cosid/cosid/Radix62CosIdGeneratorTest.java @@ -27,7 +27,7 @@ class Radix62CosIdGeneratorTest { private final Radix62CosIdGenerator radix62CosIdGenerator = new Radix62CosIdGenerator(1); - + @Test void generateAsString() { String id1 = radix62CosIdGenerator.generateAsString(); @@ -37,21 +37,21 @@ void generateAsString() { assertThat(id2.length(), equalTo(id1.length())); assertThat(radix62CosIdGenerator.getLastTimestamp(), greaterThan(0L)); } - + @Test void generateAsState() { CosIdState state1 = radix62CosIdGenerator.generateAsState(); CosIdState state2 = radix62CosIdGenerator.generateAsState(); assertThat(state2, greaterThan(state1)); } - + @Test void customizeOverflowMachineId() { Assertions.assertThrows(IllegalArgumentException.class, () -> { new Radix62CosIdGenerator(~(-1 << DEFAULT_MACHINE_BIT) + 1); }); } - + @Test void generateSlow() { Radix62CosIdGenerator cosIdGenerator = new Radix62CosIdGenerator(DEFAULT_TIMESTAMP_BIT, DEFAULT_MACHINE_BIT, DEFAULT_SEQUENCE_BIT, 1, 2); @@ -62,14 +62,24 @@ void generateSlow() { CosIdState state3 = cosIdGenerator.generateAsState(); assertThat(state3, greaterThan(state2)); assertThat(state2, greaterThan(state1)); - + assertThat(state1.getSequence(), equalTo(1)); assertThat(state2.getSequence(), equalTo(2)); assertThat(state1.getSequence(), equalTo(1)); } - + @Test public void generateWhenConcurrentString() { new ConcurrentGenerateStingSpec(new Radix62CosIdGenerator(1)).verify(); } + + @Test + public void idConvert() { + Assertions.assertThrows(UnsupportedOperationException.class, radix62CosIdGenerator::idConverter); + } + + @Test + public void generate() { + Assertions.assertThrows(UnsupportedOperationException.class, radix62CosIdGenerator::generate); + } }