-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test for idConverter with some docs. (#36)
* fix demo not runnable by correct application.yaml * ignore type check while read integer field. * update CosIdIntervalShardingAlgorithm type name. * add test for idConverter. and add some docs.
- Loading branch information
1 parent
2ffc0e3
commit bd66715
Showing
4 changed files
with
178 additions
and
3 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
cosid-core/src/test/java/me/ahoo/cosid/converter/PrefixIdConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package me.ahoo.cosid.converter; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
/** | ||
* @author rocher kong | ||
*/ | ||
class PrefixIdConverterTest { | ||
private String prefix = "no_"; | ||
private final PrefixIdConverter prefixIdConverter = new PrefixIdConverter(prefix, ToStringIdConverter.INSTANCE); | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"no_1","no_100","no_1000"}) | ||
void asString(String argId) { | ||
long idStr = prefixIdConverter.asLong(argId); | ||
Assertions.assertNotNull(idStr); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(longs = {-1,1, 5, 62, 63, 124, Integer.MAX_VALUE, Long.MAX_VALUE}) | ||
void asLong(long argId) { | ||
String idStr = prefixIdConverter.asString(argId); | ||
long actual = prefixIdConverter.asLong(idStr); | ||
Assertions.assertEquals(argId, actual); | ||
} | ||
|
||
@Test | ||
void asLongWhenNumberFormat() { | ||
Assertions.assertDoesNotThrow(() -> { | ||
prefixIdConverter.asLong("no_-1"); | ||
}); | ||
Assertions.assertThrows(StringIndexOutOfBoundsException.class,() -> { | ||
prefixIdConverter.asLong("-1"); | ||
}); | ||
Assertions.assertDoesNotThrow(() -> { | ||
prefixIdConverter.asLong("no_111"); | ||
}); | ||
Assertions.assertThrows(NumberFormatException.class, () -> { | ||
prefixIdConverter.asLong("no_1_"); | ||
}); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
cosid-core/src/test/java/me/ahoo/cosid/converter/SnowflakeFriendlyIdConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package me.ahoo.cosid.converter; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
/** | ||
* @author rocher kong | ||
*/ | ||
class SnowflakeFriendlyIdConverterTest { | ||
@ParameterizedTest | ||
@ValueSource(longs = {295913926632165376L}) | ||
void asString(long argId) { | ||
String idStr = SnowflakeFriendlyIdConverter.INSTANCE.asString(argId); | ||
Assertions.assertNotNull(idStr); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(longs = {295913926632165376L}) | ||
void asLong(long argId) { | ||
String idStr = SnowflakeFriendlyIdConverter.INSTANCE.asString(argId); | ||
long actual = SnowflakeFriendlyIdConverter.INSTANCE.asLong(idStr); | ||
Assertions.assertEquals(argId, actual); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"20220320133617924-5-0"}) | ||
void asLong2(String argId) { | ||
long actual = SnowflakeFriendlyIdConverter.INSTANCE.asLong(argId); | ||
Assertions.assertNotNull(actual); | ||
} | ||
|
||
|
||
|
||
} |
56 changes: 56 additions & 0 deletions
56
cosid-core/src/test/java/me/ahoo/cosid/converter/ToStringIdConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package me.ahoo.cosid.converter; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
/** | ||
* @author rocher kong | ||
*/ | ||
class ToStringIdConverterTest { | ||
|
||
@ParameterizedTest | ||
@ValueSource(longs = {1, 5, 62, 63, 124, Integer.MAX_VALUE, Long.MAX_VALUE}) | ||
void asString(long argId) { | ||
String idStr = ToStringIdConverter.INSTANCE.asString(argId); | ||
Assertions.assertNotNull(idStr); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(longs = {1, 5, 62, 63, 124, Integer.MAX_VALUE, Long.MAX_VALUE}) | ||
void asLong(long argId) { | ||
String idStr = ToStringIdConverter.INSTANCE.asString(argId); | ||
long actual = ToStringIdConverter.INSTANCE.asLong(idStr); | ||
Assertions.assertEquals(argId, actual); | ||
} | ||
|
||
@Test | ||
void asLongWhenNumberFormat() { | ||
ToStringIdConverter idConvert = new ToStringIdConverter(); | ||
|
||
Assertions.assertDoesNotThrow(() -> { | ||
idConvert.asLong("-1"); | ||
}); | ||
Assertions.assertDoesNotThrow(() -> { | ||
idConvert.asLong("111"); | ||
}); | ||
Assertions.assertThrows(NumberFormatException.class, () -> { | ||
idConvert.asLong("1_"); | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters