Skip to content

Commit

Permalink
add test for idConverter with some docs. (#36)
Browse files Browse the repository at this point in the history
* 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
RocherKong authored Mar 20, 2022
1 parent 2ffc0e3 commit bd66715
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 3 deletions.
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_");
});
}

}
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);
}



}
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_");
});
}

}
17 changes: 14 additions & 3 deletions document/docs/guide/api/id-converter.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# IdConverter

TODO

> **ID转换器**,用于将 `long` 类型ID转换为 `String`,反之亦然。
```java
Expand All @@ -27,9 +24,23 @@ public interface IdConverter {
```

## ToStringIdConverter
> String 转换器,用于将 `long` 转换成String 或者将 String 转换成 long 类型
- 规则
- long 转 String:String.valueOf
- String 转 long: Long.parseLong

## Radix62IdConverter
> 62进制转换器,用于将 `long` 类型转换成 `62进制字符串`,或者将 `62进制字符串` 转换成`long` 类型
- 规则:[0-9][A-Z][a-z]{11}

## SnowflakeFriendlyIdConverter
> 雪花Id转换器,将符合雪花规则的字符串,转换成 long ,或者long 转换成雪花规则字符串

## PrefixIdConverter
> 将带有前缀的字符串转换成long,或者将long转换成带前缀字符串
- 规则
- 例如:前缀为:`no_`, 转换器选用`ToStringIdConverter`,数字`1` 经过转换得到`no_1`,反之亦然。

0 comments on commit bd66715

Please sign in to comment.